[PATCH] D123056: [clang][extract-api] Undefining macros should not result in a crash

Daniel Grumberg via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 4 10:56:45 PDT 2022

dang created this revision.
dang added reviewers: zixuw, ributzka, QuietMisdreavus.
Herald added a project: All.
dang requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This fixes the situation where a undefining a not previously defined
macro resulted in a crash. Before trying to remove a definition from
PendingMacros we first check to see if the macro did indeed have a
previous definition.

  rG LLVM Github Monorepo



Index: clang/test/ExtractAPI/macro_undefined.c
--- clang/test/ExtractAPI/macro_undefined.c
+++ clang/test/ExtractAPI/macro_undefined.c
@@ -19,6 +19,8 @@
 FUNC_GEN(bar, const int *, unsigned);
 #undef FUNC_GEN
+// Undefining a not previously defined macro should not result in a crash.
+#undef FOO
 //--- reference.output.json.in
Index: clang/lib/ExtractAPI/ExtractAPIConsumer.cpp
--- clang/lib/ExtractAPI/ExtractAPIConsumer.cpp
+++ clang/lib/ExtractAPI/ExtractAPIConsumer.cpp
@@ -534,6 +534,11 @@
   // macro definition for it.
   void MacroUndefined(const Token &MacroNameToken, const MacroDefinition &MD,
                       const MacroDirective *Undef) override {
+    // If this macro wasn't previously defined we don't need to do anything
+    // here.
+    if (!Undef)
+      return;
     llvm::erase_if(PendingMacros, [&MD](const PendingMacro &PM) {
       return MD.getMacroInfo()->getDefinitionLoc() ==

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D123056.420241.patch
Type: text/x-patch
Size: 1130 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220404/9bb1eff0/attachment.bin>

More information about the cfe-commits mailing list