1- From 6b7b31df2a614557d3c86fed4779535f3f782f19 Mon Sep 17 00:00:00 2001
1+ From ea53234d5061cfc725c9b21798ed39ce09ac3e8e Mon Sep 17 00:00:00 2001
22From: Peter Goodman <
[email protected] >
33Date: Mon, 14 Nov 2022 14:51:27 -0500
44Subject: [PATCH] Patches for PASTA
55
66---
7- clang/include/clang/Lex/PPCallbacks.h | 120 +++++++++++
7+ clang/include/clang/Lex/PPCallbacks.h | 120 +++++++++
88 clang/include/clang/Lex/Preprocessor.h | 46 ++--
99 clang/include/clang/Lex/TokenLexer.h | 7 +-
10- clang/lib/Lex/PPDirectives.cpp | 147 +++++++++ ----
10+ clang/lib/Lex/PPDirectives.cpp | 147 +++++++----
1111 clang/lib/Lex/PPExpressions.cpp | 28 +++
12- clang/lib/Lex/PPLexerChange.cpp | 38 ++++
13- clang/lib/Lex/PPMacroExpansion.cpp | 285 ++++++++++++++++++++++++-
12+ clang/lib/Lex/PPLexerChange.cpp | 38 +++
13+ clang/lib/Lex/PPMacroExpansion.cpp | 323 ++++++++++++++++++++++++-
1414 clang/lib/Lex/Pragma.cpp | 59 +++++
1515 clang/lib/Lex/Preprocessor.cpp | 43 +++-
16- clang/lib/Lex/TokenLexer.cpp | 39 +++ -
16+ clang/lib/Lex/TokenLexer.cpp | 39 ++-
1717 clang/lib/Parse/ParseTemplate.cpp | 7 +
18- 11 files changed, 740 insertions(+), 79 deletions(-)
18+ 11 files changed, 772 insertions(+), 85 deletions(-)
1919
2020diff --git a/clang/include/clang/Lex/PPCallbacks.h b/clang/include/clang/Lex/PPCallbacks.h
2121index 045df8711..cba195cff 100644
@@ -825,7 +825,7 @@ index 36d3aa59b..19697799e 100644
825825 MacroExpandingLexersStack.back().first == CurTokenLexer.get())
826826 removeCachedMacroExpandedTokensOfLastLexer();
827827diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp
828- index c56f41c44..573a91bd8 100644
828+ index c56f41c44..14a91d86b 100644
829829--- a/clang/lib/Lex/PPMacroExpansion.cpp
830830+++ b/clang/lib/Lex/PPMacroExpansion.cpp
831831@@ -60,6 +60,58 @@
@@ -1147,7 +1147,7 @@ index c56f41c44..573a91bd8 100644
11471147 bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
11481148 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
11491149 // error.
1150- @@ -1270,9 +1446,27 @@ static void EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS,
1150+ @@ -1270,6 +1446,14 @@ static void EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS,
11511151 llvm::function_ref<
11521152 int(Token &Tok,
11531153 bool &HasLexedNextTok)> Op) {
@@ -1158,100 +1158,102 @@ index c56f41c44..573a91bd8 100644
11581158+ SavedIdentifier.setIdentifierInfo(II);
11591159+ MacroDefinition MD = PP.getMacroDefinition(II);
11601160+ MacroInfo *MI = MD.getMacroInfo();
1161- + if (Callbacks) {
1162- + Callbacks->Event(SavedIdentifier, PPCallbacks::BeginMacroCallArgumentList,
1163- + reinterpret_cast<uintptr_t>(MI));
1164- + }
11651161+
11661162 // Parse the initial '('.
11671163 PP.LexUnexpandedToken(Tok);
11681164 if (Tok.isNot(tok::l_paren)) {
1169- +
1170- + // PASTA PATCH
1171- + if (Callbacks) {
1172- + Callbacks->Event(Tok, PPCallbacks::EndMacroCallArgumentList, 0);
1173- + }
1174- +
1175- PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II
1176- << tok::l_paren;
1177-
1178- @@ -1284,6 +1478,22 @@ static void EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS,
1179- return;
1180- }
1165+ @@ -1288,14 +1472,84 @@ static void EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS,
1166+ SourceLocation LParenLoc = Tok.getLocation();
1167+ llvm::Optional<int> Result;
11811168
1182- + // PASTA PATCH
1183- + Token ArgBeginTok = Tok;
1169+ + // PASTA PATCH: Re-create the loop that collects the function-like macro use,
1170+ + // do the lexing and argument splitting ourselves, then go and
1171+ + // run the original loop over our pre-lexed loop.
1172+ + Token ArgSepTok = Tok;
1173+ + SmallVector<clang::Token, 6> LexedToks;
1174+ + bool DoneLexingCall = false;
1175+ + bool LastWasSep = true;
11841176+ if (Callbacks) {
1185- + Callbacks->Event(ArgBeginTok , PPCallbacks::BeginMacroCallArgument ,
1186- + reinterpret_cast<uintptr_t>(&SavedIdentifier ));
1177+ + Callbacks->Event(SavedIdentifier , PPCallbacks::BeginMacroCallArgumentList ,
1178+ + reinterpret_cast<uintptr_t>(MI ));
11871179+ }
1188- + bool DoneDone = false;
1189- + auto Done = [&] (void) {
1190- + if (!DoneDone && Callbacks) {
1191- + DoneDone = true;
1192- + Callbacks->Event(ArgBeginTok, PPCallbacks::EndMacroCallArgument,
1193- + reinterpret_cast<uintptr_t>(&Tok));
1194- + Callbacks->Event(Tok, PPCallbacks::EndMacroCallArgumentList, 0);
1180+ + while (!DoneLexingCall) {
1181+ +
1182+ + // Split before the first argument, or the Nth argument.
1183+ + if (LastWasSep) {
1184+ + if (Callbacks) {
1185+ + Callbacks->Event(ArgSepTok, PPCallbacks::BeginMacroCallArgument,
1186+ + reinterpret_cast<uintptr_t>(&SavedIdentifier));
1187+ + }
1188+ + LastWasSep = false;
11951189+ }
1196- + };
11971190+
1198- unsigned ParenDepth = 1;
1199- SourceLocation LParenLoc = Tok.getLocation();
1200- llvm::Optional<int> Result;
1201- @@ -1304,6 +1514,7 @@ already_lexed:
1202- // Don't provide even a dummy value if the eod or eof marker is
1203- // reached. Simply provide a diagnostic.
1204- PP.Diag(Tok.getLocation(), diag::err_unterm_macro_invoc);
1205- + Done(); // PASTA PATCH
1206- return;
1207-
1208- case tok::comma:
1209- @@ -1311,6 +1522,13 @@ already_lexed:
1210- PP.Diag(Tok.getLocation(), diag::err_too_many_args_in_macro_invoc);
1211- SuppressDiagnostic = true;
1212- }
1213- + if (Callbacks) { // PASTA PATCH
1214- + Callbacks->Event(ArgBeginTok, PPCallbacks::EndMacroCallArgument,
1215- + reinterpret_cast<uintptr_t>(&Tok));
1216- + ArgBeginTok = Tok;
1217- + Callbacks->Event(ArgBeginTok, PPCallbacks::BeginMacroCallArgument,
1218- + reinterpret_cast<uintptr_t>(&SavedIdentifier));
1191+ + // Parse next token.
1192+ + auto &SubTok = LexedToks.emplace_back();
1193+ + if (ExpandArgs) {
1194+ + PP.Lex(SubTok);
1195+ + } else {
1196+ + PP.LexUnexpandedToken(SubTok);
1197+ + }
1198+ +
1199+ + switch (SubTok.getKind()) {
1200+ + case tok::eof:
1201+ + case tok::eod:
1202+ + // Don't provide even a dummy value if the eod or eof marker is
1203+ + // reached. Simply provide a diagnostic.
1204+ + PP.Diag(SubTok.getLocation(), diag::err_unterm_macro_invoc);
1205+ + return;
1206+ +
1207+ + case tok::comma:
1208+ + if (1 == ParenDepth) {
1209+ + ArgSepTok = SubTok;
1210+ + if (Callbacks) {
1211+ + Callbacks->Event(ArgSepTok, PPCallbacks::EndMacroCallArgument,
1212+ + reinterpret_cast<uintptr_t>(&ArgSepTok));
1213+ + LastWasSep = true;
1214+ + }
12191215+ }
1220- continue;
1221-
1222- case tok::l_paren:
1223- @@ -1340,6 +1558,7 @@ already_lexed:
1224- if (!SuppressDiagnostic)
1225- PP.Diag(Tok.getLocation(), diag::err_too_few_args_in_macro_invoc);
1226- }
1227- + Done(); // PASTA PATCH
1228- Tok.setKind(tok::numeric_constant);
1229- return;
1230-
1231- @@ -1348,6 +1567,13 @@ already_lexed:
1232- if (Result)
1233- break;
1234-
1235- + // PASTA PATCH
1236- + Done();
1237- + if (Callbacks) {
1238- + Callbacks->Event(SavedIdentifier, PPCallbacks::SwitchToExpansion,
1239- + reinterpret_cast<uintptr_t>(MI));
1216+ + continue;
1217+ +
1218+ + case tok::l_paren:
1219+ + ++ParenDepth;
1220+ + continue;
1221+ +
1222+ + case tok::r_paren:
1223+ + if (!--ParenDepth) {
1224+ + DoneLexingCall = true;
1225+ + ArgSepTok = Tok;
12401226+ }
1227+ + continue;
12411228+
1242- bool HasLexedNextToken = false;
1243- Result = Op(Tok, HasLexedNextToken);
1244- ResultTok = Tok;
1245- @@ -1370,6 +1596,8 @@ already_lexed:
1246- SuppressDiagnostic = true;
1247- }
1248- }
1229+ + default:
1230+ + continue;
1231+ + }
1232+ + }
12491233+
1250- + Done(); // PASTA PATCH
1251- }
1252-
1253- /// Helper function to return the IdentifierInfo structure of a Token
1254- @@ -1493,7 +1721,22 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1234+ + if (Callbacks) {
1235+ + Callbacks->Event(ArgSepTok, PPCallbacks::EndMacroCallArgument,
1236+ + reinterpret_cast<uintptr_t>(&ArgSepTok));
1237+ + Callbacks->Event(ArgSepTok, PPCallbacks::EndMacroCallArgumentList, 0);
1238+ + Callbacks->Event(SavedIdentifier, PPCallbacks::SwitchToExpansion,
1239+ + reinterpret_cast<uintptr_t>(MI));
1240+ + }
1241+ +
1242+ + ParenDepth = 1;
1243+ Token ResultTok;
1244+ bool SuppressDiagnostic = false;
1245+ - while (true) {
1246+ - // Parse next token.
1247+ - if (ExpandArgs)
1248+ - PP.Lex(Tok);
1249+ - else
1250+ - PP.LexUnexpandedToken(Tok);
1251+ + for (const auto &LexedTok : LexedToks) { // PASTA PATCH
1252+ + Tok = LexedTok; // PASTA PATCH
1253+
1254+ already_lexed:
1255+ switch (Tok.getKind()) {
1256+ @@ -1493,7 +1747,22 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
12551257 bool IsAtStartOfLine = Tok.isAtStartOfLine();
12561258 bool HasLeadingSpace = Tok.hasLeadingSpace();
12571259
@@ -1274,71 +1276,71 @@ index c56f41c44..573a91bd8 100644
12741276 // C99 6.10.8: "__LINE__: The presumed line number (within the current
12751277 // source file) of the current source line (an integer constant)". This can
12761278 // be affected by #line.
1277- @@ -1516,6 +1759 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1279+ @@ -1516,6 +1785 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
12781280 Tok.setKind(tok::numeric_constant);
12791281 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__ ||
12801282 II == Ident__FILE_NAME__) {
12811283+ SwitchToExpansion(); // PASTA PATCH
12821284 // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
12831285 // character string literal)". This can be affected by #line.
12841286 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1285- @@ -1555,6 +1799 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1287+ @@ -1555,6 +1825 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
12861288 }
12871289 Tok.setKind(tok::string_literal);
12881290 } else if (II == Ident__DATE__) {
12891291+ SwitchToExpansion(); // PASTA PATCH
12901292 Diag(Tok.getLocation(), diag::warn_pp_date_time);
12911293 if (!DATELoc.isValid())
12921294 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1293- @@ -1565,6 +1810 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1295+ @@ -1565,6 +1836 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
12941296 Tok.getLength()));
12951297 return;
12961298 } else if (II == Ident__TIME__) {
12971299+ SwitchToExpansion(); // PASTA PATCH
12981300 Diag(Tok.getLocation(), diag::warn_pp_date_time);
12991301 if (!TIMELoc.isValid())
13001302 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1301- @@ -1575,6 +1821 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1303+ @@ -1575,6 +1847 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
13021304 Tok.getLength()));
13031305 return;
13041306 } else if (II == Ident__INCLUDE_LEVEL__) {
13051307+ SwitchToExpansion(); // PASTA PATCH
13061308 // Compute the presumed include depth of this token. This can be affected
13071309 // by GNU line markers.
13081310 unsigned Depth = 0;
1309- @@ -1590,6 +1837 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1311+ @@ -1590,6 +1863 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
13101312 OS << Depth;
13111313 Tok.setKind(tok::numeric_constant);
13121314 } else if (II == Ident__TIMESTAMP__) {
13131315+ SwitchToExpansion(); // PASTA PATCH
13141316 Diag(Tok.getLocation(), diag::warn_pp_date_time);
13151317 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
13161318 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
1317- @@ -1614,6 +1862 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1319+ @@ -1614,6 +1888 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
13181320 OS << '"' << StringRef(Result).drop_back() << '"';
13191321 Tok.setKind(tok::string_literal);
13201322 } else if (II == Ident__FLT_EVAL_METHOD__) {
13211323+ SwitchToExpansion(); // PASTA PATCH
13221324 // __FLT_EVAL_METHOD__ is set to the default value.
13231325 if (getTUFPEvalMethod() ==
13241326 LangOptions::FPEvalMethodKind::FEM_Indeterminable) {
1325- @@ -1646,6 +1895 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1327+ @@ -1646,6 +1921 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
13261328 }
13271329 }
13281330 } else if (II == Ident__COUNTER__) {
13291331+ SwitchToExpansion(); // PASTA PATCH
13301332 // __COUNTER__ expands to a simple numeric value.
13311333 OS << CounterValue++;
13321334 Tok.setKind(tok::numeric_constant);
1333- @@ -1834,6 +2084 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1335+ @@ -1834,6 +2110 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
13341336 (II->getName() == getLangOpts().CurrentModule);
13351337 });
13361338 } else if (II == Ident__MODULE__) {
13371339+ SwitchToExpansion(); // PASTA PATCH
13381340 // The current module as an identifier.
13391341 OS << getLangOpts().CurrentModule;
13401342 IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule);
1341- @@ -1842,6 +2093 ,12 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1343+ @@ -1842,6 +2119 ,12 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
13421344 } else if (II == Ident__identifier) {
13431345 SourceLocation Loc = Tok.getLocation();
13441346
@@ -1351,7 +1353,7 @@ index c56f41c44..573a91bd8 100644
13511353 // We're expecting '__identifier' '(' identifier ')'. Try to recover
13521354 // if the parens are missing.
13531355 LexNonComment(Tok);
1354- @@ -1855,6 +2112 ,13 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1356+ @@ -1855,6 +2138 ,13 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
13551357 return;
13561358 }
13571359
@@ -1365,7 +1367,7 @@ index c56f41c44..573a91bd8 100644
13651367 SourceLocation LParenLoc = Tok.getLocation();
13661368 LexNonComment(Tok);
13671369
1368- @@ -1883,6 +2147 ,15 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1370+ @@ -1883,6 +2173 ,15 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
13691371 << Tok.getKind() << tok::r_paren;
13701372 Diag(LParenLoc, diag::note_matching) << tok::l_paren;
13711373 }
0 commit comments