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

via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 7 22:09:00 PST 2026


Author: Chen Miao
Date: 2026-11-08T14:08:55+08:00
New Revision: ff5dd667de49fd041863821164494f3d40fc9594

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

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

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

Added: 
    

Modified: 
    clang/lib/Lex/PPExpressions.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Lex/PPExpressions.cpp b/clang/lib/Lex/PPExpressions.cpp
index ebf797c3ad6a1c..2fcf449458e299 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};


        


More information about the cfe-commits mailing list