[clang] [Parser][BoundsSafety] Print attribute as macro if it's system defined (PR #107619)

Yeoul Na via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 16 09:53:11 PDT 2024


================
@@ -5055,6 +5058,17 @@ void Parser::ParseLexedCAttribute(LateParsedAttribute &LA, bool EnterScope,
   ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, nullptr, nullptr,
                         SourceLocation(), ParsedAttr::Form::GNU(), nullptr);
 
+  const auto &SM = PP.getSourceManager();
+  CharSourceRange ExpansionRange = SM.getExpansionRange(LA.AttrNameLoc);
+  StringRef FoundName =
+      Lexer::getSourceText(ExpansionRange, SM, PP.getLangOpts())
+          .split('(')
+          .first;
+  IdentifierInfo *MacroII = PP.getIdentifierInfo(FoundName);
+  for (unsigned i = 0; i < Attrs.size(); ++i)
+    Attrs[i].setMacroIdentifier(MacroII, ExpansionRange.getBegin(),
----------------
rapidsna wrote:

> when multiple are defined by the same one?

Do you have a specific example in mind? 

> Also, I find myself wondering if we should just use MacroIdentifier != nullptr instead of this? 

We could, I just wanted to make sure a system-defined macro is printed as macro, which likely provides a meaningful name, then user-defined macros. And doing so has much less impact on the existing regression tests. 

https://github.com/llvm/llvm-project/pull/107619


More information about the cfe-commits mailing list