2017-12-09 06:39:26 +08:00
|
|
|
//===- TokenLexer.cpp - Lex from a token stream ---------------------------===//
|
2006-06-18 13:43:12 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-06-18 13:43:12 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-03-09 10:18:51 +08:00
|
|
|
// This file implements the TokenLexer interface.
|
2006-06-18 13:43:12 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-03-09 10:22:57 +08:00
|
|
|
#include "clang/Lex/TokenLexer.h"
|
2017-12-09 06:39:26 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
|
|
|
#include "clang/Basic/LangOptions.h"
|
|
|
|
#include "clang/Basic/SourceLocation.h"
|
2006-06-21 11:01:55 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2017-12-09 06:39:26 +08:00
|
|
|
#include "clang/Basic/TokenKinds.h"
|
2009-01-29 13:15:15 +08:00
|
|
|
#include "clang/Lex/LexDiagnostic.h"
|
2017-12-09 06:39:26 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
2014-01-07 19:51:46 +08:00
|
|
|
#include "clang/Lex/MacroArgs.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Lex/MacroInfo.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2017-12-09 06:39:26 +08:00
|
|
|
#include "clang/Lex/Token.h"
|
2017-10-15 09:26:26 +08:00
|
|
|
#include "clang/Lex/VariadicMacroSupport.h"
|
2017-12-09 06:39:26 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2012-02-04 21:45:25 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2017-12-09 06:39:26 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/iterator_range.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstring>
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2016-02-13 06:53:10 +08:00
|
|
|
using namespace clang;
|
2006-07-09 08:45:31 +08:00
|
|
|
|
2008-03-09 10:18:51 +08:00
|
|
|
/// Create a TokenLexer for the specified macro with the specified actual
|
2006-07-17 02:16:58 +08:00
|
|
|
/// arguments. Note that this ctor takes ownership of the ActualArgs pointer.
|
2012-08-30 21:38:46 +08:00
|
|
|
void TokenLexer::Init(Token &Tok, SourceLocation ELEnd, MacroInfo *MI,
|
|
|
|
MacroArgs *Actuals) {
|
2008-03-09 10:18:51 +08:00
|
|
|
// If the client is reusing a TokenLexer, make sure to free any memory
|
2007-07-15 08:25:26 +08:00
|
|
|
// associated with it.
|
|
|
|
destroy();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-08-30 21:38:46 +08:00
|
|
|
Macro = MI;
|
2007-07-15 08:25:26 +08:00
|
|
|
ActualArgs = Actuals;
|
2017-10-03 08:52:14 +08:00
|
|
|
CurTokenIdx = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-14 16:20:34 +08:00
|
|
|
ExpandLocStart = Tok.getLocation();
|
|
|
|
ExpandLocEnd = ELEnd;
|
2007-07-15 08:25:26 +08:00
|
|
|
AtStartOfLine = Tok.isAtStartOfLine();
|
|
|
|
HasLeadingSpace = Tok.hasLeadingSpace();
|
2014-02-05 03:18:32 +08:00
|
|
|
NextTokGetsSpace = false;
|
2008-03-09 10:07:49 +08:00
|
|
|
Tokens = &*Macro->tokens_begin();
|
|
|
|
OwnsTokens = false;
|
2008-03-10 14:06:04 +08:00
|
|
|
DisableMacroExpansion = false;
|
2008-03-09 10:07:49 +08:00
|
|
|
NumTokens = Macro->tokens_end()-Macro->tokens_begin();
|
2011-07-07 11:40:34 +08:00
|
|
|
MacroExpansionStart = SourceLocation();
|
|
|
|
|
|
|
|
SourceManager &SM = PP.getSourceManager();
|
Revamp the SourceManager to separate the representation of parsed
source locations from source locations loaded from an AST/PCH file.
Previously, loading an AST/PCH file involved carefully pre-allocating
space at the beginning of the source manager for the source locations
and FileIDs that correspond to the prefix, and then appending the
source locations/FileIDs used for parsing the remaining translation
unit. This design forced us into loading PCH files early, as a prefix,
whic has become a rather significant limitation.
This patch splits the SourceManager space into two parts: for source
location "addresses", the lower values (growing upward) are used to
describe parsed code, while upper values (growing downward) are used
for source locations loaded from AST/PCH files. Similarly, positive
FileIDs are used to describe parsed code while negative FileIDs are
used to file/macro locations loaded from AST/PCH files. As a result,
we can load PCH/AST files even during parsing, making various
improvemnts in the future possible, e.g., teaching #include <foo.h> to
look for and load <foo.h.gch> if it happens to be already available.
This patch was originally written by Sebastian Redl, then brought
forward to the modern age by Jonathan Turner, and finally
polished/finished by me to be committed.
llvm-svn: 135484
2011-07-20 00:10:42 +08:00
|
|
|
MacroStartSLocOffset = SM.getNextLocalOffset();
|
2011-07-07 11:40:34 +08:00
|
|
|
|
|
|
|
if (NumTokens > 0) {
|
|
|
|
assert(Tokens[0].getLocation().isValid());
|
|
|
|
assert((Tokens[0].getLocation().isFileID() || Tokens[0].is(tok::comment)) &&
|
|
|
|
"Macro defined in macro?");
|
2011-07-14 16:20:34 +08:00
|
|
|
assert(ExpandLocStart.isValid());
|
2011-07-07 11:40:34 +08:00
|
|
|
|
|
|
|
// Reserve a source location entry chunk for the length of the macro
|
|
|
|
// definition. Tokens that get lexed directly from the definition will
|
|
|
|
// have their locations pointing inside this chunk. This is to avoid
|
|
|
|
// creating separate source location entries for each token.
|
2011-08-24 05:02:38 +08:00
|
|
|
MacroDefStart = SM.getExpansionLoc(Tokens[0].getLocation());
|
|
|
|
MacroDefLength = Macro->getDefinitionLength(SM);
|
|
|
|
MacroExpansionStart = SM.createExpansionLoc(MacroDefStart,
|
2011-07-26 11:03:05 +08:00
|
|
|
ExpandLocStart,
|
|
|
|
ExpandLocEnd,
|
2011-08-24 05:02:38 +08:00
|
|
|
MacroDefLength);
|
2011-07-07 11:40:34 +08:00
|
|
|
}
|
2006-07-14 14:54:44 +08:00
|
|
|
|
|
|
|
// If this is a function-like macro, expand the arguments and change
|
2008-03-09 10:07:49 +08:00
|
|
|
// Tokens to point to the expanded tokens.
|
2017-07-18 01:18:43 +08:00
|
|
|
if (Macro->isFunctionLike() && Macro->getNumParams())
|
2006-07-14 14:54:44 +08:00
|
|
|
ExpandFunctionArguments();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-03-09 10:18:51 +08:00
|
|
|
/// Create a TokenLexer for the specified token stream. This does not
|
2006-07-17 02:16:58 +08:00
|
|
|
/// take ownership of the specified token vector.
|
2008-03-10 14:06:04 +08:00
|
|
|
void TokenLexer::Init(const Token *TokArray, unsigned NumToks,
|
|
|
|
bool disableMacroExpansion, bool ownsTokens) {
|
2008-03-09 10:18:51 +08:00
|
|
|
// If the client is reusing a TokenLexer, make sure to free any memory
|
2007-07-15 08:25:26 +08:00
|
|
|
// associated with it.
|
|
|
|
destroy();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-05-18 07:10:59 +08:00
|
|
|
Macro = nullptr;
|
|
|
|
ActualArgs = nullptr;
|
2008-03-09 10:07:49 +08:00
|
|
|
Tokens = TokArray;
|
2008-03-10 14:06:04 +08:00
|
|
|
OwnsTokens = ownsTokens;
|
|
|
|
DisableMacroExpansion = disableMacroExpansion;
|
2008-03-09 10:07:49 +08:00
|
|
|
NumTokens = NumToks;
|
2017-10-03 08:52:14 +08:00
|
|
|
CurTokenIdx = 0;
|
2011-07-14 16:20:34 +08:00
|
|
|
ExpandLocStart = ExpandLocEnd = SourceLocation();
|
2007-07-15 08:25:26 +08:00
|
|
|
AtStartOfLine = false;
|
|
|
|
HasLeadingSpace = false;
|
2014-02-05 03:18:32 +08:00
|
|
|
NextTokGetsSpace = false;
|
2011-07-07 11:40:34 +08:00
|
|
|
MacroExpansionStart = SourceLocation();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-17 02:16:58 +08:00
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-09 10:18:51 +08:00
|
|
|
void TokenLexer::destroy() {
|
2006-07-14 14:54:44 +08:00
|
|
|
// If this was a function-like macro that actually uses its arguments, delete
|
|
|
|
// the expanded tokens.
|
2008-03-09 10:07:49 +08:00
|
|
|
if (OwnsTokens) {
|
|
|
|
delete [] Tokens;
|
2014-05-18 07:10:59 +08:00
|
|
|
Tokens = nullptr;
|
2009-03-04 14:50:57 +08:00
|
|
|
OwnsTokens = false;
|
2007-07-22 09:16:55 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 10:18:51 +08:00
|
|
|
// TokenLexer owns its formal arguments.
|
2009-12-15 06:12:52 +08:00
|
|
|
if (ActualArgs) ActualArgs->destroy(PP);
|
2006-06-19 00:22:51 +08:00
|
|
|
}
|
|
|
|
|
2014-05-09 09:00:48 +08:00
|
|
|
bool TokenLexer::MaybeRemoveCommaBeforeVaArgs(
|
|
|
|
SmallVectorImpl<Token> &ResultToks, bool HasPasteOperator, MacroInfo *Macro,
|
|
|
|
unsigned MacroArgNo, Preprocessor &PP) {
|
2012-11-09 21:24:30 +08:00
|
|
|
// Is the macro argument __VA_ARGS__?
|
2017-07-18 01:18:43 +08:00
|
|
|
if (!Macro->isVariadic() || MacroArgNo != Macro->getNumParams()-1)
|
2012-11-09 21:24:30 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// In Microsoft-compatibility mode, a comma is removed in the expansion
|
|
|
|
// of " ... , __VA_ARGS__ " if __VA_ARGS__ is empty. This extension is
|
|
|
|
// not supported by gcc.
|
2014-01-14 20:51:41 +08:00
|
|
|
if (!HasPasteOperator && !PP.getLangOpts().MSVCCompat)
|
2012-11-09 21:24:30 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// GCC removes the comma in the expansion of " ... , ## __VA_ARGS__ " if
|
|
|
|
// __VA_ARGS__ is empty, but not in strict C99 mode where there are no
|
|
|
|
// named arguments, where it remains. In all other modes, including C99
|
|
|
|
// with GNU extensions, it is removed regardless of named arguments.
|
|
|
|
// Microsoft also appears to support this extension, unofficially.
|
|
|
|
if (PP.getLangOpts().C99 && !PP.getLangOpts().GNUMode
|
2017-07-18 01:18:43 +08:00
|
|
|
&& Macro->getNumParams() < 2)
|
2012-11-09 21:24:30 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Is a comma available to be removed?
|
|
|
|
if (ResultToks.empty() || !ResultToks.back().is(tok::comma))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Issue an extension diagnostic for the paste operator.
|
|
|
|
if (HasPasteOperator)
|
|
|
|
PP.Diag(ResultToks.back().getLocation(), diag::ext_paste_comma);
|
|
|
|
|
|
|
|
// Remove the comma.
|
|
|
|
ResultToks.pop_back();
|
|
|
|
|
2016-01-23 03:26:44 +08:00
|
|
|
if (!ResultToks.empty()) {
|
|
|
|
// If the comma was right after another paste (e.g. "X##,##__VA_ARGS__"),
|
|
|
|
// then removal of the comma should produce a placemarker token (in C99
|
|
|
|
// terms) which we model by popping off the previous ##, giving us a plain
|
|
|
|
// "X" when __VA_ARGS__ is empty.
|
|
|
|
if (ResultToks.back().is(tok::hashhash))
|
|
|
|
ResultToks.pop_back();
|
|
|
|
|
|
|
|
// Remember that this comma was elided.
|
|
|
|
ResultToks.back().setFlag(Token::CommaAfterElided);
|
|
|
|
}
|
2012-11-09 21:24:30 +08:00
|
|
|
|
|
|
|
// Never add a space, even if the comma, ##, or arg had a space.
|
|
|
|
NextTokGetsSpace = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-15 09:26:26 +08:00
|
|
|
void TokenLexer::stringifyVAOPTContents(
|
|
|
|
SmallVectorImpl<Token> &ResultToks, const VAOptExpansionContext &VCtx,
|
|
|
|
const SourceLocation VAOPTClosingParenLoc) {
|
|
|
|
const int NumToksPriorToVAOpt = VCtx.getNumberOfTokensPriorToVAOpt();
|
|
|
|
const unsigned int NumVAOptTokens = ResultToks.size() - NumToksPriorToVAOpt;
|
|
|
|
Token *const VAOPTTokens =
|
|
|
|
NumVAOptTokens ? &ResultToks[NumToksPriorToVAOpt] : nullptr;
|
|
|
|
|
|
|
|
SmallVector<Token, 64> ConcatenatedVAOPTResultToks;
|
|
|
|
// FIXME: Should we keep track within VCtx that we did or didnot
|
|
|
|
// encounter pasting - and only then perform this loop.
|
|
|
|
|
|
|
|
// Perform token pasting (concatenation) prior to stringization.
|
|
|
|
for (unsigned int CurTokenIdx = 0; CurTokenIdx != NumVAOptTokens;
|
|
|
|
++CurTokenIdx) {
|
|
|
|
if (VAOPTTokens[CurTokenIdx].is(tok::hashhash)) {
|
|
|
|
assert(CurTokenIdx != 0 &&
|
|
|
|
"Can not have __VAOPT__ contents begin with a ##");
|
|
|
|
Token &LHS = VAOPTTokens[CurTokenIdx - 1];
|
|
|
|
pasteTokens(LHS, llvm::makeArrayRef(VAOPTTokens, NumVAOptTokens),
|
|
|
|
CurTokenIdx);
|
|
|
|
// Replace the token prior to the first ## in this iteration.
|
|
|
|
ConcatenatedVAOPTResultToks.back() = LHS;
|
|
|
|
if (CurTokenIdx == NumVAOptTokens)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ConcatenatedVAOPTResultToks.push_back(VAOPTTokens[CurTokenIdx]);
|
|
|
|
}
|
|
|
|
|
|
|
|
ConcatenatedVAOPTResultToks.push_back(VCtx.getEOFTok());
|
|
|
|
// Get the SourceLocation that represents the start location within
|
|
|
|
// the macro definition that marks where this string is substituted
|
|
|
|
// into: i.e. the __VA_OPT__ and the ')' within the spelling of the
|
|
|
|
// macro definition, and use it to indicate that the stringified token
|
|
|
|
// was generated from that location.
|
|
|
|
const SourceLocation ExpansionLocStartWithinMacro =
|
|
|
|
getExpansionLocForMacroDefLoc(VCtx.getVAOptLoc());
|
|
|
|
const SourceLocation ExpansionLocEndWithinMacro =
|
|
|
|
getExpansionLocForMacroDefLoc(VAOPTClosingParenLoc);
|
|
|
|
|
|
|
|
Token StringifiedVAOPT = MacroArgs::StringifyArgument(
|
|
|
|
&ConcatenatedVAOPTResultToks[0], PP, VCtx.hasCharifyBefore() /*Charify*/,
|
|
|
|
ExpansionLocStartWithinMacro, ExpansionLocEndWithinMacro);
|
|
|
|
|
|
|
|
if (VCtx.getLeadingSpaceForStringifiedToken())
|
|
|
|
StringifiedVAOPT.setFlag(Token::LeadingSpace);
|
|
|
|
|
|
|
|
StringifiedVAOPT.setFlag(Token::StringifiedInMacro);
|
|
|
|
// Resize (shrink) the token stream to just capture this stringified token.
|
|
|
|
ResultToks.resize(NumToksPriorToVAOpt + 1);
|
|
|
|
ResultToks.back() = StringifiedVAOPT;
|
|
|
|
}
|
|
|
|
|
2006-07-14 14:54:44 +08:00
|
|
|
/// Expand the arguments of a function-like macro so that we can quickly
|
2008-03-09 10:07:49 +08:00
|
|
|
/// return preexpanded tokens from Tokens.
|
2008-03-09 10:18:51 +08:00
|
|
|
void TokenLexer::ExpandFunctionArguments() {
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<Token, 128> ResultToks;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 10:07:49 +08:00
|
|
|
// Loop through 'Tokens', expanding them into ResultToks. Keep
|
2006-07-14 14:54:44 +08:00
|
|
|
// track of whether we change anything. If not, no need to keep them. If so,
|
2008-03-09 10:07:49 +08:00
|
|
|
// we install the newly expanded sequence as the new 'Tokens' list.
|
2006-07-14 14:54:44 +08:00
|
|
|
bool MadeChange = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2017-10-15 09:26:26 +08:00
|
|
|
const bool CalledWithVariadicArguments =
|
|
|
|
ActualArgs->invokedWithVariadicArgument(Macro);
|
|
|
|
|
|
|
|
VAOptExpansionContext VCtx(PP);
|
|
|
|
|
2017-07-20 09:10:56 +08:00
|
|
|
for (unsigned I = 0, E = NumTokens; I != E; ++I) {
|
|
|
|
const Token &CurTok = Tokens[I];
|
2017-05-05 05:31:17 +08:00
|
|
|
// We don't want a space for the next token after a paste
|
|
|
|
// operator. In valid code, the token will get smooshed onto the
|
|
|
|
// preceding one anyway. In assembler-with-cpp mode, invalid
|
|
|
|
// pastes are allowed through: in this case, we do not want the
|
|
|
|
// extra whitespace to be added. For example, we want ". ## foo"
|
|
|
|
// -> ".foo" not ". foo".
|
2017-07-20 09:10:56 +08:00
|
|
|
if (I != 0 && !Tokens[I-1].is(tok::hashhash) && CurTok.hasLeadingSpace())
|
2014-02-05 03:18:37 +08:00
|
|
|
NextTokGetsSpace = true;
|
|
|
|
|
2017-10-15 09:26:26 +08:00
|
|
|
if (VCtx.isVAOptToken(CurTok)) {
|
|
|
|
MadeChange = true;
|
|
|
|
assert(Tokens[I + 1].is(tok::l_paren) &&
|
|
|
|
"__VA_OPT__ must be followed by '('");
|
|
|
|
|
|
|
|
++I; // Skip the l_paren
|
|
|
|
VCtx.sawVAOptFollowedByOpeningParens(CurTok.getLocation(),
|
|
|
|
ResultToks.size());
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have entered into the __VA_OPT__ context, so handle tokens
|
|
|
|
// appropriately.
|
|
|
|
if (VCtx.isInVAOpt()) {
|
|
|
|
// If we are about to process a token that is either an argument to
|
|
|
|
// __VA_OPT__ or its closing rparen, then:
|
|
|
|
// 1) If the token is the closing rparen that exits us out of __VA_OPT__,
|
|
|
|
// perform any necessary stringification or placemarker processing,
|
|
|
|
// and/or skip to the next token.
|
|
|
|
// 2) else if macro was invoked without variadic arguments skip this
|
|
|
|
// token.
|
|
|
|
// 3) else (macro was invoked with variadic arguments) process the token
|
|
|
|
// normally.
|
|
|
|
|
|
|
|
if (Tokens[I].is(tok::l_paren))
|
|
|
|
VCtx.sawOpeningParen(Tokens[I].getLocation());
|
|
|
|
// Continue skipping tokens within __VA_OPT__ if the macro was not
|
|
|
|
// called with variadic arguments, else let the rest of the loop handle
|
|
|
|
// this token. Note sawClosingParen() returns true only if the r_paren matches
|
|
|
|
// the closing r_paren of the __VA_OPT__.
|
|
|
|
if (!Tokens[I].is(tok::r_paren) || !VCtx.sawClosingParen()) {
|
|
|
|
if (!CalledWithVariadicArguments) {
|
|
|
|
// Skip this token.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// ... else the macro was called with variadic arguments, and we do not
|
|
|
|
// have a closing rparen - so process this token normally.
|
|
|
|
} else {
|
|
|
|
// Current token is the closing r_paren which marks the end of the
|
|
|
|
// __VA_OPT__ invocation, so handle any place-marker pasting (if
|
|
|
|
// empty) by removing hashhash either before (if exists) or after. And
|
|
|
|
// also stringify the entire contents if VAOPT was preceded by a hash,
|
|
|
|
// but do so only after any token concatenation that needs to occur
|
|
|
|
// within the contents of VAOPT.
|
|
|
|
|
|
|
|
if (VCtx.hasStringifyOrCharifyBefore()) {
|
|
|
|
// Replace all the tokens just added from within VAOPT into a single
|
|
|
|
// stringified token. This requires token-pasting to eagerly occur
|
|
|
|
// within these tokens. If either the contents of VAOPT were empty
|
|
|
|
// or the macro wasn't called with any variadic arguments, the result
|
|
|
|
// is a token that represents an empty string.
|
|
|
|
stringifyVAOPTContents(ResultToks, VCtx,
|
|
|
|
/*ClosingParenLoc*/ Tokens[I].getLocation());
|
|
|
|
|
|
|
|
} else if (/*No tokens within VAOPT*/ !(
|
|
|
|
ResultToks.size() - VCtx.getNumberOfTokensPriorToVAOpt())) {
|
|
|
|
// Treat VAOPT as a placemarker token. Eat either the '##' before the
|
|
|
|
// RHS/VAOPT (if one exists, suggesting that the LHS (if any) to that
|
|
|
|
// hashhash was not a placemarker) or the '##'
|
|
|
|
// after VAOPT, but not both.
|
|
|
|
|
|
|
|
if (ResultToks.size() && ResultToks.back().is(tok::hashhash)) {
|
|
|
|
ResultToks.pop_back();
|
|
|
|
} else if ((I + 1 != E) && Tokens[I + 1].is(tok::hashhash)) {
|
|
|
|
++I; // Skip the following hashhash.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VCtx.reset();
|
|
|
|
// We processed __VA_OPT__'s closing paren (and the exit out of
|
|
|
|
// __VA_OPT__), so skip to the next token.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we found the stringify operator, get the argument stringified. The
|
|
|
|
// preprocessor already verified that the following token is a macro
|
|
|
|
// parameter or __VA_OPT__ when the #define was lexed.
|
|
|
|
|
2015-06-18 18:59:26 +08:00
|
|
|
if (CurTok.isOneOf(tok::hash, tok::hashat)) {
|
2017-07-20 09:10:56 +08:00
|
|
|
int ArgNo = Macro->getParameterNum(Tokens[I+1].getIdentifierInfo());
|
2017-10-15 09:26:26 +08:00
|
|
|
assert((ArgNo != -1 || VCtx.isVAOptToken(Tokens[I + 1])) &&
|
|
|
|
"Token following # is not an argument or __VA_OPT__!");
|
|
|
|
|
|
|
|
if (ArgNo == -1) {
|
|
|
|
// Handle the __VA_OPT__ case.
|
|
|
|
VCtx.sawHashOrHashAtBefore(NextTokGetsSpace,
|
|
|
|
CurTok.is(tok::hashat));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Else handle the simple argument case.
|
2011-10-04 02:39:03 +08:00
|
|
|
SourceLocation ExpansionLocStart =
|
2011-08-24 05:02:32 +08:00
|
|
|
getExpansionLocForMacroDefLoc(CurTok.getLocation());
|
2011-10-04 02:39:03 +08:00
|
|
|
SourceLocation ExpansionLocEnd =
|
2017-07-20 09:10:56 +08:00
|
|
|
getExpansionLocForMacroDefLoc(Tokens[I+1].getLocation());
|
2011-07-07 11:40:34 +08:00
|
|
|
|
2007-07-21 00:59:19 +08:00
|
|
|
Token Res;
|
2007-10-10 02:02:16 +08:00
|
|
|
if (CurTok.is(tok::hash)) // Stringify
|
2011-10-04 02:39:03 +08:00
|
|
|
Res = ActualArgs->getStringifiedArgument(ArgNo, PP,
|
|
|
|
ExpansionLocStart,
|
|
|
|
ExpansionLocEnd);
|
2006-07-15 14:11:25 +08:00
|
|
|
else {
|
|
|
|
// 'charify': don't bother caching these.
|
2008-03-09 10:55:12 +08:00
|
|
|
Res = MacroArgs::StringifyArgument(ActualArgs->getUnexpArgument(ArgNo),
|
2011-10-04 02:39:03 +08:00
|
|
|
PP, true,
|
|
|
|
ExpansionLocStart,
|
|
|
|
ExpansionLocEnd);
|
2006-07-15 14:11:25 +08:00
|
|
|
}
|
2014-12-15 12:18:11 +08:00
|
|
|
Res.setFlag(Token::StringifiedInMacro);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-15 14:48:02 +08:00
|
|
|
// The stringified/charified string leading space flag gets set to match
|
|
|
|
// the #/#@ operator.
|
2014-02-05 03:18:37 +08:00
|
|
|
if (NextTokGetsSpace)
|
2007-07-21 00:59:19 +08:00
|
|
|
Res.setFlag(Token::LeadingSpace);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-26 12:55:32 +08:00
|
|
|
ResultToks.push_back(Res);
|
2006-07-14 14:54:44 +08:00
|
|
|
MadeChange = true;
|
2017-07-20 09:10:56 +08:00
|
|
|
++I; // Skip arg name.
|
2006-07-29 12:16:20 +08:00
|
|
|
NextTokGetsSpace = false;
|
2006-07-28 13:10:36 +08:00
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-02-05 03:18:28 +08:00
|
|
|
// Find out if there is a paste (##) operator before or after the token.
|
|
|
|
bool NonEmptyPasteBefore =
|
|
|
|
!ResultToks.empty() && ResultToks.back().is(tok::hashhash);
|
2017-07-20 09:10:56 +08:00
|
|
|
bool PasteBefore = I != 0 && Tokens[I-1].is(tok::hashhash);
|
|
|
|
bool PasteAfter = I+1 != E && Tokens[I+1].is(tok::hashhash);
|
2017-10-15 09:26:26 +08:00
|
|
|
|
|
|
|
assert((!NonEmptyPasteBefore || PasteBefore || VCtx.isInVAOpt()) &&
|
|
|
|
"unexpected ## in ResultToks");
|
2014-02-05 03:18:28 +08:00
|
|
|
|
2006-07-28 13:10:36 +08:00
|
|
|
// Otherwise, if this is not an argument token, just add the token to the
|
|
|
|
// output buffer.
|
|
|
|
IdentifierInfo *II = CurTok.getIdentifierInfo();
|
2017-07-18 01:18:43 +08:00
|
|
|
int ArgNo = II ? Macro->getParameterNum(II) : -1;
|
2006-07-28 13:10:36 +08:00
|
|
|
if (ArgNo == -1) {
|
2006-07-30 16:40:43 +08:00
|
|
|
// This isn't an argument, just add it.
|
|
|
|
ResultToks.push_back(CurTok);
|
2006-07-29 12:16:20 +08:00
|
|
|
|
2006-07-30 16:40:43 +08:00
|
|
|
if (NextTokGetsSpace) {
|
2007-07-21 00:59:19 +08:00
|
|
|
ResultToks.back().setFlag(Token::LeadingSpace);
|
2006-07-30 16:40:43 +08:00
|
|
|
NextTokGetsSpace = false;
|
2014-02-05 03:18:28 +08:00
|
|
|
} else if (PasteBefore && !NonEmptyPasteBefore)
|
|
|
|
ResultToks.back().clearFlag(Token::LeadingSpace);
|
|
|
|
|
2006-07-30 16:40:43 +08:00
|
|
|
continue;
|
2006-07-28 13:10:36 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +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
|
|
|
|
2014-02-05 03:18:28 +08:00
|
|
|
// Otherwise, this is a use of the argument.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-11-09 21:24:30 +08:00
|
|
|
// In Microsoft mode, remove the comma before __VA_ARGS__ to ensure there
|
|
|
|
// are no trailing commas if __VA_ARGS__ is empty.
|
|
|
|
if (!PasteBefore && ActualArgs->isVarargsElidedUse() &&
|
2014-02-05 03:18:32 +08:00
|
|
|
MaybeRemoveCommaBeforeVaArgs(ResultToks,
|
2012-11-09 21:24:30 +08:00
|
|
|
/*HasPasteOperator=*/false,
|
|
|
|
Macro, ArgNo, PP))
|
|
|
|
continue;
|
|
|
|
|
2006-07-28 13:10:36 +08:00
|
|
|
// 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) {
|
2007-07-21 00:59:19 +08:00
|
|
|
const Token *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.
|
2007-07-21 00:59:19 +08:00
|
|
|
const Token *ArgTok = ActualArgs->getUnexpArgument(ArgNo);
|
2007-10-07 16:44:20 +08:00
|
|
|
if (ActualArgs->ArgNeedsPreexpansion(ArgTok, PP))
|
2017-09-30 21:58:38 +08:00
|
|
|
ResultArgToks = &ActualArgs->getPreExpArgument(ArgNo, PP)[0];
|
2006-07-28 13:10:36 +08:00
|
|
|
else
|
|
|
|
ResultArgToks = ArgTok; // Use non-preexpanded tokens.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-28 13:10:36 +08:00
|
|
|
// If the arg token expanded into anything, append it.
|
2007-10-10 02:02:16 +08:00
|
|
|
if (ResultArgToks->isNot(tok::eof)) {
|
2016-10-26 21:06:13 +08:00
|
|
|
size_t FirstResult = ResultToks.size();
|
2006-07-28 13:10:36 +08:00
|
|
|
unsigned NumToks = MacroArgs::getArgLength(ResultArgToks);
|
|
|
|
ResultToks.append(ResultArgToks, ResultArgToks+NumToks);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-06-27 01:16:08 +08:00
|
|
|
// In Microsoft-compatibility mode, we follow MSVC's preprocessing
|
|
|
|
// behavior by not considering single commas from nested macro
|
|
|
|
// expansions as argument separators. Set a flag on the token so we can
|
|
|
|
// test for this later when the macro expansion is processed.
|
2014-01-14 20:51:41 +08:00
|
|
|
if (PP.getLangOpts().MSVCCompat && NumToks == 1 &&
|
2013-06-27 01:16:08 +08:00
|
|
|
ResultToks.back().is(tok::comma))
|
|
|
|
ResultToks.back().setFlag(Token::IgnoredComma);
|
|
|
|
|
2011-07-08 02:04:47 +08:00
|
|
|
// If the '##' came from expanding an argument, turn it into 'unknown'
|
|
|
|
// to avoid pasting.
|
2016-10-26 21:06:13 +08:00
|
|
|
for (Token &Tok : llvm::make_range(ResultToks.begin() + FirstResult,
|
|
|
|
ResultToks.end())) {
|
2011-07-08 02:04:47 +08:00
|
|
|
if (Tok.is(tok::hashhash))
|
|
|
|
Tok.setKind(tok::unknown);
|
|
|
|
}
|
|
|
|
|
2011-07-14 16:20:34 +08:00
|
|
|
if(ExpandLocStart.isValid()) {
|
2011-08-20 06:34:17 +08:00
|
|
|
updateLocForMacroArgTokens(CurTok.getLocation(),
|
|
|
|
ResultToks.begin()+FirstResult,
|
|
|
|
ResultToks.end());
|
2011-07-07 11:40:34 +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.
|
2007-07-21 00:59:19 +08:00
|
|
|
ResultToks[FirstResult].setFlagValue(Token::LeadingSpace,
|
2006-07-29 12:16:20 +08:00
|
|
|
NextTokGetsSpace);
|
2016-01-15 11:24:18 +08:00
|
|
|
ResultToks[FirstResult].setFlagValue(Token::StartOfLine, false);
|
2006-07-29 12:16:20 +08:00
|
|
|
NextTokGetsSpace = false;
|
2006-07-28 13:07:04 +08:00
|
|
|
}
|
|
|
|
continue;
|
2006-07-14 14:54:44 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +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.
|
2007-07-21 00:59:19 +08:00
|
|
|
const Token *ArgToks = ActualArgs->getUnexpArgument(ArgNo);
|
2006-07-28 13:10:36 +08:00
|
|
|
unsigned NumToks = MacroArgs::getArgLength(ArgToks);
|
|
|
|
if (NumToks) { // Not an empty argument?
|
2017-05-05 05:31:17 +08:00
|
|
|
bool VaArgsPseudoPaste = false;
|
2012-06-23 07:59:08 +08:00
|
|
|
// If this is the GNU ", ## __VA_ARGS__" extension, and we just learned
|
|
|
|
// that __VA_ARGS__ expands to multiple tokens, avoid a pasting error when
|
2018-04-06 23:14:32 +08:00
|
|
|
// the expander tries to paste ',' with the first token of the __VA_ARGS__
|
2008-01-29 15:54:23 +08:00
|
|
|
// expansion.
|
2013-05-25 09:35:18 +08:00
|
|
|
if (NonEmptyPasteBefore && ResultToks.size() >= 2 &&
|
2008-01-29 15:54:23 +08:00
|
|
|
ResultToks[ResultToks.size()-2].is(tok::comma) &&
|
2017-07-18 01:18:43 +08:00
|
|
|
(unsigned)ArgNo == Macro->getNumParams()-1 &&
|
2008-01-29 15:54:23 +08:00
|
|
|
Macro->isVariadic()) {
|
2017-05-05 05:31:17 +08:00
|
|
|
VaArgsPseudoPaste = true;
|
2008-01-29 15:54:23 +08:00
|
|
|
// Remove the paste operator, report use of the extension.
|
2013-08-24 00:11:15 +08:00
|
|
|
PP.Diag(ResultToks.pop_back_val().getLocation(), diag::ext_paste_comma);
|
2008-01-29 15:54:23 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-28 13:10:36 +08:00
|
|
|
ResultToks.append(ArgToks, ArgToks+NumToks);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-08 02:04:47 +08:00
|
|
|
// If the '##' came from expanding an argument, turn it into 'unknown'
|
|
|
|
// to avoid pasting.
|
2016-10-26 21:06:13 +08:00
|
|
|
for (Token &Tok : llvm::make_range(ResultToks.end() - NumToks,
|
|
|
|
ResultToks.end())) {
|
2011-07-08 02:04:47 +08:00
|
|
|
if (Tok.is(tok::hashhash))
|
|
|
|
Tok.setKind(tok::unknown);
|
|
|
|
}
|
|
|
|
|
2011-07-14 16:20:34 +08:00
|
|
|
if (ExpandLocStart.isValid()) {
|
2011-08-20 06:34:17 +08:00
|
|
|
updateLocForMacroArgTokens(CurTok.getLocation(),
|
|
|
|
ResultToks.end()-NumToks, ResultToks.end());
|
2011-07-07 11:40:34 +08:00
|
|
|
}
|
|
|
|
|
2017-05-05 05:31:17 +08:00
|
|
|
// Transfer the leading whitespace information from the token
|
|
|
|
// (the macro argument) onto the first token of the
|
|
|
|
// expansion. Note that we don't do this for the GNU
|
|
|
|
// pseudo-paste extension ", ## __VA_ARGS__".
|
|
|
|
if (!VaArgsPseudoPaste) {
|
|
|
|
ResultToks[ResultToks.size() - NumToks].setFlagValue(Token::StartOfLine,
|
|
|
|
false);
|
|
|
|
ResultToks[ResultToks.size() - NumToks].setFlagValue(
|
|
|
|
Token::LeadingSpace, NextTokGetsSpace);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-26 00:23:08 +08:00
|
|
|
NextTokGetsSpace = false;
|
2006-07-28 13:10:36 +08:00
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-28 13:10:36 +08:00
|
|
|
// 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.
|
2017-07-20 09:10:56 +08:00
|
|
|
++I;
|
2006-07-28 13:10:36 +08:00
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-28 13:10:36 +08:00
|
|
|
// If this is on the RHS of a paste operator, we've already copied the
|
2013-05-25 09:35:18 +08:00
|
|
|
// paste operator to the ResultToks list, unless the LHS was empty too.
|
|
|
|
// Remove it.
|
|
|
|
assert(PasteBefore);
|
|
|
|
if (NonEmptyPasteBefore) {
|
|
|
|
assert(ResultToks.back().is(tok::hashhash));
|
2017-10-15 09:26:26 +08:00
|
|
|
// Do not remove the paste operator if it is the one before __VA_OPT__
|
|
|
|
// (and we are still processing tokens within VA_OPT). We handle the case
|
|
|
|
// of removing the paste operator if __VA_OPT__ reduces to the notional
|
|
|
|
// placemarker above when we encounter the closing paren of VA_OPT.
|
|
|
|
if (!VCtx.isInVAOpt() ||
|
|
|
|
ResultToks.size() > VCtx.getNumberOfTokensPriorToVAOpt())
|
|
|
|
ResultToks.pop_back();
|
2013-05-25 09:35:18 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-29 12:39:41 +08:00
|
|
|
// If this is the __VA_ARGS__ token, and if the argument wasn't provided,
|
|
|
|
// and if the macro had at least one real argument, and if the token before
|
2012-11-09 21:24:30 +08:00
|
|
|
// the ## was a comma, remove the comma. This is a GCC extension which is
|
|
|
|
// disabled when using -std=c99.
|
|
|
|
if (ActualArgs->isVarargsElidedUse())
|
2014-02-05 03:18:32 +08:00
|
|
|
MaybeRemoveCommaBeforeVaArgs(ResultToks,
|
2012-11-09 21:24:30 +08:00
|
|
|
/*HasPasteOperator=*/true,
|
|
|
|
Macro, ArgNo, PP);
|
2006-07-14 14:54:44 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 10:07:49 +08:00
|
|
|
// If anything changed, install this as the new Tokens list.
|
2006-07-14 14:54:44 +08:00
|
|
|
if (MadeChange) {
|
2009-03-04 14:50:57 +08:00
|
|
|
assert(!OwnsTokens && "This would leak if we already own the token list");
|
2006-07-14 14:54:44 +08:00
|
|
|
// This is deleted in the dtor.
|
2008-03-09 10:07:49 +08:00
|
|
|
NumTokens = ResultToks.size();
|
2011-06-30 06:20:11 +08:00
|
|
|
// The tokens will be added to Preprocessor's cache and will be removed
|
|
|
|
// when this TokenLexer finishes lexing them.
|
|
|
|
Tokens = PP.cacheMacroExpandedTokens(this, ResultToks);
|
|
|
|
|
|
|
|
// The preprocessor cache of macro expanded tokens owns these tokens,not us.
|
2009-03-04 14:50:57 +08:00
|
|
|
OwnsTokens = false;
|
2006-07-14 14:54:44 +08:00
|
|
|
}
|
|
|
|
}
|
2006-06-26 10:03:42 +08:00
|
|
|
|
2014-12-15 12:18:11 +08:00
|
|
|
/// \brief Checks if two tokens form wide string literal.
|
|
|
|
static bool isWideStringLiteralFromMacro(const Token &FirstTok,
|
|
|
|
const Token &SecondTok) {
|
|
|
|
return FirstTok.is(tok::identifier) &&
|
|
|
|
FirstTok.getIdentifierInfo()->isStr("L") && SecondTok.isLiteral() &&
|
|
|
|
SecondTok.stringifiedInMacro();
|
|
|
|
}
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
/// Lex - Lex and return a token from this macro stream.
|
2013-09-19 08:41:32 +08:00
|
|
|
bool TokenLexer::Lex(Token &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();
|
|
|
|
|
2013-09-19 08:41:32 +08:00
|
|
|
Tok.startToken();
|
|
|
|
Tok.setFlagValue(Token::StartOfLine , AtStartOfLine);
|
2014-02-05 03:18:32 +08:00
|
|
|
Tok.setFlagValue(Token::LeadingSpace, HasLeadingSpace || NextTokGetsSpace);
|
2017-10-03 08:52:14 +08:00
|
|
|
if (CurTokenIdx == 0)
|
2013-09-19 08:41:32 +08:00
|
|
|
Tok.setFlag(Token::LeadingEmptyMacro);
|
|
|
|
return PP.HandleEndOfTokenLexer(Tok);
|
2006-07-17 02:16:58 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-07 11:40:37 +08:00
|
|
|
SourceManager &SM = PP.getSourceManager();
|
|
|
|
|
2006-07-19 13:45:55 +08:00
|
|
|
// If this is the first token of the expanded result, we inherit spacing
|
|
|
|
// properties later.
|
2017-10-03 08:52:14 +08:00
|
|
|
bool isFirstToken = CurTokenIdx == 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Get the next token to return.
|
2017-10-03 08:52:14 +08:00
|
|
|
Tok = Tokens[CurTokenIdx++];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-20 04:29:42 +08:00
|
|
|
bool TokenIsFromPaste = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-19 13:42:48 +08:00
|
|
|
// If this token is followed by a token paste (##) operator, paste the tokens!
|
2011-06-15 02:19:37 +08:00
|
|
|
// Note that ## is a normal token when not expanding a macro.
|
2014-12-15 12:18:11 +08:00
|
|
|
if (!isAtEnd() && Macro &&
|
2017-10-03 08:52:14 +08:00
|
|
|
(Tokens[CurTokenIdx].is(tok::hashhash) ||
|
2014-12-15 12:18:11 +08:00
|
|
|
// Special processing of L#x macros in -fms-compatibility mode.
|
|
|
|
// Microsoft compiler is able to form a wide string literal from
|
|
|
|
// 'L#macro_arg' construct in a function-like macro.
|
|
|
|
(PP.getLangOpts().MSVCCompat &&
|
2017-10-03 08:52:14 +08:00
|
|
|
isWideStringLiteralFromMacro(Tok, Tokens[CurTokenIdx])))) {
|
2009-12-04 14:14:03 +08:00
|
|
|
// When handling the microsoft /##/ extension, the final token is
|
2017-10-03 08:52:14 +08:00
|
|
|
// returned by pasteTokens, not the pasted token.
|
|
|
|
if (pasteTokens(Tok))
|
2013-09-19 08:41:32 +08:00
|
|
|
return true;
|
2010-03-13 16:53:33 +08:00
|
|
|
|
2009-12-04 14:14:03 +08:00
|
|
|
TokenIsFromPaste = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
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
|
2011-07-14 16:20:34 +08:00
|
|
|
// ExpansionLoc. Pull this information together into a new SourceLocation
|
2006-06-30 14:10:41 +08:00
|
|
|
// that captures all of this.
|
2011-07-14 16:20:34 +08:00
|
|
|
if (ExpandLocStart.isValid() && // Don't do this for token streams.
|
2011-07-07 11:40:34 +08:00
|
|
|
// Check that the token's location was not already set properly.
|
2011-08-24 05:02:35 +08:00
|
|
|
SM.isBeforeInSLocAddrSpace(Tok.getLocation(), MacroStartSLocOffset)) {
|
2011-07-07 11:40:34 +08:00
|
|
|
SourceLocation instLoc;
|
|
|
|
if (Tok.is(tok::comment)) {
|
2011-07-26 11:03:05 +08:00
|
|
|
instLoc = SM.createExpansionLoc(Tok.getLocation(),
|
|
|
|
ExpandLocStart,
|
|
|
|
ExpandLocEnd,
|
|
|
|
Tok.getLength());
|
2011-07-07 11:40:34 +08:00
|
|
|
} else {
|
2011-08-20 06:34:14 +08:00
|
|
|
instLoc = getExpansionLocForMacroDefLoc(Tok.getLocation());
|
2011-07-07 11:40:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Tok.setLocation(instLoc);
|
2006-07-17 02:16:58 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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) {
|
2007-07-21 00:59:19 +08:00
|
|
|
Tok.setFlagValue(Token::StartOfLine , AtStartOfLine);
|
|
|
|
Tok.setFlagValue(Token::LeadingSpace, HasLeadingSpace);
|
2014-02-05 03:18:35 +08:00
|
|
|
} else {
|
|
|
|
// If this is not the first token, we may still need to pass through
|
|
|
|
// leading whitespace if we've expanded a macro.
|
2014-02-25 04:45:00 +08:00
|
|
|
if (AtStartOfLine) Tok.setFlag(Token::StartOfLine);
|
2014-02-05 03:18:35 +08:00
|
|
|
if (HasLeadingSpace) Tok.setFlag(Token::LeadingSpace);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
2014-02-05 03:18:35 +08:00
|
|
|
AtStartOfLine = false;
|
|
|
|
HasLeadingSpace = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Handle recursive expansion!
|
2014-05-18 07:10:59 +08:00
|
|
|
if (!Tok.isAnnotation() && Tok.getIdentifierInfo() != nullptr) {
|
2009-01-24 02:35:48 +08:00
|
|
|
// Change the kind of this identifier to the appropriate token kind, e.g.
|
|
|
|
// turning "for" into a keyword.
|
2009-05-23 05:09:31 +08:00
|
|
|
IdentifierInfo *II = Tok.getIdentifierInfo();
|
2009-01-24 02:35:48 +08:00
|
|
|
Tok.setKind(II->getTokenID());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-20 04:29:42 +08:00
|
|
|
// If this identifier was poisoned and from a paste, emit an error. This
|
|
|
|
// won't be handled by Preprocessor::HandleIdentifier because this is coming
|
|
|
|
// from a macro expansion.
|
|
|
|
if (II->isPoisoned() && TokenIsFromPaste) {
|
2011-04-28 09:08:34 +08:00
|
|
|
PP.HandlePoisonedIdentifier(Tok);
|
2009-04-20 04:29:42 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-24 02:35:48 +08:00
|
|
|
if (!DisableMacroExpansion && II->isHandleIdentifierCase())
|
2013-09-19 08:41:32 +08:00
|
|
|
return PP.HandleIdentifier(Tok);
|
2009-01-24 02:35:48 +08:00
|
|
|
}
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Otherwise, return a normal token.
|
2013-09-19 08:41:32 +08:00
|
|
|
return true;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
2006-07-11 12:02:46 +08:00
|
|
|
|
2017-10-03 08:52:14 +08:00
|
|
|
bool TokenLexer::pasteTokens(Token &Tok) {
|
|
|
|
return pasteTokens(Tok, llvm::makeArrayRef(Tokens, NumTokens), CurTokenIdx);
|
|
|
|
}
|
2017-12-09 06:39:26 +08:00
|
|
|
|
2017-10-03 08:52:14 +08:00
|
|
|
/// LHSTok is the LHS of a ## operator, and CurTokenIdx is the ##
|
2006-07-19 13:42:48 +08:00
|
|
|
/// operator. Read the ## and RHS, and paste the LHS/RHS together. If there
|
2017-10-03 08:52:14 +08:00
|
|
|
/// are more ## after it, chomp them iteratively. Return the result as LHSTok.
|
2008-02-07 14:03:59 +08:00
|
|
|
/// If this returns true, the caller should immediately return the token.
|
2017-10-03 08:52:14 +08:00
|
|
|
bool TokenLexer::pasteTokens(Token &LHSTok, ArrayRef<Token> TokenStream,
|
|
|
|
unsigned int &CurIdx) {
|
|
|
|
assert(CurIdx > 0 && "## can not be the first token within tokens");
|
2017-10-03 09:33:36 +08:00
|
|
|
assert((TokenStream[CurIdx].is(tok::hashhash) ||
|
2017-10-03 08:52:14 +08:00
|
|
|
(PP.getLangOpts().MSVCCompat &&
|
2017-10-03 09:33:36 +08:00
|
|
|
isWideStringLiteralFromMacro(LHSTok, TokenStream[CurIdx]))) &&
|
2017-10-03 08:52:14 +08:00
|
|
|
"Token at this Index must be ## or part of the MSVC 'L "
|
|
|
|
"#macro-arg' pasting pair");
|
|
|
|
|
2015-04-17 20:43:57 +08:00
|
|
|
// MSVC: If previous token was pasted, this must be a recovery from an invalid
|
|
|
|
// paste operation. Ignore spaces before this token to mimic MSVC output.
|
|
|
|
// Required for generating valid UUID strings in some MS headers.
|
2017-10-03 08:52:14 +08:00
|
|
|
if (PP.getLangOpts().MicrosoftExt && (CurIdx >= 2) &&
|
|
|
|
TokenStream[CurIdx - 2].is(tok::hashhash))
|
|
|
|
LHSTok.clearFlag(Token::LeadingSpace);
|
2015-04-17 20:43:57 +08:00
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<128> Buffer;
|
2014-05-18 07:10:59 +08:00
|
|
|
const char *ResultTokStrPtr = nullptr;
|
2017-10-03 08:52:14 +08:00
|
|
|
SourceLocation StartLoc = LHSTok.getLocation();
|
2011-07-07 11:40:34 +08:00
|
|
|
SourceLocation PasteOpLoc;
|
2017-10-03 08:52:14 +08:00
|
|
|
|
|
|
|
auto IsAtEnd = [&TokenStream, &CurIdx] {
|
|
|
|
return TokenStream.size() == CurIdx;
|
|
|
|
};
|
|
|
|
|
2006-07-19 13:42:48 +08:00
|
|
|
do {
|
2014-12-15 12:18:11 +08:00
|
|
|
// Consume the ## operator if any.
|
2017-10-03 08:52:14 +08:00
|
|
|
PasteOpLoc = TokenStream[CurIdx].getLocation();
|
|
|
|
if (TokenStream[CurIdx].is(tok::hashhash))
|
|
|
|
++CurIdx;
|
|
|
|
assert(!IsAtEnd() && "No token on the RHS of a paste operator!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-19 13:42:48 +08:00
|
|
|
// Get the RHS token.
|
2017-10-03 08:52:14 +08:00
|
|
|
const Token &RHS = TokenStream[CurIdx];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-19 13:42:48 +08:00
|
|
|
// Allocate space for the result token. This is guaranteed to be enough for
|
2009-03-08 16:08:45 +08:00
|
|
|
// the two tokens.
|
2017-10-03 08:52:14 +08:00
|
|
|
Buffer.resize(LHSTok.getLength() + RHS.getLength());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-19 13:42:48 +08:00
|
|
|
// Get the spelling of the LHS token in Buffer.
|
2006-11-03 15:45:04 +08:00
|
|
|
const char *BufPtr = &Buffer[0];
|
2010-03-17 06:30:13 +08:00
|
|
|
bool Invalid = false;
|
2017-10-03 08:52:14 +08:00
|
|
|
unsigned LHSLen = PP.getSpelling(LHSTok, BufPtr, &Invalid);
|
2006-11-03 15:45:04 +08:00
|
|
|
if (BufPtr != &Buffer[0]) // Really, we want the chars in Buffer!
|
|
|
|
memcpy(&Buffer[0], BufPtr, LHSLen);
|
2010-03-17 06:30:13 +08:00
|
|
|
if (Invalid)
|
|
|
|
return true;
|
2014-10-25 19:40:40 +08:00
|
|
|
|
|
|
|
BufPtr = Buffer.data() + LHSLen;
|
2010-03-17 06:30:13 +08:00
|
|
|
unsigned RHSLen = PP.getSpelling(RHS, BufPtr, &Invalid);
|
|
|
|
if (Invalid)
|
|
|
|
return true;
|
2014-10-25 19:40:40 +08:00
|
|
|
if (RHSLen && BufPtr != &Buffer[LHSLen])
|
|
|
|
// Really, we want the chars in Buffer!
|
2006-11-03 15:45:04 +08:00
|
|
|
memcpy(&Buffer[LHSLen], BufPtr, RHSLen);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-11-03 15:45:04 +08:00
|
|
|
// Trim excess space.
|
2009-03-08 16:08:45 +08:00
|
|
|
Buffer.resize(LHSLen+RHSLen);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-19 13:42:48 +08:00
|
|
|
// Plop the pasted result (including the trailing newline and null) into a
|
|
|
|
// scratch buffer where we can lex it.
|
2009-01-27 03:29:26 +08:00
|
|
|
Token ResultTokTmp;
|
|
|
|
ResultTokTmp.startToken();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-27 03:29:26 +08:00
|
|
|
// Claim that the tmp token is a string_literal so that we can get the
|
2009-12-24 05:29:53 +08:00
|
|
|
// character pointer back from CreateString in getLiteralData().
|
2009-01-27 03:29:26 +08:00
|
|
|
ResultTokTmp.setKind(tok::string_literal);
|
2012-09-25 05:07:17 +08:00
|
|
|
PP.CreateString(Buffer, ResultTokTmp);
|
2009-01-27 03:29:26 +08:00
|
|
|
SourceLocation ResultTokLoc = ResultTokTmp.getLocation();
|
|
|
|
ResultTokStrPtr = ResultTokTmp.getLiteralData();
|
|
|
|
|
2006-07-19 13:42:48 +08:00
|
|
|
// Lex the resultant pasted token into Result.
|
2007-07-21 00:59:19 +08:00
|
|
|
Token Result;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2017-10-03 08:52:14 +08:00
|
|
|
if (LHSTok.isAnyIdentifier() && RHS.isAnyIdentifier()) {
|
2006-07-20 12:47:30 +08:00
|
|
|
// Common paste case: identifier+identifier = identifier. Avoid creating
|
|
|
|
// a lexer and other overhead.
|
|
|
|
PP.IncrementPasteCounter(true);
|
2006-10-14 13:19:21 +08:00
|
|
|
Result.startToken();
|
2010-12-22 16:23:18 +08:00
|
|
|
Result.setKind(tok::raw_identifier);
|
|
|
|
Result.setRawIdentifierData(ResultTokStrPtr);
|
2006-10-14 13:19:21 +08:00
|
|
|
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);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-19 14:46:35 +08:00
|
|
|
assert(ResultTokLoc.isFileID() &&
|
|
|
|
"Should be a raw location into scratch buffer");
|
2006-07-19 14:32:35 +08:00
|
|
|
SourceManager &SourceMgr = PP.getSourceManager();
|
2009-01-27 03:29:26 +08:00
|
|
|
FileID LocFileID = SourceMgr.getFileID(ResultTokLoc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-03-16 08:06:06 +08:00
|
|
|
bool Invalid = false;
|
2010-03-16 06:54:52 +08:00
|
|
|
const char *ScratchBufStart
|
2010-03-16 22:14:31 +08:00
|
|
|
= SourceMgr.getBufferData(LocFileID, &Invalid).data();
|
2010-03-16 08:06:06 +08:00
|
|
|
if (Invalid)
|
2010-03-16 06:54:52 +08:00
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-19 14:46:35 +08:00
|
|
|
// Make a lexer to lex this string from. Lex just this one token.
|
2006-07-19 14:32:35 +08:00
|
|
|
// Make a lexer object so that we lex and expand the paste result.
|
2009-01-27 03:29:26 +08:00
|
|
|
Lexer TL(SourceMgr.getLocForStartOfFile(LocFileID),
|
2012-03-11 15:00:24 +08:00
|
|
|
PP.getLangOpts(), ScratchBufStart,
|
2009-03-08 16:08:45 +08:00
|
|
|
ResultTokStrPtr, ResultTokStrPtr+LHSLen+RHSLen);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-19 14:32:35 +08:00
|
|
|
// 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.
|
2009-03-08 16:08:45 +08:00
|
|
|
bool isInvalid = !TL.LexFromRawLexer(Result);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-19 14:32:35 +08:00
|
|
|
// If we got an EOF token, we didn't form even ONE token. For example, we
|
|
|
|
// did "/ ## /" to get "//".
|
2009-03-08 16:08:45 +08:00
|
|
|
isInvalid |= Result.is(tok::eof);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-08 16:08:45 +08:00
|
|
|
// If pasting the two tokens didn't form a full new token, this is an
|
2017-10-03 08:52:14 +08:00
|
|
|
// error. This occurs with "x ## +" and other stuff. Return with LHSTok
|
2009-03-08 16:08:45 +08:00
|
|
|
// unmodified and with RHS as the next token to lex.
|
|
|
|
if (isInvalid) {
|
2015-12-30 07:06:17 +08:00
|
|
|
// Explicitly convert the token location to have proper expansion
|
|
|
|
// information so that the user knows where it came from.
|
|
|
|
SourceManager &SM = PP.getSourceManager();
|
|
|
|
SourceLocation Loc =
|
|
|
|
SM.createExpansionLoc(PasteOpLoc, ExpandLocStart, ExpandLocEnd, 2);
|
|
|
|
|
2009-03-08 16:08:45 +08:00
|
|
|
// Test for the Microsoft extension of /##/ turning into // here on the
|
|
|
|
// error path.
|
2017-10-03 08:52:14 +08:00
|
|
|
if (PP.getLangOpts().MicrosoftExt && LHSTok.is(tok::slash) &&
|
2009-03-08 16:08:45 +08:00
|
|
|
RHS.is(tok::slash)) {
|
2017-10-03 08:52:14 +08:00
|
|
|
HandleMicrosoftCommentPaste(LHSTok, Loc);
|
2009-03-08 16:08:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-07-18 00:24:30 +08:00
|
|
|
// Do not emit the error when preprocessing assembler code.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (!PP.getLangOpts().AsmPreprocessor) {
|
2010-07-18 00:24:30 +08:00
|
|
|
// If we're in microsoft extensions mode, downgrade this from a hard
|
2014-02-18 08:45:50 +08:00
|
|
|
// error to an extension that defaults to an error. This allows
|
2010-07-18 00:24:30 +08:00
|
|
|
// disabling it.
|
2014-02-18 08:45:50 +08:00
|
|
|
PP.Diag(Loc, PP.getLangOpts().MicrosoftExt ? diag::ext_pp_bad_paste_ms
|
|
|
|
: diag::err_pp_bad_paste)
|
2015-03-18 18:17:07 +08:00
|
|
|
<< Buffer;
|
fix the "pasting formed 'a]', an invalid preprocessing token"
diagnostic to include the full instantiation location for the
invalid paste. For:
#define foo(a, b) a ## b
#define bar(x) foo(x, ])
bar(a)
bar(zdy)
Instead of:
t.c:3:22: error: pasting formed 'a]', an invalid preprocessing token
#define foo(a, b) a ## b
^
t.c:3:22: error: pasting formed 'zdy]', an invalid preprocessing token
we now produce:
t.c:7:1: error: pasting formed 'a]', an invalid preprocessing token
bar(a)
^
t.c:4:16: note: instantiated from:
#define bar(x) foo(x, ])
^
t.c:3:22: note: instantiated from:
#define foo(a, b) a ## b
^
t.c:8:1: error: pasting formed 'zdy]', an invalid preprocessing token
bar(zdy)
^
t.c:4:16: note: instantiated from:
#define bar(x) foo(x, ])
^
t.c:3:22: note: instantiated from:
#define foo(a, b) a ## b
^
llvm-svn: 72519
2009-05-28 13:39:39 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-06-14 03:02:56 +08:00
|
|
|
// An error has occurred so exit loop.
|
|
|
|
break;
|
2009-03-08 16:08:45 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-08 16:08:45 +08:00
|
|
|
// Turn ## into 'unknown' to avoid # ## # from looking like a paste
|
|
|
|
// operator.
|
|
|
|
if (Result.is(tok::hashhash))
|
|
|
|
Result.setKind(tok::unknown);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-07-23 16:59:39 +08:00
|
|
|
// Transfer properties of the LHS over the Result.
|
2017-10-03 08:52:14 +08:00
|
|
|
Result.setFlagValue(Token::StartOfLine , LHSTok.isAtStartOfLine());
|
|
|
|
Result.setFlagValue(Token::LeadingSpace, LHSTok.hasLeadingSpace());
|
2011-07-07 11:40:34 +08:00
|
|
|
|
2006-07-19 13:42:48 +08:00
|
|
|
// Finally, replace LHS with the result, consume the RHS, and iterate.
|
2017-10-03 08:52:14 +08:00
|
|
|
++CurIdx;
|
|
|
|
LHSTok = Result;
|
|
|
|
} while (!IsAtEnd() && TokenStream[CurIdx].is(tok::hashhash));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2017-10-03 08:52:14 +08:00
|
|
|
SourceLocation EndLoc = TokenStream[CurIdx - 1].getLocation();
|
2011-10-04 02:39:03 +08:00
|
|
|
|
2011-07-07 11:40:34 +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 the token was
|
2011-10-04 02:39:03 +08:00
|
|
|
// expanded from the full ## expression. Pull this information together into
|
2011-07-07 11:40:34 +08:00
|
|
|
// a new SourceLocation that captures all of this.
|
2011-08-24 05:02:32 +08:00
|
|
|
SourceManager &SM = PP.getSourceManager();
|
2011-10-04 02:39:03 +08:00
|
|
|
if (StartLoc.isFileID())
|
|
|
|
StartLoc = getExpansionLocForMacroDefLoc(StartLoc);
|
|
|
|
if (EndLoc.isFileID())
|
|
|
|
EndLoc = getExpansionLocForMacroDefLoc(EndLoc);
|
2012-12-01 09:15:54 +08:00
|
|
|
FileID MacroFID = SM.getFileID(MacroExpansionStart);
|
|
|
|
while (SM.getFileID(StartLoc) != MacroFID)
|
|
|
|
StartLoc = SM.getImmediateExpansionRange(StartLoc).first;
|
|
|
|
while (SM.getFileID(EndLoc) != MacroFID)
|
|
|
|
EndLoc = SM.getImmediateExpansionRange(EndLoc).second;
|
|
|
|
|
2017-10-03 08:52:14 +08:00
|
|
|
LHSTok.setLocation(SM.createExpansionLoc(LHSTok.getLocation(), StartLoc, EndLoc,
|
|
|
|
LHSTok.getLength()));
|
2011-07-07 11:40:34 +08:00
|
|
|
|
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.
|
2017-10-03 08:52:14 +08:00
|
|
|
if (LHSTok.is(tok::raw_identifier)) {
|
2006-07-20 12:16:23 +08:00
|
|
|
// 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.
|
2017-10-03 08:52:14 +08:00
|
|
|
PP.LookUpIdentifierInfo(LHSTok);
|
2006-07-20 12:16:23 +08:00
|
|
|
}
|
2008-02-07 14:03:59 +08:00
|
|
|
return false;
|
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.
|
2008-03-09 10:18:51 +08:00
|
|
|
unsigned TokenLexer::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;
|
2017-10-03 08:52:14 +08:00
|
|
|
return Tokens[CurTokenIdx].is(tok::l_paren);
|
2006-07-11 12:02:46 +08:00
|
|
|
}
|
2008-02-07 14:03:59 +08:00
|
|
|
|
2011-02-22 21:49:00 +08:00
|
|
|
/// isParsingPreprocessorDirective - Return true if we are in the middle of a
|
|
|
|
/// preprocessor directive.
|
|
|
|
bool TokenLexer::isParsingPreprocessorDirective() const {
|
2011-02-28 10:37:51 +08:00
|
|
|
return Tokens[NumTokens-1].is(tok::eod) && !isAtEnd();
|
2011-02-22 21:49:00 +08:00
|
|
|
}
|
2008-02-07 14:03:59 +08:00
|
|
|
|
|
|
|
/// HandleMicrosoftCommentPaste - In microsoft compatibility mode, /##/ pastes
|
|
|
|
/// together to form a comment that comments out everything in the current
|
|
|
|
/// macro, other active macros, and anything left on the current physical
|
2011-07-14 16:20:34 +08:00
|
|
|
/// source line of the expanded buffer. Handle this by returning the
|
2008-02-07 14:03:59 +08:00
|
|
|
/// first token on the next line.
|
2015-12-30 07:06:17 +08:00
|
|
|
void TokenLexer::HandleMicrosoftCommentPaste(Token &Tok, SourceLocation OpLoc) {
|
|
|
|
PP.Diag(OpLoc, diag::ext_comment_paste_microsoft);
|
|
|
|
|
2008-02-07 14:03:59 +08:00
|
|
|
// We 'comment out' the rest of this macro by just ignoring the rest of the
|
|
|
|
// tokens that have not been lexed yet, if any.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-07 14:03:59 +08:00
|
|
|
// Since this must be a macro, mark the macro enabled now that it is no longer
|
|
|
|
// being expanded.
|
|
|
|
assert(Macro && "Token streams can't paste comments");
|
|
|
|
Macro->EnableMacro();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-07 14:03:59 +08:00
|
|
|
PP.HandleMicrosoftCommentPaste(Tok);
|
|
|
|
}
|
2011-07-07 11:40:34 +08:00
|
|
|
|
2011-08-20 06:34:14 +08:00
|
|
|
/// \brief If \arg loc is a file ID and points inside the current macro
|
2011-07-07 11:40:34 +08:00
|
|
|
/// definition, returns the appropriate source location pointing at the
|
2011-08-20 06:34:14 +08:00
|
|
|
/// macro expansion source location entry, otherwise it returns an invalid
|
|
|
|
/// SourceLocation.
|
|
|
|
SourceLocation
|
|
|
|
TokenLexer::getExpansionLocForMacroDefLoc(SourceLocation loc) const {
|
2011-07-14 16:20:34 +08:00
|
|
|
assert(ExpandLocStart.isValid() && MacroExpansionStart.isValid() &&
|
2011-07-07 11:40:34 +08:00
|
|
|
"Not appropriate for token streams");
|
2011-08-24 05:02:32 +08:00
|
|
|
assert(loc.isValid() && loc.isFileID());
|
2011-07-07 11:40:34 +08:00
|
|
|
|
|
|
|
SourceManager &SM = PP.getSourceManager();
|
2011-08-24 05:02:38 +08:00
|
|
|
assert(SM.isInSLocAddrSpace(loc, MacroDefStart, MacroDefLength) &&
|
|
|
|
"Expected loc to come from the macro definition");
|
|
|
|
|
|
|
|
unsigned relativeOffset = 0;
|
|
|
|
SM.isInSLocAddrSpace(loc, MacroDefStart, MacroDefLength, &relativeOffset);
|
2011-09-20 04:40:19 +08:00
|
|
|
return MacroExpansionStart.getLocWithOffset(relativeOffset);
|
2011-07-07 11:40:34 +08:00
|
|
|
}
|
2011-08-20 06:34:17 +08:00
|
|
|
|
|
|
|
/// \brief Finds the tokens that are consecutive (from the same FileID)
|
|
|
|
/// creates a single SLocEntry, and assigns SourceLocations to each token that
|
|
|
|
/// point to that SLocEntry. e.g for
|
|
|
|
/// assert(foo == bar);
|
|
|
|
/// There will be a single SLocEntry for the "foo == bar" chunk and locations
|
|
|
|
/// for the 'foo', '==', 'bar' tokens will point inside that chunk.
|
|
|
|
///
|
|
|
|
/// \arg begin_tokens will be updated to a position past all the found
|
|
|
|
/// consecutive tokens.
|
|
|
|
static void updateConsecutiveMacroArgTokens(SourceManager &SM,
|
|
|
|
SourceLocation InstLoc,
|
|
|
|
Token *&begin_tokens,
|
|
|
|
Token * end_tokens) {
|
|
|
|
assert(begin_tokens < end_tokens);
|
|
|
|
|
2011-08-24 05:02:41 +08:00
|
|
|
SourceLocation FirstLoc = begin_tokens->getLocation();
|
|
|
|
SourceLocation CurLoc = FirstLoc;
|
|
|
|
|
|
|
|
// Compare the source location offset of tokens and group together tokens that
|
|
|
|
// are close, even if their locations point to different FileIDs. e.g.
|
|
|
|
//
|
|
|
|
// |bar | foo | cake | (3 tokens from 3 consecutive FileIDs)
|
|
|
|
// ^ ^
|
|
|
|
// |bar foo cake| (one SLocEntry chunk for all tokens)
|
|
|
|
//
|
|
|
|
// we can perform this "merge" since the token's spelling location depends
|
|
|
|
// on the relative offset.
|
|
|
|
|
|
|
|
Token *NextTok = begin_tokens + 1;
|
|
|
|
for (; NextTok < end_tokens; ++NextTok) {
|
2012-12-20 07:55:44 +08:00
|
|
|
SourceLocation NextLoc = NextTok->getLocation();
|
|
|
|
if (CurLoc.isFileID() != NextLoc.isFileID())
|
|
|
|
break; // Token from different kind of FileID.
|
|
|
|
|
2011-08-24 05:02:41 +08:00
|
|
|
int RelOffs;
|
2012-12-20 07:55:44 +08:00
|
|
|
if (!SM.isInSameSLocAddrSpace(CurLoc, NextLoc, &RelOffs))
|
2011-08-24 05:02:41 +08:00
|
|
|
break; // Token from different local/loaded location.
|
|
|
|
// Check that token is not before the previous token or more than 50
|
|
|
|
// "characters" away.
|
|
|
|
if (RelOffs < 0 || RelOffs > 50)
|
2011-08-20 06:34:17 +08:00
|
|
|
break;
|
2016-07-08 06:38:29 +08:00
|
|
|
|
|
|
|
if (CurLoc.isMacroID() && !SM.isWrittenInSameFile(CurLoc, NextLoc))
|
|
|
|
break; // Token from a different macro.
|
|
|
|
|
2012-12-20 07:55:44 +08:00
|
|
|
CurLoc = NextLoc;
|
2011-08-24 05:02:41 +08:00
|
|
|
}
|
2011-08-20 06:34:17 +08:00
|
|
|
|
|
|
|
// For the consecutive tokens, find the length of the SLocEntry to contain
|
|
|
|
// all of them.
|
2011-08-24 05:02:41 +08:00
|
|
|
Token &LastConsecutiveTok = *(NextTok-1);
|
2011-08-25 04:33:05 +08:00
|
|
|
int LastRelOffs = 0;
|
2011-08-24 05:02:41 +08:00
|
|
|
SM.isInSameSLocAddrSpace(FirstLoc, LastConsecutiveTok.getLocation(),
|
|
|
|
&LastRelOffs);
|
|
|
|
unsigned FullLength = LastRelOffs + LastConsecutiveTok.getLength();
|
2011-08-20 06:34:17 +08:00
|
|
|
|
|
|
|
// Create a macro expansion SLocEntry that will "contain" all of the tokens.
|
|
|
|
SourceLocation Expansion =
|
2011-08-24 05:02:41 +08:00
|
|
|
SM.createMacroArgExpansionLoc(FirstLoc, InstLoc,FullLength);
|
2011-08-20 06:34:17 +08:00
|
|
|
|
|
|
|
// Change the location of the tokens from the spelling location to the new
|
|
|
|
// expanded location.
|
2011-08-24 05:02:41 +08:00
|
|
|
for (; begin_tokens < NextTok; ++begin_tokens) {
|
2011-08-20 06:34:17 +08:00
|
|
|
Token &Tok = *begin_tokens;
|
2011-08-25 04:33:05 +08:00
|
|
|
int RelOffs = 0;
|
2011-08-24 05:02:41 +08:00
|
|
|
SM.isInSameSLocAddrSpace(FirstLoc, Tok.getLocation(), &RelOffs);
|
2011-09-20 04:40:19 +08:00
|
|
|
Tok.setLocation(Expansion.getLocWithOffset(RelOffs));
|
2011-08-20 06:34:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Creates SLocEntries and updates the locations of macro argument
|
|
|
|
/// tokens to their new expanded locations.
|
|
|
|
///
|
2017-10-12 17:42:14 +08:00
|
|
|
/// \param ArgIdSpellLoc the location of the macro argument id inside the macro
|
2011-08-20 06:34:17 +08:00
|
|
|
/// definition.
|
|
|
|
void TokenLexer::updateLocForMacroArgTokens(SourceLocation ArgIdSpellLoc,
|
|
|
|
Token *begin_tokens,
|
|
|
|
Token *end_tokens) {
|
|
|
|
SourceManager &SM = PP.getSourceManager();
|
|
|
|
|
2011-08-24 05:02:41 +08:00
|
|
|
SourceLocation InstLoc =
|
2011-08-20 06:34:17 +08:00
|
|
|
getExpansionLocForMacroDefLoc(ArgIdSpellLoc);
|
|
|
|
|
2011-08-24 05:02:41 +08:00
|
|
|
while (begin_tokens < end_tokens) {
|
|
|
|
// If there's only one token just create a SLocEntry for it.
|
|
|
|
if (end_tokens - begin_tokens == 1) {
|
|
|
|
Token &Tok = *begin_tokens;
|
|
|
|
Tok.setLocation(SM.createMacroArgExpansionLoc(Tok.getLocation(),
|
|
|
|
InstLoc,
|
|
|
|
Tok.getLength()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateConsecutiveMacroArgTokens(SM, InstLoc, begin_tokens, end_tokens);
|
|
|
|
}
|
2011-08-20 06:34:17 +08:00
|
|
|
}
|
2013-09-19 08:41:32 +08:00
|
|
|
|
|
|
|
void TokenLexer::PropagateLineStartLeadingSpaceInfo(Token &Result) {
|
|
|
|
AtStartOfLine = Result.isAtStartOfLine();
|
|
|
|
HasLeadingSpace = Result.hasLeadingSpace();
|
|
|
|
}
|