[clang] Extension: allow recursive macros (PR #65851)

via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 24 03:05:18 PDT 2023


github-actions[bot] wrote:


<!--LLVM CODE FORMAT COMMENT: {clang-format}-->

:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff 6fbbcb4ee724e23edd0fcd5b51877aff19dabd77 49d7fa2e638f3903375f6f1de8443c63ff909a5d -- clang/test/Preprocessor/macro_infinite_recursion.cpp clang/test/Preprocessor/macro_recursion.cpp clang/include/clang/Lex/MacroInfo.h clang/include/clang/Lex/Preprocessor.h clang/lib/Basic/IdentifierTable.cpp clang/lib/Format/WhitespaceManager.cpp clang/lib/Lex/Lexer.cpp clang/lib/Lex/MacroInfo.cpp clang/lib/Lex/PPDirectives.cpp clang/lib/Lex/PPMacroExpansion.cpp clang/lib/Lex/Preprocessor.cpp clang/lib/Lex/TokenLexer.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/clang/include/clang/Lex/MacroInfo.h b/clang/include/clang/Lex/MacroInfo.h
index 8b91d33e9..895b15e10 100644
--- a/clang/include/clang/Lex/MacroInfo.h
+++ b/clang/include/clang/Lex/MacroInfo.h
@@ -190,7 +190,7 @@ public:
     return ArrayRef<IdentifierInfo *>(ParameterList, NumParameters);
   }
   ArrayRef<const IdentifierInfo *> params() const {
-    return const_cast<MacroInfo*>(this)->params();
+    return const_cast<MacroInfo *>(this)->params();
   }
 
   /// Return the parameter number of the specified identifier,
