2006-06-18 13:43:12 +08:00
|
|
|
//===--- MacroExpander.cpp - Lex from a macro expansion -------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Chris Lattner and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the MacroExpander interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Lex/MacroExpander.h"
|
|
|
|
#include "clang/Lex/MacroInfo.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2006-06-21 11:01:55 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2006-07-15 13:23:58 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2006-07-27 11:42:15 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2006-07-19 13:42:48 +08:00
|
|
|
#include "llvm/Config/Alloca.h"
|
2006-06-18 13:43:12 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang;
|
|
|
|
|
2006-07-09 08:45:31 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2006-07-15 15:42:55 +08:00
|
|
|
// MacroArgs Implementation
|
2006-07-09 08:45:31 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-07-26 13:22:49 +08:00
|
|
|
/// MacroArgs ctor function - This destroys the vector passed in.
|
|
|
|
MacroArgs *MacroArgs::create(const MacroInfo *MI,
|
2006-07-26 14:26:52 +08:00
|
|
|
const LexerToken *UnexpArgTokens,
|
|
|
|
unsigned NumToks) {
|
2006-07-09 08:45:31 +08:00
|
|
|
assert(MI->isFunctionLike() &&
|
2006-07-15 15:42:55 +08:00
|
|
|
"Can't have args for an object-like macro!");
|
2006-07-26 13:22:49 +08:00
|
|
|
|
|
|
|
// Allocate memory for the MacroArgs object with the lexer tokens at the end.
|
|
|
|
MacroArgs *Result = (MacroArgs*)malloc(sizeof(MacroArgs) +
|
|
|
|
NumToks*sizeof(LexerToken));
|
|
|
|
// Construct the macroargs object.
|
|
|
|
new (Result) MacroArgs(NumToks);
|
|
|
|
|
|
|
|
// Copy the actual unexpanded tokens to immediately after the result ptr.
|
|
|
|
if (NumToks)
|
|
|
|
memcpy(const_cast<LexerToken*>(Result->getUnexpArgument(0)),
|
2006-07-26 14:26:52 +08:00
|
|
|
UnexpArgTokens, NumToks*sizeof(LexerToken));
|
2006-07-26 13:22:49 +08:00
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// destroy - Destroy and deallocate the memory for this object.
|
|
|
|
///
|
|
|
|
void MacroArgs::destroy() {
|
|
|
|
// Run the dtor to deallocate the vectors.
|
|
|
|
this->~MacroArgs();
|
|
|
|
// Release the memory for the object.
|
|
|
|
free(this);
|
2006-07-09 08:45:31 +08:00
|
|
|
}
|
|
|
|
|
2006-07-26 13:22:49 +08:00
|
|
|
|
2006-07-26 12:55:32 +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 LexerToken *ArgPtr) {
|
|
|
|
unsigned NumArgTokens = 0;
|
|
|
|
for (; ArgPtr->getKind() != tok::eof; ++ArgPtr)
|
|
|
|
++NumArgTokens;
|
|
|
|
return NumArgTokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-21 14:38:30 +08:00
|
|
|
/// getUnexpArgument - Return the unexpanded tokens for the specified formal.
|
|
|
|
///
|
|
|
|
const LexerToken *MacroArgs::getUnexpArgument(unsigned Arg) const {
|
2006-07-26 13:22:49 +08:00
|
|
|
// The unexpanded argument tokens start immediately after the MacroArgs object
|
|
|
|
// in memory.
|
|
|
|
const LexerToken *Start = (const LexerToken *)(this+1);
|
2006-07-21 14:38:30 +08:00
|
|
|
const LexerToken *Result = Start;
|
2006-07-26 13:22:49 +08:00
|
|
|
// Scan to find Arg.
|
2006-07-21 14:38:30 +08:00
|
|
|
for (; Arg; ++Result) {
|
2006-07-26 13:22:49 +08:00
|
|
|
assert(Result < Start+NumUnexpArgTokens && "Invalid arg #");
|
2006-07-21 14:38:30 +08:00
|
|
|
if (Result->getKind() == tok::eof)
|
|
|
|
--Arg;
|
|
|
|
}
|
|
|
|
return Result;
|
2006-07-15 15:42:55 +08:00
|
|
|
}
|
|
|
|
|
2006-07-21 14:38:30 +08:00
|
|
|
|
2006-07-16 05:07:40 +08:00
|
|
|
/// ArgNeedsPreexpansion - If we can prove that the argument won't be affected
|
|
|
|
/// by pre-expansion, return false. Otherwise, conservatively return true.
|
2006-07-21 14:38:30 +08:00
|
|
|
bool MacroArgs::ArgNeedsPreexpansion(const LexerToken *ArgTok) const {
|
2006-07-16 05:07:40 +08:00
|
|
|
// 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.
|
2006-07-21 14:38:30 +08:00
|
|
|
for (; ArgTok->getKind() != tok::eof; ++ArgTok)
|
|
|
|
if (IdentifierInfo *II = ArgTok->getIdentifierInfo()) {
|
2006-07-16 05:07:40 +08:00
|
|
|
if (II->getMacroInfo() && II->getMacroInfo()->isEnabled())
|
|
|
|
// Return true even though the macro could be a function-like macro
|
|
|
|
// without a following '(' token.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-07-17 02:16:58 +08:00
|
|
|
/// getPreExpArgument - Return the pre-expanded form of the specified
|
|
|
|
/// argument.
|
|
|
|
const std::vector<LexerToken> &
|
|
|
|
MacroArgs::getPreExpArgument(unsigned Arg, Preprocessor &PP) {
|
2006-07-26 13:22:49 +08:00
|
|
|
assert(Arg < NumUnexpArgTokens && "Invalid argument number!");
|
2006-07-17 02:16:58 +08:00
|
|
|
|
|
|
|
// If we have already computed this, return it.
|
|
|
|
if (PreExpArgTokens.empty())
|
2006-07-26 13:22:49 +08:00
|
|
|
PreExpArgTokens.resize(NumUnexpArgTokens);
|
2006-07-17 02:16:58 +08:00
|
|
|
|
|
|
|
std::vector<LexerToken> &Result = PreExpArgTokens[Arg];
|
|
|
|
if (!Result.empty()) return Result;
|
|
|
|
|
2006-07-21 14:38:30 +08:00
|
|
|
const LexerToken *AT = getUnexpArgument(Arg);
|
2006-07-26 12:55:32 +08:00
|
|
|
unsigned NumToks = getArgLength(AT)+1; // Include the EOF.
|
2006-07-21 14:38:30 +08:00
|
|
|
|
2006-07-17 02:16:58 +08:00
|
|
|
// Otherwise, we have to pre-expand this argument, populating Result. To do
|
|
|
|
// this, we set up a fake MacroExpander 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.
|
2006-07-26 11:50:40 +08:00
|
|
|
PP.EnterTokenStream(AT, NumToks);
|
2006-07-17 02:16:58 +08:00
|
|
|
|
|
|
|
// Lex all of the macro-expanded tokens into Result.
|
|
|
|
do {
|
|
|
|
Result.push_back(LexerToken());
|
|
|
|
PP.Lex(Result.back());
|
|
|
|
} while (Result.back().getKind() != tok::eof);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
PP.RemoveTopOfLexerStack();
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2006-07-15 15:42:55 +08:00
|
|
|
|
2006-07-15 13:23:58 +08:00
|
|
|
/// 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.
|
|
|
|
///
|
2006-07-21 14:38:30 +08:00
|
|
|
static LexerToken StringifyArgument(const LexerToken *ArgToks,
|
2006-07-15 14:11:25 +08:00
|
|
|
Preprocessor &PP, bool Charify = false) {
|
2006-07-14 14:54:44 +08:00
|
|
|
LexerToken Tok;
|
|
|
|
Tok.StartToken();
|
|
|
|
Tok.SetKind(tok::string_literal);
|
2006-07-15 13:23:58 +08:00
|
|
|
|
2006-07-21 14:38:30 +08:00
|
|
|
const LexerToken *ArgTokStart = ArgToks;
|
|
|
|
|
2006-07-15 13:23:58 +08:00
|
|
|
// Stringify all the tokens.
|
|
|
|
std::string Result = "\"";
|
2006-07-17 02:16:58 +08:00
|
|
|
// FIXME: Optimize this loop to not use std::strings.
|
2006-07-21 14:38:30 +08:00
|
|
|
bool isFirst = true;
|
|
|
|
for (; ArgToks->getKind() != tok::eof; ++ArgToks) {
|
|
|
|
const LexerToken &Tok = *ArgToks;
|
|
|
|
if (!isFirst && Tok.hasLeadingSpace())
|
2006-07-15 13:23:58 +08:00
|
|
|
Result += ' ';
|
2006-07-21 14:38:30 +08:00
|
|
|
isFirst = false;
|
2006-07-15 13:23:58 +08:00
|
|
|
|
|
|
|
// If this is a string or character constant, escape the token as specified
|
|
|
|
// by 6.10.3.2p2.
|
|
|
|
if (Tok.getKind() == tok::string_literal || // "foo" and L"foo".
|
|
|
|
Tok.getKind() == tok::char_constant) { // 'x' and L'x'.
|
|
|
|
Result += Lexer::Stringify(PP.getSpelling(Tok));
|
|
|
|
} else {
|
|
|
|
// Otherwise, just append the token.
|
|
|
|
Result += PP.getSpelling(Tok);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if (Result[Result.size()-1] == '\\') {
|
|
|
|
// 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) {
|
2006-07-15 13:27:44 +08:00
|
|
|
// Diagnose errors for things like: #define F(X) #X / F(\)
|
2006-07-21 14:38:30 +08:00
|
|
|
PP.Diag(ArgToks[-1], diag::pp_invalid_string_literal);
|
2006-07-15 13:23:58 +08:00
|
|
|
Result.erase(Result.end()-1); // remove one of the \'s.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Result += '"';
|
2006-07-14 14:54:44 +08:00
|
|
|
|
2006-07-15 14:11:25 +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] = '\'';
|
|
|
|
|
|
|
|
// Check for bogus character.
|
|
|
|
bool isBad = false;
|
2006-07-15 15:51:24 +08:00
|
|
|
if (Result.size() == 3) {
|
2006-07-15 14:11:25 +08:00
|
|
|
isBad = Result[1] == '\''; // ''' is not legal. '\' already fixed above.
|
|
|
|
} else {
|
|
|
|
isBad = (Result.size() != 4 || Result[1] != '\\'); // Not '\x'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isBad) {
|
2006-07-21 14:38:30 +08:00
|
|
|
PP.Diag(ArgTokStart[0], diag::err_invalid_character_to_charify);
|
2006-07-15 15:56:31 +08:00
|
|
|
Result = "' '"; // Use something arbitrary, but legal.
|
2006-07-15 14:11:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-15 13:23:58 +08:00
|
|
|
Tok.SetLength(Result.size());
|
|
|
|
Tok.SetLocation(PP.CreateString(&Result[0], Result.size()));
|
2006-07-14 14:54:44 +08:00
|
|
|
return Tok;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getStringifiedArgument - Compute, cache, and return the specified argument
|
|
|
|
/// that has been 'stringified' as required by the # operator.
|
2006-07-15 15:42:55 +08:00
|
|
|
const LexerToken &MacroArgs::getStringifiedArgument(unsigned ArgNo,
|
|
|
|
Preprocessor &PP) {
|
2006-07-26 13:22:49 +08:00
|
|
|
assert(ArgNo < NumUnexpArgTokens && "Invalid argument number!");
|
2006-07-14 14:54:44 +08:00
|
|
|
if (StringifiedArgs.empty()) {
|
2006-07-15 15:51:24 +08:00
|
|
|
StringifiedArgs.resize(getNumArguments());
|
2006-07-15 15:42:55 +08:00
|
|
|
memset(&StringifiedArgs[0], 0,
|
|
|
|
sizeof(StringifiedArgs[0])*getNumArguments());
|
2006-07-14 14:54:44 +08:00
|
|
|
}
|
|
|
|
if (StringifiedArgs[ArgNo].getKind() != tok::string_literal)
|
2006-07-21 14:38:30 +08:00
|
|
|
StringifiedArgs[ArgNo] = StringifyArgument(getUnexpArgument(ArgNo), PP);
|
2006-07-14 14:54:44 +08:00
|
|
|
return StringifiedArgs[ArgNo];
|
|
|
|
}
|
|
|
|
|
2006-07-09 08:45:31 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MacroExpander Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-07-17 02:16:58 +08:00
|
|
|
/// Create a macro expander for the specified macro with the specified actual
|
|
|
|
/// arguments. Note that this ctor takes ownership of the ActualArgs pointer.
|
2006-07-15 15:42:55 +08:00
|
|
|
MacroExpander::MacroExpander(LexerToken &Tok, MacroArgs *Actuals,
|
2006-07-09 08:45:31 +08:00
|
|
|
Preprocessor &pp)
|
2006-07-17 02:16:58 +08:00
|
|
|
: Macro(Tok.getIdentifierInfo()->getMacroInfo()),
|
2006-07-15 15:42:55 +08:00
|
|
|
ActualArgs(Actuals), PP(pp), CurToken(0),
|
2006-06-19 00:32:35 +08:00
|
|
|
InstantiateLoc(Tok.getLocation()),
|
2006-06-19 00:22:51 +08:00
|
|
|
AtStartOfLine(Tok.isAtStartOfLine()),
|
|
|
|
HasLeadingSpace(Tok.hasLeadingSpace()) {
|
2006-07-26 11:50:40 +08:00
|
|
|
MacroTokens = &Macro->getReplacementTokens()[0];
|
|
|
|
NumMacroTokens = Macro->getReplacementTokens().size();
|
2006-07-14 14:54:44 +08:00
|
|
|
|
|
|
|
// If this is a function-like macro, expand the arguments and change
|
|
|
|
// MacroTokens to point to the expanded tokens.
|
2006-07-29 12:04:22 +08:00
|
|
|
if (Macro->isFunctionLike() && Macro->getNumArgs() || Macro->isC99Varargs())
|
2006-07-14 14:54:44 +08:00
|
|
|
ExpandFunctionArguments();
|
2006-07-17 02:16:58 +08:00
|
|
|
|
|
|
|
// Mark the macro as currently disabled, so that it is not recursively
|
|
|
|
// expanded. The macro must be disabled only after argument pre-expansion of
|
|
|
|
// function-like macro arguments occurs.
|
|
|
|
Macro->DisableMacro();
|
2006-07-14 14:54:44 +08:00
|
|
|
}
|
|
|
|
|
2006-07-17 02:16:58 +08:00
|
|
|
/// Create a macro expander for the specified token stream. This does not
|
|
|
|
/// take ownership of the specified token vector.
|
2006-07-26 11:50:40 +08:00
|
|
|
MacroExpander::MacroExpander(const LexerToken *TokArray, unsigned NumToks,
|
2006-07-17 02:16:58 +08:00
|
|
|
Preprocessor &pp)
|
2006-07-26 11:50:40 +08:00
|
|
|
: Macro(0), ActualArgs(0), PP(pp), MacroTokens(TokArray),
|
|
|
|
NumMacroTokens(NumToks), CurToken(0),
|
2006-07-17 02:16:58 +08:00
|
|
|
InstantiateLoc(SourceLocation()), AtStartOfLine(false),
|
|
|
|
HasLeadingSpace(false) {
|
|
|
|
|
|
|
|
// Set HasLeadingSpace/AtStartOfLine so that the first token will be
|
|
|
|
// returned unmodified.
|
2006-07-26 11:50:40 +08:00
|
|
|
if (NumToks != 0) {
|
|
|
|
AtStartOfLine = TokArray[0].isAtStartOfLine();
|
|
|
|
HasLeadingSpace = TokArray[0].hasLeadingSpace();
|
2006-07-17 02:16:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-14 14:54:44 +08:00
|
|
|
MacroExpander::~MacroExpander() {
|
|
|
|
// If this was a function-like macro that actually uses its arguments, delete
|
|
|
|
// the expanded tokens.
|
2006-07-26 11:50:40 +08:00
|
|
|
if (Macro && MacroTokens != &Macro->getReplacementTokens()[0])
|
|
|
|
delete [] MacroTokens;
|
2006-07-14 14:54:44 +08:00
|
|
|
|
|
|
|
// MacroExpander owns its formal arguments.
|
2006-07-26 13:22:49 +08:00
|
|
|
if (ActualArgs) ActualArgs->destroy();
|
2006-06-19 00:22:51 +08:00
|
|
|
}
|
|
|
|
|
2006-07-14 14:54:44 +08:00
|
|
|
/// Expand the arguments of a function-like macro so that we can quickly
|
|
|
|
/// return preexpanded tokens from MacroTokens.
|
|
|
|
void MacroExpander::ExpandFunctionArguments() {
|
2006-07-27 11:42:15 +08:00
|
|
|
SmallVector<LexerToken, 128> ResultToks;
|
2006-07-14 14:54:44 +08:00
|
|
|
|
2006-07-29 12:04:22 +08:00
|
|
|
IdentifierInfo *VAARGSii = PP.get__VA_ARGS__Identifier();
|
|
|
|
|
2006-07-14 14:54:44 +08:00
|
|
|
// Loop through the MacroTokens tokens, expanding them into ResultToks. Keep
|
|
|
|
// track of whether we change anything. If not, no need to keep them. If so,
|
|
|
|
// we install the newly expanded sequence as MacroTokens.
|
|
|
|
bool MadeChange = false;
|
2006-07-26 11:50:40 +08:00
|
|
|
for (unsigned i = 0, e = NumMacroTokens; i != e; ++i) {
|
2006-07-14 14:54:44 +08:00
|
|
|
// If we found the stringify operator, get the argument stringified. The
|
|
|
|
// preprocessor already verified that the following token is a macro name
|
|
|
|
// when the #define was parsed.
|
2006-07-26 11:50:40 +08:00
|
|
|
const LexerToken &CurTok = MacroTokens[i];
|
2006-07-15 14:11:25 +08:00
|
|
|
if (CurTok.getKind() == tok::hash || CurTok.getKind() == tok::hashat) {
|
2006-07-26 11:50:40 +08:00
|
|
|
int ArgNo = Macro->getArgumentNum(MacroTokens[i+1].getIdentifierInfo());
|
2006-07-14 14:54:44 +08:00
|
|
|
assert(ArgNo != -1 && "Token following # is not an argument?");
|
|
|
|
|
2006-07-26 12:55:32 +08:00
|
|
|
LexerToken Res;
|
2006-07-15 14:11:25 +08:00
|
|
|
if (CurTok.getKind() == tok::hash) // Stringify
|
2006-07-26 12:55:32 +08:00
|
|
|
Res = ActualArgs->getStringifiedArgument(ArgNo, PP);
|
2006-07-15 14:11:25 +08:00
|
|
|
else {
|
|
|
|
// 'charify': don't bother caching these.
|
2006-07-26 12:55:32 +08:00
|
|
|
Res = StringifyArgument(ActualArgs->getUnexpArgument(ArgNo), PP, true);
|
2006-07-15 14:11:25 +08:00
|
|
|
}
|
2006-07-14 14:54:44 +08:00
|
|
|
|
2006-07-15 14:48:02 +08:00
|
|
|
// The stringified/charified string leading space flag gets set to match
|
|
|
|
// the #/#@ operator.
|
|
|
|
if (CurTok.hasLeadingSpace())
|
2006-07-26 12:55:32 +08:00
|
|
|
Res.SetFlag(LexerToken::LeadingSpace);
|
2006-07-14 14:54:44 +08:00
|
|
|
|
2006-07-26 12:55:32 +08:00
|
|
|
ResultToks.push_back(Res);
|
2006-07-14 14:54:44 +08:00
|
|
|
MadeChange = true;
|
|
|
|
++i; // Skip arg name.
|
2006-07-28 13:10:36 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, if this is not an argument token, just add the token to the
|
|
|
|
// output buffer.
|
|
|
|
IdentifierInfo *II = CurTok.getIdentifierInfo();
|
|
|
|
int ArgNo = II ? Macro->getArgumentNum(II) : -1;
|
|
|
|
if (ArgNo == -1) {
|
2006-07-29 12:04:22 +08:00
|
|
|
if (II != VAARGSii || !Macro->isC99Varargs()) {
|
|
|
|
// This isn't an argument and isn't __VA_ARGS__. Just add it.
|
|
|
|
ResultToks.push_back(CurTok);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, this *is* __VA_ARGS__. Set ArgNo to the last argument.
|
|
|
|
ArgNo = Macro->getNumArgs();
|
2006-07-28 13:10:36 +08:00
|
|
|
}
|
2006-07-16 05:07:40 +08:00
|
|
|
|
2006-07-28 13:10:36 +08:00
|
|
|
// An argument is expanded somehow, the result is different than the
|
|
|
|
// input.
|
|
|
|
MadeChange = true;
|
2006-07-21 14:38:30 +08:00
|
|
|
|
2006-07-28 13:10:36 +08:00
|
|
|
// Otherwise, this is a use of the argument. Find out if there is a paste
|
|
|
|
// (##) operator before or after the argument.
|
|
|
|
bool PasteBefore =
|
|
|
|
!ResultToks.empty() && ResultToks.back().getKind() == tok::hashhash;
|
|
|
|
bool PasteAfter = i+1 != e && MacroTokens[i+1].getKind() == tok::hashhash;
|
|
|
|
|
|
|
|
// If it is not the LHS/RHS of a ## operator, we must pre-expand the
|
|
|
|
// argument and substitute the expanded tokens into the result. This is
|
|
|
|
// C99 6.10.3.1p1.
|
|
|
|
if (!PasteBefore && !PasteAfter) {
|
|
|
|
const LexerToken *ResultArgToks;
|
2006-07-19 11:51:26 +08:00
|
|
|
|
2006-07-28 13:10:36 +08:00
|
|
|
// Only preexpand the argument if it could possibly need it. This
|
|
|
|
// avoids some work in common cases.
|
|
|
|
const LexerToken *ArgTok = ActualArgs->getUnexpArgument(ArgNo);
|
|
|
|
if (ActualArgs->ArgNeedsPreexpansion(ArgTok))
|
|
|
|
ResultArgToks = &ActualArgs->getPreExpArgument(ArgNo, PP)[0];
|
|
|
|
else
|
|
|
|
ResultArgToks = ArgTok; // Use non-preexpanded tokens.
|
2006-07-17 02:16:58 +08:00
|
|
|
|
2006-07-28 13:10:36 +08:00
|
|
|
// If the arg token expanded into anything, append it.
|
|
|
|
if (ResultArgToks->getKind() != tok::eof) {
|
|
|
|
unsigned FirstResult = ResultToks.size();
|
|
|
|
unsigned NumToks = MacroArgs::getArgLength(ResultArgToks);
|
|
|
|
ResultToks.append(ResultArgToks, ResultArgToks+NumToks);
|
2006-07-28 13:07:04 +08:00
|
|
|
|
2006-07-28 13:10:36 +08:00
|
|
|
// If any tokens were substituted from the argument, the whitespace
|
|
|
|
// before the first token should match the whitespace of the arg
|
|
|
|
// identifier.
|
|
|
|
ResultToks[FirstResult].SetFlagValue(LexerToken::LeadingSpace,
|
|
|
|
CurTok.hasLeadingSpace());
|
2006-07-28 13:07:04 +08:00
|
|
|
}
|
|
|
|
continue;
|
2006-07-14 14:54:44 +08:00
|
|
|
}
|
2006-07-28 13:10:36 +08:00
|
|
|
|
|
|
|
// Okay, we have a token that is either the LHS or RHS of a paste (##)
|
|
|
|
// argument. It gets substituted as its non-pre-expanded tokens.
|
|
|
|
const LexerToken *ArgToks = ActualArgs->getUnexpArgument(ArgNo);
|
|
|
|
unsigned NumToks = MacroArgs::getArgLength(ArgToks);
|
|
|
|
if (NumToks) { // Not an empty argument?
|
|
|
|
ResultToks.append(ArgToks, ArgToks+NumToks);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Handle comma swallowing GNU extension.
|
|
|
|
|
|
|
|
// If an empty argument is on the LHS or RHS of a paste, the standard (C99
|
|
|
|
// 6.10.3.3p2,3) calls for a bunch of placemarker stuff to occur. We
|
|
|
|
// implement this by eating ## operators when a LHS or RHS expands to
|
|
|
|
// empty.
|
|
|
|
if (PasteAfter) {
|
|
|
|
// Discard the argument token and skip (don't copy to the expansion
|
|
|
|
// buffer) the paste operator after it.
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is on the RHS of a paste operator, we've already copied the
|
|
|
|
// paste operator to the ResultToks list. Remove it.
|
|
|
|
assert(PasteBefore && ResultToks.back().getKind() == tok::hashhash);
|
|
|
|
ResultToks.pop_back();
|
|
|
|
continue;
|
2006-07-14 14:54:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If anything changed, install this as the new MacroTokens list.
|
|
|
|
if (MadeChange) {
|
|
|
|
// This is deleted in the dtor.
|
2006-07-26 11:50:40 +08:00
|
|
|
NumMacroTokens = ResultToks.size();
|
|
|
|
LexerToken *Res = new LexerToken[ResultToks.size()];
|
|
|
|
memcpy(Res, &ResultToks[0], NumMacroTokens*sizeof(LexerToken));
|
2006-07-14 14:54:44 +08:00
|
|
|
MacroTokens = Res;
|
|
|
|
}
|
|
|
|
}
|
2006-06-26 10:03:42 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
/// Lex - Lex and return a token from this macro stream.
|
2006-06-19 00:22:51 +08:00
|
|
|
///
|
2006-06-18 14:48:37 +08:00
|
|
|
void MacroExpander::Lex(LexerToken &Tok) {
|
2006-06-18 13:43:12 +08:00
|
|
|
// Lexing off the end of the macro, pop this macro off the expansion stack.
|
2006-07-17 02:16:58 +08:00
|
|
|
if (isAtEnd()) {
|
|
|
|
// If this is a macro (not a token stream), mark the macro enabled now
|
|
|
|
// that it is no longer being expanded.
|
|
|
|
if (Macro) Macro->EnableMacro();
|
|
|
|
|
|
|
|
// Pop this context off the preprocessors lexer stack and get the next
|
2006-07-18 14:36:12 +08:00
|
|
|
// token. This will delete "this" so remember the PP instance var.
|
|
|
|
Preprocessor &PPCache = PP;
|
|
|
|
if (PP.HandleEndOfMacro(Tok))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// HandleEndOfMacro may not return a token. If it doesn't, lex whatever is
|
|
|
|
// next.
|
|
|
|
return PPCache.Lex(Tok);
|
2006-07-17 02:16:58 +08:00
|
|
|
}
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-07-19 13:45:55 +08:00
|
|
|
// If this is the first token of the expanded result, we inherit spacing
|
|
|
|
// properties later.
|
|
|
|
bool isFirstToken = CurToken == 0;
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Get the next token to return.
|
2006-07-26 11:50:40 +08:00
|
|
|
Tok = MacroTokens[CurToken++];
|
2006-07-19 13:42:48 +08:00
|
|
|
|
|
|
|
// If this token is followed by a token paste (##) operator, paste the tokens!
|
2006-07-26 11:50:40 +08:00
|
|
|
if (!isAtEnd() && MacroTokens[CurToken].getKind() == tok::hashhash)
|
2006-07-19 13:42:48 +08:00
|
|
|
PasteTokens(Tok);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-06-30 14:10:41 +08:00
|
|
|
// The token's current location indicate where the token was lexed from. We
|
|
|
|
// need this information to compute the spelling of the token, but any
|
|
|
|
// diagnostics for the expanded token should appear as if they came from
|
|
|
|
// InstantiationLoc. Pull this information together into a new SourceLocation
|
|
|
|
// that captures all of this.
|
2006-07-17 02:16:58 +08:00
|
|
|
if (InstantiateLoc.isValid()) { // Don't do this for token streams.
|
|
|
|
SourceManager &SrcMgr = PP.getSourceManager();
|
|
|
|
// The token could have come from a prior macro expansion. In that case,
|
|
|
|
// ignore the macro expand part to get to the physloc. This happens for
|
|
|
|
// stuff like: #define A(X) X A(A(X)) A(1)
|
|
|
|
SourceLocation PhysLoc = SrcMgr.getPhysicalLoc(Tok.getLocation());
|
|
|
|
Tok.SetLocation(SrcMgr.getInstantiationLoc(PhysLoc, InstantiateLoc));
|
|
|
|
}
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// If this is the first token, set the lexical properties of the token to
|
|
|
|
// match the lexical properties of the macro identifier.
|
2006-07-19 13:45:55 +08:00
|
|
|
if (isFirstToken) {
|
2006-06-18 13:43:12 +08:00
|
|
|
Tok.SetFlagValue(LexerToken::StartOfLine , AtStartOfLine);
|
|
|
|
Tok.SetFlagValue(LexerToken::LeadingSpace, HasLeadingSpace);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle recursive expansion!
|
|
|
|
if (Tok.getIdentifierInfo())
|
|
|
|
return PP.HandleIdentifier(Tok);
|
|
|
|
|
|
|
|
// Otherwise, return a normal token.
|
|
|
|
}
|
2006-07-11 12:02:46 +08:00
|
|
|
|
2006-07-19 13:42:48 +08:00
|
|
|
/// PasteTokens - Tok is the LHS of a ## operator, and CurToken is the ##
|
|
|
|
/// operator. Read the ## and RHS, and paste the LHS/RHS together. If there
|
|
|
|
/// are is another ## after it, chomp it iteratively. Return the result as Tok.
|
|
|
|
void MacroExpander::PasteTokens(LexerToken &Tok) {
|
|
|
|
do {
|
|
|
|
// Consume the ## operator.
|
2006-07-26 11:50:40 +08:00
|
|
|
SourceLocation PasteOpLoc = MacroTokens[CurToken].getLocation();
|
2006-07-19 13:42:48 +08:00
|
|
|
++CurToken;
|
|
|
|
assert(!isAtEnd() && "No token on the RHS of a paste operator!");
|
|
|
|
|
|
|
|
// Get the RHS token.
|
2006-07-26 11:50:40 +08:00
|
|
|
const LexerToken &RHS = MacroTokens[CurToken];
|
2006-07-19 13:42:48 +08:00
|
|
|
|
|
|
|
bool isInvalid = false;
|
|
|
|
|
|
|
|
// Allocate space for the result token. This is guaranteed to be enough for
|
|
|
|
// the two tokens and a null terminator.
|
|
|
|
char *Buffer = (char*)alloca(Tok.getLength() + RHS.getLength() + 1);
|
|
|
|
|
|
|
|
// Get the spelling of the LHS token in Buffer.
|
|
|
|
const char *BufPtr = Buffer;
|
|
|
|
unsigned LHSLen = PP.getSpelling(Tok, BufPtr);
|
|
|
|
if (BufPtr != Buffer) // Really, we want the chars in Buffer!
|
|
|
|
memcpy(Buffer, BufPtr, LHSLen);
|
|
|
|
|
|
|
|
BufPtr = Buffer+LHSLen;
|
|
|
|
unsigned RHSLen = PP.getSpelling(RHS, BufPtr);
|
|
|
|
if (BufPtr != Buffer+LHSLen) // Really, we want the chars in Buffer!
|
|
|
|
memcpy(Buffer+LHSLen, BufPtr, RHSLen);
|
|
|
|
|
|
|
|
// Add null terminator.
|
|
|
|
Buffer[LHSLen+RHSLen] = '\0';
|
|
|
|
|
|
|
|
// Plop the pasted result (including the trailing newline and null) into a
|
|
|
|
// scratch buffer where we can lex it.
|
|
|
|
SourceLocation ResultTokLoc = PP.CreateString(Buffer, LHSLen+RHSLen+1);
|
|
|
|
|
|
|
|
// Lex the resultant pasted token into Result.
|
|
|
|
LexerToken Result;
|
|
|
|
|
2006-07-19 14:32:35 +08:00
|
|
|
// Avoid testing /*, as the lexer would think it is the start of a comment
|
|
|
|
// and emit an error that it is unterminated.
|
|
|
|
if (Tok.getKind() == tok::slash && RHS.getKind() == tok::star) {
|
|
|
|
isInvalid = true;
|
2006-07-20 12:47:30 +08:00
|
|
|
} else if (Tok.getKind() == tok::identifier &&
|
2006-07-20 12:52:59 +08:00
|
|
|
RHS.getKind() == tok::identifier) {
|
2006-07-20 12:47:30 +08:00
|
|
|
// Common paste case: identifier+identifier = identifier. Avoid creating
|
|
|
|
// a lexer and other overhead.
|
|
|
|
PP.IncrementPasteCounter(true);
|
|
|
|
Result.StartToken();
|
|
|
|
Result.SetKind(tok::identifier);
|
|
|
|
Result.SetLocation(ResultTokLoc);
|
|
|
|
Result.SetLength(LHSLen+RHSLen);
|
2006-07-19 14:32:35 +08:00
|
|
|
} else {
|
2006-07-20 12:47:30 +08:00
|
|
|
PP.IncrementPasteCounter(false);
|
|
|
|
|
2006-07-19 14:32:35 +08:00
|
|
|
// Make a lexer to lex this string from.
|
|
|
|
SourceManager &SourceMgr = PP.getSourceManager();
|
|
|
|
const char *ResultStrData = SourceMgr.getCharacterData(ResultTokLoc);
|
|
|
|
|
|
|
|
unsigned FileID = ResultTokLoc.getFileID();
|
|
|
|
assert(FileID && "Could not get FileID for paste?");
|
|
|
|
|
|
|
|
// Make a lexer object so that we lex and expand the paste result.
|
|
|
|
Lexer *TL = new Lexer(SourceMgr.getBuffer(FileID), FileID, PP,
|
|
|
|
ResultStrData,
|
|
|
|
ResultStrData+LHSLen+RHSLen /*don't include null*/);
|
|
|
|
|
|
|
|
// Lex a token in raw mode. This way it won't look up identifiers
|
|
|
|
// automatically, lexing off the end will return an eof token, and
|
|
|
|
// warnings are disabled. This returns true if the result token is the
|
|
|
|
// entire buffer.
|
|
|
|
bool IsComplete = TL->LexRawToken(Result);
|
|
|
|
|
|
|
|
// If we got an EOF token, we didn't form even ONE token. For example, we
|
|
|
|
// did "/ ## /" to get "//".
|
|
|
|
IsComplete &= Result.getKind() != tok::eof;
|
|
|
|
isInvalid = !IsComplete;
|
|
|
|
|
|
|
|
// We're now done with the temporary lexer.
|
|
|
|
delete TL;
|
|
|
|
}
|
2006-07-19 13:42:48 +08:00
|
|
|
|
|
|
|
// If pasting the two tokens didn't form a full new token, this is an error.
|
2006-07-19 14:32:35 +08:00
|
|
|
// This occurs with "x ## +" and other stuff. Return with Tok unmodified
|
|
|
|
// and with RHS as the next token to lex.
|
|
|
|
if (isInvalid) {
|
2006-07-19 13:42:48 +08:00
|
|
|
// If not in assembler language mode.
|
|
|
|
PP.Diag(PasteOpLoc, diag::err_pp_bad_paste,
|
|
|
|
std::string(Buffer, Buffer+LHSLen+RHSLen));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Turn ## into 'other' to avoid # ## # from looking like a paste operator.
|
|
|
|
if (Result.getKind() == tok::hashhash)
|
|
|
|
Result.SetKind(tok::unknown);
|
|
|
|
// FIXME: Turn __VARRGS__ into "not a token"?
|
|
|
|
|
|
|
|
// Transfer properties of the LHS over the the Result.
|
|
|
|
Result.SetFlagValue(LexerToken::StartOfLine , Tok.isAtStartOfLine());
|
|
|
|
Result.SetFlagValue(LexerToken::LeadingSpace, Tok.hasLeadingSpace());
|
|
|
|
|
|
|
|
// Finally, replace LHS with the result, consume the RHS, and iterate.
|
|
|
|
++CurToken;
|
|
|
|
Tok = Result;
|
2006-07-26 11:50:40 +08:00
|
|
|
} while (!isAtEnd() && MacroTokens[CurToken].getKind() == tok::hashhash);
|
2006-07-20 12:16:23 +08:00
|
|
|
|
|
|
|
// Now that we got the result token, it will be subject to expansion. Since
|
|
|
|
// token pasting re-lexes the result token in raw mode, identifier information
|
|
|
|
// isn't looked up. As such, if the result is an identifier, look up id info.
|
|
|
|
if (Tok.getKind() == tok::identifier) {
|
|
|
|
// Look up the identifier info for the token. We disabled identifier lookup
|
|
|
|
// by saying we're skipping contents, so we need to do this manually.
|
|
|
|
Tok.SetIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
|
|
|
|
}
|
2006-07-19 13:42:48 +08:00
|
|
|
}
|
|
|
|
|
2006-07-11 13:04:55 +08:00
|
|
|
/// isNextTokenLParen - If the next token lexed will pop this macro off the
|
|
|
|
/// expansion stack, return 2. If the next unexpanded token is a '(', return
|
|
|
|
/// 1, otherwise return 0.
|
|
|
|
unsigned MacroExpander::isNextTokenLParen() const {
|
2006-07-11 12:02:46 +08:00
|
|
|
// Out of tokens?
|
2006-07-14 14:54:44 +08:00
|
|
|
if (isAtEnd())
|
2006-07-11 13:04:55 +08:00
|
|
|
return 2;
|
2006-07-26 11:50:40 +08:00
|
|
|
return MacroTokens[CurToken].getKind() == tok::l_paren;
|
2006-07-11 12:02:46 +08:00
|
|
|
}
|