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));
58 *ResultEnt = Result->ArgCache;
59 Result->NumUnexpArgTokens = UnexpArgTokens.size();
60 Result->VarargsElided = VarargsElided;
64 if (!UnexpArgTokens.empty())
65 std::copy(UnexpArgTokens.begin(), UnexpArgTokens.end(),
74 StringifiedArgs.clear();
78 for (
unsigned i = 0, e = PreExpArgTokens.size(); i != e; ++i)
79 PreExpArgTokens[i].
clear();
82 ArgCache = PP.MacroArgCache;
83 PP.MacroArgCache =
this;
104 unsigned NumArgTokens = 0;
120 assert(Result < Start+NumUnexpArgTokens &&
"Invalid arg #");
124 assert(Result < Start+NumUnexpArgTokens &&
"Invalid arg #");
137 if (II->hasMacroDefinition())
146 const std::vector<Token> &
149 assert(Arg < MI->
getNumArgs() &&
"Invalid argument number!");
152 if (PreExpArgTokens.size() < MI->
getNumArgs())
155 std::vector<Token> &
Result = PreExpArgTokens[Arg];
156 if (!Result.empty())
return Result;
167 PP.EnterTokenStream(AT, NumToks,
false ,
172 Result.push_back(
Token());
173 Token &Tok = Result.back();
175 }
while (Result.back().isNot(
tok::eof));
182 if (PP.InCachingLexMode())
183 PP.ExitCachingLexMode();
200 Tok.
setKind(Charify ? tok::char_constant : tok::string_literal);
202 const Token *ArgTokStart = ArgToks;
210 const Token &Tok = *ArgToks;
218 Tok.
is(tok::char_constant) ||
219 Tok.
is(tok::wide_char_constant) ||
220 Tok.
is(tok::utf8_char_constant) ||
221 Tok.
is(tok::utf16_char_constant) ||
222 Tok.
is(tok::utf32_char_constant)) {
223 bool Invalid =
false;
224 std::string TokStr = PP.
getSpelling(Tok, &Invalid);
227 Result.append(Str.begin(), Str.end());
229 }
else if (Tok.
is(tok::code_completion)) {
234 unsigned CurStrLen = Result.size();
235 Result.resize(CurStrLen+Tok.
getLength());
236 const char *BufPtr = Result.data() + CurStrLen;
237 bool Invalid =
false;
238 unsigned ActualTokLen = PP.
getSpelling(Tok, BufPtr, &Invalid);
243 if (ActualTokLen && BufPtr != &Result[CurStrLen])
244 memcpy(&Result[CurStrLen], BufPtr, ActualTokLen);
248 Result.resize(CurStrLen+ActualTokLen);
255 if (Result.back() ==
'\\') {
258 unsigned FirstNonSlash = Result.size()-2;
260 while (Result[FirstNonSlash] ==
'\\')
262 if ((Result.size()-1-FirstNonSlash) & 1) {
264 PP.
Diag(ArgToks[-1], diag::pp_invalid_string_literal);
275 Result[Result.size()-1] =
'\'';
279 if (Result.size() == 3)
280 isBad = Result[1] ==
'\'';
282 isBad = (Result.size() != 4 || Result[1] !=
'\\');
285 PP.
Diag(ArgTokStart[0], diag::err_invalid_character_to_charify);
291 ExpansionLocStart, ExpansionLocEnd);
301 assert(ArgNo < NumUnexpArgTokens &&
"Invalid argument number!");
302 if (StringifiedArgs.empty()) {
304 memset((
void*)&StringifiedArgs[0], 0,
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.
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
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 ...
unsigned getNumArgs() const
Defines the clang::Preprocessor interface.
bool isNot(tok::TokenKind K) const
The result type of a method or function.
unsigned getNumArguments() const
getNumArguments - Return the number of arguments passed into this macro invocation.
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
unsigned getNumArgs() const
Retrieve the number of template arguments.
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