1- From ea53234d5061cfc725c9b21798ed39ce09ac3e8e Mon Sep 17 00:00:00 2001
1+ From c18338dd444d7252e22735d3ee6c50c2320a7f17 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 +++++++++
8- clang/include/clang/Lex/Preprocessor.h | 46 ++--
7+ clang/include/clang/Lex/PPCallbacks.h | 120 +++++++++++
8+ 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 | 323 ++++++++++++++++++++++++-
14- clang/lib/Lex/Pragma.cpp | 59 +++++
12+ clang/lib/Lex/PPLexerChange.cpp | 38 ++++
13+ clang/lib/Lex/PPMacroExpansion.cpp | 267 ++++++++++++++++++++++++-
14+ 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, 772 insertions(+), 85 deletions(-)
18+ 11 files changed, 722 insertions(+), 79 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..14a91d86b 100644
828+ index c56f41c44..5f61b2476 100644
829829--- a/clang/lib/Lex/PPMacroExpansion.cpp
830830+++ b/clang/lib/Lex/PPMacroExpansion.cpp
831831@@ -60,6 +60,58 @@
@@ -1147,11 +1147,10 @@ index c56f41c44..14a91d86b 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,6 +1446,14 @@ static void EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS,
1150+ @@ -1270,6 +1446,13 @@ static void EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS,
11511151 llvm::function_ref<
11521152 int(Token &Tok,
11531153 bool &HasLexedNextTok)> Op) {
1154- +
11551154+ // PASTA PATCH: Visibility into macro expansion.
11561155+ PPCallbacks *Callbacks = PP.getPPCallbacks();
11571156+ Token SavedIdentifier = Tok;
@@ -1162,98 +1161,55 @@ index c56f41c44..14a91d86b 100644
11621161 // Parse the initial '('.
11631162 PP.LexUnexpandedToken(Tok);
11641163 if (Tok.isNot(tok::l_paren)) {
1165- @@ -1288,14 +1472,84 @@ static void EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS,
1164+ @@ -1288,6 +1471,15 @@ static void EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS,
11661165 SourceLocation LParenLoc = Tok.getLocation();
11671166 llvm::Optional<int> Result;
11681167
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;
1168+ + // PASTA PATCH
1169+ + clang::Token ArgSepTok = Tok;
11761170+ if (Callbacks) {
11771171+ Callbacks->Event(SavedIdentifier, PPCallbacks::BeginMacroCallArgumentList,
11781172+ reinterpret_cast<uintptr_t>(MI));
1173+ + Callbacks->Event(ArgSepTok, PPCallbacks::BeginMacroCallArgument,
1174+ + reinterpret_cast<uintptr_t>(&SavedIdentifier));
11791175+ }
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;
1189- + }
1190- +
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- + }
1215- + }
1216- + continue;
1217- +
1218- + case tok::l_paren:
1219- + ++ParenDepth;
1220- + continue;
12211176+
1222- + case tok::r_paren:
1223- + if (!--ParenDepth) {
1224- + DoneLexingCall = true;
1177+ Token ResultTok;
1178+ bool SuppressDiagnostic = false;
1179+ while (true) {
1180+ @@ -1307,6 +1499,15 @@ already_lexed:
1181+ return;
1182+
1183+ case tok::comma:
1184+ + // PASTA PATCH
1185+ + if (ParenDepth == 1 && Callbacks) {
1186+ + Callbacks->Event(ArgSepTok, PPCallbacks::EndMacroCallArgument,
1187+ + reinterpret_cast<uintptr_t>(&Tok));
12251188+ ArgSepTok = Tok;
1189+ + Callbacks->Event(ArgSepTok, PPCallbacks::BeginMacroCallArgument,
1190+ + reinterpret_cast<uintptr_t>(&SavedIdentifier));
12261191+ }
1227- + continue;
1228- +
1229- + default:
1230- + continue;
1231- + }
1232- + }
12331192+
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- + }
1193+ if (!SuppressDiagnostic) {
1194+ PP.Diag(Tok.getLocation(), diag::err_too_many_args_in_macro_invoc);
1195+ SuppressDiagnostic = true;
1196+ @@ -1327,6 +1528,15 @@ already_lexed:
1197+ if (--ParenDepth > 0)
1198+ continue;
1199+
1200+ + // PASTA PATCH
1201+ + if (Callbacks) {
1202+ + Callbacks->Event(ArgSepTok, PPCallbacks::EndMacroCallArgument,
1203+ + reinterpret_cast<uintptr_t>(&Tok));
1204+ + Callbacks->Event(ArgSepTok, PPCallbacks::EndMacroCallArgumentList, 0);
1205+ + Callbacks->Event(SavedIdentifier, PPCallbacks::SwitchToExpansion,
1206+ + reinterpret_cast<uintptr_t>(MI));
1207+ + }
12411208+
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) {
1209+ // The last ')' has been reached; return the value if one found or
1210+ // a diagnostic and a dummy value.
1211+ if (Result) {
1212+ @@ -1493,7 +1703,22 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
12571213 bool IsAtStartOfLine = Tok.isAtStartOfLine();
12581214 bool HasLeadingSpace = Tok.hasLeadingSpace();
12591215
@@ -1276,71 +1232,71 @@ index c56f41c44..14a91d86b 100644
12761232 // C99 6.10.8: "__LINE__: The presumed line number (within the current
12771233 // source file) of the current source line (an integer constant)". This can
12781234 // be affected by #line.
1279- @@ -1516,6 +1785 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1235+ @@ -1516,6 +1741 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
12801236 Tok.setKind(tok::numeric_constant);
12811237 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__ ||
12821238 II == Ident__FILE_NAME__) {
12831239+ SwitchToExpansion(); // PASTA PATCH
12841240 // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
12851241 // character string literal)". This can be affected by #line.
12861242 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1287- @@ -1555,6 +1825 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1243+ @@ -1555,6 +1781 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
12881244 }
12891245 Tok.setKind(tok::string_literal);
12901246 } else if (II == Ident__DATE__) {
12911247+ SwitchToExpansion(); // PASTA PATCH
12921248 Diag(Tok.getLocation(), diag::warn_pp_date_time);
12931249 if (!DATELoc.isValid())
12941250 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1295- @@ -1565,6 +1836 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1251+ @@ -1565,6 +1792 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
12961252 Tok.getLength()));
12971253 return;
12981254 } else if (II == Ident__TIME__) {
12991255+ SwitchToExpansion(); // PASTA PATCH
13001256 Diag(Tok.getLocation(), diag::warn_pp_date_time);
13011257 if (!TIMELoc.isValid())
13021258 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1303- @@ -1575,6 +1847 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1259+ @@ -1575,6 +1803 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
13041260 Tok.getLength()));
13051261 return;
13061262 } else if (II == Ident__INCLUDE_LEVEL__) {
13071263+ SwitchToExpansion(); // PASTA PATCH
13081264 // Compute the presumed include depth of this token. This can be affected
13091265 // by GNU line markers.
13101266 unsigned Depth = 0;
1311- @@ -1590,6 +1863 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1267+ @@ -1590,6 +1819 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
13121268 OS << Depth;
13131269 Tok.setKind(tok::numeric_constant);
13141270 } else if (II == Ident__TIMESTAMP__) {
13151271+ SwitchToExpansion(); // PASTA PATCH
13161272 Diag(Tok.getLocation(), diag::warn_pp_date_time);
13171273 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
13181274 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
1319- @@ -1614,6 +1888 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1275+ @@ -1614,6 +1844 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
13201276 OS << '"' << StringRef(Result).drop_back() << '"';
13211277 Tok.setKind(tok::string_literal);
13221278 } else if (II == Ident__FLT_EVAL_METHOD__) {
13231279+ SwitchToExpansion(); // PASTA PATCH
13241280 // __FLT_EVAL_METHOD__ is set to the default value.
13251281 if (getTUFPEvalMethod() ==
13261282 LangOptions::FPEvalMethodKind::FEM_Indeterminable) {
1327- @@ -1646,6 +1921 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1283+ @@ -1646,6 +1877 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
13281284 }
13291285 }
13301286 } else if (II == Ident__COUNTER__) {
13311287+ SwitchToExpansion(); // PASTA PATCH
13321288 // __COUNTER__ expands to a simple numeric value.
13331289 OS << CounterValue++;
13341290 Tok.setKind(tok::numeric_constant);
1335- @@ -1834,6 +2110 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1291+ @@ -1834,6 +2066 ,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
13361292 (II->getName() == getLangOpts().CurrentModule);
13371293 });
13381294 } else if (II == Ident__MODULE__) {
13391295+ SwitchToExpansion(); // PASTA PATCH
13401296 // The current module as an identifier.
13411297 OS << getLangOpts().CurrentModule;
13421298 IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule);
1343- @@ -1842,6 +2119 ,12 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1299+ @@ -1842,6 +2075 ,12 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
13441300 } else if (II == Ident__identifier) {
13451301 SourceLocation Loc = Tok.getLocation();
13461302
@@ -1353,7 +1309,7 @@ index c56f41c44..14a91d86b 100644
13531309 // We're expecting '__identifier' '(' identifier ')'. Try to recover
13541310 // if the parens are missing.
13551311 LexNonComment(Tok);
1356- @@ -1855,6 +2138 ,13 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1312+ @@ -1855,6 +2094 ,13 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
13571313 return;
13581314 }
13591315
@@ -1367,7 +1323,7 @@ index c56f41c44..14a91d86b 100644
13671323 SourceLocation LParenLoc = Tok.getLocation();
13681324 LexNonComment(Tok);
13691325
1370- @@ -1883,6 +2173 ,15 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1326+ @@ -1883,6 +2129 ,15 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
13711327 << Tok.getKind() << tok::r_paren;
13721328 Diag(LParenLoc, diag::note_matching) << tok::l_paren;
13731329 }
0 commit comments