2006-06-18 13:43:12 +08:00
|
|
|
//===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Chris Lattner and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Preprocessor interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// TODO: GCC Diagnostics emitted by the lexer:
|
|
|
|
//
|
|
|
|
// ERROR : __VA_ARGS__ can only appear in the expansion of a C99 variadic macro
|
|
|
|
//
|
|
|
|
// Options to support:
|
|
|
|
// -H - Print the name of each header file used.
|
|
|
|
// -C -CC - Do not discard comments for cpp.
|
|
|
|
// -P - Do not emit #line directives.
|
|
|
|
// -d[MDNI] - Dump various things.
|
|
|
|
// -fworking-directory - #line's with preprocessor's working dir.
|
|
|
|
// -fpreprocessed
|
|
|
|
// -dependency-file,-M,-MM,-MF,-MG,-MP,-MT,-MQ,-MD,-MMD
|
|
|
|
// -W*
|
|
|
|
// -w
|
|
|
|
//
|
|
|
|
// Messages to emit:
|
|
|
|
// "Multiple include guards may be useful for:\n"
|
|
|
|
//
|
|
|
|
// TODO: Implement the include guard optimization.
|
|
|
|
//
|
2006-06-26 09:36:29 +08:00
|
|
|
// Predefined Macros: _Pragma, __TIMESTAMP__, __TIME__, ...
|
|
|
|
//
|
2006-06-18 13:43:12 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "clang/Lex/MacroInfo.h"
|
2006-06-25 05:31:03 +08:00
|
|
|
#include "clang/Lex/Pragma.h"
|
2006-06-18 13:43:12 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include <iostream>
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
|
|
|
|
FileManager &FM, SourceManager &SM)
|
|
|
|
: Diags(diags), Features(opts), FileMgr(FM), SourceMgr(SM),
|
|
|
|
SystemDirIdx(0), NoCurDirSearch(false),
|
2006-06-22 13:52:16 +08:00
|
|
|
CurLexer(0), CurDirLookup(0), CurMacroExpander(0) {
|
2006-06-18 13:43:12 +08:00
|
|
|
// Clear stats.
|
|
|
|
NumDirectives = NumIncluded = NumDefined = NumUndefined = NumPragma = 0;
|
|
|
|
NumIf = NumElse = NumEndif = 0;
|
|
|
|
NumEnteredSourceFiles = NumMacroExpanded = NumFastMacroExpanded = 0;
|
|
|
|
MaxIncludeStackDepth = MaxMacroStackDepth = 0;
|
|
|
|
NumSkipped = 0;
|
2006-06-21 14:50:18 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Macro expansion is enabled.
|
|
|
|
DisableMacroExpansion = false;
|
|
|
|
SkippingContents = false;
|
2006-06-21 14:50:18 +08:00
|
|
|
|
|
|
|
// There is no file-change handler yet.
|
|
|
|
FileChangeHandler = 0;
|
2006-06-25 05:31:03 +08:00
|
|
|
|
|
|
|
// Initialize the pragma handlers.
|
|
|
|
PragmaHandlers = new PragmaNamespace(0);
|
|
|
|
RegisterBuiltinPragmas();
|
2006-06-28 13:26:32 +08:00
|
|
|
|
|
|
|
// Initialize builtin macros like __LINE__ and friends.
|
|
|
|
RegisterBuiltinMacros();
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Preprocessor::~Preprocessor() {
|
|
|
|
// Free any active lexers.
|
|
|
|
delete CurLexer;
|
|
|
|
|
|
|
|
while (!IncludeStack.empty()) {
|
|
|
|
delete IncludeStack.back().TheLexer;
|
|
|
|
IncludeStack.pop_back();
|
|
|
|
}
|
2006-06-25 05:31:03 +08:00
|
|
|
|
|
|
|
// Release pragma information.
|
|
|
|
delete PragmaHandlers;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// getFileInfo - Return the PerFileInfo structure for the specified
|
|
|
|
/// FileEntry.
|
|
|
|
Preprocessor::PerFileInfo &Preprocessor::getFileInfo(const FileEntry *FE) {
|
|
|
|
if (FE->getUID() >= FileInfo.size())
|
|
|
|
FileInfo.resize(FE->getUID()+1);
|
|
|
|
return FileInfo[FE->getUID()];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// AddKeywords - Add all keywords to the symbol table.
|
|
|
|
///
|
|
|
|
void Preprocessor::AddKeywords() {
|
|
|
|
enum {
|
|
|
|
C90Shift = 0,
|
|
|
|
EXTC90 = 1 << C90Shift,
|
|
|
|
NOTC90 = 2 << C90Shift,
|
|
|
|
C99Shift = 2,
|
|
|
|
EXTC99 = 1 << C99Shift,
|
|
|
|
NOTC99 = 2 << C99Shift,
|
|
|
|
CPPShift = 4,
|
|
|
|
EXTCPP = 1 << CPPShift,
|
|
|
|
NOTCPP = 2 << CPPShift,
|
|
|
|
Mask = 3
|
|
|
|
};
|
|
|
|
|
|
|
|
// Add keywords and tokens for the current language.
|
|
|
|
#define KEYWORD(NAME, FLAGS) \
|
|
|
|
AddKeyword(#NAME+1, tok::kw##NAME, \
|
|
|
|
(FLAGS >> C90Shift) & Mask, \
|
|
|
|
(FLAGS >> C99Shift) & Mask, \
|
|
|
|
(FLAGS >> CPPShift) & Mask);
|
|
|
|
#define ALIAS(NAME, TOK) \
|
|
|
|
AddKeyword(NAME, tok::kw_ ## TOK, 0, 0, 0);
|
|
|
|
#include "clang/Basic/TokenKinds.def"
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Diag - Forwarding function for diagnostics. This emits a diagnostic at
|
|
|
|
/// the specified LexerToken's location, translating the token's start
|
|
|
|
/// position in the current buffer into a SourcePosition object for rendering.
|
2006-06-18 14:48:37 +08:00
|
|
|
void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID,
|
2006-06-18 13:43:12 +08:00
|
|
|
const std::string &Msg) {
|
|
|
|
// If we are in a '#if 0' block, don't emit any diagnostics for notes,
|
|
|
|
// warnings or extensions.
|
|
|
|
if (isSkipping() && Diagnostic::isNoteWarningOrExtension(DiagID))
|
2006-06-18 14:48:37 +08:00
|
|
|
return;
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-06-18 14:48:37 +08:00
|
|
|
Diags.Report(Loc, DiagID, Msg);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
2006-06-18 14:48:37 +08:00
|
|
|
void Preprocessor::Diag(const LexerToken &Tok, unsigned DiagID,
|
2006-06-18 13:43:12 +08:00
|
|
|
const std::string &Msg) {
|
|
|
|
// If we are in a '#if 0' block, don't emit any diagnostics for notes,
|
|
|
|
// warnings or extensions.
|
|
|
|
if (isSkipping() && Diagnostic::isNoteWarningOrExtension(DiagID))
|
2006-06-18 14:48:37 +08:00
|
|
|
return;
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-06-19 00:32:35 +08:00
|
|
|
Diag(Tok.getLocation(), DiagID, Msg);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
2006-06-19 00:22:51 +08:00
|
|
|
|
|
|
|
void Preprocessor::DumpToken(const LexerToken &Tok, bool DumpFlags) const {
|
|
|
|
std::cerr << tok::getTokenName(Tok.getKind()) << " '"
|
|
|
|
<< getSpelling(Tok) << "'";
|
|
|
|
|
|
|
|
if (!DumpFlags) return;
|
|
|
|
std::cerr << "\t";
|
|
|
|
if (Tok.isAtStartOfLine())
|
|
|
|
std::cerr << " [StartOfLine]";
|
|
|
|
if (Tok.hasLeadingSpace())
|
|
|
|
std::cerr << " [LeadingSpace]";
|
|
|
|
if (Tok.needsCleaning()) {
|
2006-06-19 00:32:35 +08:00
|
|
|
const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
|
2006-06-19 00:22:51 +08:00
|
|
|
std::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength())
|
|
|
|
<< "']";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Preprocessor::DumpMacro(const MacroInfo &MI) const {
|
|
|
|
std::cerr << "MACRO: ";
|
|
|
|
for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
|
|
|
|
DumpToken(MI.getReplacementToken(i));
|
|
|
|
std::cerr << " ";
|
|
|
|
}
|
|
|
|
std::cerr << "\n";
|
|
|
|
}
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
void Preprocessor::PrintStats() {
|
|
|
|
std::cerr << "\n*** Preprocessor Stats:\n";
|
|
|
|
std::cerr << FileInfo.size() << " files tracked.\n";
|
|
|
|
unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
|
|
|
|
for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
|
|
|
|
NumOnceOnlyFiles += FileInfo[i].isImport;
|
|
|
|
if (MaxNumIncludes < FileInfo[i].NumIncludes)
|
|
|
|
MaxNumIncludes = FileInfo[i].NumIncludes;
|
|
|
|
NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
|
|
|
|
}
|
|
|
|
std::cerr << " " << NumOnceOnlyFiles << " #import/#pragma once files.\n";
|
|
|
|
std::cerr << " " << NumSingleIncludedFiles << " included exactly once.\n";
|
|
|
|
std::cerr << " " << MaxNumIncludes << " max times a file is included.\n";
|
|
|
|
|
|
|
|
std::cerr << NumDirectives << " directives found:\n";
|
|
|
|
std::cerr << " " << NumDefined << " #define.\n";
|
|
|
|
std::cerr << " " << NumUndefined << " #undef.\n";
|
|
|
|
std::cerr << " " << NumIncluded << " #include/#include_next/#import.\n";
|
|
|
|
std::cerr << " " << NumEnteredSourceFiles << " source files entered.\n";
|
|
|
|
std::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n";
|
|
|
|
std::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n";
|
|
|
|
std::cerr << " " << NumElse << " #else/#elif.\n";
|
|
|
|
std::cerr << " " << NumEndif << " #endif.\n";
|
|
|
|
std::cerr << " " << NumPragma << " #pragma.\n";
|
|
|
|
std::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
|
|
|
|
|
|
|
|
std::cerr << NumMacroExpanded << " macros expanded, "
|
|
|
|
<< NumFastMacroExpanded << " on the fast path.\n";
|
|
|
|
if (MaxMacroStackDepth > 1)
|
|
|
|
std::cerr << " " << MaxMacroStackDepth << " max macroexpand stack depth\n";
|
|
|
|
}
|
|
|
|
|
2006-06-19 00:22:51 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Token Spelling
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
|
|
/// getSpelling() - Return the 'spelling' of this token. The spelling of a
|
|
|
|
/// token are the characters used to represent the token in the source file
|
|
|
|
/// after trigraph expansion and escaped-newline folding. In particular, this
|
|
|
|
/// wants to get the true, uncanonicalized, spelling of things like digraphs
|
|
|
|
/// UCNs, etc.
|
|
|
|
std::string Preprocessor::getSpelling(const LexerToken &Tok) const {
|
|
|
|
assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
|
|
|
|
|
|
|
|
// If this token contains nothing interesting, return it directly.
|
2006-06-19 00:32:35 +08:00
|
|
|
const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
|
2006-06-19 00:22:51 +08:00
|
|
|
assert(TokStart && "Token has invalid location!");
|
|
|
|
if (!Tok.needsCleaning())
|
|
|
|
return std::string(TokStart, TokStart+Tok.getLength());
|
|
|
|
|
|
|
|
// Otherwise, hard case, relex the characters into the string.
|
|
|
|
std::string Result;
|
|
|
|
Result.reserve(Tok.getLength());
|
|
|
|
|
|
|
|
for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
|
|
|
|
Ptr != End; ) {
|
|
|
|
unsigned CharSize;
|
|
|
|
Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features));
|
|
|
|
Ptr += CharSize;
|
|
|
|
}
|
|
|
|
assert(Result.size() != unsigned(Tok.getLength()) &&
|
|
|
|
"NeedsCleaning flag set on something that didn't need cleaning!");
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getSpelling - This method is used to get the spelling of a token into a
|
|
|
|
/// preallocated buffer, instead of as an std::string. The caller is required
|
|
|
|
/// to allocate enough space for the token, which is guaranteed to be at least
|
|
|
|
/// Tok.getLength() bytes long. The actual length of the token is returned.
|
|
|
|
unsigned Preprocessor::getSpelling(const LexerToken &Tok, char *Buffer) const {
|
|
|
|
assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
|
|
|
|
|
2006-06-19 00:32:35 +08:00
|
|
|
const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
|
2006-06-19 00:22:51 +08:00
|
|
|
assert(TokStart && "Token has invalid location!");
|
|
|
|
|
|
|
|
// If this token contains nothing interesting, return it directly.
|
|
|
|
if (!Tok.needsCleaning()) {
|
|
|
|
unsigned Size = Tok.getLength();
|
|
|
|
memcpy(Buffer, TokStart, Size);
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
// Otherwise, hard case, relex the characters into the string.
|
|
|
|
std::string Result;
|
|
|
|
Result.reserve(Tok.getLength());
|
|
|
|
|
|
|
|
char *OutBuf = Buffer;
|
|
|
|
for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
|
|
|
|
Ptr != End; ) {
|
|
|
|
unsigned CharSize;
|
|
|
|
*OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features);
|
|
|
|
Ptr += CharSize;
|
|
|
|
}
|
|
|
|
assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
|
|
|
|
"NeedsCleaning flag set on something that didn't need cleaning!");
|
|
|
|
|
|
|
|
return OutBuf-Buffer;
|
|
|
|
}
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Source File Location Methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
|
|
/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
|
|
|
|
/// return null on failure. isAngled indicates whether the file reference is
|
|
|
|
/// for system #include's or not (i.e. using <> instead of "").
|
|
|
|
const FileEntry *Preprocessor::LookupFile(const std::string &Filename,
|
2006-06-22 13:52:16 +08:00
|
|
|
bool isAngled,
|
2006-06-18 13:43:12 +08:00
|
|
|
const DirectoryLookup *FromDir,
|
2006-06-22 13:52:16 +08:00
|
|
|
const DirectoryLookup *&CurDir) {
|
2006-06-18 13:43:12 +08:00
|
|
|
assert(CurLexer && "Cannot enter a #include inside a macro expansion!");
|
2006-06-22 13:52:16 +08:00
|
|
|
CurDir = 0;
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// If 'Filename' is absolute, check to see if it exists and no searching.
|
|
|
|
// FIXME: this should be a sys::Path interface, this doesn't handle things
|
|
|
|
// like C:\foo.txt right, nor win32 \\network\device\blah.
|
|
|
|
if (Filename[0] == '/') {
|
|
|
|
// If this was an #include_next "/absolute/file", fail.
|
|
|
|
if (FromDir) return 0;
|
|
|
|
|
|
|
|
// Otherwise, just return the file.
|
|
|
|
return FileMgr.getFile(Filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step #0, unless disabled, check to see if the file is in the #includer's
|
|
|
|
// directory. This search is not done for <> headers.
|
2006-06-22 13:52:16 +08:00
|
|
|
if (!isAngled && !FromDir && !NoCurDirSearch) {
|
2006-06-18 13:43:12 +08:00
|
|
|
const FileEntry *CurFE =
|
|
|
|
SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID());
|
|
|
|
if (CurFE) {
|
2006-06-22 13:52:16 +08:00
|
|
|
// Concatenate the requested file onto the directory.
|
|
|
|
// FIXME: should be in sys::Path.
|
2006-06-18 13:43:12 +08:00
|
|
|
if (const FileEntry *FE =
|
|
|
|
FileMgr.getFile(CurFE->getDir()->getName()+"/"+Filename)) {
|
2006-06-22 13:52:16 +08:00
|
|
|
if (CurDirLookup)
|
|
|
|
CurDir = CurDirLookup;
|
2006-06-18 13:43:12 +08:00
|
|
|
else
|
2006-06-22 13:52:16 +08:00
|
|
|
CurDir = 0;
|
|
|
|
|
|
|
|
// This file is a system header or C++ unfriendly if the old file is.
|
|
|
|
getFileInfo(FE).DirInfo = getFileInfo(CurFE).DirInfo;
|
2006-06-18 13:43:12 +08:00
|
|
|
return FE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a system #include, ignore the user #include locs.
|
2006-06-22 13:52:16 +08:00
|
|
|
unsigned i = isAngled ? SystemDirIdx : 0;
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// If this is a #include_next request, start searching after the directory the
|
|
|
|
// file was found in.
|
|
|
|
if (FromDir)
|
|
|
|
i = FromDir-&SearchDirs[0];
|
|
|
|
|
|
|
|
// Check each directory in sequence to see if it contains this file.
|
|
|
|
for (; i != SearchDirs.size(); ++i) {
|
|
|
|
// Concatenate the requested file onto the directory.
|
|
|
|
// FIXME: should be in sys::Path.
|
|
|
|
if (const FileEntry *FE =
|
|
|
|
FileMgr.getFile(SearchDirs[i].getDir()->getName()+"/"+Filename)) {
|
2006-06-22 13:52:16 +08:00
|
|
|
CurDir = &SearchDirs[i];
|
|
|
|
|
|
|
|
// This file is a system header or C++ unfriendly if the dir is.
|
|
|
|
getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
|
2006-06-18 13:43:12 +08:00
|
|
|
return FE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, didn't find it.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EnterSourceFile - Add a source file to the top of the include stack and
|
|
|
|
/// start lexing tokens from it instead of the current buffer. Return true
|
|
|
|
/// on failure.
|
|
|
|
void Preprocessor::EnterSourceFile(unsigned FileID,
|
2006-06-22 13:52:16 +08:00
|
|
|
const DirectoryLookup *CurDir) {
|
2006-06-18 13:43:12 +08:00
|
|
|
++NumEnteredSourceFiles;
|
|
|
|
|
|
|
|
// Add the current lexer to the include stack.
|
|
|
|
if (CurLexer) {
|
2006-06-22 13:52:16 +08:00
|
|
|
IncludeStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup));
|
2006-06-18 13:43:12 +08:00
|
|
|
} else {
|
|
|
|
assert(CurMacroExpander == 0 && "Cannot #include a file inside a macro!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MaxIncludeStackDepth < IncludeStack.size())
|
|
|
|
MaxIncludeStackDepth = IncludeStack.size();
|
|
|
|
|
|
|
|
const SourceBuffer *Buffer = SourceMgr.getBuffer(FileID);
|
|
|
|
|
2006-06-22 13:52:16 +08:00
|
|
|
CurLexer = new Lexer(Buffer, FileID, *this);
|
|
|
|
CurDirLookup = CurDir;
|
2006-06-21 14:50:18 +08:00
|
|
|
|
|
|
|
// Notify the client, if desired, that we are in a new source file.
|
2006-06-22 13:52:16 +08:00
|
|
|
if (FileChangeHandler) {
|
|
|
|
DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
|
|
|
|
|
|
|
|
// Get the file entry for the current file.
|
|
|
|
if (const FileEntry *FE =
|
|
|
|
SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
|
|
|
|
FileType = getFileInfo(FE).DirInfo;
|
|
|
|
|
2006-06-25 12:20:34 +08:00
|
|
|
FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferStart),
|
|
|
|
EnterFile, FileType);
|
2006-06-22 13:52:16 +08:00
|
|
|
}
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// EnterMacro - Add a Macro to the top of the include stack and start lexing
|
2006-06-18 14:48:37 +08:00
|
|
|
/// tokens from it instead of the current buffer.
|
|
|
|
void Preprocessor::EnterMacro(LexerToken &Tok) {
|
2006-06-18 13:43:12 +08:00
|
|
|
IdentifierTokenInfo *Identifier = Tok.getIdentifierInfo();
|
|
|
|
MacroInfo &MI = *Identifier->getMacroInfo();
|
|
|
|
if (CurLexer) {
|
2006-06-22 13:52:16 +08:00
|
|
|
IncludeStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup));
|
|
|
|
CurLexer = 0;
|
|
|
|
CurDirLookup = 0;
|
2006-06-18 13:43:12 +08:00
|
|
|
} else if (CurMacroExpander) {
|
|
|
|
MacroStack.push_back(CurMacroExpander);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MaxMacroStackDepth < MacroStack.size())
|
|
|
|
MaxMacroStackDepth = MacroStack.size();
|
|
|
|
|
|
|
|
// TODO: Figure out arguments.
|
|
|
|
|
|
|
|
// Mark the macro as currently disabled, so that it is not recursively
|
|
|
|
// expanded.
|
|
|
|
MI.DisableMacro();
|
2006-06-19 00:22:51 +08:00
|
|
|
CurMacroExpander = new MacroExpander(Tok, *this);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2006-06-28 13:26:32 +08:00
|
|
|
// Macro Expansion Handling.
|
2006-06-18 13:43:12 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-06-28 13:26:32 +08:00
|
|
|
/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
|
|
|
|
/// identifier table.
|
|
|
|
void Preprocessor::RegisterBuiltinMacros() {
|
|
|
|
// Do this for each thing.
|
|
|
|
MacroInfo *MI = new MacroInfo(SourceLocation());
|
|
|
|
MI->setIsBuiltinMacro();
|
|
|
|
getIdentifierInfo("__LINE__")->setMacroInfo(MI);
|
|
|
|
|
|
|
|
// FIXME: Warn on #undef / #define of a builtin macro.
|
|
|
|
// FIXME: make HandleMacroExpandedIdentifier handle this case.
|
|
|
|
// FIXME: implement them all, including _Pragma.
|
|
|
|
//MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
2006-06-28 13:26:32 +08:00
|
|
|
|
2006-06-26 14:16:29 +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'.
|
|
|
|
void Preprocessor::HandleMacroExpandedIdentifier(LexerToken &Identifier,
|
|
|
|
MacroInfo *MI) {
|
|
|
|
++NumMacroExpanded;
|
|
|
|
// If we started lexing a macro, enter the macro expansion body.
|
|
|
|
// FIXME: Read/Validate the argument list here!
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
// Ignore this macro use, just return the next token in the current
|
|
|
|
// buffer.
|
|
|
|
bool HadLeadingSpace = Identifier.hasLeadingSpace();
|
|
|
|
bool IsAtStartOfLine = Identifier.isAtStartOfLine();
|
|
|
|
|
|
|
|
Lex(Identifier);
|
|
|
|
|
|
|
|
// 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(LexerToken::StartOfLine);
|
|
|
|
if (HadLeadingSpace) Identifier.SetFlag(LexerToken::LeadingSpace);
|
|
|
|
}
|
|
|
|
++NumFastMacroExpanded;
|
|
|
|
return;
|
|
|
|
|
|
|
|
} else if (MI->getNumTokens() == 1 &&
|
|
|
|
// Don't handle identifiers if they need recursive expansion.
|
|
|
|
(MI->getReplacementToken(0).getIdentifierInfo() == 0 ||
|
|
|
|
!MI->getReplacementToken(0).getIdentifierInfo()->getMacroInfo())){
|
|
|
|
// FIXME: Function-style macros only if no arguments?
|
|
|
|
|
|
|
|
// Otherwise, if this macro expands into a single trivially-expanded
|
|
|
|
// token: expand it now. This handles common cases like
|
|
|
|
// "#define VAL 42".
|
|
|
|
|
|
|
|
// Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
|
|
|
|
// identifier to the expanded token.
|
|
|
|
bool isAtStartOfLine = Identifier.isAtStartOfLine();
|
|
|
|
bool hasLeadingSpace = Identifier.hasLeadingSpace();
|
|
|
|
|
|
|
|
// Remember where the token is instantiated.
|
|
|
|
SourceLocation InstantiateLoc = Identifier.getLocation();
|
|
|
|
|
|
|
|
// Replace the result token.
|
|
|
|
Identifier = MI->getReplacementToken(0);
|
|
|
|
|
|
|
|
// Restore the StartOfLine/LeadingSpace markers.
|
|
|
|
Identifier.SetFlagValue(LexerToken::StartOfLine , isAtStartOfLine);
|
|
|
|
Identifier.SetFlagValue(LexerToken::LeadingSpace, hasLeadingSpace);
|
|
|
|
|
|
|
|
// Update the tokens location to include both its logical and physical
|
|
|
|
// locations.
|
|
|
|
SourceLocation Loc =
|
|
|
|
MacroExpander::getInstantiationLoc(*this, Identifier.getLocation(),
|
|
|
|
InstantiateLoc);
|
|
|
|
Identifier.SetLocation(Loc);
|
|
|
|
|
|
|
|
// Since this is not an identifier token, it can't be macro expanded, so
|
|
|
|
// we're done.
|
|
|
|
++NumFastMacroExpanded;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start expanding the macro (FIXME, pass arguments).
|
|
|
|
EnterMacro(Identifier);
|
|
|
|
|
|
|
|
// Now that the macro is at the top of the include stack, ask the
|
|
|
|
// preprocessor to read the next token from it.
|
|
|
|
return Lex(Identifier);
|
|
|
|
}
|
|
|
|
|
2006-06-28 13:26:32 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Lexer Event Handling.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// HandleIdentifier - This callback is invoked when the lexer reads an
|
|
|
|
/// identifier. This callback looks up the identifier in the map and/or
|
|
|
|
/// potentially macro expands it or turns it into a named token (like 'for').
|
|
|
|
void Preprocessor::HandleIdentifier(LexerToken &Identifier) {
|
|
|
|
if (Identifier.getIdentifierInfo() == 0) {
|
|
|
|
// If we are skipping tokens (because we are in a #if 0 block), there will
|
|
|
|
// be no identifier info, just return the token.
|
|
|
|
assert(isSkipping() && "Token isn't an identifier?");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
IdentifierTokenInfo &ITI = *Identifier.getIdentifierInfo();
|
|
|
|
|
|
|
|
// If this identifier was poisoned, and if it was not produced from a macro
|
|
|
|
// expansion, emit an error.
|
|
|
|
if (ITI.isPoisoned() && CurLexer)
|
|
|
|
Diag(Identifier, diag::err_pp_used_poisoned_id);
|
|
|
|
|
|
|
|
if (MacroInfo *MI = ITI.getMacroInfo())
|
|
|
|
if (MI->isEnabled() && !DisableMacroExpansion)
|
|
|
|
return HandleMacroExpandedIdentifier(Identifier, MI);
|
|
|
|
|
|
|
|
// Change the kind of this identifier to the appropriate token kind, e.g.
|
|
|
|
// turning "for" into a keyword.
|
|
|
|
Identifier.SetKind(ITI.getTokenID());
|
|
|
|
|
|
|
|
// If this is an extension token, diagnose its use.
|
|
|
|
if (ITI.isExtensionToken()) Diag(Identifier, diag::ext_token_used);
|
|
|
|
}
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
|
|
|
|
/// the current file. This either returns the EOF token or pops a level off
|
|
|
|
/// the include stack and keeps going.
|
2006-06-21 14:50:18 +08:00
|
|
|
void Preprocessor::HandleEndOfFile(LexerToken &Result, bool isEndOfMacro) {
|
2006-06-18 13:43:12 +08:00
|
|
|
assert(!CurMacroExpander &&
|
|
|
|
"Ending a file when currently in a macro!");
|
|
|
|
|
|
|
|
// If we are in a #if 0 block skipping tokens, and we see the end of the file,
|
|
|
|
// this is an error condition. Just return the EOF token up to
|
|
|
|
// SkipExcludedConditionalBlock. The Lexer will have already have issued
|
|
|
|
// errors for the unterminated #if's on the conditional stack.
|
|
|
|
if (isSkipping()) {
|
2006-06-19 00:22:51 +08:00
|
|
|
Result.StartToken();
|
|
|
|
CurLexer->BufferPtr = CurLexer->BufferEnd;
|
|
|
|
CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
|
2006-06-18 13:43:12 +08:00
|
|
|
Result.SetKind(tok::eof);
|
2006-06-18 14:48:37 +08:00
|
|
|
return;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a #include'd file, pop it off the include stack and continue
|
|
|
|
// lexing the #includer file.
|
|
|
|
if (!IncludeStack.empty()) {
|
|
|
|
// We're done with the #included file.
|
|
|
|
delete CurLexer;
|
2006-06-22 13:52:16 +08:00
|
|
|
CurLexer = IncludeStack.back().TheLexer;
|
|
|
|
CurDirLookup = IncludeStack.back().TheDirLookup;
|
2006-06-18 13:43:12 +08:00
|
|
|
IncludeStack.pop_back();
|
2006-06-21 14:50:18 +08:00
|
|
|
|
|
|
|
// Notify the client, if desired, that we are in a new source file.
|
2006-06-22 13:52:16 +08:00
|
|
|
if (FileChangeHandler && !isEndOfMacro) {
|
|
|
|
DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
|
|
|
|
|
|
|
|
// Get the file entry for the current file.
|
|
|
|
if (const FileEntry *FE =
|
|
|
|
SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
|
|
|
|
FileType = getFileInfo(FE).DirInfo;
|
|
|
|
|
2006-06-21 14:50:18 +08:00
|
|
|
FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferPtr),
|
2006-06-25 12:20:34 +08:00
|
|
|
ExitFile, FileType);
|
2006-06-22 13:52:16 +08:00
|
|
|
}
|
2006-06-21 14:50:18 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
return Lex(Result);
|
|
|
|
}
|
|
|
|
|
2006-06-19 00:22:51 +08:00
|
|
|
Result.StartToken();
|
|
|
|
CurLexer->BufferPtr = CurLexer->BufferEnd;
|
|
|
|
CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
|
2006-06-18 13:43:12 +08:00
|
|
|
Result.SetKind(tok::eof);
|
|
|
|
|
|
|
|
// We're done with the #included file.
|
|
|
|
delete CurLexer;
|
|
|
|
CurLexer = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// HandleEndOfMacro - This callback is invoked when the lexer hits the end of
|
2006-06-18 14:48:37 +08:00
|
|
|
/// the current macro line.
|
|
|
|
void Preprocessor::HandleEndOfMacro(LexerToken &Result) {
|
2006-06-18 13:43:12 +08:00
|
|
|
assert(CurMacroExpander && !CurLexer &&
|
|
|
|
"Ending a macro when currently in a #include file!");
|
|
|
|
|
|
|
|
// Mark macro not ignored now that it is no longer being expanded.
|
|
|
|
CurMacroExpander->getMacro().EnableMacro();
|
|
|
|
delete CurMacroExpander;
|
|
|
|
|
|
|
|
if (!MacroStack.empty()) {
|
|
|
|
// In a nested macro invocation, continue lexing from the macro.
|
|
|
|
CurMacroExpander = MacroStack.back();
|
|
|
|
MacroStack.pop_back();
|
|
|
|
return Lex(Result);
|
|
|
|
} else {
|
|
|
|
CurMacroExpander = 0;
|
|
|
|
// Handle this like a #include file being popped off the stack.
|
2006-06-21 14:50:18 +08:00
|
|
|
return HandleEndOfFile(Result, true);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utility Methods for Preprocessor Directive Handling.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
|
|
|
|
/// current line until the tok::eom token is found.
|
2006-06-18 14:48:37 +08:00
|
|
|
void Preprocessor::DiscardUntilEndOfDirective() {
|
2006-06-18 13:43:12 +08:00
|
|
|
LexerToken Tmp;
|
|
|
|
do {
|
2006-06-18 14:48:37 +08:00
|
|
|
LexUnexpandedToken(Tmp);
|
2006-06-18 13:43:12 +08:00
|
|
|
} while (Tmp.getKind() != tok::eom);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ReadMacroName - Lex and validate a macro name, which occurs after a
|
|
|
|
/// #define or #undef. This sets the token kind to eom and discards the rest
|
|
|
|
/// of the macro line if the macro name is invalid.
|
2006-06-18 14:48:37 +08:00
|
|
|
void Preprocessor::ReadMacroName(LexerToken &MacroNameTok) {
|
2006-06-18 13:43:12 +08:00
|
|
|
// Read the token, don't allow macro expansion on it.
|
2006-06-18 14:48:37 +08:00
|
|
|
LexUnexpandedToken(MacroNameTok);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Missing macro name?
|
|
|
|
if (MacroNameTok.getKind() == tok::eom)
|
|
|
|
return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
|
|
|
|
|
|
|
|
if (MacroNameTok.getIdentifierInfo() == 0) {
|
2006-06-18 14:48:37 +08:00
|
|
|
Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
|
2006-06-18 13:43:12 +08:00
|
|
|
// Fall through on error.
|
|
|
|
} else if (0) {
|
|
|
|
// FIXME: Error if defining a C++ named operator.
|
|
|
|
|
|
|
|
} else if (0) {
|
|
|
|
// FIXME: Error if defining "defined", "__DATE__", and other predef macros
|
|
|
|
// in C99 6.10.8.4.
|
|
|
|
} else {
|
|
|
|
// Okay, we got a good identifier node. Return it.
|
2006-06-18 14:48:37 +08:00
|
|
|
return;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Invalid macro name, read and discard the rest of the line. Then set the
|
|
|
|
// token kind to tok::eom.
|
|
|
|
MacroNameTok.SetKind(tok::eom);
|
|
|
|
return DiscardUntilEndOfDirective();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
|
|
|
|
/// not, emit a diagnostic and consume up until the eom.
|
2006-06-18 14:48:37 +08:00
|
|
|
void Preprocessor::CheckEndOfDirective(const char *DirType) {
|
2006-06-18 13:43:12 +08:00
|
|
|
LexerToken Tmp;
|
2006-06-18 14:48:37 +08:00
|
|
|
Lex(Tmp);
|
2006-06-18 13:43:12 +08:00
|
|
|
// There should be no tokens after the directive, but we allow them as an
|
|
|
|
// extension.
|
|
|
|
if (Tmp.getKind() != tok::eom) {
|
2006-06-18 14:48:37 +08:00
|
|
|
Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
|
|
|
|
DiscardUntilEndOfDirective();
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// SkipExcludedConditionalBlock - We just read a #if or related directive and
|
|
|
|
/// decided that the subsequent tokens are in the #if'd out portion of the
|
|
|
|
/// file. Lex the rest of the file, until we see an #endif. If
|
|
|
|
/// FoundNonSkipPortion is true, then we have already emitted code for part of
|
|
|
|
/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
|
|
|
|
/// is true, then #else directives are ok, if not, then we have already seen one
|
|
|
|
/// so a #else directive is a duplicate. When this returns, the caller can lex
|
|
|
|
/// the first valid token.
|
2006-06-19 00:22:51 +08:00
|
|
|
void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
|
2006-06-18 13:43:12 +08:00
|
|
|
bool FoundNonSkipPortion,
|
|
|
|
bool FoundElse) {
|
|
|
|
++NumSkipped;
|
|
|
|
assert(MacroStack.empty() && CurMacroExpander == 0 && CurLexer &&
|
|
|
|
"Lexing a macro, not a file?");
|
|
|
|
|
|
|
|
CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
|
|
|
|
FoundNonSkipPortion, FoundElse);
|
|
|
|
|
|
|
|
// Know that we are going to be skipping tokens. Set this flag to indicate
|
|
|
|
// this, which has a couple of effects:
|
|
|
|
// 1. If EOF of the current lexer is found, the include stack isn't popped.
|
|
|
|
// 2. Identifier information is not looked up for identifier tokens. As an
|
|
|
|
// effect of this, implicit macro expansion is naturally disabled.
|
|
|
|
// 3. "#" tokens at the start of a line are treated as normal tokens, not
|
|
|
|
// implicitly transformed by the lexer.
|
|
|
|
// 4. All notes, warnings, and extension messages are disabled.
|
|
|
|
//
|
|
|
|
SkippingContents = true;
|
|
|
|
LexerToken Tok;
|
|
|
|
while (1) {
|
2006-06-18 14:48:37 +08:00
|
|
|
CurLexer->Lex(Tok);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// If this is the end of the buffer, we have an error. The lexer will have
|
|
|
|
// already handled this error condition, so just return and let the caller
|
|
|
|
// lex after this #include.
|
|
|
|
if (Tok.getKind() == tok::eof) break;
|
|
|
|
|
|
|
|
// If this token is not a preprocessor directive, just skip it.
|
|
|
|
if (Tok.getKind() != tok::hash || !Tok.isAtStartOfLine())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// We just parsed a # character at the start of a line, so we're in
|
|
|
|
// directive mode. Tell the lexer this so any newlines we see will be
|
|
|
|
// converted into an EOM token (this terminates the macro).
|
|
|
|
CurLexer->ParsingPreprocessorDirective = true;
|
|
|
|
|
|
|
|
// Read the next token, the directive flavor.
|
2006-06-18 14:48:37 +08:00
|
|
|
LexUnexpandedToken(Tok);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
|
|
|
|
// something bogus), skip it.
|
|
|
|
if (Tok.getKind() != tok::identifier) {
|
|
|
|
CurLexer->ParsingPreprocessorDirective = false;
|
|
|
|
continue;
|
|
|
|
}
|
2006-06-22 14:36:29 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// If the first letter isn't i or e, it isn't intesting to us. We know that
|
|
|
|
// this is safe in the face of spelling differences, because there is no way
|
|
|
|
// to spell an i/e in a strange way that is another letter. Skipping this
|
2006-06-22 14:36:29 +08:00
|
|
|
// allows us to avoid looking up the identifier info for #define/#undef and
|
|
|
|
// other common directives.
|
|
|
|
const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
|
|
|
|
char FirstChar = RawCharData[0];
|
2006-06-18 13:43:12 +08:00
|
|
|
if (FirstChar >= 'a' && FirstChar <= 'z' &&
|
|
|
|
FirstChar != 'i' && FirstChar != 'e') {
|
|
|
|
CurLexer->ParsingPreprocessorDirective = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-06-22 14:36:29 +08:00
|
|
|
// Get the identifier name without trigraphs or embedded newlines. Note
|
|
|
|
// that we can't use Tok.getIdentifierInfo() because its lookup is disabled
|
|
|
|
// when skipping.
|
|
|
|
// TODO: could do this with zero copies in the no-clean case by using
|
|
|
|
// strncmp below.
|
|
|
|
char Directive[20];
|
|
|
|
unsigned IdLen;
|
|
|
|
if (!Tok.needsCleaning() && Tok.getLength() < 20) {
|
|
|
|
IdLen = Tok.getLength();
|
|
|
|
memcpy(Directive, RawCharData, IdLen);
|
|
|
|
Directive[IdLen] = 0;
|
|
|
|
} else {
|
|
|
|
std::string DirectiveStr = getSpelling(Tok);
|
|
|
|
IdLen = DirectiveStr.size();
|
|
|
|
if (IdLen >= 20) {
|
|
|
|
CurLexer->ParsingPreprocessorDirective = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
memcpy(Directive, &DirectiveStr[0], IdLen);
|
|
|
|
Directive[IdLen] = 0;
|
|
|
|
}
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
if (FirstChar == 'i' && Directive[1] == 'f') {
|
2006-06-22 14:36:29 +08:00
|
|
|
if ((IdLen == 2) || // "if"
|
|
|
|
(IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef"
|
|
|
|
(IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef"
|
2006-06-18 13:43:12 +08:00
|
|
|
// We know the entire #if/#ifdef/#ifndef block will be skipped, don't
|
|
|
|
// bother parsing the condition.
|
2006-06-18 14:48:37 +08:00
|
|
|
DiscardUntilEndOfDirective();
|
2006-06-19 00:32:35 +08:00
|
|
|
CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
|
2006-06-19 00:22:51 +08:00
|
|
|
/*foundnonskip*/false,
|
|
|
|
/*fnddelse*/false);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
} else if (FirstChar == 'e') {
|
2006-06-22 14:36:29 +08:00
|
|
|
if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif"
|
2006-06-18 14:48:37 +08:00
|
|
|
CheckEndOfDirective("#endif");
|
2006-06-18 13:43:12 +08:00
|
|
|
PPConditionalInfo CondInfo;
|
|
|
|
CondInfo.WasSkipping = true; // Silence bogus warning.
|
|
|
|
bool InCond = CurLexer->popConditionalLevel(CondInfo);
|
|
|
|
assert(!InCond && "Can't be skipping if not in a conditional!");
|
|
|
|
|
|
|
|
// If we popped the outermost skipping block, we're done skipping!
|
|
|
|
if (!CondInfo.WasSkipping)
|
|
|
|
break;
|
2006-06-22 14:36:29 +08:00
|
|
|
} else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else".
|
2006-06-18 13:43:12 +08:00
|
|
|
// #else directive in a skipping conditional. If not in some other
|
|
|
|
// skipping conditional, and if #else hasn't already been seen, enter it
|
|
|
|
// as a non-skipping conditional.
|
2006-06-18 14:48:37 +08:00
|
|
|
CheckEndOfDirective("#else");
|
2006-06-18 13:43:12 +08:00
|
|
|
PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
|
|
|
|
|
|
|
|
// If this is a #else with a #else before it, report the error.
|
2006-06-18 14:48:37 +08:00
|
|
|
if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Note that we've seen a #else in this conditional.
|
|
|
|
CondInfo.FoundElse = true;
|
|
|
|
|
|
|
|
// If the conditional is at the top level, and the #if block wasn't
|
|
|
|
// entered, enter the #else block now.
|
|
|
|
if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
|
|
|
|
CondInfo.FoundNonSkip = true;
|
|
|
|
break;
|
|
|
|
}
|
2006-06-22 14:36:29 +08:00
|
|
|
} else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif".
|
2006-06-18 13:43:12 +08:00
|
|
|
PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
|
|
|
|
|
|
|
|
bool ShouldEnter;
|
|
|
|
// If this is in a skipping block or if we're already handled this #if
|
|
|
|
// block, don't bother parsing the condition.
|
|
|
|
if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
|
2006-06-18 14:48:37 +08:00
|
|
|
DiscardUntilEndOfDirective();
|
2006-06-18 13:43:12 +08:00
|
|
|
ShouldEnter = false;
|
|
|
|
} else {
|
|
|
|
// Restore the value of SkippingContents so that identifiers are
|
|
|
|
// looked up, etc, inside the #elif expression.
|
|
|
|
assert(SkippingContents && "We have to be skipping here!");
|
|
|
|
SkippingContents = false;
|
2006-06-18 14:50:36 +08:00
|
|
|
ShouldEnter = EvaluateDirectiveExpression();
|
2006-06-18 13:43:12 +08:00
|
|
|
SkippingContents = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a #elif with a #else before it, report the error.
|
2006-06-18 14:48:37 +08:00
|
|
|
if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// If this condition is true, enter it!
|
|
|
|
if (ShouldEnter) {
|
|
|
|
CondInfo.FoundNonSkip = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CurLexer->ParsingPreprocessorDirective = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, if we are out of the conditional (saw an #endif or ran off the end
|
|
|
|
// of the file, just stop skipping and return to lexing whatever came after
|
|
|
|
// the #if block.
|
|
|
|
SkippingContents = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Preprocessor Directive Handling.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// HandleDirective - This callback is invoked when the lexer sees a # token
|
|
|
|
/// at the start of a line. This consumes the directive, modifies the
|
|
|
|
/// lexer/preprocessor state, and advances the lexer(s) so that the next token
|
|
|
|
/// read is the correct one.
|
2006-06-18 14:48:37 +08:00
|
|
|
void Preprocessor::HandleDirective(LexerToken &Result) {
|
2006-06-18 13:43:12 +08:00
|
|
|
// FIXME: TRADITIONAL: # with whitespace before it not recognized by K&R?
|
|
|
|
|
|
|
|
// We just parsed a # character at the start of a line, so we're in directive
|
|
|
|
// mode. Tell the lexer this so any newlines we see will be converted into an
|
|
|
|
// EOM token (this terminates the macro).
|
|
|
|
CurLexer->ParsingPreprocessorDirective = true;
|
|
|
|
|
|
|
|
++NumDirectives;
|
|
|
|
|
|
|
|
// Read the next token, the directive flavor.
|
2006-06-18 14:48:37 +08:00
|
|
|
LexUnexpandedToken(Result);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
switch (Result.getKind()) {
|
|
|
|
default: break;
|
|
|
|
case tok::eom:
|
2006-06-18 14:48:37 +08:00
|
|
|
return; // null directive.
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
case tok::numeric_constant:
|
|
|
|
// FIXME: implement # 7 line numbers!
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case tok::kw_else:
|
|
|
|
return HandleElseDirective(Result);
|
|
|
|
case tok::kw_if:
|
|
|
|
return HandleIfDirective(Result);
|
|
|
|
case tok::identifier:
|
2006-06-22 14:14:04 +08:00
|
|
|
// Get the identifier name without trigraphs or embedded newlines.
|
|
|
|
const char *Directive = Result.getIdentifierInfo()->getName();
|
2006-06-18 13:43:12 +08:00
|
|
|
bool isExtension = false;
|
2006-06-22 14:14:04 +08:00
|
|
|
switch (Result.getIdentifierInfo()->getNameLength()) {
|
2006-06-18 13:43:12 +08:00
|
|
|
case 4:
|
2006-06-22 14:14:04 +08:00
|
|
|
if (Directive[0] == 'l' && !strcmp(Directive, "line"))
|
2006-06-25 05:31:03 +08:00
|
|
|
; // FIXME: implement #line
|
2006-06-22 14:14:04 +08:00
|
|
|
if (Directive[0] == 'e' && !strcmp(Directive, "elif"))
|
2006-06-18 13:43:12 +08:00
|
|
|
return HandleElifDirective(Result);
|
2006-06-22 14:14:04 +08:00
|
|
|
if (Directive[0] == 's' && !strcmp(Directive, "sccs")) {
|
2006-06-25 05:31:03 +08:00
|
|
|
isExtension = true; // FIXME: implement #sccs
|
2006-06-18 13:43:12 +08:00
|
|
|
// SCCS is the same as #ident.
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 5:
|
2006-06-22 14:14:04 +08:00
|
|
|
if (Directive[0] == 'e' && !strcmp(Directive, "endif"))
|
2006-06-18 13:43:12 +08:00
|
|
|
return HandleEndifDirective(Result);
|
2006-06-22 14:14:04 +08:00
|
|
|
if (Directive[0] == 'i' && !strcmp(Directive, "ifdef"))
|
2006-06-18 13:43:12 +08:00
|
|
|
return HandleIfdefDirective(Result, false);
|
2006-06-22 14:14:04 +08:00
|
|
|
if (Directive[0] == 'u' && !strcmp(Directive, "undef"))
|
2006-06-18 13:43:12 +08:00
|
|
|
return HandleUndefDirective(Result);
|
2006-06-22 14:14:04 +08:00
|
|
|
if (Directive[0] == 'e' && !strcmp(Directive, "error"))
|
2006-06-18 13:43:12 +08:00
|
|
|
return HandleUserDiagnosticDirective(Result, false);
|
2006-06-22 14:14:04 +08:00
|
|
|
if (Directive[0] == 'i' && !strcmp(Directive, "ident"))
|
2006-06-25 05:31:03 +08:00
|
|
|
isExtension = true; // FIXME: implement #ident
|
2006-06-18 13:43:12 +08:00
|
|
|
break;
|
|
|
|
case 6:
|
2006-06-22 14:14:04 +08:00
|
|
|
if (Directive[0] == 'd' && !strcmp(Directive, "define"))
|
2006-06-18 13:43:12 +08:00
|
|
|
return HandleDefineDirective(Result);
|
2006-06-22 14:14:04 +08:00
|
|
|
if (Directive[0] == 'i' && !strcmp(Directive, "ifndef"))
|
2006-06-18 13:43:12 +08:00
|
|
|
return HandleIfdefDirective(Result, true);
|
2006-06-22 14:14:04 +08:00
|
|
|
if (Directive[0] == 'i' && !strcmp(Directive, "import"))
|
2006-06-18 13:43:12 +08:00
|
|
|
return HandleImportDirective(Result);
|
2006-06-25 05:31:03 +08:00
|
|
|
if (Directive[0] == 'p' && !strcmp(Directive, "pragma"))
|
|
|
|
return HandlePragmaDirective(Result);
|
|
|
|
if (Directive[0] == 'a' && !strcmp(Directive, "assert"))
|
|
|
|
isExtension = true; // FIXME: implement #assert
|
2006-06-18 13:43:12 +08:00
|
|
|
break;
|
|
|
|
case 7:
|
2006-06-22 14:14:04 +08:00
|
|
|
if (Directive[0] == 'i' && !strcmp(Directive, "include"))
|
|
|
|
return HandleIncludeDirective(Result); // Handle #include.
|
|
|
|
if (Directive[0] == 'w' && !strcmp(Directive, "warning")) {
|
2006-06-18 14:48:37 +08:00
|
|
|
Diag(Result, diag::ext_pp_warning_directive);
|
2006-06-18 15:19:54 +08:00
|
|
|
return HandleUserDiagnosticDirective(Result, true);
|
2006-06-18 14:48:37 +08:00
|
|
|
}
|
2006-06-18 13:43:12 +08:00
|
|
|
break;
|
|
|
|
case 8:
|
2006-06-22 14:14:04 +08:00
|
|
|
if (Directive[0] == 'u' && !strcmp(Directive, "unassert")) {
|
2006-06-25 05:31:03 +08:00
|
|
|
isExtension = true; // FIXME: implement #unassert
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 12:
|
2006-06-22 14:14:04 +08:00
|
|
|
if (Directive[0] == 'i' && !strcmp(Directive, "include_next"))
|
|
|
|
return HandleIncludeNextDirective(Result); // Handle #include_next.
|
2006-06-18 13:43:12 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we reached here, the preprocessing token is not valid!
|
2006-06-18 14:48:37 +08:00
|
|
|
Diag(Result, diag::err_pp_invalid_directive);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Read the rest of the PP line.
|
|
|
|
do {
|
2006-06-18 14:48:37 +08:00
|
|
|
Lex(Result);
|
2006-06-18 13:43:12 +08:00
|
|
|
} while (Result.getKind() != tok::eom);
|
|
|
|
|
|
|
|
// Okay, we're done parsing the directive.
|
|
|
|
}
|
|
|
|
|
2006-06-18 14:48:37 +08:00
|
|
|
void Preprocessor::HandleUserDiagnosticDirective(LexerToken &Result,
|
2006-06-18 13:43:12 +08:00
|
|
|
bool isWarning) {
|
|
|
|
// Read the rest of the line raw. We do this because we don't want macros
|
|
|
|
// to be expanded and we don't require that the tokens be valid preprocessing
|
|
|
|
// tokens. For example, this is allowed: "#warning ` 'foo". GCC does
|
|
|
|
// collapse multiple consequtive white space between tokens, but this isn't
|
|
|
|
// specified by the standard.
|
|
|
|
std::string Message = CurLexer->ReadToEndOfLine();
|
|
|
|
|
|
|
|
unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error;
|
|
|
|
return Diag(Result, DiagID, Message);
|
|
|
|
}
|
|
|
|
|
2006-06-25 05:31:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Preprocessor Include Directive Handling.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
/// HandleIncludeDirective - The "#include" tokens have just been read, read the
|
|
|
|
/// file to be included from the lexer, then include it! This is a common
|
|
|
|
/// routine with functionality shared between #include, #include_next and
|
|
|
|
/// #import.
|
2006-06-18 14:48:37 +08:00
|
|
|
void Preprocessor::HandleIncludeDirective(LexerToken &IncludeTok,
|
2006-06-18 13:43:12 +08:00
|
|
|
const DirectoryLookup *LookupFrom,
|
|
|
|
bool isImport) {
|
|
|
|
++NumIncluded;
|
|
|
|
LexerToken FilenameTok;
|
2006-06-25 14:23:00 +08:00
|
|
|
std::string Filename = CurLexer->LexIncludeFilename(FilenameTok);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// If the token kind is EOM, the error has already been diagnosed.
|
|
|
|
if (FilenameTok.getKind() == tok::eom)
|
2006-06-18 14:48:37 +08:00
|
|
|
return;
|
2006-06-25 14:23:00 +08:00
|
|
|
|
|
|
|
// Verify that there is nothing after the filename, other than EOM. Use the
|
|
|
|
// preprocessor to lex this in case lexing the filename entered a macro.
|
|
|
|
CheckEndOfDirective("#include");
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Check that we don't have infinite #include recursion.
|
|
|
|
if (IncludeStack.size() == MaxAllowedIncludeStackDepth-1)
|
|
|
|
return Diag(FilenameTok, diag::err_pp_include_too_deep);
|
|
|
|
|
2006-06-25 14:23:00 +08:00
|
|
|
// Find out whether the filename is <x> or "x".
|
|
|
|
bool isAngled = Filename[0] == '<';
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Remove the quotes.
|
|
|
|
Filename = std::string(Filename.begin()+1, Filename.end()-1);
|
|
|
|
|
|
|
|
// Search include directories.
|
2006-06-22 13:52:16 +08:00
|
|
|
const DirectoryLookup *CurDir;
|
|
|
|
const FileEntry *File = LookupFile(Filename, isAngled, LookupFrom, CurDir);
|
2006-06-18 13:43:12 +08:00
|
|
|
if (File == 0)
|
|
|
|
return Diag(FilenameTok, diag::err_pp_file_not_found);
|
|
|
|
|
|
|
|
// Get information about this file.
|
|
|
|
PerFileInfo &FileInfo = getFileInfo(File);
|
|
|
|
|
|
|
|
// If this is a #import directive, check that we have not already imported
|
|
|
|
// this header.
|
|
|
|
if (isImport) {
|
|
|
|
// If this has already been imported, don't import it again.
|
|
|
|
FileInfo.isImport = true;
|
|
|
|
|
|
|
|
// Has this already been #import'ed or #include'd?
|
2006-06-18 14:48:37 +08:00
|
|
|
if (FileInfo.NumIncludes) return;
|
2006-06-18 13:43:12 +08:00
|
|
|
} else {
|
|
|
|
// Otherwise, if this is a #include of a file that was previously #import'd
|
|
|
|
// or if this is the second #include of a #pragma once file, ignore it.
|
|
|
|
if (FileInfo.isImport)
|
2006-06-18 14:48:37 +08:00
|
|
|
return;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Look up the file, create a File ID for it.
|
|
|
|
unsigned FileID =
|
2006-06-19 00:32:35 +08:00
|
|
|
SourceMgr.createFileID(File, FilenameTok.getLocation());
|
2006-06-18 13:43:12 +08:00
|
|
|
if (FileID == 0)
|
|
|
|
return Diag(FilenameTok, diag::err_pp_file_not_found);
|
|
|
|
|
|
|
|
// Finally, if all is good, enter the new file!
|
2006-06-22 13:52:16 +08:00
|
|
|
EnterSourceFile(FileID, CurDir);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Increment the number of times this file has been included.
|
|
|
|
++FileInfo.NumIncludes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// HandleIncludeNextDirective - Implements #include_next.
|
|
|
|
///
|
2006-06-18 14:48:37 +08:00
|
|
|
void Preprocessor::HandleIncludeNextDirective(LexerToken &IncludeNextTok) {
|
|
|
|
Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// #include_next is like #include, except that we start searching after
|
|
|
|
// the current found directory. If we can't do this, issue a
|
|
|
|
// diagnostic.
|
2006-06-22 13:52:16 +08:00
|
|
|
const DirectoryLookup *Lookup = CurDirLookup;
|
2006-06-18 13:43:12 +08:00
|
|
|
if (IncludeStack.empty()) {
|
|
|
|
Lookup = 0;
|
2006-06-18 14:48:37 +08:00
|
|
|
Diag(IncludeNextTok, diag::pp_include_next_in_primary);
|
2006-06-18 13:43:12 +08:00
|
|
|
} else if (Lookup == 0) {
|
2006-06-18 14:48:37 +08:00
|
|
|
Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
|
2006-06-22 13:52:16 +08:00
|
|
|
} else {
|
|
|
|
// Start looking up in the next directory.
|
|
|
|
++Lookup;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return HandleIncludeDirective(IncludeNextTok, Lookup);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// HandleImportDirective - Implements #import.
|
|
|
|
///
|
2006-06-18 14:48:37 +08:00
|
|
|
void Preprocessor::HandleImportDirective(LexerToken &ImportTok) {
|
|
|
|
Diag(ImportTok, diag::ext_pp_import_directive);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
return HandleIncludeDirective(ImportTok, 0, true);
|
|
|
|
}
|
|
|
|
|
2006-06-25 05:31:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Preprocessor Macro Directive Handling.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
/// HandleDefineDirective - Implements #define. This consumes the entire macro
|
|
|
|
/// line then lets the caller lex the next real token.
|
|
|
|
///
|
2006-06-18 14:48:37 +08:00
|
|
|
void Preprocessor::HandleDefineDirective(LexerToken &DefineTok) {
|
2006-06-18 13:43:12 +08:00
|
|
|
++NumDefined;
|
|
|
|
LexerToken MacroNameTok;
|
2006-06-18 14:48:37 +08:00
|
|
|
ReadMacroName(MacroNameTok);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Error reading macro name? If so, diagnostic already issued.
|
|
|
|
if (MacroNameTok.getKind() == tok::eom)
|
2006-06-18 14:48:37 +08:00
|
|
|
return;
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-06-19 00:32:35 +08:00
|
|
|
MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
LexerToken Tok;
|
2006-06-18 14:48:37 +08:00
|
|
|
LexUnexpandedToken(Tok);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
if (Tok.getKind() == tok::eom) {
|
|
|
|
// If there is no body to this macro, we have no special handling here.
|
|
|
|
} else if (Tok.getKind() == tok::l_paren && !Tok.hasLeadingSpace()) {
|
|
|
|
// This is a function-like macro definition.
|
|
|
|
//assert(0 && "Function-like macros not implemented!");
|
|
|
|
return DiscardUntilEndOfDirective();
|
|
|
|
|
|
|
|
} else if (!Tok.hasLeadingSpace()) {
|
|
|
|
// C99 requires whitespace between the macro definition and the body. Emit
|
|
|
|
// a diagnostic for something like "#define X+".
|
|
|
|
if (Features.C99) {
|
2006-06-18 14:48:37 +08:00
|
|
|
Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
|
2006-06-18 13:43:12 +08:00
|
|
|
} else {
|
|
|
|
// FIXME: C90/C++ do not get this diagnostic, but it does get a similar
|
|
|
|
// one in some cases!
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// This is a normal token with leading space. Clear the leading space
|
|
|
|
// marker on the first token to get proper expansion.
|
|
|
|
Tok.ClearFlag(LexerToken::LeadingSpace);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the rest of the macro body.
|
|
|
|
while (Tok.getKind() != tok::eom) {
|
|
|
|
MI->AddTokenToBody(Tok);
|
|
|
|
|
|
|
|
// FIXME: See create_iso_definition.
|
|
|
|
|
|
|
|
// Get the next token of the macro.
|
2006-06-18 14:48:37 +08:00
|
|
|
LexUnexpandedToken(Tok);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, if this identifier already had a macro defined for it, verify that
|
|
|
|
// the macro bodies are identical and free the old definition.
|
|
|
|
if (MacroInfo *OtherMI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) {
|
2006-06-28 13:26:32 +08:00
|
|
|
if (OtherMI->isBuiltinMacro())
|
|
|
|
Diag(MacroNameTok, diag::pp_redef_builtin_macro);
|
|
|
|
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// FIXME: Verify the definition is the same.
|
|
|
|
// Macros must be identical. This means all tokes and whitespace separation
|
|
|
|
// must be the same.
|
|
|
|
delete OtherMI;
|
|
|
|
}
|
|
|
|
|
|
|
|
MacroNameTok.getIdentifierInfo()->setMacroInfo(MI);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// HandleUndefDirective - Implements #undef.
|
|
|
|
///
|
2006-06-18 14:48:37 +08:00
|
|
|
void Preprocessor::HandleUndefDirective(LexerToken &UndefTok) {
|
2006-06-18 13:43:12 +08:00
|
|
|
++NumUndefined;
|
|
|
|
LexerToken MacroNameTok;
|
2006-06-18 14:48:37 +08:00
|
|
|
ReadMacroName(MacroNameTok);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Error reading macro name? If so, diagnostic already issued.
|
|
|
|
if (MacroNameTok.getKind() == tok::eom)
|
2006-06-18 14:48:37 +08:00
|
|
|
return;
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Check to see if this is the last token on the #undef line.
|
2006-06-18 14:48:37 +08:00
|
|
|
CheckEndOfDirective("#undef");
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Okay, we finally have a valid identifier to undef.
|
|
|
|
MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo();
|
|
|
|
|
|
|
|
// If the macro is not defined, this is a noop undef, just return.
|
2006-06-18 14:48:37 +08:00
|
|
|
if (MI == 0) return;
|
2006-06-28 13:26:32 +08:00
|
|
|
|
|
|
|
if (MI->isBuiltinMacro())
|
|
|
|
Diag(MacroNameTok, diag::pp_undef_builtin_macro);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
#if 0 // FIXME: implement warn_unused_macros.
|
|
|
|
if (CPP_OPTION (pfile, warn_unused_macros))
|
|
|
|
_cpp_warn_if_unused_macro (pfile, node, NULL);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Free macro definition.
|
|
|
|
delete MI;
|
|
|
|
MacroNameTok.getIdentifierInfo()->setMacroInfo(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-25 05:31:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Preprocessor Conditional Directive Handling.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
|
|
|
|
/// true when this is a #ifndef directive.
|
|
|
|
///
|
2006-06-18 14:48:37 +08:00
|
|
|
void Preprocessor::HandleIfdefDirective(LexerToken &Result, bool isIfndef) {
|
2006-06-18 13:43:12 +08:00
|
|
|
++NumIf;
|
|
|
|
LexerToken DirectiveTok = Result;
|
|
|
|
|
|
|
|
LexerToken MacroNameTok;
|
2006-06-18 14:48:37 +08:00
|
|
|
ReadMacroName(MacroNameTok);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Error reading macro name? If so, diagnostic already issued.
|
|
|
|
if (MacroNameTok.getKind() == tok::eom)
|
2006-06-18 14:48:37 +08:00
|
|
|
return;
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Check to see if this is the last token on the #if[n]def line.
|
2006-06-18 14:48:37 +08:00
|
|
|
CheckEndOfDirective("#ifdef");
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Should we include the stuff contained by this directive?
|
|
|
|
if (!MacroNameTok.getIdentifierInfo()->getMacroInfo() == isIfndef) {
|
|
|
|
// Yes, remember that we are inside a conditional, then lex the next token.
|
2006-06-19 00:32:35 +08:00
|
|
|
CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
|
2006-06-18 13:43:12 +08:00
|
|
|
/*foundnonskip*/true, /*foundelse*/false);
|
|
|
|
} else {
|
|
|
|
// No, skip the contents of this block and return the first token after it.
|
2006-06-19 00:32:35 +08:00
|
|
|
SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
|
2006-06-18 14:48:37 +08:00
|
|
|
/*Foundnonskip*/false,
|
|
|
|
/*FoundElse*/false);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// HandleIfDirective - Implements the #if directive.
|
|
|
|
///
|
2006-06-18 14:48:37 +08:00
|
|
|
void Preprocessor::HandleIfDirective(LexerToken &IfToken) {
|
2006-06-18 13:43:12 +08:00
|
|
|
++NumIf;
|
2006-06-18 14:50:36 +08:00
|
|
|
bool ConditionalTrue = EvaluateDirectiveExpression();
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Should we include the stuff contained by this directive?
|
|
|
|
if (ConditionalTrue) {
|
|
|
|
// Yes, remember that we are inside a conditional, then lex the next token.
|
2006-06-19 00:32:35 +08:00
|
|
|
CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
|
2006-06-18 13:43:12 +08:00
|
|
|
/*foundnonskip*/true, /*foundelse*/false);
|
|
|
|
} else {
|
|
|
|
// No, skip the contents of this block and return the first token after it.
|
2006-06-19 00:32:35 +08:00
|
|
|
SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
|
2006-06-18 14:48:37 +08:00
|
|
|
/*FoundElse*/false);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// HandleEndifDirective - Implements the #endif directive.
|
|
|
|
///
|
2006-06-18 14:48:37 +08:00
|
|
|
void Preprocessor::HandleEndifDirective(LexerToken &EndifToken) {
|
2006-06-18 13:43:12 +08:00
|
|
|
++NumEndif;
|
|
|
|
// Check that this is the whole directive.
|
2006-06-18 14:48:37 +08:00
|
|
|
CheckEndOfDirective("#endif");
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
PPConditionalInfo CondInfo;
|
|
|
|
if (CurLexer->popConditionalLevel(CondInfo)) {
|
|
|
|
// No conditionals on the stack: this is an #endif without an #if.
|
|
|
|
return Diag(EndifToken, diag::err_pp_endif_without_if);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!CondInfo.WasSkipping && !isSkipping() &&
|
|
|
|
"This code should only be reachable in the non-skipping case!");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-18 14:48:37 +08:00
|
|
|
void Preprocessor::HandleElseDirective(LexerToken &Result) {
|
2006-06-18 13:43:12 +08:00
|
|
|
++NumElse;
|
|
|
|
// #else directive in a non-skipping conditional... start skipping.
|
2006-06-18 14:48:37 +08:00
|
|
|
CheckEndOfDirective("#else");
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
PPConditionalInfo CI;
|
|
|
|
if (CurLexer->popConditionalLevel(CI))
|
|
|
|
return Diag(Result, diag::pp_err_else_without_if);
|
|
|
|
|
|
|
|
// If this is a #else with a #else before it, report the error.
|
2006-06-18 14:48:37 +08:00
|
|
|
if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Finally, skip the rest of the contents of this block and return the first
|
|
|
|
// token after it.
|
|
|
|
return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
|
|
|
|
/*FoundElse*/true);
|
|
|
|
}
|
|
|
|
|
2006-06-18 14:48:37 +08:00
|
|
|
void Preprocessor::HandleElifDirective(LexerToken &ElifToken) {
|
2006-06-18 13:43:12 +08:00
|
|
|
++NumElse;
|
|
|
|
// #elif directive in a non-skipping conditional... start skipping.
|
|
|
|
// We don't care what the condition is, because we will always skip it (since
|
|
|
|
// the block immediately before it was included).
|
2006-06-18 14:48:37 +08:00
|
|
|
DiscardUntilEndOfDirective();
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
PPConditionalInfo CI;
|
|
|
|
if (CurLexer->popConditionalLevel(CI))
|
|
|
|
return Diag(ElifToken, diag::pp_err_elif_without_if);
|
|
|
|
|
|
|
|
// If this is a #elif with a #else before it, report the error.
|
2006-06-18 14:48:37 +08:00
|
|
|
if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Finally, skip the rest of the contents of this block and return the first
|
|
|
|
// token after it.
|
|
|
|
return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
|
|
|
|
/*FoundElse*/CI.FoundElse);
|
|
|
|
}
|
2006-06-25 05:31:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Preprocessor Pragma Directive Handling.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// HandlePragmaDirective - The "#pragma" directive has been parsed with
|
|
|
|
/// PragmaTok containing the "pragma" identifier. Lex the rest of the pragma,
|
|
|
|
/// passing it to the registered pragma handlers.
|
|
|
|
void Preprocessor::HandlePragmaDirective(LexerToken &PragmaTok) {
|
|
|
|
++NumPragma;
|
|
|
|
|
|
|
|
// Invoke the first level of pragma handlers which reads the namespace id.
|
|
|
|
LexerToken Tok;
|
|
|
|
PragmaHandlers->HandlePragma(*this, Tok);
|
|
|
|
|
|
|
|
// If the pragma handler didn't read the rest of the line, consume it now.
|
2006-06-25 06:12:56 +08:00
|
|
|
if (CurLexer->ParsingPreprocessorDirective)
|
|
|
|
DiscardUntilEndOfDirective();
|
2006-06-25 05:31:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'.
|
2006-06-25 06:12:56 +08:00
|
|
|
///
|
2006-06-25 05:31:03 +08:00
|
|
|
void Preprocessor::HandlePragmaOnce(LexerToken &OnceTok) {
|
|
|
|
if (IncludeStack.empty()) {
|
|
|
|
Diag(OnceTok, diag::pp_pragma_once_in_main_file);
|
|
|
|
return;
|
|
|
|
}
|
2006-06-25 06:12:56 +08:00
|
|
|
|
|
|
|
// FIXME: implement the _Pragma thing.
|
|
|
|
assert(CurLexer && "Cannot have a pragma in a macro expansion yet!");
|
|
|
|
|
|
|
|
// Mark the file as a once-only file now.
|
|
|
|
const FileEntry *File =
|
|
|
|
SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID());
|
|
|
|
getFileInfo(File).isImport = true;
|
2006-06-25 05:31:03 +08:00
|
|
|
}
|
|
|
|
|
2006-06-25 06:12:56 +08:00
|
|
|
/// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
|
|
|
|
///
|
|
|
|
void Preprocessor::HandlePragmaPoison(LexerToken &PoisonTok) {
|
|
|
|
LexerToken Tok;
|
|
|
|
assert(!SkippingContents && "Why are we handling pragmas while skipping?");
|
|
|
|
while (1) {
|
|
|
|
// Read the next token to poison. While doing this, pretend that we are
|
|
|
|
// skipping while reading the identifier to poison.
|
|
|
|
// This avoids errors on code like:
|
|
|
|
// #pragma GCC poison X
|
|
|
|
// #pragma GCC poison X
|
|
|
|
SkippingContents = true;
|
|
|
|
LexUnexpandedToken(Tok);
|
|
|
|
SkippingContents = false;
|
|
|
|
|
|
|
|
// If we reached the end of line, we're done.
|
|
|
|
if (Tok.getKind() == tok::eom) return;
|
|
|
|
|
|
|
|
// Can only poison identifiers.
|
|
|
|
if (Tok.getKind() != tok::identifier) {
|
|
|
|
Diag(Tok, diag::err_pp_invalid_poison);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look up the identifier info for the token.
|
|
|
|
std::string TokStr = getSpelling(Tok);
|
|
|
|
IdentifierTokenInfo *II =
|
|
|
|
getIdentifierInfo(&TokStr[0], &TokStr[0]+TokStr.size());
|
|
|
|
|
|
|
|
// Already poisoned.
|
|
|
|
if (II->isPoisoned()) continue;
|
|
|
|
|
|
|
|
// If this is a macro identifier, emit a warning.
|
|
|
|
if (II->getMacroInfo())
|
|
|
|
Diag(Tok, diag::pp_poisoning_existing_macro);
|
|
|
|
|
|
|
|
// Finally, poison it!
|
|
|
|
II->setIsPoisoned();
|
|
|
|
}
|
|
|
|
}
|
2006-06-25 05:31:03 +08:00
|
|
|
|
2006-06-25 14:23:00 +08:00
|
|
|
/// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know
|
|
|
|
/// that the whole directive has been parsed.
|
2006-06-25 12:20:34 +08:00
|
|
|
void Preprocessor::HandlePragmaSystemHeader(LexerToken &SysHeaderTok) {
|
|
|
|
if (IncludeStack.empty()) {
|
|
|
|
Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark the file as a system header.
|
|
|
|
const FileEntry *File =
|
|
|
|
SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID());
|
|
|
|
getFileInfo(File).DirInfo = DirectoryLookup::SystemHeaderDir;
|
|
|
|
|
|
|
|
|
|
|
|
// Notify the client, if desired, that we are in a new source file.
|
|
|
|
if (FileChangeHandler)
|
|
|
|
FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferPtr),
|
|
|
|
SystemHeaderPragma, DirectoryLookup::SystemHeaderDir);
|
|
|
|
}
|
|
|
|
|
2006-06-25 14:23:00 +08:00
|
|
|
/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
|
|
|
|
///
|
|
|
|
void Preprocessor::HandlePragmaDependency(LexerToken &DependencyTok) {
|
|
|
|
LexerToken FilenameTok;
|
|
|
|
std::string Filename = CurLexer->LexIncludeFilename(FilenameTok);
|
|
|
|
|
|
|
|
// If the token kind is EOM, the error has already been diagnosed.
|
|
|
|
if (FilenameTok.getKind() == tok::eom)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Find out whether the filename is <x> or "x".
|
|
|
|
bool isAngled = Filename[0] == '<';
|
|
|
|
|
|
|
|
// Remove the quotes.
|
|
|
|
Filename = std::string(Filename.begin()+1, Filename.end()-1);
|
|
|
|
|
|
|
|
// Search include directories.
|
|
|
|
const DirectoryLookup *CurDir;
|
|
|
|
const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir);
|
|
|
|
if (File == 0)
|
|
|
|
return Diag(FilenameTok, diag::err_pp_file_not_found);
|
|
|
|
|
|
|
|
Lexer *TheLexer = CurLexer;
|
|
|
|
if (TheLexer == 0) {
|
|
|
|
assert(!IncludeStack.empty() && "No current lexer?");
|
|
|
|
TheLexer = IncludeStack.back().TheLexer;
|
|
|
|
}
|
|
|
|
const FileEntry *CurFile =
|
|
|
|
SourceMgr.getFileEntryForFileID(TheLexer->getCurFileID());
|
|
|
|
|
|
|
|
// If this file is older than the file it depends on, emit a diagnostic.
|
|
|
|
if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
|
|
|
|
// Lex tokens at the end of the message and include them in the message.
|
|
|
|
std::string Message;
|
|
|
|
Lex(DependencyTok);
|
|
|
|
while (DependencyTok.getKind() != tok::eom) {
|
|
|
|
Message += getSpelling(DependencyTok) + " ";
|
|
|
|
Lex(DependencyTok);
|
|
|
|
}
|
|
|
|
|
|
|
|
Message.erase(Message.end()-1);
|
|
|
|
Diag(FilenameTok, diag::pp_out_of_date_dependency, Message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-25 05:31:03 +08:00
|
|
|
/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
|
|
|
|
/// If 'Namespace' is non-null, then it is a token required to exist on the
|
|
|
|
/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
|
|
|
|
void Preprocessor::AddPragmaHandler(const char *Namespace,
|
|
|
|
PragmaHandler *Handler) {
|
|
|
|
PragmaNamespace *InsertNS = PragmaHandlers;
|
|
|
|
|
|
|
|
// If this is specified to be in a namespace, step down into it.
|
|
|
|
if (Namespace) {
|
|
|
|
IdentifierTokenInfo *NSID = getIdentifierInfo(Namespace);
|
|
|
|
|
|
|
|
// If there is already a pragma handler with the name of this namespace,
|
|
|
|
// we either have an error (directive with the same name as a namespace) or
|
|
|
|
// we already have the namespace to insert into.
|
|
|
|
if (PragmaHandler *Existing = PragmaHandlers->FindHandler(NSID)) {
|
|
|
|
InsertNS = Existing->getIfNamespace();
|
|
|
|
assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
|
|
|
|
" handler with the same name!");
|
|
|
|
} else {
|
|
|
|
// Otherwise, this namespace doesn't exist yet, create and insert the
|
|
|
|
// handler for it.
|
|
|
|
InsertNS = new PragmaNamespace(NSID);
|
|
|
|
PragmaHandlers->AddPragma(InsertNS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to make sure we don't already have a pragma for this identifier.
|
|
|
|
assert(!InsertNS->FindHandler(Handler->getName()) &&
|
|
|
|
"Pragma handler already exists for this identifier!");
|
|
|
|
InsertNS->AddPragma(Handler);
|
|
|
|
}
|
|
|
|
|
2006-06-25 06:12:56 +08:00
|
|
|
namespace {
|
2006-06-25 12:20:34 +08:00
|
|
|
struct PragmaOnceHandler : public PragmaHandler {
|
2006-06-25 05:31:03 +08:00
|
|
|
PragmaOnceHandler(const IdentifierTokenInfo *OnceID) : PragmaHandler(OnceID){}
|
|
|
|
virtual void HandlePragma(Preprocessor &PP, LexerToken &OnceTok) {
|
|
|
|
PP.CheckEndOfDirective("#pragma once");
|
|
|
|
PP.HandlePragmaOnce(OnceTok);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-06-25 12:20:34 +08:00
|
|
|
struct PragmaPoisonHandler : public PragmaHandler {
|
2006-06-25 06:12:56 +08:00
|
|
|
PragmaPoisonHandler(const IdentifierTokenInfo *ID) : PragmaHandler(ID) {}
|
|
|
|
virtual void HandlePragma(Preprocessor &PP, LexerToken &PoisonTok) {
|
|
|
|
PP.HandlePragmaPoison(PoisonTok);
|
|
|
|
}
|
|
|
|
};
|
2006-06-25 12:20:34 +08:00
|
|
|
|
|
|
|
struct PragmaSystemHeaderHandler : public PragmaHandler {
|
|
|
|
PragmaSystemHeaderHandler(const IdentifierTokenInfo *ID) : PragmaHandler(ID){}
|
|
|
|
virtual void HandlePragma(Preprocessor &PP, LexerToken &SHToken) {
|
|
|
|
PP.HandlePragmaSystemHeader(SHToken);
|
|
|
|
PP.CheckEndOfDirective("#pragma");
|
|
|
|
}
|
|
|
|
};
|
2006-06-25 14:23:00 +08:00
|
|
|
struct PragmaDependencyHandler : public PragmaHandler {
|
|
|
|
PragmaDependencyHandler(const IdentifierTokenInfo *ID) : PragmaHandler(ID) {}
|
|
|
|
virtual void HandlePragma(Preprocessor &PP, LexerToken &DepToken) {
|
|
|
|
PP.HandlePragmaDependency(DepToken);
|
|
|
|
}
|
|
|
|
};
|
2006-06-25 06:12:56 +08:00
|
|
|
}
|
|
|
|
|
2006-06-25 05:31:03 +08:00
|
|
|
|
|
|
|
/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
|
|
|
|
/// #pragma GCC poison/system_header/dependency and #pragma once.
|
|
|
|
void Preprocessor::RegisterBuiltinPragmas() {
|
|
|
|
AddPragmaHandler(0, new PragmaOnceHandler(getIdentifierInfo("once")));
|
2006-06-25 06:12:56 +08:00
|
|
|
AddPragmaHandler("GCC", new PragmaPoisonHandler(getIdentifierInfo("poison")));
|
2006-06-25 12:20:34 +08:00
|
|
|
AddPragmaHandler("GCC", new PragmaSystemHeaderHandler(
|
|
|
|
getIdentifierInfo("system_header")));
|
2006-06-25 14:23:00 +08:00
|
|
|
AddPragmaHandler("GCC", new PragmaDependencyHandler(
|
|
|
|
getIdentifierInfo("dependency")));
|
2006-06-25 05:31:03 +08:00
|
|
|
}
|