From 2a72f0f15d9c433d8d03873637e84fe2e10735b7 Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Mon, 18 Oct 2021 18:12:39 +0200 Subject: Fix compilation with QT_NAMESPACE and tracing on Linux The tracegen tool was not taking into account that Qt could be build with a custom namespace. As a result, the combination of namespace build and tracing enabled was not working, because tracegen generated classes without the namespace. This patch fixes it. We cannot add QT_BEGIN_NAMESPACE/QT_END_NAMESPACE because of the tricky logic that recursively includes the generated header file multiple times, also including the code like static const struct lttng_event_desc *_TP_COMBINE_TOKENS(__event_desc___, TRACEPOINT_PROVIDER)[] = { (hash)include TRACEPOINT_INCLUDE NULL, /* Dummy, C99 forbids 0-len array. */ }; where TRACEPOINT_INCLUDE is the path to the generated header. This patch is using QT_USE_NAMESPACE, wrapped into some #ifdefs. This should be safe, considering that the generated trace headers are only used as private API. The windows tracing support seems to be broken even in a non-namespace build, so it's not handled in this patch. Task-number: QTBUG-97246 Pick-to: 6.2 Change-Id: I12db76e199a3aa3abde641fbf99a6e1a3d7de203 Reviewed-by: Thiago Macieira Reviewed-by: Ievgenii Meshcheriakov --- src/tools/tracegen/lttng.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/tools/tracegen/lttng.cpp b/src/tools/tracegen/lttng.cpp index e628fbd680..9a8dfc9f4a 100644 --- a/src/tools/tracegen/lttng.cpp +++ b/src/tools/tracegen/lttng.cpp @@ -130,6 +130,12 @@ static void writePrologue(QTextStream &stream, const QString &fileName, const Pr << "#define TRACEPOINT_INCLUDE \"" << fileName << "\"\n\n"; stream << "#include \n\n"; + + const QString namespaceGuard = guard + QStringLiteral("_USE_NAMESPACE"); + stream << "#if !defined(" << namespaceGuard << ")\n" + << "#define " << namespaceGuard << "\n" + << "QT_USE_NAMESPACE\n" + << "#endif // " << namespaceGuard << "\n\n"; } static void writeEpilogue(QTextStream &stream, const QString &fileName) @@ -154,6 +160,7 @@ static void writeWrapper(QTextStream &stream, stream << "\n" << "#ifndef " << includeGuard << "\n" << "#define " << includeGuard << "\n" + << "QT_BEGIN_NAMESPACE\n" << "namespace QtPrivate {\n"; stream << "inline void trace_" << name << "(" << argList << ")\n" @@ -172,6 +179,7 @@ static void writeWrapper(QTextStream &stream, << "}\n"; stream << "} // namespace QtPrivate\n" + << "QT_END_NAMESPACE\n" << "#endif // " << includeGuard << "\n\n"; } -- cgit v1.2.3