Skip to content

Commit

Permalink
moc: handle nested structs with meta content
Browse files Browse the repository at this point in the history
We already disallow them for classes; this was a mistake in not handling
the "struct" keyword. The C++ syntax is valid, but moc is unable to
parse two classes at the same time. Users can define the inner outside
of the body of the outer class and that works.

[ChangeLog][moc] Fixed a bug that caused moc to attempt to parse a
nested struct (not class) with meta keywords and this produced code that
wouldn't compile. Nested classes and structures are permitted, but must
be defined outside of the declaration of the outer class.

The fix for this revealed a bug in the presence of Q_DECLARE_FLAGS for
inner classes. This can be easily fixed by moving the token for
Q_DECLARE_FLAGS value outside of the [Q_META_TOKEN_BEGIN,
Q_META_TOKEN_END] range.

[ChangeLog][moc] Fixed a bug that caused moc to reject a class (not
struct) containing Q_DECLARE_FLAGS that was defined inside the body of
an outer class containing Q_OBJECT or Q_GADGET.

Fixes: QTBUG-130498
Pick-to: 6.8 6.5
Change-Id: I2a3f148f70280e87302afffd3e7617e5861bb338
Reviewed-by: Fabian Kosmale <[email protected]>
  • Loading branch information
thiagomacieira committed Nov 2, 2024
1 parent cf8b9c1 commit 4e4eef1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/tools/moc/moc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,7 @@ void Moc::parse()
if (test(Q_SIGNALS_TOKEN))
error("Signals cannot have access specifier");
break;
case STRUCT:
case CLASS: {
ClassDef nestedDef;
if (parseClassHead(&nestedDef)) {
Expand Down
6 changes: 3 additions & 3 deletions src/tools/moc/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ QT_BEGIN_NAMESPACE
F(SIGNALS) \
F(SLOTS) \
F(RETURN) \
F(Q_META_TOKEN_BEGIN) \
F(Q_OBJECT_TOKEN) \
F(Q_GADGET_TOKEN) \
F(Q_GADGET_EXPORT_TOKEN) \
Expand All @@ -141,7 +142,6 @@ QT_BEGIN_NAMESPACE
F(Q_FLAGS_TOKEN) \
F(Q_FLAG_TOKEN) \
F(Q_FLAG_NS_TOKEN) \
F(Q_DECLARE_FLAGS_TOKEN) \
F(Q_DECLARE_INTERFACE_TOKEN) \
F(Q_DECLARE_METATYPE_TOKEN) \
F(Q_CLASSINFO_TOKEN) \
Expand All @@ -158,6 +158,8 @@ QT_BEGIN_NAMESPACE
F(QT_ANONYMOUS_PRIVATE_PROPERTY_TOKEN) \
F(Q_REVISION_TOKEN) \
F(Q_MOC_INCLUDE_TOKEN) \
F(Q_META_TOKEN_END) \
F(Q_DECLARE_FLAGS_TOKEN) \
F(SPECIAL_TREATMENT_MARK) \
F(MOC_INCLUDE_BEGIN) \
F(MOC_INCLUDE_END) \
Expand Down Expand Up @@ -238,8 +240,6 @@ enum Token {
PP_STRING_LITERAL = STRING_LITERAL,
PP_TILDE = TILDE,
PP_WHITESPACE = WHITESPACE,
Q_META_TOKEN_BEGIN = Q_OBJECT_TOKEN,
Q_META_TOKEN_END = SPECIAL_TREATMENT_MARK
};

// for debugging only
Expand Down
35 changes: 35 additions & 0 deletions tests/auto/tools/moc/tst_moc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,18 @@ class TestClass : public MyNamespace::TestSuperClass, public DONT_CONFUSE_MOC(My
public:
inline TestClass() {}

// These two here test that Q_DECLARE_FLAGS is permitted in a class or
// struct nested inside of a Q_OBJECT and defined within the body of the
// class. A Q_OBJECT is not allowed here (see privateClass()).
struct NestedStruct {
enum E {};
Q_DECLARE_FLAGS(Flags, E)
};
class NestedClass {
enum E {};
Q_DECLARE_FLAGS(Flags, E)
};

private slots:
inline void dummy1() MACRO_WITH_POSSIBLE_COMPILER_SPECIFIC_ATTRIBUTES {}
inline void dummy2() MACRO_WITH_POSSIBLE_COMPILER_SPECIFIC_ATTRIBUTES const {}
Expand Down Expand Up @@ -2497,6 +2509,29 @@ void tst_Moc::warnings_data()
<< QString()
<< QString("standard input:2:1: error: Plugin Metadata file \".\" could not be opened: file to open is a directory");
#endif

static const char *tags[] = { "class", "struct" };
static const char *metaKeywords[] = { "Q_OBJECT", "Q_GADGET" };
for (size_t i = 0; i < std::size(tags) * 2 * std::size(metaKeywords) * 2; ++i) {
const char *tag1 = tags[i & 1];
const char *tag2 = tags[(i >> 1) & 1];
const char *meta1 = metaKeywords[(i >> 2) & 1];
const char *meta2 = metaKeywords[(i >> 3) & 1];
QByteArray input = tag1;
input += " X : public Base {\n ";
input += meta1;
input += "\n ";
input += tag2;
input += " Nested : public Base {\n ";
input += meta2;
input += " };\n};\n";
QTest::addRow("nested-%s-%s-%s-%s", tag1, meta1, tag2, meta2)
<< input
<< QStringList()
<< 1
<< QString()
<< "standard input:4:1: error: Meta object features not supported for nested classes";
}
}

void tst_Moc::warnings()
Expand Down

0 comments on commit 4e4eef1

Please sign in to comment.