@@ -300,10 +300,10 @@ public:
     --Depth;
   }
   enum : uint16_t { macro_recursion_depth_limit = 16'000 };
-  
-  [[nodiscard("infinite recursion check ignored")]]
-  bool TryDisableMacro() {
-    assert((AllowRecurse || Depth == 0) && "Cannot disable an already-disabled macro!");
+
+  [[nodiscard("infinite recursion check ignored")]] bool TryDisableMacro() {
+    assert((AllowRecurse || Depth == 0) &&
+           "Cannot disable an already-disabled macro!");
     return ++Depth < macro_recursion_depth_limit;
   }
 
diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h
index 356be1d3b..361553e84 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -2429,7 +2429,7 @@ private:
   /// If macro definition containts __THIS_MACRO__ creates impl-only recursive
   /// version of macro, and replaces all __THIS_MACRO__ tokens
   /// with new created recusive version
-  void appendRecursiveVersionIfRequired(IdentifierInfo*, MacroInfo*);
+  void appendRecursiveVersionIfRequired(IdentifierInfo *, MacroInfo *);
 
   void PushIncludeMacroStack() {
     assert(CurLexerKind != CLK_CachingLexer && "cannot push a caching lexer");
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index d8ab76d67..db75e21f8 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -743,8 +743,10 @@ void WhitespaceManager::alignConsecutiveMacros() {
     if (!Current || Current->isNot(tok::identifier))
       return false;
 
-    if (!Current->Previous || !Current->Previous->isOneOf(tok::pp_define, tok::pp_define2))
+    if (!Current->Previous ||
+        !Current->Previous->isOneOf(tok::pp_define, tok::pp_define2)) {
       return false;
+    }
 
     // For a macro function, 0 spaces are required between the
     // identifier and the lparen that opens the parameter list.
diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp
index 178bcc08a..70ba8f4f9 100644
--- a/clang/lib/Lex/Lexer.cpp
+++ b/clang/lib/Lex/Lexer.cpp
@@ -847,7 +847,7 @@ bool Lexer::isAtEndOfMacroExpansion(SourceLocation loc,
                                     const SourceManager &SM,
                                     const LangOptions &LangOpts,
                                     SourceLocation *MacroEnd) {
-  for(SourceLocation expansionLoc; true;loc = expansionLoc) {
+  for (SourceLocation expansionLoc; true; loc = expansionLoc) {
     assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc");
 
     SourceLocation spellLoc = SM.getSpellingLoc(loc);
diff --git a/clang/lib/Lex/MacroInfo.cpp b/clang/lib/Lex/MacroInfo.cpp
index 110e3232b..e042dd38a 100644
--- a/clang/lib/Lex/MacroInfo.cpp
+++ b/clang/lib/Lex/MacroInfo.cpp
@@ -155,9 +155,11 @@ LLVM_DUMP_METHOD void MacroInfo::dump() const {
   // FIXME: Dump locations.
   Out << "MacroInfo " << this;
   if (IsBuiltinMacro) Out << " builtin";
-  if (!isEnabled()) Out << " disabled";
+  if (!isEnabled())
+    Out << " disabled";
   if (IsUsed) Out << " used";
-  if (AllowRecurse) Out << " allow_recurse";
+  if (AllowRecurse)
+    Out << " allow_recurse";
   if (IsAllowRedefinitionsWithoutWarning)
     Out << " allow_redefinitions_without_warning";
   if (IsWarnIfUnused) Out << " warn_if_unused";
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 06b819857..6bb1612a9 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -1109,7 +1109,8 @@ private:
 void Preprocessor::HandleSkippedDirectiveWhileUsingPCH(Token &Result,
                                                        SourceLocation HashLoc) {
   if (const IdentifierInfo *II = Result.getIdentifierInfo()) {
-    if (II->getPPKeywordID() == tok::pp_define || II->getPPKeywordID() == tok::pp_define2) {
+    if (II->getPPKeywordID() == tok::pp_define ||
+        II->getPPKeywordID() == tok::pp_define2) {
       return HandleDefineDirective(Result,
                                    /*ImmediatelyAfterHeaderGuard=*/false);
     }
@@ -3037,8 +3038,8 @@ static bool isObjCProtectedMacro(const IdentifierInfo *II) {
          II->isStr("__unsafe_unretained") || II->isStr("__autoreleasing");
 }
 
-/// HandleDefineDirective - Implements \#define and define2. This consumes the entire macro
-/// line then lets the caller lex the next real token.
+/// HandleDefineDirective - Implements \#define and define2. This consumes the
+/// entire macro line then lets the caller lex the next real token.
 void Preprocessor::HandleDefineDirective(
     Token &DefineTok, const bool ImmediatelyAfterHeaderGuard) {
   ++NumDefined;
@@ -3065,7 +3066,8 @@ void Preprocessor::HandleDefineDirective(
       MacroNameTok, ImmediatelyAfterHeaderGuard);
 
   if (!MI) return;
-  MI->setAllowRecursive(DefineTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_define2);
+  MI->setAllowRecursive(DefineTok.getIdentifierInfo()->getPPKeywordID() ==
+                        tok::pp_define2);
   if (MacroShadowsKeyword &&
       !isConfigurationPattern(MacroNameTok, MI, getLangOpts())) {
     Diag(MacroNameTok, diag::warn_pp_macro_hides_keyword);
diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp
index ca1f78280..fa46d9807 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -59,26 +59,30 @@
 
 using namespace clang;
 
-void Preprocessor::appendRecursiveVersionIfRequired(IdentifierInfo* II, MacroInfo* MI) {
+void Preprocessor::appendRecursiveVersionIfRequired(IdentifierInfo *II,
+                                                    MacroInfo *MI) {
   if (!MI->isFunctionLike())
     return;
-  auto is_this_macro_tok = [&] (const Token& t) {
-    return t.getKind() == tok::identifier && t.getIdentifierInfo() == Ident__THIS_MACRO__;
+  auto is_this_macro_tok = [&](const Token &t) {
+    return t.getKind() == tok::identifier &&
+           t.getIdentifierInfo() == Ident__THIS_MACRO__;
   };
   if (llvm::none_of(MI->tokens(), is_this_macro_tok))
     return;
-  IdentifierInfo* ImplMacroII = [&] {
+  IdentifierInfo *ImplMacroII = [&] {
     std::string ImplMacroName = "__THIS_MACRO__";
     ImplMacroName += II->getName();
     return getIdentifierInfo(ImplMacroName);
   }();
-  MacroInfo* NewMI = AllocateMacroInfo(MI->getDefinitionLoc());
+  MacroInfo *NewMI = AllocateMacroInfo(MI->getDefinitionLoc());
   NewMI->setIsFunctionLike();
   NewMI->setParameterList(MI->params(), getPreprocessorAllocator());
   NewMI->setDefinitionEndLoc(MI->getDefinitionEndLoc());
-  if (MI->isC99Varargs()) NewMI->setIsC99Varargs();
-  if (MI->isGNUVarargs()) NewMI->setIsGNUVarargs();
-  for (auto& t : MI->tokens()) {
+  if (MI->isC99Varargs())
+    NewMI->setIsC99Varargs();
+  if (MI->isGNUVarargs())
+    NewMI->setIsGNUVarargs();
+  for (auto &t : MI->tokens()) {
     if (is_this_macro_tok(t))
       t.setIdentifierInfo(ImplMacroII);
   }

``````````

</details>


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


More information about the cfe-commits mailing list