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

Chen Miao via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 6 23:50:02 PST 2026


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

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

>From 891545ad3fe3472fb6825f50424ba7b20d49c11c Mon Sep 17 00:00:00 2001
From: Chen Miao <chenmiao.ku at gmail.com>
Date: Sat, 7 Nov 2026 15:48:43 +0800
Subject: [PATCH] [clang][Lex][NFC] Simplify macro expansion state restoration

Replace the repeated restoration assignments with `llvm::scope_exit`.
---
 clang/lib/Lex/PPExpressions.cpp | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)

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