Skip to content

Commit

Permalink
Merge pull request #996 from lifting-bits/pasta_patches
Browse files Browse the repository at this point in the history
More patches for function-like builtin feature macros
  • Loading branch information
pgoodman authored Dec 2, 2022
2 parents 1e979f6 + d5c9cb8 commit 365c59e
Showing 1 changed file with 103 additions and 101 deletions.
204 changes: 103 additions & 101 deletions ports/llvm-15/0025-PASTA-patches.patch
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
From 6b7b31df2a614557d3c86fed4779535f3f782f19 Mon Sep 17 00:00:00 2001
From ea53234d5061cfc725c9b21798ed39ce09ac3e8e Mon Sep 17 00:00:00 2001
From: Peter Goodman <[email protected]>
Date: Mon, 14 Nov 2022 14:51:27 -0500
Subject: [PATCH] Patches for PASTA

---
clang/include/clang/Lex/PPCallbacks.h | 120 +++++++++++
clang/include/clang/Lex/PPCallbacks.h | 120 +++++++++
clang/include/clang/Lex/Preprocessor.h | 46 ++--
clang/include/clang/Lex/TokenLexer.h | 7 +-
clang/lib/Lex/PPDirectives.cpp | 147 +++++++++----
clang/lib/Lex/PPDirectives.cpp | 147 +++++++----
clang/lib/Lex/PPExpressions.cpp | 28 +++
clang/lib/Lex/PPLexerChange.cpp | 38 ++++
clang/lib/Lex/PPMacroExpansion.cpp | 285 ++++++++++++++++++++++++-
clang/lib/Lex/PPLexerChange.cpp | 38 +++
clang/lib/Lex/PPMacroExpansion.cpp | 323 ++++++++++++++++++++++++-
clang/lib/Lex/Pragma.cpp | 59 +++++
clang/lib/Lex/Preprocessor.cpp | 43 +++-
clang/lib/Lex/TokenLexer.cpp | 39 +++-
clang/lib/Lex/TokenLexer.cpp | 39 ++-
clang/lib/Parse/ParseTemplate.cpp | 7 +
11 files changed, 740 insertions(+), 79 deletions(-)
11 files changed, 772 insertions(+), 85 deletions(-)

