[Lldb-commits] [lldb] 9f3a3e1 - [lldb] Disable macro redefinition warnings in expression wrapper

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Tue Feb 14 14:21:28 PST 2023


Author: Raphael Isemann
Date: 2023-02-14T23:20:56+01:00
New Revision: 9f3a3e1f3f9767ae52c492d20d63f65e82319ed2

URL: https://github.com/llvm/llvm-project/commit/9f3a3e1f3f9767ae52c492d20d63f65e82319ed2
DIFF: https://github.com/llvm/llvm-project/commit/9f3a3e1f3f9767ae52c492d20d63f65e82319ed2.diff

LOG: [lldb] Disable macro redefinition warnings in expression wrapper

GCC emits macro definitions into debug info when compiling with `-g3`. LLDB is
translating this information into `#define` directives which are injected into
the source code of user expressions. While this mechanism itself works fine,
it can lead to spurious "... macro redefined" warnings when the defined macro
is also a builtin Clang macro:

```
warning: <lldb wrapper prefix>:46:9: '__VERSION__' macro redefined
        ^
<built-in>:19:9: previous definition is here
[repeated about a 100 more times for every builtin macro]
```

This patch just disables the diagnostic when parsing LLDB's generated list of
macros definitions.

Reviewed By: Michael137

Differential Revision: https://reviews.llvm.org/D139740

Added: 
    

Modified: 
    lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionSourceCode.cpp
    lldb/test/API/commands/expression/macros/TestMacros.py

Removed: 
    


################################################################################
diff  --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionSourceCode.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionSourceCode.cpp
index 56c00b35ba11b..cc3abfdfde394 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionSourceCode.cpp
+++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionSourceCode.cpp
@@ -14,6 +14,7 @@
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Lex/Lexer.h"
+#include "llvm/ADT/ScopeExit.h"
 #include "llvm/ADT/StringRef.h"
 
 #include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
@@ -141,6 +142,17 @@ static void AddMacros(const DebugMacros *dm, CompileUnit *comp_unit,
   if (dm == nullptr)
     return;
 
+  // The macros directives below can potentially redefine builtin macros of the
+  // Clang instance which parses the user expression. The Clang diagnostics
+  // caused by this are not useful for the user as the source code here is
+  // generated by LLDB.
+  stream << "#pragma clang diagnostic push\n";
+  stream << "#pragma clang diagnostic ignored \"-Wmacro-redefined\"\n";
+  stream << "#pragma clang diagnostic ignored \"-Wbuiltin-macro-redefined\"\n";
+  auto pop_warning = llvm::make_scope_exit([&stream](){
+    stream << "#pragma clang diagnostic pop\n";
+  });
+
   for (size_t i = 0; i < dm->GetNumMacroEntries(); i++) {
     const DebugMacroEntry &entry = dm->GetMacroEntryAtIndex(i);
     uint32_t line;

diff  --git a/lldb/test/API/commands/expression/macros/TestMacros.py b/lldb/test/API/commands/expression/macros/TestMacros.py
index 3e5d720aef77b..b39572b6e4408 100644
--- a/lldb/test/API/commands/expression/macros/TestMacros.py
+++ b/lldb/test/API/commands/expression/macros/TestMacros.py
@@ -129,3 +129,9 @@ def test_expr_with_macros(self):
         result = frame.EvaluateExpression("MACRO_2")
         self.assertTrue(result.GetError().Fail(),
                         "Printing MACRO_2 fails in the header file")
+
+        # Check that the macro definitions do not trigger bogus Clang
+        # diagnostics about macro redefinitions.
+        result = frame.EvaluateExpression("does_not_parse")
+        self.assertNotIn("macro redefined", str(result.GetError()))
+        self.assertNotIn("redefining builtin macro", str(result.GetError()))


        


More information about the lldb-commits mailing list