2014-05-09 08:39:59 +08:00
|
|
|
//===--- MacroArgs.cpp - Formal argument info for Macros ------------------===//
|
2008-03-09 10:55:12 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2014-05-09 08:42:08 +08:00
|
|
|
// This file implements the MacroArgs interface.
|
2008-03-09 10:55:12 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-05-04 06:31:32 +08:00
|
|
|
#include "clang/Lex/MacroArgs.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Lex/LexDiagnostic.h"
|
2008-03-09 10:55:12 +08:00
|
|
|
#include "clang/Lex/MacroInfo.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2012-02-04 21:45:25 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2012-04-04 00:47:40 +08:00
|
|
|
#include "llvm/Support/SaveAndRestore.h"
|
2011-09-22 10:03:12 +08:00
|
|
|
#include <algorithm>
|
|
|
|
|
2008-03-09 10:55:12 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
/// MacroArgs ctor function - This destroys the vector passed in.
|
|
|
|
MacroArgs *MacroArgs::create(const MacroInfo *MI,
|
2013-01-13 03:30:44 +08:00
|
|
|
ArrayRef<Token> UnexpArgTokens,
|
2011-09-22 10:03:12 +08:00
|
|
|
bool VarargsElided, Preprocessor &PP) {
|
2008-03-09 10:55:12 +08:00
|
|
|
assert(MI->isFunctionLike() &&
|
|
|
|
"Can't have args for an object-like macro!");
|
2014-05-18 07:10:59 +08:00
|
|
|
MacroArgs **ResultEnt = nullptr;
|
2009-12-28 14:36:46 +08:00
|
|
|
unsigned ClosestMatch = ~0U;
|
2009-12-16 04:48:12 +08:00
|
|
|
|
|
|
|
// See if we have an entry with a big enough argument list to reuse on the
|
|
|
|
// free list. If so, reuse it.
|
|
|
|
for (MacroArgs **Entry = &PP.MacroArgCache; *Entry;
|
2017-09-28 09:50:23 +08:00
|
|
|
Entry = &(*Entry)->ArgCache) {
|
2011-09-22 10:03:12 +08:00
|
|
|
if ((*Entry)->NumUnexpArgTokens >= UnexpArgTokens.size() &&
|
2009-12-28 14:36:46 +08:00
|
|
|
(*Entry)->NumUnexpArgTokens < ClosestMatch) {
|
|
|
|
ResultEnt = Entry;
|
|
|
|
|
|
|
|
// If we have an exact match, use it.
|
2011-09-22 10:03:12 +08:00
|
|
|
if ((*Entry)->NumUnexpArgTokens == UnexpArgTokens.size())
|
2009-12-28 14:36:46 +08:00
|
|
|
break;
|
|
|
|
// Otherwise, use the best fit.
|
|
|
|
ClosestMatch = (*Entry)->NumUnexpArgTokens;
|
2009-12-16 04:48:12 +08:00
|
|
|
}
|
2017-09-28 09:50:23 +08:00
|
|
|
}
|
2009-12-28 14:36:46 +08:00
|
|
|
MacroArgs *Result;
|
2014-05-18 07:10:59 +08:00
|
|
|
if (!ResultEnt) {
|
2017-09-28 09:50:23 +08:00
|
|
|
// Allocate memory for a MacroArgs object with the lexer tokens at the end,
|
|
|
|
// and construct the MacroArgs object.
|
|
|
|
Result = new (std::malloc(totalSizeToAlloc<Token>(UnexpArgTokens.size())))
|
2017-07-18 01:18:43 +08:00
|
|
|
MacroArgs(UnexpArgTokens.size(), VarargsElided, MI->getNumParams());
|
2009-12-16 04:48:12 +08:00
|
|
|
} else {
|
2009-12-28 14:36:46 +08:00
|
|
|
Result = *ResultEnt;
|
|
|
|
// Unlink this node from the preprocessors singly linked list.
|
|
|
|
*ResultEnt = Result->ArgCache;
|
2011-09-22 10:03:12 +08:00
|
|
|
Result->NumUnexpArgTokens = UnexpArgTokens.size();
|
2009-12-16 04:48:12 +08:00
|
|
|
Result->VarargsElided = VarargsElided;
|
2017-07-18 01:18:43 +08:00
|
|
|
Result->NumMacroArgs = MI->getNumParams();
|
2009-12-16 04:48:12 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 10:55:12 +08:00
|
|
|
// Copy the actual unexpanded tokens to immediately after the result ptr.
|
2017-09-28 09:50:23 +08:00
|
|
|
if (!UnexpArgTokens.empty()) {
|
2017-09-28 10:00:40 +08:00
|
|
|
static_assert(std::is_trivially_copyable<Token>::value,
|
2017-09-28 09:50:23 +08:00
|
|
|
"assume trivial copyability if copying into the "
|
|
|
|
"uninitialized array (as opposed to reusing a cached "
|
|
|
|
"MacroArgs)");
|
2011-09-22 10:03:12 +08:00
|
|
|
std::copy(UnexpArgTokens.begin(), UnexpArgTokens.end(),
|
2017-09-28 09:50:23 +08:00
|
|
|
Result->getTrailingObjects<Token>());
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 10:55:12 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// destroy - Destroy and deallocate the memory for this object.
|
|
|
|
///
|
2009-12-15 06:12:52 +08:00
|
|
|
void MacroArgs::destroy(Preprocessor &PP) {
|
2009-12-16 04:48:12 +08:00
|
|
|
StringifiedArgs.clear();
|
|
|
|
|
|
|
|
// Don't clear PreExpArgTokens, just clear the entries. Clearing the entries
|
|
|
|
// would deallocate the element vectors.
|
|
|
|
for (unsigned i = 0, e = PreExpArgTokens.size(); i != e; ++i)
|
|
|
|
PreExpArgTokens[i].clear();
|
|
|
|
|
|
|
|
// Add this to the preprocessor's free list.
|
|
|
|
ArgCache = PP.MacroArgCache;
|
|
|
|
PP.MacroArgCache = this;
|
2008-03-09 10:55:12 +08:00
|
|
|
}
|
|
|
|
|
2009-12-15 09:51:03 +08:00
|
|
|
/// deallocate - This should only be called by the Preprocessor when managing
|
|
|
|
/// its freelist.
|
|
|
|
MacroArgs *MacroArgs::deallocate() {
|
|
|
|
MacroArgs *Next = ArgCache;
|
|
|
|
|
|
|
|
// Run the dtor to deallocate the vectors.
|
|
|
|
this->~MacroArgs();
|
|
|
|
// Release the memory for the object.
|
2017-09-28 10:00:40 +08:00
|
|
|
static_assert(std::is_trivially_destructible<Token>::value,
|
2017-09-28 09:50:23 +08:00
|
|
|
"assume trivially destructible and forego destructors");
|
2009-12-15 09:51:03 +08:00
|
|
|
free(this);
|
|
|
|
|
|
|
|
return Next;
|
|
|
|
}
|
|
|
|
|
2008-03-09 10:55:12 +08:00
|
|
|
|
|
|
|
/// getArgLength - Given a pointer to an expanded or unexpanded argument,
|
|
|
|
/// return the number of tokens, not counting the EOF, that make up the
|
|
|
|
/// argument.
|
|
|
|
unsigned MacroArgs::getArgLength(const Token *ArgPtr) {
|
|
|
|
unsigned NumArgTokens = 0;
|
|
|
|
for (; ArgPtr->isNot(tok::eof); ++ArgPtr)
|
|
|
|
++NumArgTokens;
|
|
|
|
return NumArgTokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// getUnexpArgument - Return the unexpanded tokens for the specified formal.
|
|
|
|
///
|
|
|
|
const Token *MacroArgs::getUnexpArgument(unsigned Arg) const {
|
|
|
|
// The unexpanded argument tokens start immediately after the MacroArgs object
|
|
|
|
// in memory.
|
2017-09-28 09:50:23 +08:00
|
|
|
const Token *Start = getTrailingObjects<Token>();
|
2008-03-09 10:55:12 +08:00
|
|
|
const Token *Result = Start;
|
|
|
|
// Scan to find Arg.
|
|
|
|
for (; Arg; ++Result) {
|
|
|
|
assert(Result < Start+NumUnexpArgTokens && "Invalid arg #");
|
|
|
|
if (Result->is(tok::eof))
|
|
|
|
--Arg;
|
|
|
|
}
|
2009-05-13 08:55:26 +08:00
|
|
|
assert(Result < Start+NumUnexpArgTokens && "Invalid arg #");
|
2008-03-09 10:55:12 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// ArgNeedsPreexpansion - If we can prove that the argument won't be affected
|
|
|
|
/// by pre-expansion, return false. Otherwise, conservatively return true.
|
|
|
|
bool MacroArgs::ArgNeedsPreexpansion(const Token *ArgTok,
|
|
|
|
Preprocessor &PP) const {
|
|
|
|
// If there are no identifiers in the argument list, or if the identifiers are
|
|
|
|
// known to not be macros, pre-expansion won't modify it.
|
|
|
|
for (; ArgTok->isNot(tok::eof); ++ArgTok)
|
2015-04-30 07:20:19 +08:00
|
|
|
if (IdentifierInfo *II = ArgTok->getIdentifierInfo())
|
|
|
|
if (II->hasMacroDefinition())
|
2008-03-09 10:55:12 +08:00
|
|
|
// Return true even though the macro could be a function-like macro
|
2015-04-30 07:20:19 +08:00
|
|
|
// without a following '(' token, or could be disabled, or not visible.
|
2008-03-09 10:55:12 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getPreExpArgument - Return the pre-expanded form of the specified
|
|
|
|
/// argument.
|
|
|
|
const std::vector<Token> &
|
2009-12-28 14:17:16 +08:00
|
|
|
MacroArgs::getPreExpArgument(unsigned Arg, const MacroInfo *MI,
|
|
|
|
Preprocessor &PP) {
|
2017-07-18 01:18:43 +08:00
|
|
|
assert(Arg < MI->getNumParams() && "Invalid argument number!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 10:55:12 +08:00
|
|
|
// If we have already computed this, return it.
|
2017-07-18 01:18:43 +08:00
|
|
|
if (PreExpArgTokens.size() < MI->getNumParams())
|
|
|
|
PreExpArgTokens.resize(MI->getNumParams());
|
2009-12-28 14:17:16 +08:00
|
|
|
|
2008-03-09 10:55:12 +08:00
|
|
|
std::vector<Token> &Result = PreExpArgTokens[Arg];
|
|
|
|
if (!Result.empty()) return Result;
|
|
|
|
|
2012-04-04 00:47:40 +08:00
|
|
|
SaveAndRestore<bool> PreExpandingMacroArgs(PP.InMacroArgPreExpansion, true);
|
|
|
|
|
2008-03-09 10:55:12 +08:00
|
|
|
const Token *AT = getUnexpArgument(Arg);
|
|
|
|
unsigned NumToks = getArgLength(AT)+1; // Include the EOF.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 10:55:12 +08:00
|
|
|
// Otherwise, we have to pre-expand this argument, populating Result. To do
|
|
|
|
// this, we set up a fake TokenLexer to lex from the unexpanded argument
|
|
|
|
// list. With this installed, we lex expanded tokens until we hit the EOF
|
|
|
|
// token at the end of the unexp list.
|
2009-09-09 23:08:12 +08:00
|
|
|
PP.EnterTokenStream(AT, NumToks, false /*disable expand*/,
|
2008-03-10 14:06:04 +08:00
|
|
|
false /*owns tokens*/);
|
2008-03-09 10:55:12 +08:00
|
|
|
|
|
|
|
// Lex all of the macro-expanded tokens into Result.
|
|
|
|
do {
|
|
|
|
Result.push_back(Token());
|
2009-01-26 12:33:10 +08:00
|
|
|
Token &Tok = Result.back();
|
|
|
|
PP.Lex(Tok);
|
2008-03-09 10:55:12 +08:00
|
|
|
} while (Result.back().isNot(tok::eof));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 10:55:12 +08:00
|
|
|
// Pop the token stream off the top of the stack. We know that the internal
|
|
|
|
// pointer inside of it is to the "end" of the token stream, but the stack
|
|
|
|
// will not otherwise be popped until the next token is lexed. The problem is
|
|
|
|
// that the token may be lexed sometime after the vector of tokens itself is
|
|
|
|
// destroyed, which would be badness.
|
2012-04-04 00:47:40 +08:00
|
|
|
if (PP.InCachingLexMode())
|
|
|
|
PP.ExitCachingLexMode();
|
2008-03-09 10:55:12 +08:00
|
|
|
PP.RemoveTopOfLexerStack();
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// StringifyArgument - Implement C99 6.10.3.2p2, converting a sequence of
|
|
|
|
/// tokens into the literal string token that should be produced by the C #
|
|
|
|
/// preprocessor operator. If Charify is true, then it should be turned into
|
|
|
|
/// a character literal for the Microsoft charize (#@) extension.
|
|
|
|
///
|
|
|
|
Token MacroArgs::StringifyArgument(const Token *ArgToks,
|
2011-07-07 11:40:34 +08:00
|
|
|
Preprocessor &PP, bool Charify,
|
2011-10-04 02:39:03 +08:00
|
|
|
SourceLocation ExpansionLocStart,
|
|
|
|
SourceLocation ExpansionLocEnd) {
|
2008-03-09 10:55:12 +08:00
|
|
|
Token Tok;
|
|
|
|
Tok.startToken();
|
2009-12-24 03:15:27 +08:00
|
|
|
Tok.setKind(Charify ? tok::char_constant : tok::string_literal);
|
2008-03-09 10:55:12 +08:00
|
|
|
|
|
|
|
const Token *ArgTokStart = ArgToks;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 10:55:12 +08:00
|
|
|
// Stringify all the tokens.
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<128> Result;
|
2009-01-06 07:04:18 +08:00
|
|
|
Result += "\"";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 10:55:12 +08:00
|
|
|
bool isFirst = true;
|
|
|
|
for (; ArgToks->isNot(tok::eof); ++ArgToks) {
|
|
|
|
const Token &Tok = *ArgToks;
|
|
|
|
if (!isFirst && (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()))
|
|
|
|
Result += ' ';
|
|
|
|
isFirst = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 10:55:12 +08:00
|
|
|
// If this is a string or character constant, escape the token as specified
|
|
|
|
// by 6.10.3.2p2.
|
2013-03-10 07:30:15 +08:00
|
|
|
if (tok::isStringLiteral(Tok.getKind()) || // "foo", u8R"x(foo)x"_bar, etc.
|
|
|
|
Tok.is(tok::char_constant) || // 'x'
|
|
|
|
Tok.is(tok::wide_char_constant) || // L'x'.
|
2014-11-08 14:08:42 +08:00
|
|
|
Tok.is(tok::utf8_char_constant) || // u8'x'.
|
2013-03-10 07:30:15 +08:00
|
|
|
Tok.is(tok::utf16_char_constant) || // u'x'.
|
|
|
|
Tok.is(tok::utf32_char_constant)) { // U'x'.
|
2010-03-17 06:30:13 +08:00
|
|
|
bool Invalid = false;
|
|
|
|
std::string TokStr = PP.getSpelling(Tok, &Invalid);
|
|
|
|
if (!Invalid) {
|
|
|
|
std::string Str = Lexer::Stringify(TokStr);
|
|
|
|
Result.append(Str.begin(), Str.end());
|
|
|
|
}
|
2011-09-04 11:32:19 +08:00
|
|
|
} else if (Tok.is(tok::code_completion)) {
|
|
|
|
PP.CodeCompleteNaturalLanguage();
|
2008-03-09 10:55:12 +08:00
|
|
|
} else {
|
2009-01-06 07:04:18 +08:00
|
|
|
// Otherwise, just append the token. Do some gymnastics to get the token
|
|
|
|
// in place and avoid copies where possible.
|
|
|
|
unsigned CurStrLen = Result.size();
|
|
|
|
Result.resize(CurStrLen+Tok.getLength());
|
2014-10-25 19:40:40 +08:00
|
|
|
const char *BufPtr = Result.data() + CurStrLen;
|
2010-03-17 06:30:13 +08:00
|
|
|
bool Invalid = false;
|
|
|
|
unsigned ActualTokLen = PP.getSpelling(Tok, BufPtr, &Invalid);
|
|
|
|
|
|
|
|
if (!Invalid) {
|
|
|
|
// If getSpelling returned a pointer to an already uniqued version of
|
|
|
|
// the string instead of filling in BufPtr, memcpy it onto our string.
|
2014-10-25 19:40:40 +08:00
|
|
|
if (ActualTokLen && BufPtr != &Result[CurStrLen])
|
2010-03-17 06:30:13 +08:00
|
|
|
memcpy(&Result[CurStrLen], BufPtr, ActualTokLen);
|
|
|
|
|
|
|
|
// If the token was dirty, the spelling may be shorter than the token.
|
|
|
|
if (ActualTokLen != Tok.getLength())
|
|
|
|
Result.resize(CurStrLen+ActualTokLen);
|
|
|
|
}
|
2008-03-09 10:55:12 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 10:55:12 +08:00
|
|
|
// If the last character of the string is a \, and if it isn't escaped, this
|
|
|
|
// is an invalid string literal, diagnose it as specified in C99.
|
2009-01-06 07:04:18 +08:00
|
|
|
if (Result.back() == '\\') {
|
2008-03-09 10:55:12 +08:00
|
|
|
// Count the number of consequtive \ characters. If even, then they are
|
|
|
|
// just escaped backslashes, otherwise it's an error.
|
|
|
|
unsigned FirstNonSlash = Result.size()-2;
|
|
|
|
// Guaranteed to find the starting " if nothing else.
|
|
|
|
while (Result[FirstNonSlash] == '\\')
|
|
|
|
--FirstNonSlash;
|
|
|
|
if ((Result.size()-1-FirstNonSlash) & 1) {
|
|
|
|
// Diagnose errors for things like: #define F(X) #X / F(\)
|
|
|
|
PP.Diag(ArgToks[-1], diag::pp_invalid_string_literal);
|
2009-01-06 07:04:18 +08:00
|
|
|
Result.pop_back(); // remove one of the \'s.
|
2008-03-09 10:55:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Result += '"';
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 10:55:12 +08:00
|
|
|
// If this is the charify operation and the result is not a legal character
|
|
|
|
// constant, diagnose it.
|
|
|
|
if (Charify) {
|
|
|
|
// First step, turn double quotes into single quotes:
|
|
|
|
Result[0] = '\'';
|
|
|
|
Result[Result.size()-1] = '\'';
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 10:55:12 +08:00
|
|
|
// Check for bogus character.
|
|
|
|
bool isBad = false;
|
2009-01-06 07:04:18 +08:00
|
|
|
if (Result.size() == 3)
|
2008-03-09 10:55:12 +08:00
|
|
|
isBad = Result[1] == '\''; // ''' is not legal. '\' already fixed above.
|
2009-01-06 07:04:18 +08:00
|
|
|
else
|
2008-03-09 10:55:12 +08:00
|
|
|
isBad = (Result.size() != 4 || Result[1] != '\\'); // Not '\x'
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 10:55:12 +08:00
|
|
|
if (isBad) {
|
|
|
|
PP.Diag(ArgTokStart[0], diag::err_invalid_character_to_charify);
|
|
|
|
Result = "' '"; // Use something arbitrary, but legal.
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-09-25 05:07:17 +08:00
|
|
|
PP.CreateString(Result, Tok,
|
2011-10-04 02:39:03 +08:00
|
|
|
ExpansionLocStart, ExpansionLocEnd);
|
2008-03-09 10:55:12 +08:00
|
|
|
return Tok;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getStringifiedArgument - Compute, cache, and return the specified argument
|
|
|
|
/// that has been 'stringified' as required by the # operator.
|
|
|
|
const Token &MacroArgs::getStringifiedArgument(unsigned ArgNo,
|
2011-07-07 11:40:34 +08:00
|
|
|
Preprocessor &PP,
|
2011-10-04 02:39:03 +08:00
|
|
|
SourceLocation ExpansionLocStart,
|
|
|
|
SourceLocation ExpansionLocEnd) {
|
2017-06-15 07:09:01 +08:00
|
|
|
assert(ArgNo < getNumMacroArguments() && "Invalid argument number!");
|
|
|
|
if (StringifiedArgs.empty())
|
|
|
|
StringifiedArgs.resize(getNumMacroArguments(), {});
|
|
|
|
|
2008-03-09 10:55:12 +08:00
|
|
|
if (StringifiedArgs[ArgNo].isNot(tok::string_literal))
|
2011-07-07 11:40:34 +08:00
|
|
|
StringifiedArgs[ArgNo] = StringifyArgument(getUnexpArgument(ArgNo), PP,
|
2011-10-04 02:39:03 +08:00
|
|
|
/*Charify=*/false,
|
|
|
|
ExpansionLocStart,
|
|
|
|
ExpansionLocEnd);
|
2008-03-09 10:55:12 +08:00
|
|
|
return StringifiedArgs[ArgNo];
|
|
|
|
}
|