diff --git a/clang/include/clang/Lex/PPCallbacks.h b/clang/include/clang/Lex/PPCallbacks.h
index 045df8711..cba195cff 100644
Expand Down Expand Up @@ -825,7 +825,7 @@ index 36d3aa59b..19697799e 100644
MacroExpandingLexersStack.back().first == CurTokenLexer.get())
removeCachedMacroExpandedTokensOfLastLexer();
diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp
index c56f41c44..573a91bd8 100644
index c56f41c44..14a91d86b 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -60,6 +60,58 @@
Expand Down Expand Up @@ -1147,7 +1147,7 @@ index c56f41c44..573a91bd8 100644
bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
// If GetIncludeFilenameSpelling set the start ptr to null, there was an
// error.
@@ -1270,9 +1446,27 @@ static void EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS,
@@ -1270,6 +1446,14 @@ static void EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS,
llvm::function_ref<
int(Token &Tok,
bool &HasLexedNextTok)> Op) {
Expand All @@ -1158,100 +1158,102 @@ index c56f41c44..573a91bd8 100644
+ SavedIdentifier.setIdentifierInfo(II);
+ MacroDefinition MD = PP.getMacroDefinition(II);
+ MacroInfo *MI = MD.getMacroInfo();
+ if (Callbacks) {
+ Callbacks->Event(SavedIdentifier, PPCallbacks::BeginMacroCallArgumentList,
+ reinterpret_cast<uintptr_t>(MI));
+ }
+
// Parse the initial '('.
PP.LexUnexpandedToken(Tok);
if (Tok.isNot(tok::l_paren)) {
+
+ // PASTA PATCH
+ if (Callbacks) {
+ Callbacks->Event(Tok, PPCallbacks::EndMacroCallArgumentList, 0);
+ }
+
PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II
<< tok::l_paren;

@@ -1284,6 +1478,22 @@ static void EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS,
return;
}
@@ -1288,14 +1472,84 @@ static void EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS,
SourceLocation LParenLoc = Tok.getLocation();
llvm::Optional<int> Result;

+ // PASTA PATCH
+ Token ArgBeginTok = Tok;
+ // PASTA PATCH: Re-create the loop that collects the function-like macro use,
+ // do the lexing and argument splitting ourselves, then go and
+ // run the original loop over our pre-lexed loop.
+ Token ArgSepTok = Tok;
+ SmallVector<clang::Token, 6> LexedToks;
+ bool DoneLexingCall = false;
+ bool LastWasSep = true;
+ if (Callbacks) {
+ Callbacks->Event(ArgBeginTok, PPCallbacks::BeginMacroCallArgument,
+ reinterpret_cast<uintptr_t>(&SavedIdentifier));
+ Callbacks->Event(SavedIdentifier, PPCallbacks::BeginMacroCallArgumentList,
+ reinterpret_cast<uintptr_t>(MI));
+ }
+ bool DoneDone = false;
+ auto Done = [&] (void) {
+ if (!DoneDone && Callbacks) {
+ DoneDone = true;
+ Callbacks->Event(ArgBeginTok, PPCallbacks::EndMacroCallArgument,
+ reinterpret_cast<uintptr_t>(&Tok));
+ Callbacks->Event(Tok, PPCallbacks::EndMacroCallArgumentList, 0);
+ while (!DoneLexingCall) {
+
+ // Split before the first argument, or the Nth argument.
+ if (LastWasSep) {
+ if (Callbacks) {
+ Callbacks->Event(ArgSepTok, PPCallbacks::BeginMacroCallArgument,
+ reinterpret_cast<uintptr_t>(&SavedIdentifier));
+ }
+ LastWasSep = false;
+ }
+ };
+
unsigned ParenDepth = 1;
SourceLocation LParenLoc = Tok.getLocation();
llvm::Optional<int> Result;
@@ -1304,6 +1514,7 @@ already_lexed:
// Don't provide even a dummy value if the eod or eof marker is
// reached. Simply provide a diagnostic.
PP.Diag(Tok.getLocation(), diag::err_unterm_macro_invoc);
+ Done(); // PASTA PATCH
return;

case tok::comma:
@@ -1311,6 +1522,13 @@ already_lexed:
PP.Diag(Tok.getLocation(), diag::err_too_many_args_in_macro_invoc);
SuppressDiagnostic = true;
}
+ if (Callbacks) { // PASTA PATCH
+ Callbacks->Event(ArgBeginTok, PPCallbacks::EndMacroCallArgument,
+ reinterpret_cast<uintptr_t>(&Tok));
+ ArgBeginTok = Tok;
+ Callbacks->Event(ArgBeginTok, PPCallbacks::BeginMacroCallArgument,
+ reinterpret_cast<uintptr_t>(&SavedIdentifier));
+ // Parse next token.
+ auto &SubTok = LexedToks.emplace_back();
+ if (ExpandArgs) {
+ PP.Lex(SubTok);
+ } else {
+ PP.LexUnexpandedToken(SubTok);
+ }
+
+ switch (SubTok.getKind()) {
+ case tok::eof:
+ case tok::eod:
+ // Don't provide even a dummy value if the eod or eof marker is
+ // reached. Simply provide a diagnostic.
+ PP.Diag(SubTok.getLocation(), diag::err_unterm_macro_invoc);
+ return;
+
+ case tok::comma:
+ if (1 == ParenDepth) {
+ ArgSepTok = SubTok;
+ if (Callbacks) {
+ Callbacks->Event(ArgSepTok, PPCallbacks::EndMacroCallArgument,
+ reinterpret_cast<uintptr_t>(&ArgSepTok));
+ LastWasSep = true;
+ }
+ }
continue;

case tok::l_paren:
@@ -1340,6 +1558,7 @@ already_lexed:
if (!SuppressDiagnostic)
PP.Diag(Tok.getLocation(), diag::err_too_few_args_in_macro_invoc);
}
+ Done(); // PASTA PATCH
Tok.setKind(tok::numeric_constant);
return;

@@ -1348,6 +1567,13 @@ already_lexed:
if (Result)
break;

+ // PASTA PATCH
+ Done();
+ if (Callbacks) {
+ Callbacks->Event(SavedIdentifier, PPCallbacks::SwitchToExpansion,
+ reinterpret_cast<uintptr_t>(MI));
+ continue;
+
+ case tok::l_paren:
+ ++ParenDepth;
+ continue;
+
+ case tok::r_paren:
+ if (!--ParenDepth) {
+ DoneLexingCall = true;
+ ArgSepTok = Tok;
+ }
+ continue;
+
bool HasLexedNextToken = false;
Result = Op(Tok, HasLexedNextToken);
ResultTok = Tok;
@@ -1370,6 +1596,8 @@ already_lexed:
SuppressDiagnostic = true;
}
}
+ default:
+ continue;
+ }
+ }
+
+ Done(); // PASTA PATCH
}

