[clang] [Clang] [C++26] Iterating expansion statements using string injection (PR #208877)
Ambrose Leeb via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 4 10:19:51 PDT 2026
================
@@ -48,6 +48,58 @@ class ActionCommentHandler : public CommentHandler {
};
} // end anonymous namespace
+class Parser::TokenInjectionHandlerImpl : public sema::TokenInjectionHandler {
+ Parser &P;
+
+public:
+ explicit TokenInjectionHandlerImpl(Parser &P) : P{P} {}
+
+ ExprResult ParseAsExpression(StringRef Code,
+ const llvm::StringMap<Token> &Replacements,
+ SourceLocation InjectionLoc) override {
+ // Collect tokens.
+ SmallVector<Token> Tokens;
+ if (P.PP.LexTokensInString(Tokens, Code, InjectionLoc))
+ return ExprError();
+
+ // Apply replacements.
+ for (Token &T : Tokens) {
+ if (T.is(tok::identifier)) {
+ auto It = Replacements.find(T.getIdentifierInfo()->getName());
+ if (It != Replacements.end())
+ T = It->getValue();
+ }
+ }
+
+ // The lexer should have added an EOF marker to the token list.
+ assert(Tokens.back().is(tok::eof));
+ char EofMarker{};
+ Tokens.back().setEofData(&EofMarker);
+
+ // Start parsing the tokens; we need to save the current token so we
+ // don't lose it, and consume it since EnterTokenStream() doesn't change
+ // the current token.
+ //
+ // Disable macro expansion since that was already done as part of the call
+ // to LexTokensInString() above; 'IsReinjected' is for phase 4 tokens, so
+ // we don't want that either here.
+ SaveAndRestore SaveCurTok{P.Tok};
+ P.PP.EnterTokenStream(Tokens, /*DisableMacroExpansion=*/true,
+ /*IsReinjected=*/false);
+ P.ConsumeAnyToken();
+ ExprResult Res = P.ParseExpression();
+
+ // We should have parsed exactly one expression; if we still have tokens
+ // left, then there was probably an error; don't diagnose this and just
----------------
Sirraide wrote:
The thing I’m worried about is that if some of the code is invalid (e.g. because the iterator type has no `operator*`), then I don’t know what the parser is going to do in the general case wrt synchronisation—`ParseExpression()` might return with some tokens left unparsed if there was an error
https://github.com/llvm/llvm-project/pull/208877
More information about the cfe-commits
mailing list