17 #include "llvm/ADT/SmallString.h" 18 #include "llvm/Support/SaveAndRestore.h" 21 using namespace clang;
28 "Can't have args for an object-like macro!");
30 unsigned ClosestMatch = ~0U;
34 for (
MacroArgs **Entry = &PP.MacroArgCache; *Entry;
35 Entry = &(*Entry)->ArgCache) {
36 if ((*Entry)->NumUnexpArgTokens >= UnexpArgTokens.size() &&
37 (*Entry)->NumUnexpArgTokens < ClosestMatch) {
41 if ((*Entry)->NumUnexpArgTokens == UnexpArgTokens.size())
44 ClosestMatch = (*Entry)->NumUnexpArgTokens;
52 llvm::safe_malloc(totalSizeToAlloc<Token>(UnexpArgTokens.size())))
57 *ResultEnt = Result->ArgCache;
58 Result->NumUnexpArgTokens = UnexpArgTokens.size();
59 Result->VarargsElided = VarargsElided;
64 if (!UnexpArgTokens.empty()) {
65 static_assert(std::is_trivial<Token>::value,
66 "assume trivial copyability if copying into the " 67 "uninitialized array (as opposed to reusing a cached " 69 std::copy(UnexpArgTokens.begin(), UnexpArgTokens.end(),
70 Result->getTrailingObjects<
Token>());
81 for (
unsigned i = 0, e = PreExpArgTokens.size(); i != e; ++i)
82 PreExpArgTokens[i].
clear();
85 ArgCache = PP.MacroArgCache;
86 PP.MacroArgCache =
this;
97 static_assert(std::is_trivially_destructible<Token>::value,
98 "assume trivially destructible and forego destructors");
109 unsigned NumArgTokens = 0;
123 const Token *Start = getTrailingObjects<Token>();
128 assert(Result < Start+NumUnexpArgTokens &&
"Invalid arg #");
132 assert(Result < Start+NumUnexpArgTokens &&
"Invalid arg #");
152 if (II->hasMacroDefinition())
169 std::vector<Token> &
Result = PreExpArgTokens[Arg];
170 if (!Result.empty())
return Result;
181 PP.EnterTokenStream(AT, NumToks,
false ,
186 Result.push_back(
Token());
189 }
while (Result.back().isNot(
tok::eof));
196 if (PP.InCachingLexMode())
197 PP.ExitCachingLexMode();
214 Tok.
setKind(Charify ? tok::char_constant : tok::string_literal);
216 const Token *ArgTokStart = ArgToks;
224 const Token &Tok = *ArgToks;
232 Tok.
is(tok::char_constant) ||
233 Tok.
is(tok::wide_char_constant) ||
234 Tok.
is(tok::utf8_char_constant) ||
235 Tok.
is(tok::utf16_char_constant) ||
236 Tok.
is(tok::utf32_char_constant)) {
237 bool Invalid =
false;
238 std::string TokStr = PP.
getSpelling(Tok, &Invalid);
241 Result.append(Str.begin(), Str.end());
243 }
else if (Tok.
is(tok::code_completion)) {
248 unsigned CurStrLen = Result.size();
249 Result.resize(CurStrLen+Tok.
getLength());
250 const char *BufPtr = Result.data() + CurStrLen;
251 bool Invalid =
false;
252 unsigned ActualTokLen = PP.
getSpelling(Tok, BufPtr, &Invalid);
257 if (ActualTokLen && BufPtr != &Result[CurStrLen])
258 memcpy(&Result[CurStrLen], BufPtr, ActualTokLen);
262 Result.resize(CurStrLen+ActualTokLen);
269 if (Result.back() ==
'\\') {
272 unsigned FirstNonSlash = Result.size()-2;
274 while (Result[FirstNonSlash] ==
'\\')
276 if ((Result.size()-1-FirstNonSlash) & 1) {
278 PP.
Diag(ArgToks[-1], diag::pp_invalid_string_literal);
289 Result[Result.size()-1] =
'\'';
293 if (Result.size() == 3)
294 isBad = Result[1] ==
'\'';
296 isBad = (Result.size() != 4 || Result[1] !=
'\\');
299 PP.
Diag(ArgTokStart[0], diag::err_invalid_character_to_charify);
305 ExpansionLocStart, ExpansionLocEnd);
bool ArgNeedsPreexpansion(const Token *ArgTok, Preprocessor &PP) const
ArgNeedsPreexpansion - If we can prove that the argument won't be affected by pre-expansion, return false.
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {...
Defines the clang::MacroInfo and clang::MacroDirective classes.
bool isStringLiteral(TokenKind K)
Return true if this is a C or C++ string-literal (or C++11 user-defined-string-literal) token...
tok::TokenKind getKind() const
One of these records is kept for each identifier that is lexed.
Token - This structure provides full information about a lexed token.
void setKind(tok::TokenKind K)
StringRef getSpelling(SourceLocation loc, SmallVectorImpl< char > &buffer, bool *invalid=nullptr) const
Return the 'spelling' of the token at the given location; does not go up to the spelling location or ...
void destroy(Preprocessor &PP)
destroy - Destroy and deallocate the memory for this object.
static Token StringifyArgument(const Token *ArgToks, Preprocessor &PP, bool Charify, SourceLocation ExpansionLocStart, SourceLocation ExpansionLocEnd)
StringifyArgument - Implement C99 6.10.3.2p2, converting a sequence of tokens into the literal string...
unsigned getNumParams() const
bool invokedWithVariadicArgument(const MacroInfo *const MI, Preprocessor &PP)
Returns true if the macro was defined with a variadic (ellipsis) parameter AND was invoked with at le...
MacroArgs - An instance of this class captures information about the formal arguments specified to a ...
Defines the clang::Preprocessor interface.
The result type of a method or function.
MacroArgs * deallocate()
deallocate - This should only be called by the Preprocessor when managing its freelist.
Encodes a location in the source.
IdentifierInfo * getIdentifierInfo() const
void Lex(Token &Result)
Lex the next token for this preprocessor.
bool isAtStartOfLine() const
isAtStartOfLine - Return true if this token is at the start of a line.
__DEVICE__ void * memcpy(void *__a, const void *__b, size_t __c)
bool isNot(tok::TokenKind K) const
Dataflow Directional Tag Classes.
bool isFunctionLike() const
const std::vector< Token > & getPreExpArgument(unsigned Arg, Preprocessor &PP)
getPreExpArgument - Return the pre-expanded form of the specified argument.
static unsigned getArgLength(const Token *ArgPtr)
getArgLength - Given a pointer to an expanded or unexpanded argument, return the number of tokens...
void CodeCompleteNaturalLanguage()
Hook used by the lexer to invoke the "natural language" code completion point.
unsigned getLength() const
unsigned getNumMacroArguments() const
getNumMacroArguments - Return the number of arguments the invoked macro expects.
Encapsulates the data about a macro definition (e.g.
void CreateString(StringRef Str, Token &Tok, SourceLocation ExpansionLocStart=SourceLocation(), SourceLocation ExpansionLocEnd=SourceLocation())
Plop the specified string into a scratch buffer and set the specified token's location and length to ...
static MacroArgs * create(const MacroInfo *MI, ArrayRef< Token > UnexpArgTokens, bool VarargsElided, Preprocessor &PP)
MacroArgs ctor function - Create a new MacroArgs object with the specified macro and argument info...
void RemoveTopOfLexerStack()
Pop the current lexer/macro exp off the top of the lexer stack.
bool hasLeadingSpace() const
Return true if this token has whitespace before it.
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const
Forwarding function for diagnostics.
const Token * getUnexpArgument(unsigned Arg) const
getUnexpArgument - Return a pointer to the first token of the unexpanded token list for the specified...
void startToken()
Reset all flags to cleared.
static std::string Stringify(StringRef Str, bool Charify=false)
Stringify - Convert the specified string into a C string by i) escaping '\' and " characters and ii) ...
Engages in a tight little dance with the lexer to efficiently preprocess tokens.