/// Helper function to return the IdentifierInfo structure of a Token
@@ -1493,7 +1721,22 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
+ if (Callbacks) {
+ Callbacks->Event(ArgSepTok, PPCallbacks::EndMacroCallArgument,
+ reinterpret_cast<uintptr_t>(&ArgSepTok));
+ Callbacks->Event(ArgSepTok, PPCallbacks::EndMacroCallArgumentList, 0);
+ Callbacks->Event(SavedIdentifier, PPCallbacks::SwitchToExpansion,
+ reinterpret_cast<uintptr_t>(MI));
+ }
+
+ ParenDepth = 1;
Token ResultTok;
bool SuppressDiagnostic = false;
- while (true) {
- // Parse next token.
- if (ExpandArgs)
- PP.Lex(Tok);
- else
- PP.LexUnexpandedToken(Tok);
+ for (const auto &LexedTok : LexedToks) { // PASTA PATCH
+ Tok = LexedTok; // PASTA PATCH

already_lexed:
switch (Tok.getKind()) {
@@ -1493,7 +1747,22 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
bool IsAtStartOfLine = Tok.isAtStartOfLine();
bool HasLeadingSpace = Tok.hasLeadingSpace();

Expand All @@ -1274,71 +1276,71 @@ index c56f41c44..573a91bd8 100644
// C99 6.10.8: "__LINE__: The presumed line number (within the current
// source file) of the current source line (an integer constant)". This can
// be affected by #line.
@@ -1516,6 +1759,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
@@ -1516,6 +1785,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
Tok.setKind(tok::numeric_constant);
} else if (II == Ident__FILE__ || II == Ident__BASE_FILE__ ||
II == Ident__FILE_NAME__) {
+ SwitchToExpansion(); // PASTA PATCH
// C99 6.10.8: "__FILE__: The presumed name of the current source file (a
// character string literal)". This can be affected by #line.
PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
@@ -1555,6 +1799,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
@@ -1555,6 +1825,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
}
Tok.setKind(tok::string_literal);
} else if (II == Ident__DATE__) {
+ SwitchToExpansion(); // PASTA PATCH
Diag(Tok.getLocation(), diag::warn_pp_date_time);
if (!DATELoc.isValid())
ComputeDATE_TIME(DATELoc, TIMELoc, *this);
@@ -1565,6 +1810,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
@@ -1565,6 +1836,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
Tok.getLength()));
return;
} else if (II == Ident__TIME__) {
+ SwitchToExpansion(); // PASTA PATCH
Diag(Tok.getLocation(), diag::warn_pp_date_time);
if (!TIMELoc.isValid())
ComputeDATE_TIME(DATELoc, TIMELoc, *this);
@@ -1575,6 +1821,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
@@ -1575,6 +1847,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
Tok.getLength()));
return;
} else if (II == Ident__INCLUDE_LEVEL__) {
+ SwitchToExpansion(); // PASTA PATCH
// Compute the presumed include depth of this token. This can be affected
// by GNU line markers.
unsigned Depth = 0;
@@ -1590,6 +1837,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
@@ -1590,6 +1863,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
OS << Depth;
Tok.setKind(tok::numeric_constant);
} else if (II == Ident__TIMESTAMP__) {
+ SwitchToExpansion(); // PASTA PATCH
Diag(Tok.getLocation(), diag::warn_pp_date_time);
// MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
// of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
@@ -1614,6 +1862,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
@@ -1614,6 +1888,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
OS << '"' << StringRef(Result).drop_back() << '"';
Tok.setKind(tok::string_literal);
} else if (II == Ident__FLT_EVAL_METHOD__) {
+ SwitchToExpansion(); // PASTA PATCH
// __FLT_EVAL_METHOD__ is set to the default value.
if (getTUFPEvalMethod() ==
LangOptions::FPEvalMethodKind::FEM_Indeterminable) {
@@ -1646,6 +1895,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
@@ -1646,6 +1921,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
}
}
} else if (II == Ident__COUNTER__) {
+ SwitchToExpansion(); // PASTA PATCH
// __COUNTER__ expands to a simple numeric value.
OS << CounterValue++;
Tok.setKind(tok::numeric_constant);
@@ -1834,6 +2084,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
@@ -1834,6 +2110,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
(II->getName() == getLangOpts().CurrentModule);
});
} else if (II == Ident__MODULE__) {
+ SwitchToExpansion(); // PASTA PATCH
// The current module as an identifier.
OS << getLangOpts().CurrentModule;
IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule);
@@ -1842,6 +2093,12 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
@@ -1842,6 +2119,12 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
} else if (II == Ident__identifier) {
SourceLocation Loc = Tok.getLocation();

Expand All @@ -1351,7 +1353,7 @@ index c56f41c44..573a91bd8 100644
// We're expecting '__identifier' '(' identifier ')'. Try to recover
// if the parens are missing.
LexNonComment(Tok);
@@ -1855,6 +2112,13 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
@@ -1855,6 +2138,13 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
return;
}

Expand All @@ -1365,7 +1367,7 @@ index c56f41c44..573a91bd8 100644
SourceLocation LParenLoc = Tok.getLocation();
LexNonComment(Tok);

@@ -1883,6 +2147,15 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
@@ -1883,6 +2173,15 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
<< Tok.getKind() << tok::r_paren;
Diag(LParenLoc, diag::note_matching) << tok::l_paren;
}
Expand Down

0 comments on commit 365c59e

Please sign in to comment.