18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/Support/SaveAndRestore.h"
22 using namespace clang;
29 "Can't have args for an object-like macro!");
31 unsigned ClosestMatch = ~0U;
35 for (
MacroArgs **Entry = &PP.MacroArgCache; *Entry;
36 Entry = &(*Entry)->ArgCache)
37 if ((*Entry)->NumUnexpArgTokens >= UnexpArgTokens.size() &&
38 (*Entry)->NumUnexpArgTokens < ClosestMatch) {
42 if ((*Entry)->NumUnexpArgTokens == UnexpArgTokens.size())
45 ClosestMatch = (*Entry)->NumUnexpArgTokens;
52 UnexpArgTokens.size() *
sizeof(
Token));
59 *ResultEnt = Result->ArgCache;
60 Result->NumUnexpArgTokens = UnexpArgTokens.size();
61 Result->VarargsElided = VarargsElided;
66 if (!UnexpArgTokens.empty())
67 std::copy(UnexpArgTokens.begin(), UnexpArgTokens.end(),
76 StringifiedArgs.clear();
80 for (
unsigned i = 0, e = PreExpArgTokens.size(); i != e; ++i)
81 PreExpArgTokens[i].
clear();
84 ArgCache = PP.MacroArgCache;
85 PP.MacroArgCache =
this;
106 unsigned NumArgTokens = 0;
122 assert(Result < Start+NumUnexpArgTokens &&
"Invalid arg #");
126 assert(Result < Start+NumUnexpArgTokens &&
"Invalid arg #");
139 if (II->hasMacroDefinition())
148 const std::vector<Token> &
151 assert(Arg < MI->getNumParams() &&
"Invalid argument number!");
157 std::vector<Token> &
Result = PreExpArgTokens[Arg];
158 if (!Result.empty())
return Result;
169 PP.EnterTokenStream(AT, NumToks,
false ,
174 Result.push_back(
Token());
175 Token &Tok = Result.back();
177 }
while (Result.back().isNot(
tok::eof));
184 if (PP.InCachingLexMode())
185 PP.ExitCachingLexMode();
202 Tok.
setKind(Charify ? tok::char_constant : tok::string_literal);
204 const Token *ArgTokStart = ArgToks;
212 const Token &Tok = *ArgToks;
220 Tok.
is(tok::char_constant) ||
221 Tok.
is(tok::wide_char_constant) ||
222 Tok.
is(tok::utf8_char_constant) ||
223 Tok.
is(tok::utf16_char_constant) ||
224 Tok.
is(tok::utf32_char_constant)) {
225 bool Invalid =
false;
226 std::string TokStr = PP.
getSpelling(Tok, &Invalid);
229 Result.append(Str.begin(), Str.end());
231 }
else if (Tok.
is(tok::code_completion)) {
236 unsigned CurStrLen = Result.size();
237 Result.resize(CurStrLen+Tok.
getLength());
238 const char *BufPtr = Result.data() + CurStrLen;
239 bool Invalid =
false;
240 unsigned ActualTokLen = PP.
getSpelling(Tok, BufPtr, &Invalid);
245 if (ActualTokLen && BufPtr != &Result[CurStrLen])
246 memcpy(&Result[CurStrLen], BufPtr, ActualTokLen);
250 Result.resize(CurStrLen+ActualTokLen);
257 if (Result.back() ==
'\\') {
260 unsigned FirstNonSlash = Result.size()-2;
262 while (Result[FirstNonSlash] ==
'\\')
264 if ((Result.size()-1-FirstNonSlash) & 1) {
266 PP.
Diag(ArgToks[-1], diag::pp_invalid_string_literal);
277 Result[Result.size()-1] =
'\'';
281 if (Result.size() == 3)
282 isBad = Result[1] ==
'\'';
284 isBad = (Result.size() != 4 || Result[1] !=
'\\');
287 PP.
Diag(ArgTokStart[0], diag::err_invalid_character_to_charify);
293 ExpansionLocStart, ExpansionLocEnd);
304 if (StringifiedArgs.empty())
307 if (StringifiedArgs[ArgNo].isNot(tok::string_literal))
312 return StringifiedArgs[ArgNo];
bool isAtStartOfLine() const
isAtStartOfLine - Return true if this token is at the start of a line.
unsigned getNumMacroArguments() const
getNumMacroArguments - Return the number of arguments the invoked macro expects.
Defines the clang::MacroInfo and clang::MacroDirective classes.
bool hasLeadingSpace() const
Return true if this token has whitespace before it.
bool isStringLiteral(TokenKind K)
Return true if this is a C or C++ string-literal (or C++11 user-defined-string-literal) token...
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 ...
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)
const std::vector< Token > & getPreExpArgument(unsigned Arg, const MacroInfo *MI, Preprocessor &PP)
getPreExpArgument - Return the pre-expanded form of the specified argument.
void destroy(Preprocessor &PP)
destroy - Destroy and deallocate the memory for this object.
const Token * getUnexpArgument(unsigned Arg) const
getUnexpArgument - Return a pointer to the first token of the unexpanded token list for the specified...
tok::TokenKind getKind() const
unsigned getNumParams() const
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const
Forwarding function for diagnostics.
const Token & getStringifiedArgument(unsigned ArgNo, Preprocessor &PP, SourceLocation ExpansionLocStart, SourceLocation ExpansionLocEnd)
getStringifiedArgument - Compute, cache, and return the specified argument that has been 'stringified...
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...
MacroArgs - An instance of this class captures information about the formal arguments specified to a ...
Defines the clang::Preprocessor interface.
bool isNot(tok::TokenKind K) const
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.
void Lex(Token &Result)
Lex the next token for this preprocessor.
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)) {...
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.
bool isFunctionLike() const
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.
unsigned getLength() const
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 escaping '\' and " characters. This does not add surrounding ""'s to the string.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
IdentifierInfo * getIdentifierInfo() const