From c2750da3431fa6e2e000526d399fd6427dbd11ac Mon Sep 17 00:00:00 2001 From: Fabian Kosmale Date: Mon, 26 Sep 2022 10:27:17 +0200 Subject: [PATCH] moc: Add basic support for nested inline namespaces This improves moc's support for nested inline namespaces, so that code containing them will not break compilation. For simplicity, we allow nested inline namespaces even in C++17 mode (the actual C++ compiler will reject the code anyway, and probably with a better error message than moc could output). moc still has no real awareness how inline namespaces work, but that is a preexisting issue. Fixes: QTBUG-106920 Change-Id: I7b415a99133575f101bc81d01d4670a5f752917f Reviewed-by: Thiago Macieira (cherry picked from commit 5222df2be7d10bf44dfc2971774eadcb526b7a13) Reviewed-by: Daniel Smith --- src/tools/moc/moc.cpp | 6 ++++++ tests/auto/tools/moc/tst_moc.cpp | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/src/tools/moc/moc.cpp b/src/tools/moc/moc.cpp index a8a1fb372ca..461382092ec 100644 --- a/src/tools/moc/moc.cpp +++ b/src/tools/moc/moc.cpp @@ -595,6 +595,12 @@ void Moc::parse() QByteArray nsName = lexem(); QByteArrayList nested; while (test(SCOPE)) { + /* treat (C++20's) namespace A::inline B {} as A::B + this is mostly to not break compilation when encountering such + a construct in a header; the interaction of Qt's meta-macros with + inline namespaces is still rather poor. + */ + test(INLINE); next(IDENTIFIER); nested.append(nsName); nsName = lexem(); diff --git a/tests/auto/tools/moc/tst_moc.cpp b/tests/auto/tools/moc/tst_moc.cpp index 7dc4f2ccdce..8862b0daf2d 100644 --- a/tests/auto/tools/moc/tst_moc.cpp +++ b/tests/auto/tools/moc/tst_moc.cpp @@ -64,6 +64,15 @@ const char *string_hash_hash = STRING_HASH_HASH("baz"); #endif +#if defined(Q_MOC_RUN) || __cplusplus > 202002L +/* Check that nested inline namespaces are at least not causing moc to break. + Check it even outside of C++20 mode as moc gets passed the wrong __cplusplus version + and also to increase coverage, given how few C++20 configurations exist in the CI at the time + of writing this comment. +*/ +namespace A::inline B {} +#endif + Q_DECLARE_METATYPE(const QMetaObject*); #define TESTEXPORTMACRO Q_DECL_EXPORT