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;
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
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
void setKind(tok::TokenKind K)
const std::vector< Token > & getPreExpArgument(unsigned Arg, const MacroInfo *MI, Preprocessor &PP)
void destroy(Preprocessor &PP)
const Token * getUnexpArgument(unsigned Arg) const
tok::TokenKind getKind() const
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const
const Token & getStringifiedArgument(unsigned ArgNo, Preprocessor &PP, SourceLocation ExpansionLocStart, SourceLocation ExpansionLocEnd)
static Token StringifyArgument(const Token *ArgToks, Preprocessor &PP, bool Charify, SourceLocation ExpansionLocStart, SourceLocation ExpansionLocEnd)
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
Encodes a location in the source. The SourceManager can decode this to get at the full include stack...
void EnterTokenStream(const Token *Toks, unsigned NumToks, bool DisableMacroExpansion, bool OwnsTokens)
Add a "macro" context to the top of the include stack, which will cause the lexer to start returning ...
void Lex(Token &Result)
Lex the next token for this preprocessor.
bool ArgNeedsPreexpansion(const Token *ArgTok, Preprocessor &PP) const
bool is(tok::TokenKind K) const
static unsigned getArgLength(const Token *ArgPtr)
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. its tokens).
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 - This destroys the vector passed in.
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)
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
IdentifierInfo * getIdentifierInfo() const