[clang] [clang][Lex][NFC] Simplify macro expansion state restoration (PR #239918)

via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 6 23:51:04 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Chen Miao (ChenMiaoi)

<details>
<summary>Changes</summary>

Replace the repeated restoration assignments with `llvm::scope_exit`.

---
Full diff: https://github.com/llvm/llvm-project/pull/239918.diff


1 Files Affected:

- (modified) clang/lib/Lex/PPExpressions.cpp (+3-10) 


``````````diff
diff --git a/clang/lib/Lex/PPExpressions.cpp b/clang/lib/Lex/PPExpressions.cpp
index ebf797c3ad6a1..2fcf449458e29 100644
--- a/clang/lib/Lex/PPExpressions.cpp
+++ b/clang/lib/Lex/PPExpressions.cpp
@@ -30,6 +30,7 @@
 #include "clang/Lex/Token.h"
 #include "llvm/ADT/APSInt.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/ScopeExit.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/ErrorHandling.h"
@@ -898,7 +899,8 @@ Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro,
   // to recursively expand in order to get more gcc-list behavior, so we force
   // DisableMacroExpansion to false and restore it when we're done parsing the
   // expression.
-  bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
+  llvm::scope_exit RestoreMacroExpansion(
+      [this, Old = DisableMacroExpansion] { DisableMacroExpansion = Old; });
   DisableMacroExpansion = false;
 
   // Peek ahead one token.
@@ -915,9 +917,6 @@ Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro,
     if (Tok.isNot(tok::eod))
       DiscardUntilEndOfDirective(Tok);
 
-    // Restore 'DisableMacroExpansion'.
-    DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
-
     // We cannot trust the source range from the value because there was a
     // parse error. Track the range manually -- the end of the directive is the
     // end of the condition range.
@@ -938,8 +937,6 @@ Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro,
     if (DT.State == DefinedTracker::NotDefinedMacro)
       IfNDefMacro = DT.TheMacro;
 
-    // Restore 'DisableMacroExpansion'.
-    DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
     bool IsNonZero = ResVal.Val != 0;
     SourceRange ValRange = ResVal.getRange();
     return {std::move(ResVal.Val), IsNonZero, DT.IncludedUndefinedIds,
@@ -954,8 +951,6 @@ Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro,
     if (Tok.isNot(tok::eod))
       DiscardUntilEndOfDirective(Tok);
 
-    // Restore 'DisableMacroExpansion'.
-    DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
     return {std::nullopt,
             false,
             DT.IncludedUndefinedIds,
@@ -973,8 +968,6 @@ Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro,
 
   EvaluatedDefined = EvaluatedDefined || DT.State != DefinedTracker::Unknown;
 
-  // Restore 'DisableMacroExpansion'.
-  DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
   bool IsNonZero = ResVal.Val != 0;
   SourceRange ValRange = ResVal.getRange();
   return {std::move(ResVal.Val), IsNonZero, DT.IncludedUndefinedIds, ValRange};

``````````

</details>


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


More information about the cfe-commits mailing list