2008-03-09 11:13:06 +08:00
|
|
|
//===--- MacroExpansion.cpp - Top level Macro Expansion -------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the top level handling of macro expasion for the
|
|
|
|
// preprocessor.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "MacroArgs.h"
|
|
|
|
#include "clang/Lex/MacroInfo.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
2010-06-24 10:02:00 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2009-01-29 13:15:15 +08:00
|
|
|
#include "clang/Lex/LexDiagnostic.h"
|
2010-08-25 06:20:20 +08:00
|
|
|
#include "clang/Lex/CodeCompletionHandler.h"
|
2010-10-30 08:23:06 +08:00
|
|
|
#include "clang/Lex/ExternalPreprocessorSource.h"
|
2011-10-13 03:46:30 +08:00
|
|
|
#include "clang/Lex/LiteralSupport.h"
|
2010-01-10 02:53:11 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2011-06-30 06:20:11 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2011-12-23 06:49:47 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2010-01-28 00:38:22 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-09-23 13:35:21 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-03-03 06:20:04 +08:00
|
|
|
#include <cstdio>
|
2008-03-18 13:59:11 +08:00
|
|
|
#include <ctime>
|
2008-03-09 11:13:06 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2010-10-30 08:23:06 +08:00
|
|
|
MacroInfo *Preprocessor::getInfoForMacro(IdentifierInfo *II) const {
|
|
|
|
assert(II->hasMacroDefinition() && "Identifier is not a macro!");
|
|
|
|
|
|
|
|
llvm::DenseMap<IdentifierInfo*, MacroInfo*>::const_iterator Pos
|
|
|
|
= Macros.find(II);
|
|
|
|
if (Pos == Macros.end()) {
|
|
|
|
// Load this macro from the external source.
|
|
|
|
getExternalSource()->LoadMacroDefinition(II);
|
|
|
|
Pos = Macros.find(II);
|
|
|
|
}
|
|
|
|
assert(Pos != Macros.end() && "Identifier macro info is missing!");
|
|
|
|
return Pos->second;
|
|
|
|
}
|
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
/// setMacroInfo - Specify a macro for this identifier.
|
|
|
|
///
|
2012-01-24 23:24:38 +08:00
|
|
|
void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI,
|
|
|
|
bool LoadedFromAST) {
|
2009-04-11 05:17:07 +08:00
|
|
|
if (MI) {
|
2008-03-09 11:13:06 +08:00
|
|
|
Macros[II] = MI;
|
|
|
|
II->setHasMacroDefinition(true);
|
2012-01-24 23:24:38 +08:00
|
|
|
if (II->isFromAST() && !LoadedFromAST)
|
Make the loading of information attached to an IdentifierInfo from an
AST file more lazy, so that we don't eagerly load that information for
all known identifiers each time a new AST file is loaded. The eager
reloading made some sense in the context of precompiled headers, since
very few identifiers were defined before PCH load time. With modules,
however, a huge amount of code can get parsed before we see an
@import, so laziness becomes important here.
The approach taken to make this information lazy is fairly simple:
when we load a new AST file, we mark all of the existing identifiers
as being out-of-date. Whenever we want to access information that may
come from an AST (e.g., whether the identifier has a macro definition,
or what top-level declarations have that name), we check the
out-of-date bit and, if it's set, ask the AST reader to update the
IdentifierInfo from the AST files. The update is a merge, and we now
take care to merge declarations before/after imports with declarations
from multiple imports.
The results of this optimization are fairly dramatic. On a small
application that brings in 14 non-trivial modules, this takes modules
from being > 3x slower than a "perfect" PCH file down to 30% slower
for a full rebuild. A partial rebuild (where the PCH file or modules
can be re-used) is down to 7% slower. Making the PCH file just a
little imperfect (e.g., adding two smallish modules used by a bunch of
.m files that aren't in the PCH file) tips the scales in favor of the
modules approach, with 24% faster partial rebuilds.
This is just a first step; the lazy scheme could possibly be improved
by adding versioning, so we don't search into modules we already
searched. Moreover, we'll need similar lazy schemes for all of the
other lookup data structures, such as DeclContexts.
llvm-svn: 143100
2011-10-27 17:33:13 +08:00
|
|
|
II->setChangedSinceDeserialization();
|
2009-04-11 05:17:07 +08:00
|
|
|
} else if (II->hasMacroDefinition()) {
|
|
|
|
Macros.erase(II);
|
|
|
|
II->setHasMacroDefinition(false);
|
2012-01-24 23:24:38 +08:00
|
|
|
if (II->isFromAST() && !LoadedFromAST)
|
Make the loading of information attached to an IdentifierInfo from an
AST file more lazy, so that we don't eagerly load that information for
all known identifiers each time a new AST file is loaded. The eager
reloading made some sense in the context of precompiled headers, since
very few identifiers were defined before PCH load time. With modules,
however, a huge amount of code can get parsed before we see an
@import, so laziness becomes important here.
The approach taken to make this information lazy is fairly simple:
when we load a new AST file, we mark all of the existing identifiers
as being out-of-date. Whenever we want to access information that may
come from an AST (e.g., whether the identifier has a macro definition,
or what top-level declarations have that name), we check the
out-of-date bit and, if it's set, ask the AST reader to update the
IdentifierInfo from the AST files. The update is a merge, and we now
take care to merge declarations before/after imports with declarations
from multiple imports.
The results of this optimization are fairly dramatic. On a small
application that brings in 14 non-trivial modules, this takes modules
from being > 3x slower than a "perfect" PCH file down to 30% slower
for a full rebuild. A partial rebuild (where the PCH file or modules
can be re-used) is down to 7% slower. Making the PCH file just a
little imperfect (e.g., adding two smallish modules used by a bunch of
.m files that aren't in the PCH file) tips the scales in favor of the
modules approach, with 24% faster partial rebuilds.
This is just a first step; the lazy scheme could possibly be improved
by adding versioning, so we don't search into modules we already
searched. Moreover, we'll need similar lazy schemes for all of the
other lookup data structures, such as DeclContexts.
llvm-svn: 143100
2011-10-27 17:33:13 +08:00
|
|
|
II->setChangedSinceDeserialization();
|
2008-03-09 11:13:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// RegisterBuiltinMacro - Register the specified identifier in the identifier
|
|
|
|
/// table and mark it as a builtin macro to be expanded.
|
2009-06-13 15:13:28 +08:00
|
|
|
static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){
|
2008-03-09 11:13:06 +08:00
|
|
|
// Get the identifier.
|
2009-06-13 15:13:28 +08:00
|
|
|
IdentifierInfo *Id = PP.getIdentifierInfo(Name);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Mark it as being a macro that is builtin.
|
2009-06-13 15:13:28 +08:00
|
|
|
MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation());
|
2008-03-09 11:13:06 +08:00
|
|
|
MI->setIsBuiltinMacro();
|
2009-06-13 15:13:28 +08:00
|
|
|
PP.setMacroInfo(Id, MI);
|
2008-03-09 11:13:06 +08:00
|
|
|
return Id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
|
|
|
|
/// identifier table.
|
|
|
|
void Preprocessor::RegisterBuiltinMacros() {
|
2009-06-13 15:13:28 +08:00
|
|
|
Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__");
|
|
|
|
Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__");
|
|
|
|
Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__");
|
|
|
|
Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__");
|
|
|
|
Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__");
|
|
|
|
Ident_Pragma = RegisterBuiltinMacro(*this, "_Pragma");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// GCC Extensions.
|
2009-06-13 15:13:28 +08:00
|
|
|
Ident__BASE_FILE__ = RegisterBuiltinMacro(*this, "__BASE_FILE__");
|
|
|
|
Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__");
|
|
|
|
Ident__TIMESTAMP__ = RegisterBuiltinMacro(*this, "__TIMESTAMP__");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-13 15:13:28 +08:00
|
|
|
// Clang Extensions.
|
2009-11-03 06:28:12 +08:00
|
|
|
Ident__has_feature = RegisterBuiltinMacro(*this, "__has_feature");
|
2011-05-14 04:54:45 +08:00
|
|
|
Ident__has_extension = RegisterBuiltinMacro(*this, "__has_extension");
|
2009-11-03 06:28:12 +08:00
|
|
|
Ident__has_builtin = RegisterBuiltinMacro(*this, "__has_builtin");
|
2010-10-20 10:31:43 +08:00
|
|
|
Ident__has_attribute = RegisterBuiltinMacro(*this, "__has_attribute");
|
2009-11-03 06:28:12 +08:00
|
|
|
Ident__has_include = RegisterBuiltinMacro(*this, "__has_include");
|
|
|
|
Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next");
|
2011-10-13 03:46:30 +08:00
|
|
|
Ident__has_warning = RegisterBuiltinMacro(*this, "__has_warning");
|
2010-08-29 06:34:47 +08:00
|
|
|
|
|
|
|
// Microsoft Extensions.
|
2011-09-18 01:15:52 +08:00
|
|
|
if (Features.MicrosoftExt)
|
2010-08-29 06:34:47 +08:00
|
|
|
Ident__pragma = RegisterBuiltinMacro(*this, "__pragma");
|
|
|
|
else
|
|
|
|
Ident__pragma = 0;
|
2008-03-09 11:13:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
|
|
|
|
/// in its expansion, currently expands to that token literally.
|
|
|
|
static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
|
|
|
|
const IdentifierInfo *MacroIdent,
|
|
|
|
Preprocessor &PP) {
|
|
|
|
IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
|
|
|
|
|
|
|
|
// If the token isn't an identifier, it's always literally expanded.
|
|
|
|
if (II == 0) return true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-12-17 12:13:31 +08:00
|
|
|
// If the information about this identifier is out of date, update it from
|
|
|
|
// the external source.
|
|
|
|
if (II->isOutOfDate())
|
|
|
|
PP.getExternalSource()->updateOutOfDateIdentifier(*II);
|
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// If the identifier is a macro, and if that macro is enabled, it may be
|
|
|
|
// expanded so it's not a trivial expansion.
|
|
|
|
if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() &&
|
|
|
|
// Fast expanding "#define X X" is ok, because X would be disabled.
|
|
|
|
II != MacroIdent)
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// If this is an object-like macro invocation, it is safe to trivially expand
|
|
|
|
// it.
|
|
|
|
if (MI->isObjectLike()) return true;
|
|
|
|
|
|
|
|
// If this is a function-like macro invocation, it's safe to trivially expand
|
|
|
|
// as long as the identifier is not a macro argument.
|
|
|
|
for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
|
|
|
|
I != E; ++I)
|
|
|
|
if (*I == II)
|
|
|
|
return false; // Identifier is a macro argument.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// isNextPPTokenLParen - Determine whether the next preprocessor token to be
|
|
|
|
/// lexed is a '('. If so, consume the token and return true, if not, this
|
|
|
|
/// method should have no observable side-effect on the lexed tokens.
|
|
|
|
bool Preprocessor::isNextPPTokenLParen() {
|
|
|
|
// Do some quick tests for rejection cases.
|
|
|
|
unsigned Val;
|
|
|
|
if (CurLexer)
|
|
|
|
Val = CurLexer->isNextPPTokenLParen();
|
2008-11-20 06:43:49 +08:00
|
|
|
else if (CurPTHLexer)
|
|
|
|
Val = CurPTHLexer->isNextPPTokenLParen();
|
2008-03-09 11:13:06 +08:00
|
|
|
else
|
|
|
|
Val = CurTokenLexer->isNextTokenLParen();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
if (Val == 2) {
|
|
|
|
// We have run off the end. If it's a source file we don't
|
|
|
|
// examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the
|
|
|
|
// macro stack.
|
2008-11-20 06:21:33 +08:00
|
|
|
if (CurPPLexer)
|
2008-03-09 11:13:06 +08:00
|
|
|
return false;
|
|
|
|
for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
|
|
|
|
IncludeStackInfo &Entry = IncludeMacroStack[i-1];
|
|
|
|
if (Entry.TheLexer)
|
|
|
|
Val = Entry.TheLexer->isNextPPTokenLParen();
|
2008-11-21 00:46:54 +08:00
|
|
|
else if (Entry.ThePTHLexer)
|
|
|
|
Val = Entry.ThePTHLexer->isNextPPTokenLParen();
|
2008-03-09 11:13:06 +08:00
|
|
|
else
|
|
|
|
Val = Entry.TheTokenLexer->isNextTokenLParen();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
if (Val != 2)
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Ran off the end of a source file?
|
2008-11-21 00:46:54 +08:00
|
|
|
if (Entry.ThePPLexer)
|
2008-03-09 11:13:06 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Okay, if we know that the token is a '(', lex it and return. Otherwise we
|
|
|
|
// have found something that isn't a '(' or we found the end of the
|
|
|
|
// translation unit. In either case, return false.
|
2009-04-18 09:13:56 +08:00
|
|
|
return Val == 1;
|
2008-03-09 11:13:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
|
|
|
|
/// expanded as a macro, handle it and return the next token as 'Identifier'.
|
2009-09-09 23:08:12 +08:00
|
|
|
bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
|
2008-03-09 11:13:06 +08:00
|
|
|
MacroInfo *MI) {
|
2010-01-27 03:43:43 +08:00
|
|
|
// If this is a macro expansion in the "#if !defined(x)" line for the file,
|
2008-03-09 11:13:06 +08:00
|
|
|
// then the macro could expand to different things in other contexts, we need
|
|
|
|
// to disable the optimization in this case.
|
2008-11-18 09:12:54 +08:00
|
|
|
if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
|
|
|
|
if (MI->isBuiltinMacro()) {
|
2011-08-18 09:05:45 +08:00
|
|
|
if (Callbacks) Callbacks->MacroExpands(Identifier, MI,
|
|
|
|
Identifier.getLocation());
|
2008-03-09 11:13:06 +08:00
|
|
|
ExpandBuiltinMacro(Identifier);
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
/// Args - If this is a function-like macro expansion, this contains,
|
|
|
|
/// for each macro argument, the list of tokens that were provided to the
|
|
|
|
/// invocation.
|
|
|
|
MacroArgs *Args = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-14 16:20:46 +08:00
|
|
|
// Remember where the end of the expansion occurred. For an object-like
|
2009-02-16 04:52:18 +08:00
|
|
|
// macro, this is the identifier. For a function-like macro, this is the ')'.
|
2011-07-14 16:20:46 +08:00
|
|
|
SourceLocation ExpansionEnd = Identifier.getLocation();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// If this is a function-like macro, read the arguments.
|
|
|
|
if (MI->isFunctionLike()) {
|
|
|
|
// C99 6.10.3p10: If the preprocessing token immediately after the the macro
|
2009-04-18 09:13:56 +08:00
|
|
|
// name isn't a '(', this macro should not be expanded.
|
2008-03-09 11:13:06 +08:00
|
|
|
if (!isNextPPTokenLParen())
|
|
|
|
return true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Remember that we are now parsing the arguments to a macro invocation.
|
|
|
|
// Preprocessor directives used inside macro arguments are not portable, and
|
|
|
|
// this enables the warning.
|
|
|
|
InMacroArgs = true;
|
2011-07-14 16:20:46 +08:00
|
|
|
Args = ReadFunctionLikeMacroArgs(Identifier, MI, ExpansionEnd);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Finished parsing args.
|
|
|
|
InMacroArgs = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// If there was an error parsing the arguments, bail out.
|
|
|
|
if (Args == 0) return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
++NumFnMacroExpanded;
|
|
|
|
} else {
|
|
|
|
++NumMacroExpanded;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Notice that this macro has been used.
|
2010-12-16 02:44:22 +08:00
|
|
|
markMacroAsUsed(MI);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-14 16:20:46 +08:00
|
|
|
// Remember where the token is expanded.
|
|
|
|
SourceLocation ExpandLoc = Identifier.getLocation();
|
2011-04-27 13:04:02 +08:00
|
|
|
|
2011-08-18 09:05:45 +08:00
|
|
|
if (Callbacks) Callbacks->MacroExpands(Identifier, MI,
|
|
|
|
SourceRange(ExpandLoc, ExpansionEnd));
|
|
|
|
|
|
|
|
// If we started lexing a macro, enter the macro expansion body.
|
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// If this macro expands to no tokens, don't bother to push it onto the
|
|
|
|
// expansion stack, only to take it right back off.
|
|
|
|
if (MI->getNumTokens() == 0) {
|
|
|
|
// No need for arg info.
|
2009-12-15 06:12:52 +08:00
|
|
|
if (Args) Args->destroy(*this);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Ignore this macro use, just return the next token in the current
|
|
|
|
// buffer.
|
|
|
|
bool HadLeadingSpace = Identifier.hasLeadingSpace();
|
|
|
|
bool IsAtStartOfLine = Identifier.isAtStartOfLine();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
Lex(Identifier);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// If the identifier isn't on some OTHER line, inherit the leading
|
|
|
|
// whitespace/first-on-a-line property of this token. This handles
|
|
|
|
// stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
|
|
|
|
// empty.
|
|
|
|
if (!Identifier.isAtStartOfLine()) {
|
|
|
|
if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine);
|
|
|
|
if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace);
|
|
|
|
}
|
2010-11-20 10:04:01 +08:00
|
|
|
Identifier.setFlag(Token::LeadingEmptyMacro);
|
2008-03-09 11:13:06 +08:00
|
|
|
++NumFastMacroExpanded;
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
} else if (MI->getNumTokens() == 1 &&
|
|
|
|
isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
|
2009-01-26 08:43:02 +08:00
|
|
|
*this)) {
|
2008-03-09 11:13:06 +08:00
|
|
|
// Otherwise, if this macro expands into a single trivially-expanded
|
2009-09-09 23:08:12 +08:00
|
|
|
// token: expand it now. This handles common cases like
|
2008-03-09 11:13:06 +08:00
|
|
|
// "#define VAL 42".
|
2008-03-21 15:13:02 +08:00
|
|
|
|
|
|
|
// No need for arg info.
|
2009-12-15 06:12:52 +08:00
|
|
|
if (Args) Args->destroy(*this);
|
2008-03-21 15:13:02 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
|
|
|
|
// identifier to the expanded token.
|
|
|
|
bool isAtStartOfLine = Identifier.isAtStartOfLine();
|
|
|
|
bool hasLeadingSpace = Identifier.hasLeadingSpace();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Replace the result token.
|
|
|
|
Identifier = MI->getReplacementToken(0);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Restore the StartOfLine/LeadingSpace markers.
|
|
|
|
Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
|
|
|
|
Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-14 16:20:46 +08:00
|
|
|
// Update the tokens location to include both its expansion and physical
|
2008-03-09 11:13:06 +08:00
|
|
|
// locations.
|
|
|
|
SourceLocation Loc =
|
2011-07-26 11:03:05 +08:00
|
|
|
SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc,
|
|
|
|
ExpansionEnd,Identifier.getLength());
|
2008-03-09 11:13:06 +08:00
|
|
|
Identifier.setLocation(Loc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-03-27 01:49:16 +08:00
|
|
|
// If this is a disabled macro or #define X X, we must mark the result as
|
|
|
|
// unexpandable.
|
|
|
|
if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) {
|
|
|
|
if (MacroInfo *NewMI = getMacroInfo(NewII))
|
2012-01-02 18:08:26 +08:00
|
|
|
if (!NewMI->isEnabled() || NewMI == MI) {
|
2010-03-27 01:49:16 +08:00
|
|
|
Identifier.setFlag(Token::DisableExpand);
|
2012-01-02 18:08:26 +08:00
|
|
|
Diag(Identifier, diag::pp_disabled_macro_expansion);
|
|
|
|
}
|
2010-03-27 01:49:16 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Since this is not an identifier token, it can't be macro expanded, so
|
|
|
|
// we're done.
|
|
|
|
++NumFastMacroExpanded;
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Start expanding the macro.
|
2011-07-14 16:20:46 +08:00
|
|
|
EnterMacro(Identifier, ExpansionEnd, Args);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Now that the macro is at the top of the include stack, ask the
|
|
|
|
// preprocessor to read the next token from it.
|
|
|
|
Lex(Identifier);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-04-18 09:13:56 +08:00
|
|
|
/// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next
|
|
|
|
/// token is the '(' of the macro, this method is invoked to read all of the
|
|
|
|
/// actual arguments specified for the macro invocation. This returns null on
|
|
|
|
/// error.
|
2008-03-09 11:13:06 +08:00
|
|
|
MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
|
2009-02-16 04:52:18 +08:00
|
|
|
MacroInfo *MI,
|
|
|
|
SourceLocation &MacroEnd) {
|
2008-03-09 11:13:06 +08:00
|
|
|
// The number of fixed arguments to parse.
|
|
|
|
unsigned NumFixedArgsLeft = MI->getNumArgs();
|
|
|
|
bool isVariadic = MI->isVariadic();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Outer loop, while there are more arguments, keep reading them.
|
|
|
|
Token Tok;
|
|
|
|
|
2009-04-18 09:13:56 +08:00
|
|
|
// Read arguments as unexpanded tokens. This avoids issues, e.g., where
|
|
|
|
// an argument value in a macro could expand to ',' or '(' or ')'.
|
|
|
|
LexUnexpandedToken(Tok);
|
|
|
|
assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// ArgTokens - Build up a list of tokens that make up each argument. Each
|
|
|
|
// argument is separated by an EOF token. Use a SmallVector so we can avoid
|
|
|
|
// heap allocations in the common case.
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<Token, 64> ArgTokens;
|
2008-03-09 11:13:06 +08:00
|
|
|
|
|
|
|
unsigned NumActuals = 0;
|
2009-04-18 09:13:56 +08:00
|
|
|
while (Tok.isNot(tok::r_paren)) {
|
|
|
|
assert((Tok.is(tok::l_paren) || Tok.is(tok::comma)) &&
|
|
|
|
"only expect argument separators here");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-18 09:13:56 +08:00
|
|
|
unsigned ArgTokenStart = ArgTokens.size();
|
|
|
|
SourceLocation ArgStartLoc = Tok.getLocation();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note
|
|
|
|
// that we already consumed the first one.
|
|
|
|
unsigned NumParens = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
while (1) {
|
|
|
|
// Read arguments as unexpanded tokens. This avoids issues, e.g., where
|
|
|
|
// an argument value in a macro could expand to ',' or '(' or ')'.
|
|
|
|
LexUnexpandedToken(Tok);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-28 10:37:51 +08:00
|
|
|
if (Tok.is(tok::eof) || Tok.is(tok::eod)) { // "#if f(<eof>" & "#if f(\n"
|
2008-03-09 11:13:06 +08:00
|
|
|
Diag(MacroName, diag::err_unterm_macro_invoc);
|
2011-02-28 10:37:51 +08:00
|
|
|
// Do not lose the EOF/EOD. Return it to the client.
|
2008-03-09 11:13:06 +08:00
|
|
|
MacroName = Tok;
|
|
|
|
return 0;
|
|
|
|
} else if (Tok.is(tok::r_paren)) {
|
|
|
|
// If we found the ) token, the macro arg list is done.
|
2009-02-16 04:52:18 +08:00
|
|
|
if (NumParens-- == 0) {
|
|
|
|
MacroEnd = Tok.getLocation();
|
2008-03-09 11:13:06 +08:00
|
|
|
break;
|
2009-02-16 04:52:18 +08:00
|
|
|
}
|
2008-03-09 11:13:06 +08:00
|
|
|
} else if (Tok.is(tok::l_paren)) {
|
|
|
|
++NumParens;
|
|
|
|
} else if (Tok.is(tok::comma) && NumParens == 0) {
|
|
|
|
// Comma ends this argument if there are more fixed arguments expected.
|
2009-04-18 09:13:56 +08:00
|
|
|
// However, if this is a variadic macro, and this is part of the
|
2009-09-09 23:08:12 +08:00
|
|
|
// variadic part, then the comma is just an argument token.
|
2009-04-18 09:13:56 +08:00
|
|
|
if (!isVariadic) break;
|
|
|
|
if (NumFixedArgsLeft > 1)
|
2008-03-09 11:13:06 +08:00
|
|
|
break;
|
|
|
|
} else if (Tok.is(tok::comment) && !KeepMacroComments) {
|
|
|
|
// If this is a comment token in the argument list and we're just in
|
|
|
|
// -C mode (not -CC mode), discard the comment.
|
|
|
|
continue;
|
2009-04-18 14:44:18 +08:00
|
|
|
} else if (Tok.getIdentifierInfo() != 0) {
|
2008-03-09 11:13:06 +08:00
|
|
|
// Reading macro arguments can cause macros that we are currently
|
|
|
|
// expanding from to be popped off the expansion stack. Doing so causes
|
|
|
|
// them to be reenabled for expansion. Here we record whether any
|
|
|
|
// identifiers we lex as macro arguments correspond to disabled macros.
|
2009-09-09 23:08:12 +08:00
|
|
|
// If so, we mark the token as noexpand. This is a subtle aspect of
|
2008-03-09 11:13:06 +08:00
|
|
|
// C99 6.10.3.4p2.
|
|
|
|
if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
|
|
|
|
if (!MI->isEnabled())
|
|
|
|
Tok.setFlag(Token::DisableExpand);
|
2011-09-04 11:32:15 +08:00
|
|
|
} else if (Tok.is(tok::code_completion)) {
|
|
|
|
if (CodeComplete)
|
|
|
|
CodeComplete->CodeCompleteMacroArgument(MacroName.getIdentifierInfo(),
|
|
|
|
MI, NumActuals);
|
|
|
|
// Don't mark that we reached the code-completion point because the
|
|
|
|
// parser is going to handle the token and there will be another
|
|
|
|
// code-completion callback.
|
2008-03-09 11:13:06 +08:00
|
|
|
}
|
2011-09-04 11:32:15 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
ArgTokens.push_back(Tok);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-18 09:13:56 +08:00
|
|
|
// If this was an empty argument list foo(), don't add this as an empty
|
|
|
|
// argument.
|
|
|
|
if (ArgTokens.empty() && Tok.getKind() == tok::r_paren)
|
|
|
|
break;
|
2008-03-09 11:13:06 +08:00
|
|
|
|
2009-04-18 09:13:56 +08:00
|
|
|
// If this is not a variadic macro, and too many args were specified, emit
|
|
|
|
// an error.
|
|
|
|
if (!isVariadic && NumFixedArgsLeft == 0) {
|
|
|
|
if (ArgTokens.size() != ArgTokenStart)
|
|
|
|
ArgStartLoc = ArgTokens[ArgTokenStart].getLocation();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-18 09:13:56 +08:00
|
|
|
// Emit the diagnostic at the macro name in case there is a missing ).
|
|
|
|
// Emitting it at the , could be far away from the macro name.
|
|
|
|
Diag(ArgStartLoc, diag::err_too_many_args_in_macro_invoc);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-04-23 07:25:09 +08:00
|
|
|
// Empty arguments are standard in C99 and C++0x, and are supported as an extension in
|
2008-03-09 11:13:06 +08:00
|
|
|
// other modes.
|
2011-10-15 09:18:56 +08:00
|
|
|
if (ArgTokens.size() == ArgTokenStart && !Features.C99)
|
|
|
|
Diag(Tok, Features.CPlusPlus0x ?
|
|
|
|
diag::warn_cxx98_compat_empty_fnmacro_arg :
|
|
|
|
diag::ext_empty_fnmacro_arg);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Add a marker EOF token to the end of the token list for this argument.
|
|
|
|
Token EOFTok;
|
|
|
|
EOFTok.startToken();
|
|
|
|
EOFTok.setKind(tok::eof);
|
2009-01-27 04:24:53 +08:00
|
|
|
EOFTok.setLocation(Tok.getLocation());
|
2008-03-09 11:13:06 +08:00
|
|
|
EOFTok.setLength(0);
|
|
|
|
ArgTokens.push_back(EOFTok);
|
|
|
|
++NumActuals;
|
2009-04-18 09:13:56 +08:00
|
|
|
assert(NumFixedArgsLeft != 0 && "Too many arguments parsed");
|
2008-03-09 11:13:06 +08:00
|
|
|
--NumFixedArgsLeft;
|
2009-02-16 04:52:18 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Okay, we either found the r_paren. Check to see if we parsed too few
|
|
|
|
// arguments.
|
|
|
|
unsigned MinArgsExpected = MI->getNumArgs();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// See MacroArgs instance var for description of this.
|
|
|
|
bool isVarargsElided = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
if (NumActuals < MinArgsExpected) {
|
|
|
|
// There are several cases where too few arguments is ok, handle them now.
|
2009-04-21 05:08:10 +08:00
|
|
|
if (NumActuals == 0 && MinArgsExpected == 1) {
|
|
|
|
// #define A(X) or #define A(...) ---> A()
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-21 05:08:10 +08:00
|
|
|
// If there is exactly one argument, and that argument is missing,
|
|
|
|
// then we have an empty "()" argument empty list. This is fine, even if
|
|
|
|
// the macro expects one argument (the argument is just empty).
|
|
|
|
isVarargsElided = MI->isVariadic();
|
|
|
|
} else if (MI->isVariadic() &&
|
|
|
|
(NumActuals+1 == MinArgsExpected || // A(x, ...) -> A(X)
|
|
|
|
(NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A()
|
2008-03-09 11:13:06 +08:00
|
|
|
// Varargs where the named vararg parameter is missing: ok as extension.
|
|
|
|
// #define A(x, ...)
|
|
|
|
// A("blah")
|
|
|
|
Diag(Tok, diag::ext_missing_varargs_arg);
|
|
|
|
|
2009-04-18 09:13:56 +08:00
|
|
|
// Remember this occurred, allowing us to elide the comma when used for
|
2008-05-08 13:10:33 +08:00
|
|
|
// cases like:
|
2009-09-09 23:08:12 +08:00
|
|
|
// #define A(x, foo...) blah(a, ## foo)
|
|
|
|
// #define B(x, ...) blah(a, ## __VA_ARGS__)
|
|
|
|
// #define C(...) blah(a, ## __VA_ARGS__)
|
2009-04-18 09:13:56 +08:00
|
|
|
// A(x) B(x) C()
|
2009-04-21 05:08:10 +08:00
|
|
|
isVarargsElided = true;
|
2008-03-09 11:13:06 +08:00
|
|
|
} else {
|
|
|
|
// Otherwise, emit the error.
|
|
|
|
Diag(Tok, diag::err_too_few_args_in_macro_invoc);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Add a marker EOF token to the end of the token list for this argument.
|
|
|
|
SourceLocation EndLoc = Tok.getLocation();
|
|
|
|
Tok.startToken();
|
|
|
|
Tok.setKind(tok::eof);
|
|
|
|
Tok.setLocation(EndLoc);
|
|
|
|
Tok.setLength(0);
|
|
|
|
ArgTokens.push_back(Tok);
|
2009-05-13 08:55:26 +08:00
|
|
|
|
|
|
|
// If we expect two arguments, add both as empty.
|
|
|
|
if (NumActuals == 0 && MinArgsExpected == 2)
|
|
|
|
ArgTokens.push_back(Tok);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-18 09:13:56 +08:00
|
|
|
} else if (NumActuals > MinArgsExpected && !MI->isVariadic()) {
|
|
|
|
// Emit the diagnostic at the macro name in case there is a missing ).
|
|
|
|
// Emitting it at the , could be far away from the macro name.
|
|
|
|
Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
|
|
|
|
return 0;
|
2008-03-09 11:13:06 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-09-22 10:03:12 +08:00
|
|
|
return MacroArgs::create(MI, ArgTokens, isVarargsElided, *this);
|
2008-03-09 11:13:06 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 06:20:11 +08:00
|
|
|
/// \brief Keeps macro expanded tokens for TokenLexers.
|
|
|
|
//
|
|
|
|
/// Works like a stack; a TokenLexer adds the macro expanded tokens that is
|
|
|
|
/// going to lex in the cache and when it finishes the tokens are removed
|
|
|
|
/// from the end of the cache.
|
|
|
|
Token *Preprocessor::cacheMacroExpandedTokens(TokenLexer *tokLexer,
|
2011-07-24 01:14:25 +08:00
|
|
|
ArrayRef<Token> tokens) {
|
2011-06-30 06:20:11 +08:00
|
|
|
assert(tokLexer);
|
|
|
|
if (tokens.empty())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
size_t newIndex = MacroExpandedTokens.size();
|
|
|
|
bool cacheNeedsToGrow = tokens.size() >
|
|
|
|
MacroExpandedTokens.capacity()-MacroExpandedTokens.size();
|
|
|
|
MacroExpandedTokens.append(tokens.begin(), tokens.end());
|
|
|
|
|
|
|
|
if (cacheNeedsToGrow) {
|
|
|
|
// Go through all the TokenLexers whose 'Tokens' pointer points in the
|
|
|
|
// buffer and update the pointers to the (potential) new buffer array.
|
|
|
|
for (unsigned i = 0, e = MacroExpandingLexersStack.size(); i != e; ++i) {
|
|
|
|
TokenLexer *prevLexer;
|
|
|
|
size_t tokIndex;
|
|
|
|
llvm::tie(prevLexer, tokIndex) = MacroExpandingLexersStack[i];
|
|
|
|
prevLexer->Tokens = MacroExpandedTokens.data() + tokIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MacroExpandingLexersStack.push_back(std::make_pair(tokLexer, newIndex));
|
|
|
|
return MacroExpandedTokens.data() + newIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Preprocessor::removeCachedMacroExpandedTokensOfLastLexer() {
|
|
|
|
assert(!MacroExpandingLexersStack.empty());
|
|
|
|
size_t tokIndex = MacroExpandingLexersStack.back().second;
|
|
|
|
assert(tokIndex < MacroExpandedTokens.size());
|
|
|
|
// Pop the cached macro expanded tokens from the end.
|
|
|
|
MacroExpandedTokens.resize(tokIndex);
|
|
|
|
MacroExpandingLexersStack.pop_back();
|
|
|
|
}
|
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
/// ComputeDATE_TIME - Compute the current time, enter it into the specified
|
|
|
|
/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
|
|
|
|
/// the identifier tokens inserted.
|
|
|
|
static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
|
|
|
|
Preprocessor &PP) {
|
|
|
|
time_t TT = time(0);
|
|
|
|
struct tm *TM = localtime(&TT);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
static const char * const Months[] = {
|
|
|
|
"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
|
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-11-09 11:20:07 +08:00
|
|
|
char TmpBuffer[32];
|
2010-11-09 12:38:09 +08:00
|
|
|
#ifdef LLVM_ON_WIN32
|
|
|
|
sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
|
|
|
|
TM->tm_year+1900);
|
|
|
|
#else
|
2010-11-09 11:20:07 +08:00
|
|
|
snprintf(TmpBuffer, sizeof(TmpBuffer), "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
|
2008-03-09 11:13:06 +08:00
|
|
|
TM->tm_year+1900);
|
2010-11-09 12:38:09 +08:00
|
|
|
#endif
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-27 03:29:26 +08:00
|
|
|
Token TmpTok;
|
|
|
|
TmpTok.startToken();
|
|
|
|
PP.CreateString(TmpBuffer, strlen(TmpBuffer), TmpTok);
|
|
|
|
DATELoc = TmpTok.getLocation();
|
2008-03-09 11:13:06 +08:00
|
|
|
|
2010-11-09 14:27:32 +08:00
|
|
|
#ifdef LLVM_ON_WIN32
|
|
|
|
sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
|
|
|
|
#else
|
2010-11-09 11:20:07 +08:00
|
|
|
snprintf(TmpBuffer, sizeof(TmpBuffer), "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
|
2010-11-09 14:27:32 +08:00
|
|
|
#endif
|
2009-01-27 03:29:26 +08:00
|
|
|
PP.CreateString(TmpBuffer, strlen(TmpBuffer), TmpTok);
|
|
|
|
TIMELoc = TmpTok.getLocation();
|
2008-03-09 11:13:06 +08:00
|
|
|
}
|
|
|
|
|
2009-06-13 15:13:28 +08:00
|
|
|
|
2011-05-14 04:54:45 +08:00
|
|
|
/// HasFeature - Return true if we recognize and implement the feature
|
|
|
|
/// specified by the identifier as a standard language feature.
|
2009-06-13 15:13:28 +08:00
|
|
|
static bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) {
|
|
|
|
const LangOptions &LangOpts = PP.getLangOptions();
|
2012-02-25 18:41:10 +08:00
|
|
|
StringRef Feature = II->getName();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-02-25 18:41:10 +08:00
|
|
|
// Normalize the feature name, __foo__ becomes foo.
|
|
|
|
if (Feature.startswith("__") && Feature.endswith("__") && Feature.size() >= 4)
|
|
|
|
Feature = Feature.substr(2, Feature.size() - 4);
|
|
|
|
|
|
|
|
return llvm::StringSwitch<bool>(Feature)
|
2011-11-22 09:28:36 +08:00
|
|
|
.Case("address_sanitizer", LangOpts.AddressSanitizer)
|
2010-04-29 10:06:42 +08:00
|
|
|
.Case("attribute_analyzer_noreturn", true)
|
2011-03-26 20:16:15 +08:00
|
|
|
.Case("attribute_availability", true)
|
2010-04-29 10:06:42 +08:00
|
|
|
.Case("attribute_cf_returns_not_retained", true)
|
|
|
|
.Case("attribute_cf_returns_retained", true)
|
2010-11-09 03:48:17 +08:00
|
|
|
.Case("attribute_deprecated_with_message", true)
|
2010-04-29 10:06:42 +08:00
|
|
|
.Case("attribute_ext_vector_type", true)
|
|
|
|
.Case("attribute_ns_returns_not_retained", true)
|
|
|
|
.Case("attribute_ns_returns_retained", true)
|
2011-01-27 14:54:14 +08:00
|
|
|
.Case("attribute_ns_consumes_self", true)
|
2011-01-28 02:43:03 +08:00
|
|
|
.Case("attribute_ns_consumed", true)
|
|
|
|
.Case("attribute_cf_consumed", true)
|
2010-04-29 10:06:42 +08:00
|
|
|
.Case("attribute_objc_ivar_unused", true)
|
2011-03-02 19:33:24 +08:00
|
|
|
.Case("attribute_objc_method_family", true)
|
2010-04-29 10:06:42 +08:00
|
|
|
.Case("attribute_overloadable", true)
|
2010-11-09 03:48:17 +08:00
|
|
|
.Case("attribute_unavailable_with_message", true)
|
2010-01-10 02:53:11 +08:00
|
|
|
.Case("blocks", LangOpts.Blocks)
|
|
|
|
.Case("cxx_exceptions", LangOpts.Exceptions)
|
2010-04-29 10:06:42 +08:00
|
|
|
.Case("cxx_rtti", LangOpts.RTTI)
|
2010-11-09 03:48:17 +08:00
|
|
|
.Case("enumerator_attributes", true)
|
2011-06-16 07:02:42 +08:00
|
|
|
// Objective-C features
|
|
|
|
.Case("objc_arr", LangOpts.ObjCAutoRefCount) // FIXME: REMOVE?
|
|
|
|
.Case("objc_arc", LangOpts.ObjCAutoRefCount)
|
|
|
|
.Case("objc_arc_weak", LangOpts.ObjCAutoRefCount &&
|
2011-07-06 08:26:06 +08:00
|
|
|
LangOpts.ObjCRuntimeHasWeak)
|
2012-02-02 08:15:51 +08:00
|
|
|
.Case("objc_default_synthesize_properties", LangOpts.ObjC2)
|
2011-09-09 01:18:35 +08:00
|
|
|
.Case("objc_fixed_enum", LangOpts.ObjC2)
|
2011-09-08 09:46:34 +08:00
|
|
|
.Case("objc_instancetype", LangOpts.ObjC2)
|
2012-01-05 05:16:09 +08:00
|
|
|
.Case("objc_modules", LangOpts.ObjC2 && LangOpts.Modules)
|
2010-01-10 02:53:11 +08:00
|
|
|
.Case("objc_nonfragile_abi", LangOpts.ObjCNonFragileABI)
|
2010-04-29 10:06:46 +08:00
|
|
|
.Case("objc_weak_class", LangOpts.ObjCNonFragileABI)
|
2010-07-31 09:52:11 +08:00
|
|
|
.Case("ownership_holds", true)
|
|
|
|
.Case("ownership_returns", true)
|
|
|
|
.Case("ownership_takes", true)
|
2011-10-19 05:18:53 +08:00
|
|
|
.Case("arc_cf_code_audited", true)
|
2011-12-24 01:00:35 +08:00
|
|
|
// C11 features
|
|
|
|
.Case("c_alignas", LangOpts.C11)
|
2012-01-17 01:27:18 +08:00
|
|
|
.Case("c_atomic", LangOpts.C11)
|
2011-12-24 01:00:35 +08:00
|
|
|
.Case("c_generic_selections", LangOpts.C11)
|
|
|
|
.Case("c_static_assert", LangOpts.C11)
|
2011-01-26 23:36:03 +08:00
|
|
|
// C++0x features
|
2011-05-12 07:45:11 +08:00
|
|
|
.Case("cxx_access_control_sfinae", LangOpts.CPlusPlus0x)
|
2011-05-06 05:57:07 +08:00
|
|
|
.Case("cxx_alias_templates", LangOpts.CPlusPlus0x)
|
2011-10-15 07:44:46 +08:00
|
|
|
.Case("cxx_alignas", LangOpts.CPlusPlus0x)
|
2012-01-17 01:27:18 +08:00
|
|
|
.Case("cxx_atomic", LangOpts.CPlusPlus0x)
|
2011-01-26 23:36:03 +08:00
|
|
|
.Case("cxx_attributes", LangOpts.CPlusPlus0x)
|
2011-02-20 20:13:05 +08:00
|
|
|
.Case("cxx_auto_type", LangOpts.CPlusPlus0x)
|
2012-02-15 06:56:17 +08:00
|
|
|
.Case("cxx_constexpr", LangOpts.CPlusPlus0x)
|
2011-01-26 23:36:03 +08:00
|
|
|
.Case("cxx_decltype", LangOpts.CPlusPlus0x)
|
2011-02-06 04:35:30 +08:00
|
|
|
.Case("cxx_default_function_template_args", LangOpts.CPlusPlus0x)
|
2011-11-01 09:19:34 +08:00
|
|
|
.Case("cxx_defaulted_functions", LangOpts.CPlusPlus0x)
|
2011-05-01 15:04:31 +08:00
|
|
|
.Case("cxx_delegating_constructors", LangOpts.CPlusPlus0x)
|
2011-01-26 23:36:03 +08:00
|
|
|
.Case("cxx_deleted_functions", LangOpts.CPlusPlus0x)
|
2011-08-30 01:28:38 +08:00
|
|
|
.Case("cxx_explicit_conversions", LangOpts.CPlusPlus0x)
|
2012-02-26 04:51:27 +08:00
|
|
|
.Case("cxx_generalized_initializers", LangOpts.CPlusPlus0x)
|
2011-09-05 02:14:28 +08:00
|
|
|
.Case("cxx_implicit_moves", LangOpts.CPlusPlus0x)
|
2011-08-30 01:28:38 +08:00
|
|
|
//.Case("cxx_inheriting_constructors", false)
|
2011-01-26 23:36:03 +08:00
|
|
|
.Case("cxx_inline_namespaces", LangOpts.CPlusPlus0x)
|
2012-02-23 11:02:32 +08:00
|
|
|
.Case("cxx_lambdas", LangOpts.CPlusPlus0x)
|
2011-08-30 01:28:38 +08:00
|
|
|
.Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus0x)
|
2011-03-16 05:17:12 +08:00
|
|
|
.Case("cxx_noexcept", LangOpts.CPlusPlus0x)
|
2011-05-22 07:15:46 +08:00
|
|
|
.Case("cxx_nullptr", LangOpts.CPlusPlus0x)
|
2011-03-25 23:04:23 +08:00
|
|
|
.Case("cxx_override_control", LangOpts.CPlusPlus0x)
|
2011-04-15 23:14:40 +08:00
|
|
|
.Case("cxx_range_for", LangOpts.CPlusPlus0x)
|
2011-11-01 09:23:44 +08:00
|
|
|
.Case("cxx_raw_string_literals", LangOpts.CPlusPlus0x)
|
2011-01-27 05:25:54 +08:00
|
|
|
.Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus0x)
|
2011-01-26 23:36:03 +08:00
|
|
|
.Case("cxx_rvalue_references", LangOpts.CPlusPlus0x)
|
|
|
|
.Case("cxx_strong_enums", LangOpts.CPlusPlus0x)
|
|
|
|
.Case("cxx_static_assert", LangOpts.CPlusPlus0x)
|
|
|
|
.Case("cxx_trailing_return", LangOpts.CPlusPlus0x)
|
2011-11-01 09:23:44 +08:00
|
|
|
.Case("cxx_unicode_literals", LangOpts.CPlusPlus0x)
|
2011-08-30 01:28:38 +08:00
|
|
|
//.Case("cxx_unrestricted_unions", false)
|
|
|
|
//.Case("cxx_user_literals", false)
|
2011-01-26 23:36:03 +08:00
|
|
|
.Case("cxx_variadic_templates", LangOpts.CPlusPlus0x)
|
2011-02-04 05:57:35 +08:00
|
|
|
// Type traits
|
|
|
|
.Case("has_nothrow_assign", LangOpts.CPlusPlus)
|
|
|
|
.Case("has_nothrow_copy", LangOpts.CPlusPlus)
|
|
|
|
.Case("has_nothrow_constructor", LangOpts.CPlusPlus)
|
|
|
|
.Case("has_trivial_assign", LangOpts.CPlusPlus)
|
|
|
|
.Case("has_trivial_copy", LangOpts.CPlusPlus)
|
|
|
|
.Case("has_trivial_constructor", LangOpts.CPlusPlus)
|
|
|
|
.Case("has_trivial_destructor", LangOpts.CPlusPlus)
|
|
|
|
.Case("has_virtual_destructor", LangOpts.CPlusPlus)
|
|
|
|
.Case("is_abstract", LangOpts.CPlusPlus)
|
|
|
|
.Case("is_base_of", LangOpts.CPlusPlus)
|
|
|
|
.Case("is_class", LangOpts.CPlusPlus)
|
|
|
|
.Case("is_convertible_to", LangOpts.CPlusPlus)
|
2011-08-04 01:01:05 +08:00
|
|
|
// __is_empty is available only if the horrible
|
|
|
|
// "struct __is_empty" parsing hack hasn't been needed in this
|
|
|
|
// translation unit. If it has, __is_empty reverts to a normal
|
|
|
|
// identifier and __has_feature(is_empty) evaluates false.
|
2011-07-30 15:01:49 +08:00
|
|
|
.Case("is_empty",
|
2011-07-30 15:08:19 +08:00
|
|
|
LangOpts.CPlusPlus &&
|
|
|
|
PP.getIdentifierInfo("__is_empty")->getTokenID()
|
|
|
|
!= tok::identifier)
|
2011-02-04 05:57:35 +08:00
|
|
|
.Case("is_enum", LangOpts.CPlusPlus)
|
2011-12-04 02:14:24 +08:00
|
|
|
.Case("is_final", LangOpts.CPlusPlus)
|
2011-04-23 18:47:20 +08:00
|
|
|
.Case("is_literal", LangOpts.CPlusPlus)
|
2011-05-13 03:52:14 +08:00
|
|
|
.Case("is_standard_layout", LangOpts.CPlusPlus)
|
2011-08-04 01:01:05 +08:00
|
|
|
// __is_pod is available only if the horrible
|
|
|
|
// "struct __is_pod" parsing hack hasn't been needed in this
|
|
|
|
// translation unit. If it has, __is_pod reverts to a normal
|
|
|
|
// identifier and __has_feature(is_pod) evaluates false.
|
2011-07-30 15:01:49 +08:00
|
|
|
.Case("is_pod",
|
2011-07-30 15:08:19 +08:00
|
|
|
LangOpts.CPlusPlus &&
|
|
|
|
PP.getIdentifierInfo("__is_pod")->getTokenID()
|
|
|
|
!= tok::identifier)
|
2011-02-04 05:57:35 +08:00
|
|
|
.Case("is_polymorphic", LangOpts.CPlusPlus)
|
2011-04-23 18:47:28 +08:00
|
|
|
.Case("is_trivial", LangOpts.CPlusPlus)
|
2012-02-23 15:33:15 +08:00
|
|
|
.Case("is_trivially_assignable", LangOpts.CPlusPlus)
|
2012-02-24 15:38:34 +08:00
|
|
|
.Case("is_trivially_constructible", LangOpts.CPlusPlus)
|
2011-05-13 08:31:07 +08:00
|
|
|
.Case("is_trivially_copyable", LangOpts.CPlusPlus)
|
2011-02-04 05:57:35 +08:00
|
|
|
.Case("is_union", LangOpts.CPlusPlus)
|
2012-01-05 05:16:09 +08:00
|
|
|
.Case("modules", LangOpts.Modules)
|
2010-06-24 10:02:00 +08:00
|
|
|
.Case("tls", PP.getTargetInfo().isTLSSupported())
|
2011-07-19 01:08:00 +08:00
|
|
|
.Case("underlying_type", LangOpts.CPlusPlus)
|
2010-01-10 02:53:11 +08:00
|
|
|
.Default(false);
|
2009-06-13 15:13:28 +08:00
|
|
|
}
|
|
|
|
|
2011-05-14 04:54:45 +08:00
|
|
|
/// HasExtension - Return true if we recognize and implement the feature
|
|
|
|
/// specified by the identifier, either as an extension or a standard language
|
|
|
|
/// feature.
|
|
|
|
static bool HasExtension(const Preprocessor &PP, const IdentifierInfo *II) {
|
|
|
|
if (HasFeature(PP, II))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// If the use of an extension results in an error diagnostic, extensions are
|
|
|
|
// effectively unavailable, so just return false here.
|
2011-09-26 07:23:43 +08:00
|
|
|
if (PP.getDiagnostics().getExtensionHandlingBehavior() ==
|
|
|
|
DiagnosticsEngine::Ext_Error)
|
2011-05-14 04:54:45 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
const LangOptions &LangOpts = PP.getLangOptions();
|
2012-02-25 18:41:10 +08:00
|
|
|
StringRef Extension = II->getName();
|
|
|
|
|
|
|
|
// Normalize the extension name, __foo__ becomes foo.
|
|
|
|
if (Extension.startswith("__") && Extension.endswith("__") &&
|
|
|
|
Extension.size() >= 4)
|
|
|
|
Extension = Extension.substr(2, Extension.size() - 4);
|
2011-05-14 04:54:45 +08:00
|
|
|
|
|
|
|
// Because we inherit the feature list from HasFeature, this string switch
|
|
|
|
// must be less restrictive than HasFeature's.
|
2012-02-25 18:41:10 +08:00
|
|
|
return llvm::StringSwitch<bool>(Extension)
|
2011-12-24 01:00:35 +08:00
|
|
|
// C11 features supported by other languages as extensions.
|
2011-10-15 07:44:46 +08:00
|
|
|
.Case("c_alignas", true)
|
2012-01-17 01:27:18 +08:00
|
|
|
.Case("c_atomic", true)
|
2011-05-14 04:54:45 +08:00
|
|
|
.Case("c_generic_selections", true)
|
|
|
|
.Case("c_static_assert", true)
|
|
|
|
// C++0x features supported by other languages as extensions.
|
2012-01-17 01:27:18 +08:00
|
|
|
.Case("cxx_atomic", LangOpts.CPlusPlus)
|
2011-05-14 04:54:45 +08:00
|
|
|
.Case("cxx_deleted_functions", LangOpts.CPlusPlus)
|
2011-08-30 01:28:38 +08:00
|
|
|
.Case("cxx_explicit_conversions", LangOpts.CPlusPlus)
|
2011-05-14 04:54:45 +08:00
|
|
|
.Case("cxx_inline_namespaces", LangOpts.CPlusPlus)
|
2011-08-30 01:28:38 +08:00
|
|
|
.Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus)
|
2011-05-14 04:54:45 +08:00
|
|
|
.Case("cxx_override_control", LangOpts.CPlusPlus)
|
2011-09-07 02:03:41 +08:00
|
|
|
.Case("cxx_range_for", LangOpts.CPlusPlus)
|
2011-05-14 04:54:45 +08:00
|
|
|
.Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus)
|
|
|
|
.Case("cxx_rvalue_references", LangOpts.CPlusPlus)
|
|
|
|
.Default(false);
|
|
|
|
}
|
|
|
|
|
2010-10-20 10:31:43 +08:00
|
|
|
/// HasAttribute - Return true if we recognize and implement the attribute
|
|
|
|
/// specified by the given identifier.
|
|
|
|
static bool HasAttribute(const IdentifierInfo *II) {
|
|
|
|
return llvm::StringSwitch<bool>(II->getName())
|
|
|
|
#include "clang/Lex/AttrSpellings.inc"
|
|
|
|
.Default(false);
|
|
|
|
}
|
|
|
|
|
2009-11-03 06:28:12 +08:00
|
|
|
/// EvaluateHasIncludeCommon - Process a '__has_include("path")'
|
|
|
|
/// or '__has_include_next("path")' expression.
|
|
|
|
/// Returns true if successful.
|
2011-01-15 14:57:04 +08:00
|
|
|
static bool EvaluateHasIncludeCommon(Token &Tok,
|
|
|
|
IdentifierInfo *II, Preprocessor &PP,
|
|
|
|
const DirectoryLookup *LookupFrom) {
|
2009-11-03 06:28:12 +08:00
|
|
|
SourceLocation LParenLoc;
|
|
|
|
|
|
|
|
// Get '('.
|
|
|
|
PP.LexNonComment(Tok);
|
|
|
|
|
|
|
|
// Ensure we have a '('.
|
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
|
|
|
PP.Diag(Tok.getLocation(), diag::err_pp_missing_lparen) << II->getName();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save '(' location for possible missing ')' message.
|
|
|
|
LParenLoc = Tok.getLocation();
|
|
|
|
|
|
|
|
// Get the file name.
|
|
|
|
PP.getCurrentLexer()->LexIncludeFilename(Tok);
|
|
|
|
|
|
|
|
// Reserve a buffer to get the spelling.
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<128> FilenameBuffer;
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Filename;
|
2010-10-21 06:00:55 +08:00
|
|
|
SourceLocation EndLoc;
|
|
|
|
|
2009-11-03 06:28:12 +08:00
|
|
|
switch (Tok.getKind()) {
|
2011-02-28 10:37:51 +08:00
|
|
|
case tok::eod:
|
|
|
|
// If the token kind is EOD, the error has already been diagnosed.
|
2009-11-03 06:28:12 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
case tok::angle_string_literal:
|
2010-03-17 06:30:13 +08:00
|
|
|
case tok::string_literal: {
|
|
|
|
bool Invalid = false;
|
|
|
|
Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid);
|
|
|
|
if (Invalid)
|
|
|
|
return false;
|
2009-11-03 06:28:12 +08:00
|
|
|
break;
|
2010-03-17 06:30:13 +08:00
|
|
|
}
|
2009-11-03 06:28:12 +08:00
|
|
|
|
|
|
|
case tok::less:
|
|
|
|
// This could be a <foo/bar.h> file coming from a macro expansion. In this
|
|
|
|
// case, glue the tokens together into FilenameBuffer and interpret those.
|
|
|
|
FilenameBuffer.push_back('<');
|
2010-10-21 06:00:55 +08:00
|
|
|
if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc))
|
2011-02-28 10:37:51 +08:00
|
|
|
return false; // Found <eod> but no ">"? Diagnostic already emitted.
|
2010-01-10 09:35:12 +08:00
|
|
|
Filename = FilenameBuffer.str();
|
2009-11-03 06:28:12 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-10 09:35:12 +08:00
|
|
|
bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
|
2009-11-03 06:28:12 +08:00
|
|
|
// If GetIncludeFilenameSpelling set the start ptr to null, there was an
|
|
|
|
// error.
|
2010-01-10 09:35:12 +08:00
|
|
|
if (Filename.empty())
|
2009-11-03 06:28:12 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Search include directories.
|
|
|
|
const DirectoryLookup *CurDir;
|
2011-03-17 02:34:36 +08:00
|
|
|
const FileEntry *File =
|
2011-09-16 06:00:41 +08:00
|
|
|
PP.LookupFile(Filename, isAngled, LookupFrom, CurDir, NULL, NULL, NULL);
|
2009-11-03 06:28:12 +08:00
|
|
|
|
|
|
|
// Get the result value. Result = true means the file exists.
|
2011-01-15 14:57:04 +08:00
|
|
|
bool Result = File != 0;
|
2009-11-03 06:28:12 +08:00
|
|
|
|
|
|
|
// Get ')'.
|
|
|
|
PP.LexNonComment(Tok);
|
|
|
|
|
|
|
|
// Ensure we have a trailing ).
|
|
|
|
if (Tok.isNot(tok::r_paren)) {
|
|
|
|
PP.Diag(Tok.getLocation(), diag::err_pp_missing_rparen) << II->getName();
|
|
|
|
PP.Diag(LParenLoc, diag::note_matching) << "(";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-15 14:57:04 +08:00
|
|
|
return Result;
|
2009-11-03 06:28:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// EvaluateHasInclude - Process a '__has_include("path")' expression.
|
|
|
|
/// Returns true if successful.
|
2011-01-15 14:57:04 +08:00
|
|
|
static bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II,
|
2009-11-03 06:28:12 +08:00
|
|
|
Preprocessor &PP) {
|
2011-01-15 14:57:04 +08:00
|
|
|
return EvaluateHasIncludeCommon(Tok, II, PP, NULL);
|
2009-11-03 06:28:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression.
|
|
|
|
/// Returns true if successful.
|
2011-01-15 14:57:04 +08:00
|
|
|
static bool EvaluateHasIncludeNext(Token &Tok,
|
2009-11-03 06:28:12 +08:00
|
|
|
IdentifierInfo *II, Preprocessor &PP) {
|
|
|
|
// __has_include_next is like __has_include, except that we start
|
|
|
|
// searching after the current found directory. If we can't do this,
|
|
|
|
// issue a diagnostic.
|
|
|
|
const DirectoryLookup *Lookup = PP.GetCurDirLookup();
|
|
|
|
if (PP.isInPrimaryFile()) {
|
|
|
|
Lookup = 0;
|
|
|
|
PP.Diag(Tok, diag::pp_include_next_in_primary);
|
|
|
|
} else if (Lookup == 0) {
|
|
|
|
PP.Diag(Tok, diag::pp_include_next_absolute_path);
|
|
|
|
} else {
|
|
|
|
// Start looking up in the next directory.
|
|
|
|
++Lookup;
|
|
|
|
}
|
|
|
|
|
2011-01-15 14:57:04 +08:00
|
|
|
return EvaluateHasIncludeCommon(Tok, II, PP, Lookup);
|
2009-11-03 06:28:12 +08:00
|
|
|
}
|
2009-06-13 15:13:28 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
|
|
|
|
/// as a builtin macro, handle it and return the next token as 'Tok'.
|
|
|
|
void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
|
|
|
|
// Figure out which token this is.
|
|
|
|
IdentifierInfo *II = Tok.getIdentifierInfo();
|
|
|
|
assert(II && "Can't be a macro without id info!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-29 06:34:47 +08:00
|
|
|
// If this is an _Pragma or Microsoft __pragma directive, expand it,
|
|
|
|
// invoke the pragma handler, then lex the token after it.
|
2008-03-09 11:13:06 +08:00
|
|
|
if (II == Ident_Pragma)
|
|
|
|
return Handle_Pragma(Tok);
|
2010-08-29 06:34:47 +08:00
|
|
|
else if (II == Ident__pragma) // in non-MS mode this is null
|
|
|
|
return HandleMicrosoft__pragma(Tok);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
++NumBuiltinMacroExpanded;
|
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<128> TmpBuffer;
|
2010-01-28 00:38:22 +08:00
|
|
|
llvm::raw_svector_ostream OS(TmpBuffer);
|
2008-03-09 11:13:06 +08:00
|
|
|
|
|
|
|
// Set up the return result.
|
|
|
|
Tok.setIdentifierInfo(0);
|
|
|
|
Tok.clearFlag(Token::NeedsCleaning);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
if (II == Ident__LINE__) {
|
2009-01-27 15:57:44 +08:00
|
|
|
// C99 6.10.8: "__LINE__: The presumed line number (within the current
|
|
|
|
// source file) of the current source line (an integer constant)". This can
|
|
|
|
// be affected by #line.
|
2009-02-16 05:06:39 +08:00
|
|
|
SourceLocation Loc = Tok.getLocation();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-19 06:29:33 +08:00
|
|
|
// Advance to the location of the first _, this might not be the first byte
|
|
|
|
// of the token if it starts with an escaped newline.
|
|
|
|
Loc = AdvanceToTokenCharacter(Loc, 0);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-16 05:06:39 +08:00
|
|
|
// One wrinkle here is that GCC expands __LINE__ to location of the *end* of
|
2011-07-14 16:20:46 +08:00
|
|
|
// a macro expansion. This doesn't matter for object-like macros, but
|
2009-02-16 05:06:39 +08:00
|
|
|
// can matter for a function-like macro that expands to contain __LINE__.
|
2011-07-14 16:20:46 +08:00
|
|
|
// Skip down through expansion points until we find a file loc for the
|
|
|
|
// end of the expansion history.
|
2011-07-26 00:56:02 +08:00
|
|
|
Loc = SourceMgr.getExpansionRange(Loc).second;
|
2009-02-16 05:06:39 +08:00
|
|
|
PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-08 16:08:45 +08:00
|
|
|
// __LINE__ expands to a simple numeric value.
|
2010-11-12 15:15:47 +08:00
|
|
|
OS << (PLoc.isValid()? PLoc.getLine() : 1);
|
2008-03-09 11:13:06 +08:00
|
|
|
Tok.setKind(tok::numeric_constant);
|
|
|
|
} else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
|
2009-01-27 15:57:44 +08:00
|
|
|
// C99 6.10.8: "__FILE__: The presumed name of the current source file (a
|
|
|
|
// character string literal)". This can be affected by #line.
|
|
|
|
PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
|
|
|
|
|
|
|
|
// __BASE_FILE__ is a GNU extension that returns the top of the presumed
|
|
|
|
// #include stack instead of the current file.
|
2010-11-12 15:15:47 +08:00
|
|
|
if (II == Ident__BASE_FILE__ && PLoc.isValid()) {
|
2009-01-27 15:57:44 +08:00
|
|
|
SourceLocation NextLoc = PLoc.getIncludeLoc();
|
2008-03-09 11:13:06 +08:00
|
|
|
while (NextLoc.isValid()) {
|
2009-01-27 15:57:44 +08:00
|
|
|
PLoc = SourceMgr.getPresumedLoc(NextLoc);
|
2010-11-12 15:15:47 +08:00
|
|
|
if (PLoc.isInvalid())
|
|
|
|
break;
|
|
|
|
|
2009-01-27 15:57:44 +08:00
|
|
|
NextLoc = PLoc.getIncludeLoc();
|
2008-03-09 11:13:06 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Escape this filename. Turn '\' -> '\\' '"' -> '\"'
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<128> FN;
|
2010-11-12 15:15:47 +08:00
|
|
|
if (PLoc.isValid()) {
|
|
|
|
FN += PLoc.getFilename();
|
|
|
|
Lexer::Stringify(FN);
|
|
|
|
OS << '"' << FN.str() << '"';
|
|
|
|
}
|
2008-03-09 11:13:06 +08:00
|
|
|
Tok.setKind(tok::string_literal);
|
|
|
|
} else if (II == Ident__DATE__) {
|
|
|
|
if (!DATELoc.isValid())
|
|
|
|
ComputeDATE_TIME(DATELoc, TIMELoc, *this);
|
|
|
|
Tok.setKind(tok::string_literal);
|
|
|
|
Tok.setLength(strlen("\"Mmm dd yyyy\""));
|
2011-07-26 11:03:05 +08:00
|
|
|
Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(),
|
|
|
|
Tok.getLocation(),
|
|
|
|
Tok.getLength()));
|
2010-01-28 00:38:22 +08:00
|
|
|
return;
|
2008-03-09 11:13:06 +08:00
|
|
|
} else if (II == Ident__TIME__) {
|
|
|
|
if (!TIMELoc.isValid())
|
|
|
|
ComputeDATE_TIME(DATELoc, TIMELoc, *this);
|
|
|
|
Tok.setKind(tok::string_literal);
|
|
|
|
Tok.setLength(strlen("\"hh:mm:ss\""));
|
2011-07-26 11:03:05 +08:00
|
|
|
Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(),
|
|
|
|
Tok.getLocation(),
|
|
|
|
Tok.getLength()));
|
2010-01-28 00:38:22 +08:00
|
|
|
return;
|
2008-03-09 11:13:06 +08:00
|
|
|
} else if (II == Ident__INCLUDE_LEVEL__) {
|
2009-01-27 15:57:44 +08:00
|
|
|
// Compute the presumed include depth of this token. This can be affected
|
|
|
|
// by GNU line markers.
|
2008-03-09 11:13:06 +08:00
|
|
|
unsigned Depth = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-27 15:57:44 +08:00
|
|
|
PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
|
2010-11-12 15:15:47 +08:00
|
|
|
if (PLoc.isValid()) {
|
2009-01-27 15:57:44 +08:00
|
|
|
PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
|
2010-11-12 15:15:47 +08:00
|
|
|
for (; PLoc.isValid(); ++Depth)
|
|
|
|
PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-08 16:08:45 +08:00
|
|
|
// __INCLUDE_LEVEL__ expands to a simple numeric value.
|
2010-01-28 00:38:22 +08:00
|
|
|
OS << Depth;
|
2008-03-09 11:13:06 +08:00
|
|
|
Tok.setKind(tok::numeric_constant);
|
|
|
|
} else if (II == Ident__TIMESTAMP__) {
|
|
|
|
// MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
|
|
|
|
// of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
|
|
|
|
|
|
|
|
// Get the file that we are lexing out of. If we're currently lexing from
|
|
|
|
// a macro, dig into the include stack.
|
|
|
|
const FileEntry *CurFile = 0;
|
2008-11-20 09:35:24 +08:00
|
|
|
PreprocessorLexer *TheLexer = getCurrentFileLexer();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
if (TheLexer)
|
2008-11-20 06:55:25 +08:00
|
|
|
CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
const char *Result;
|
|
|
|
if (CurFile) {
|
|
|
|
time_t TT = CurFile->getModificationTime();
|
|
|
|
struct tm *TM = localtime(&TT);
|
|
|
|
Result = asctime(TM);
|
|
|
|
} else {
|
|
|
|
Result = "??? ??? ?? ??:??:?? ????\n";
|
|
|
|
}
|
2010-01-28 00:38:22 +08:00
|
|
|
// Surround the string with " and strip the trailing newline.
|
2011-07-23 18:55:15 +08:00
|
|
|
OS << '"' << StringRef(Result, strlen(Result)-1) << '"';
|
2008-03-09 11:13:06 +08:00
|
|
|
Tok.setKind(tok::string_literal);
|
2009-04-13 09:29:17 +08:00
|
|
|
} else if (II == Ident__COUNTER__) {
|
|
|
|
// __COUNTER__ expands to a simple numeric value.
|
2010-01-28 00:38:22 +08:00
|
|
|
OS << CounterValue++;
|
2009-04-13 09:29:17 +08:00
|
|
|
Tok.setKind(tok::numeric_constant);
|
2011-05-14 04:54:45 +08:00
|
|
|
} else if (II == Ident__has_feature ||
|
|
|
|
II == Ident__has_extension ||
|
|
|
|
II == Ident__has_builtin ||
|
2010-10-20 10:31:43 +08:00
|
|
|
II == Ident__has_attribute) {
|
2011-05-14 04:54:45 +08:00
|
|
|
// The argument to these builtins should be a parenthesized identifier.
|
2009-06-13 15:13:28 +08:00
|
|
|
SourceLocation StartLoc = Tok.getLocation();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-13 15:13:28 +08:00
|
|
|
bool IsValid = false;
|
|
|
|
IdentifierInfo *FeatureII = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-13 15:13:28 +08:00
|
|
|
// Read the '('.
|
|
|
|
Lex(Tok);
|
|
|
|
if (Tok.is(tok::l_paren)) {
|
|
|
|
// Read the identifier
|
|
|
|
Lex(Tok);
|
|
|
|
if (Tok.is(tok::identifier)) {
|
|
|
|
FeatureII = Tok.getIdentifierInfo();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-13 15:13:28 +08:00
|
|
|
// Read the ')'.
|
|
|
|
Lex(Tok);
|
|
|
|
if (Tok.is(tok::r_paren))
|
|
|
|
IsValid = true;
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-13 15:13:28 +08:00
|
|
|
bool Value = false;
|
|
|
|
if (!IsValid)
|
|
|
|
Diag(StartLoc, diag::err_feature_check_malformed);
|
|
|
|
else if (II == Ident__has_builtin) {
|
2009-09-09 23:08:12 +08:00
|
|
|
// Check for a builtin is trivial.
|
2009-06-13 15:13:28 +08:00
|
|
|
Value = FeatureII->getBuiltinID() != 0;
|
2010-10-20 10:31:43 +08:00
|
|
|
} else if (II == Ident__has_attribute)
|
|
|
|
Value = HasAttribute(FeatureII);
|
2011-05-14 04:54:45 +08:00
|
|
|
else if (II == Ident__has_extension)
|
|
|
|
Value = HasExtension(*this, FeatureII);
|
2010-10-20 10:31:43 +08:00
|
|
|
else {
|
2009-06-13 15:13:28 +08:00
|
|
|
assert(II == Ident__has_feature && "Must be feature check");
|
|
|
|
Value = HasFeature(*this, FeatureII);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-01-28 00:38:22 +08:00
|
|
|
OS << (int)Value;
|
2012-02-01 02:53:44 +08:00
|
|
|
if (IsValid)
|
|
|
|
Tok.setKind(tok::numeric_constant);
|
2009-11-03 06:28:12 +08:00
|
|
|
} else if (II == Ident__has_include ||
|
|
|
|
II == Ident__has_include_next) {
|
|
|
|
// The argument to these two builtins should be a parenthesized
|
|
|
|
// file name string literal using angle brackets (<>) or
|
|
|
|
// double-quotes ("").
|
2011-01-15 14:57:04 +08:00
|
|
|
bool Value;
|
2009-11-03 06:28:12 +08:00
|
|
|
if (II == Ident__has_include)
|
2011-01-15 14:57:04 +08:00
|
|
|
Value = EvaluateHasInclude(Tok, II, *this);
|
2009-11-03 06:28:12 +08:00
|
|
|
else
|
2011-01-15 14:57:04 +08:00
|
|
|
Value = EvaluateHasIncludeNext(Tok, II, *this);
|
2010-01-28 00:38:22 +08:00
|
|
|
OS << (int)Value;
|
2009-11-03 06:28:12 +08:00
|
|
|
Tok.setKind(tok::numeric_constant);
|
2011-10-13 03:46:30 +08:00
|
|
|
} else if (II == Ident__has_warning) {
|
|
|
|
// The argument should be a parenthesized string literal.
|
|
|
|
// The argument to these builtins should be a parenthesized identifier.
|
|
|
|
SourceLocation StartLoc = Tok.getLocation();
|
|
|
|
bool IsValid = false;
|
|
|
|
bool Value = false;
|
|
|
|
// Read the '('.
|
|
|
|
Lex(Tok);
|
|
|
|
do {
|
|
|
|
if (Tok.is(tok::l_paren)) {
|
|
|
|
// Read the string.
|
|
|
|
Lex(Tok);
|
|
|
|
|
|
|
|
// We need at least one string literal.
|
|
|
|
if (!Tok.is(tok::string_literal)) {
|
|
|
|
StartLoc = Tok.getLocation();
|
|
|
|
IsValid = false;
|
|
|
|
// Eat tokens until ')'.
|
|
|
|
do Lex(Tok); while (!(Tok.is(tok::r_paren) || Tok.is(tok::eod)));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// String concatenation allows multiple strings, which can even come
|
|
|
|
// from macro expansion.
|
|
|
|
SmallVector<Token, 4> StrToks;
|
|
|
|
while (Tok.is(tok::string_literal)) {
|
|
|
|
StrToks.push_back(Tok);
|
|
|
|
LexUnexpandedToken(Tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is the end a ')'?
|
|
|
|
if (!(IsValid = Tok.is(tok::r_paren)))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Concatenate and parse the strings.
|
|
|
|
StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
|
|
|
|
assert(Literal.isAscii() && "Didn't allow wide strings in");
|
|
|
|
if (Literal.hadError)
|
|
|
|
break;
|
|
|
|
if (Literal.Pascal) {
|
|
|
|
Diag(Tok, diag::warn_pragma_diagnostic_invalid);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef WarningName(Literal.GetString());
|
|
|
|
|
|
|
|
if (WarningName.size() < 3 || WarningName[0] != '-' ||
|
|
|
|
WarningName[1] != 'W') {
|
|
|
|
Diag(StrToks[0].getLocation(), diag::warn_has_warning_invalid_option);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, check if the warning flags maps to a diagnostic group.
|
|
|
|
// We construct a SmallVector here to talk to getDiagnosticIDs().
|
|
|
|
// Although we don't use the result, this isn't a hot path, and not
|
|
|
|
// worth special casing.
|
|
|
|
llvm::SmallVector<diag::kind, 10> Diags;
|
|
|
|
Value = !getDiagnostics().getDiagnosticIDs()->
|
|
|
|
getDiagnosticsInGroup(WarningName.substr(2), Diags);
|
|
|
|
}
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
if (!IsValid)
|
|
|
|
Diag(StartLoc, diag::err_warning_check_malformed);
|
|
|
|
|
|
|
|
OS << (int)Value;
|
|
|
|
Tok.setKind(tok::numeric_constant);
|
2008-03-09 11:13:06 +08:00
|
|
|
} else {
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Unknown identifier!");
|
2008-03-09 11:13:06 +08:00
|
|
|
}
|
2011-10-04 02:39:03 +08:00
|
|
|
CreateString(OS.str().data(), OS.str().size(), Tok,
|
|
|
|
Tok.getLocation(), Tok.getLocation());
|
2008-03-09 11:13:06 +08:00
|
|
|
}
|
2010-12-16 02:44:22 +08:00
|
|
|
|
|
|
|
void Preprocessor::markMacroAsUsed(MacroInfo *MI) {
|
|
|
|
// If the 'used' status changed, and the macro requires 'unused' warning,
|
|
|
|
// remove its SourceLocation from the warn-for-unused-macro locations.
|
|
|
|
if (MI->isWarnIfUnused() && !MI->isUsed())
|
|
|
|
WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
|
|
|
|
MI->setIsUsed(true);
|
|
|
|
}
|