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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Options to support:
|
|
|
|
// -H - Print the name of each header file used.
|
|
|
|
// -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"
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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-28 14:49:17 +08:00
|
|
|
#include "clang/Lex/ScratchBuffer.h"
|
2006-06-18 13:43:12 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
2006-07-26 14:26:52 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2006-06-18 13:43:12 +08:00
|
|
|
#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-28 14:49:17 +08:00
|
|
|
ScratchBuf = new ScratchBuffer(SourceMgr);
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Clear stats.
|
|
|
|
NumDirectives = NumIncluded = NumDefined = NumUndefined = NumPragma = 0;
|
|
|
|
NumIf = NumElse = NumEndif = 0;
|
2006-07-09 08:45:31 +08:00
|
|
|
NumEnteredSourceFiles = 0;
|
|
|
|
NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0;
|
2006-07-20 12:47:30 +08:00
|
|
|
NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0;
|
2006-07-04 15:26:10 +08:00
|
|
|
MaxIncludeStackDepth = 0; NumMultiIncludeFileOptzn = 0;
|
2006-06-18 13:43:12 +08:00
|
|
|
NumSkipped = 0;
|
2006-06-21 14:50:18 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Macro expansion is enabled.
|
|
|
|
DisableMacroExpansion = false;
|
2006-07-15 15:42:55 +08:00
|
|
|
InMacroArgs = false;
|
2006-06-21 14:50:18 +08:00
|
|
|
|
|
|
|
// There is no file-change handler yet.
|
|
|
|
FileChangeHandler = 0;
|
2006-07-04 06:16:27 +08:00
|
|
|
IdentHandler = 0;
|
2006-06-25 05:31:03 +08:00
|
|
|
|
2006-07-06 13:17:39 +08:00
|
|
|
// "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
|
|
|
|
// This gets unpoisoned where it is allowed.
|
|
|
|
(Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned();
|
|
|
|
|
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;
|
|
|
|
|
2006-07-03 04:34:39 +08:00
|
|
|
while (!IncludeMacroStack.empty()) {
|
|
|
|
delete IncludeMacroStack.back().TheLexer;
|
|
|
|
delete IncludeMacroStack.back().TheMacroExpander;
|
|
|
|
IncludeMacroStack.pop_back();
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
2006-06-25 05:31:03 +08:00
|
|
|
|
|
|
|
// Release pragma information.
|
|
|
|
delete PragmaHandlers;
|
2006-06-28 14:49:17 +08:00
|
|
|
|
|
|
|
// Delete the scratch buffer info.
|
|
|
|
delete ScratchBuf;
|
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) \
|
2006-07-29 11:52:46 +08:00
|
|
|
AddKeyword(#NAME, tok::kw_ ## NAME, \
|
2006-08-04 14:11:48 +08:00
|
|
|
((FLAGS) >> C90Shift) & Mask, \
|
|
|
|
((FLAGS) >> C99Shift) & Mask, \
|
|
|
|
((FLAGS) >> CPPShift) & Mask);
|
2006-06-18 13:43:12 +08:00
|
|
|
#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) {
|
2006-06-18 14:48:37 +08:00
|
|
|
Diags.Report(Loc, 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]";
|
2006-07-27 14:59:25 +08:00
|
|
|
if (Tok.isExpandDisabled())
|
|
|
|
std::cerr << " [ExpandDisabled]";
|
2006-06-19 00:22:51 +08:00
|
|
|
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";
|
2006-07-04 15:26:10 +08:00
|
|
|
std::cerr << " " << NumMultiIncludeFileOptzn << " #includes skipped due to"
|
|
|
|
<< " the multi-include optimization.\n";
|
2006-06-18 13:43:12 +08:00
|
|
|
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";
|
|
|
|
|
2006-07-09 08:45:31 +08:00
|
|
|
std::cerr << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
|
|
|
|
<< NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
|
2006-06-18 13:43:12 +08:00
|
|
|
<< NumFastMacroExpanded << " on the fast path.\n";
|
2006-07-20 12:47:30 +08:00
|
|
|
std::cerr << (NumFastTokenPaste+NumTokenPaste)
|
|
|
|
<< " token paste (##) operations performed, "
|
|
|
|
<< NumFastTokenPaste << " on the fast path.\n";
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
if (!Tok.needsCleaning())
|
|
|
|
return std::string(TokStart, TokStart+Tok.getLength());
|
|
|
|
|
|
|
|
std::string Result;
|
|
|
|
Result.reserve(Tok.getLength());
|
|
|
|
|
2006-07-05 06:33:12 +08:00
|
|
|
// Otherwise, hard case, relex the characters into the string.
|
2006-06-19 00:22:51 +08:00
|
|
|
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.
|
2006-07-05 06:33:12 +08:00
|
|
|
///
|
|
|
|
/// Note that this method may do two possible things: it may either fill in
|
|
|
|
/// the buffer specified with characters, or it may *change the input pointer*
|
|
|
|
/// to point to a constant buffer with the data already in it (avoiding a
|
|
|
|
/// copy). The caller is not allowed to modify the returned buffer pointer
|
|
|
|
/// if an internal buffer is returned.
|
|
|
|
unsigned Preprocessor::getSpelling(const LexerToken &Tok,
|
|
|
|
const char *&Buffer) const {
|
2006-06-19 00:22:51 +08:00
|
|
|
assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
|
|
|
|
|
2006-07-05 07:01:03 +08:00
|
|
|
// If this token is an identifier, just return the string from the identifier
|
|
|
|
// table, which is very quick.
|
|
|
|
if (const IdentifierInfo *II = Tok.getIdentifierInfo()) {
|
|
|
|
Buffer = II->getName();
|
|
|
|
return Tok.getLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, compute the start of the token in the input lexer buffer.
|
2006-06-19 00:32:35 +08:00
|
|
|
const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
|
2006-06-19 00:22:51 +08:00
|
|
|
|
|
|
|
// If this token contains nothing interesting, return it directly.
|
|
|
|
if (!Tok.needsCleaning()) {
|
2006-07-05 06:33:12 +08:00
|
|
|
Buffer = TokStart;
|
|
|
|
return Tok.getLength();
|
2006-06-19 00:22:51 +08:00
|
|
|
}
|
|
|
|
// Otherwise, hard case, relex the characters into the string.
|
2006-07-05 06:33:12 +08:00
|
|
|
char *OutBuf = const_cast<char*>(Buffer);
|
2006-06-19 00:22:51 +08:00
|
|
|
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-07-14 14:54:10 +08:00
|
|
|
|
|
|
|
/// CreateString - Plop the specified string into a scratch buffer and return a
|
|
|
|
/// location for it. If specified, the source location provides a source
|
|
|
|
/// location for the token.
|
|
|
|
SourceLocation Preprocessor::
|
|
|
|
CreateString(const char *Buf, unsigned Len, SourceLocation SLoc) {
|
|
|
|
if (SLoc.isValid())
|
|
|
|
return ScratchBuf->getToken(Buf, Len, SLoc);
|
|
|
|
return ScratchBuf->getToken(Buf, Len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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.
|
2006-07-03 09:01:29 +08:00
|
|
|
// FIXME: Portability. This should be a sys::Path interface, this doesn't
|
|
|
|
// handle things like C:\foo.txt right, nor win32 \\network\device\blah.
|
2006-06-18 13:43:12 +08:00
|
|
|
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-07-03 13:26:05 +08:00
|
|
|
unsigned TheFileID = getCurrentFileLexer()->getCurFileID();
|
|
|
|
const FileEntry *CurFE = SourceMgr.getFileEntryForFileID(TheFileID);
|
2006-06-18 13:43:12 +08:00
|
|
|
if (CurFE) {
|
2006-06-22 13:52:16 +08:00
|
|
|
// Concatenate the requested file onto the directory.
|
2006-07-03 09:01:29 +08:00
|
|
|
// FIXME: Portability. 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.
|
2006-07-03 09:01:29 +08:00
|
|
|
// FIXME: Portability. Adding file to dir should be in sys::Path.
|
|
|
|
std::string SearchDir = SearchDirs[i].getDir()->getName()+"/"+Filename;
|
|
|
|
if (const FileEntry *FE = FileMgr.getFile(SearchDir)) {
|
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;
|
|
|
|
}
|
|
|
|
|
2006-07-03 05:26:45 +08:00
|
|
|
/// isInPrimaryFile - Return true if we're in the top-level file, not in a
|
|
|
|
/// #include.
|
|
|
|
bool Preprocessor::isInPrimaryFile() const {
|
|
|
|
if (CurLexer && !CurLexer->Is_PragmaLexer)
|
2006-07-03 13:16:44 +08:00
|
|
|
return CurLexer->isMainFile();
|
2006-07-03 05:26:45 +08:00
|
|
|
|
2006-07-03 13:16:44 +08:00
|
|
|
// If there are any stacked lexers, we're in a #include.
|
2006-07-03 05:26:45 +08:00
|
|
|
for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i)
|
2006-07-03 13:16:44 +08:00
|
|
|
if (IncludeMacroStack[i].TheLexer &&
|
|
|
|
!IncludeMacroStack[i].TheLexer->Is_PragmaLexer)
|
|
|
|
return IncludeMacroStack[i].TheLexer->isMainFile();
|
|
|
|
return false;
|
2006-07-03 05:26:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// getCurrentLexer - Return the current file lexer being lexed from. Note
|
|
|
|
/// that this ignores any potentially active macro expansions and _Pragma
|
|
|
|
/// expansions going on at the time.
|
|
|
|
Lexer *Preprocessor::getCurrentFileLexer() const {
|
|
|
|
if (CurLexer && !CurLexer->Is_PragmaLexer) return CurLexer;
|
|
|
|
|
|
|
|
// Look for a stacked lexer.
|
|
|
|
for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
|
2006-07-03 13:26:05 +08:00
|
|
|
Lexer *L = IncludeMacroStack[i-1].TheLexer;
|
2006-07-03 05:26:45 +08:00
|
|
|
if (L && !L->Is_PragmaLexer) // Ignore macro & _Pragma expansions.
|
|
|
|
return L;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
/// 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-07-03 13:16:44 +08:00
|
|
|
const DirectoryLookup *CurDir,
|
|
|
|
bool isMainFile) {
|
2006-07-03 04:34:39 +08:00
|
|
|
assert(CurMacroExpander == 0 && "Cannot #include a file inside a macro!");
|
2006-06-18 13:43:12 +08:00
|
|
|
++NumEnteredSourceFiles;
|
|
|
|
|
2006-07-03 04:34:39 +08:00
|
|
|
if (MaxIncludeStackDepth < IncludeMacroStack.size())
|
|
|
|
MaxIncludeStackDepth = IncludeMacroStack.size();
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
const SourceBuffer *Buffer = SourceMgr.getBuffer(FileID);
|
2006-07-03 04:34:39 +08:00
|
|
|
Lexer *TheLexer = new Lexer(Buffer, FileID, *this);
|
2006-07-03 13:16:44 +08:00
|
|
|
if (isMainFile) TheLexer->setIsMainFile();
|
2006-07-03 04:34:39 +08:00
|
|
|
EnterSourceFileWithLexer(TheLexer, CurDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EnterSourceFile - Add a source file to the top of the include stack and
|
|
|
|
/// start lexing tokens from it instead of the current buffer.
|
|
|
|
void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
|
|
|
|
const DirectoryLookup *CurDir) {
|
|
|
|
|
|
|
|
// Add the current lexer to the include stack.
|
|
|
|
if (CurLexer || CurMacroExpander)
|
|
|
|
IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
|
|
|
|
CurMacroExpander));
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-07-03 04:34:39 +08:00
|
|
|
CurLexer = TheLexer;
|
2006-06-22 13:52:16 +08:00
|
|
|
CurDirLookup = CurDir;
|
2006-07-03 04:34:39 +08:00
|
|
|
CurMacroExpander = 0;
|
2006-06-21 14:50:18 +08:00
|
|
|
|
|
|
|
// Notify the client, if desired, that we are in a new source file.
|
2006-07-03 07:00:20 +08:00
|
|
|
if (FileChangeHandler && !CurLexer->Is_PragmaLexer) {
|
2006-06-22 13:52:16 +08:00
|
|
|
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-07-03 06:30:01 +08:00
|
|
|
FileChangeHandler(SourceLocation(CurLexer->getCurFileID(), 0),
|
2006-06-25 12:20:34 +08:00
|
|
|
EnterFile, FileType);
|
2006-06-22 13:52:16 +08:00
|
|
|
}
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
2006-07-03 04:34:39 +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.
|
2006-07-15 15:42:55 +08:00
|
|
|
void Preprocessor::EnterMacro(LexerToken &Tok, MacroArgs *Args) {
|
2006-07-05 01:53:21 +08:00
|
|
|
IdentifierInfo *Identifier = Tok.getIdentifierInfo();
|
2006-07-03 04:34:39 +08:00
|
|
|
IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
|
|
|
|
CurMacroExpander));
|
|
|
|
CurLexer = 0;
|
|
|
|
CurDirLookup = 0;
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-07-15 15:42:55 +08:00
|
|
|
CurMacroExpander = new MacroExpander(Tok, Args, *this);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
2006-07-17 02:16:58 +08:00
|
|
|
/// EnterTokenStream - Add a "macro" context to the top of the include stack,
|
|
|
|
/// which will cause the lexer to start returning the specified tokens. Note
|
|
|
|
/// that these tokens will be re-macro-expanded when/if expansion is enabled.
|
|
|
|
/// This method assumes that the specified stream of tokens has a permanent
|
|
|
|
/// owner somewhere, so they do not need to be copied.
|
2006-07-26 11:50:40 +08:00
|
|
|
void Preprocessor::EnterTokenStream(const LexerToken *Toks, unsigned NumToks) {
|
2006-07-17 02:16:58 +08:00
|
|
|
// Save our current state.
|
|
|
|
IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
|
|
|
|
CurMacroExpander));
|
|
|
|
CurLexer = 0;
|
|
|
|
CurDirLookup = 0;
|
|
|
|
|
|
|
|
// Create a macro expander to expand from the specified token stream.
|
2006-07-26 11:50:40 +08:00
|
|
|
CurMacroExpander = new MacroExpander(Toks, NumToks, *this);
|
2006-07-17 02:16:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
|
|
|
|
/// lexer stack. This should only be used in situations where the current
|
|
|
|
/// state of the top-of-stack lexer is known.
|
|
|
|
void Preprocessor::RemoveTopOfLexerStack() {
|
|
|
|
assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load");
|
|
|
|
delete CurLexer;
|
|
|
|
delete CurMacroExpander;
|
|
|
|
CurLexer = IncludeMacroStack.back().TheLexer;
|
|
|
|
CurDirLookup = IncludeMacroStack.back().TheDirLookup;
|
|
|
|
CurMacroExpander = IncludeMacroStack.back().TheMacroExpander;
|
|
|
|
IncludeMacroStack.pop_back();
|
|
|
|
}
|
|
|
|
|
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 14:49:17 +08:00
|
|
|
/// RegisterBuiltinMacro - Register the specified identifier in the identifier
|
|
|
|
/// table and mark it as a builtin macro to be expanded.
|
2006-07-05 01:53:21 +08:00
|
|
|
IdentifierInfo *Preprocessor::RegisterBuiltinMacro(const char *Name) {
|
2006-06-28 14:49:17 +08:00
|
|
|
// Get the identifier.
|
2006-07-05 01:53:21 +08:00
|
|
|
IdentifierInfo *Id = getIdentifierInfo(Name);
|
2006-06-28 14:49:17 +08:00
|
|
|
|
|
|
|
// Mark it as being a macro that is builtin.
|
2006-06-28 13:26:32 +08:00
|
|
|
MacroInfo *MI = new MacroInfo(SourceLocation());
|
|
|
|
MI->setIsBuiltinMacro();
|
2006-06-28 14:49:17 +08:00
|
|
|
Id->setMacroInfo(MI);
|
|
|
|
return Id;
|
|
|
|
}
|
|
|
|
|
2006-06-28 13:26:32 +08:00
|
|
|
|
2006-06-28 14:49:17 +08:00
|
|
|
/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
|
|
|
|
/// identifier table.
|
|
|
|
void Preprocessor::RegisterBuiltinMacros() {
|
|
|
|
Ident__LINE__ = RegisterBuiltinMacro("__LINE__");
|
2006-07-02 06:46:53 +08:00
|
|
|
Ident__FILE__ = RegisterBuiltinMacro("__FILE__");
|
2006-06-30 14:10:41 +08:00
|
|
|
Ident__DATE__ = RegisterBuiltinMacro("__DATE__");
|
|
|
|
Ident__TIME__ = RegisterBuiltinMacro("__TIME__");
|
2006-07-03 04:34:39 +08:00
|
|
|
Ident_Pragma = RegisterBuiltinMacro("_Pragma");
|
2006-07-02 07:16:30 +08:00
|
|
|
|
|
|
|
// GCC Extensions.
|
|
|
|
Ident__BASE_FILE__ = RegisterBuiltinMacro("__BASE_FILE__");
|
|
|
|
Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro("__INCLUDE_LEVEL__");
|
2006-07-02 07:49:16 +08:00
|
|
|
Ident__TIMESTAMP__ = RegisterBuiltinMacro("__TIMESTAMP__");
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
2006-07-09 08:57:04 +08:00
|
|
|
/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
|
|
|
|
/// in its expansion, currently expands to that token literally.
|
2006-07-09 09:00:18 +08:00
|
|
|
static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
|
|
|
|
const IdentifierInfo *MacroIdent) {
|
2006-07-09 08:57:04 +08:00
|
|
|
IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
|
|
|
|
|
|
|
|
// If the token isn't an identifier, it's always literally expanded.
|
|
|
|
if (II == 0) return true;
|
|
|
|
|
|
|
|
// If the identifier is a macro, and if that macro is enabled, it may be
|
|
|
|
// expanded so it's not a trivial expansion.
|
2006-07-09 09:00:18 +08:00
|
|
|
if (II->getMacroInfo() && II->getMacroInfo()->isEnabled() &&
|
|
|
|
// Fast expanding "#define X X" is ok, because X would be disabled.
|
|
|
|
II != MacroIdent)
|
2006-07-09 08:57:04 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// 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.
|
2006-07-29 15:33:01 +08:00
|
|
|
|
2006-07-09 08:57:04 +08:00
|
|
|
return true;
|
2006-07-11 13:04:55 +08:00
|
|
|
}
|
|
|
|
|
2006-07-09 08:57:04 +08:00
|
|
|
|
2006-07-11 12:02:46 +08:00
|
|
|
/// 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.
|
2006-07-11 13:04:55 +08:00
|
|
|
unsigned Val;
|
|
|
|
if (CurLexer)
|
2006-07-11 13:46:12 +08:00
|
|
|
Val = CurLexer->isNextPPTokenLParen();
|
2006-07-11 13:04:55 +08:00
|
|
|
else
|
|
|
|
Val = CurMacroExpander->isNextTokenLParen();
|
|
|
|
|
|
|
|
if (Val == 2) {
|
|
|
|
// If we ran off the end of the lexer or macro expander, walk the include
|
|
|
|
// stack, looking for whatever will return the next token.
|
|
|
|
for (unsigned i = IncludeMacroStack.size(); Val == 2 && i != 0; --i) {
|
|
|
|
IncludeStackInfo &Entry = IncludeMacroStack[i-1];
|
|
|
|
if (Entry.TheLexer)
|
2006-07-11 13:46:12 +08:00
|
|
|
Val = Entry.TheLexer->isNextPPTokenLParen();
|
2006-07-11 13:04:55 +08:00
|
|
|
else
|
|
|
|
Val = Entry.TheMacroExpander->isNextTokenLParen();
|
|
|
|
}
|
2006-07-11 12:02:46 +08:00
|
|
|
}
|
|
|
|
|
2006-07-11 13:04:55 +08:00
|
|
|
// 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.
|
|
|
|
if (Val != 1)
|
|
|
|
return false;
|
2006-07-11 12:02:46 +08:00
|
|
|
|
|
|
|
LexerToken Tok;
|
|
|
|
LexUnexpandedToken(Tok);
|
2006-07-11 13:04:55 +08:00
|
|
|
assert(Tok.getKind() == tok::l_paren && "Error computing l-paren-ness?");
|
|
|
|
return true;
|
2006-07-11 12:02:46 +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'.
|
2006-07-09 08:45:31 +08:00
|
|
|
bool Preprocessor::HandleMacroExpandedIdentifier(LexerToken &Identifier,
|
2006-06-26 14:16:29 +08:00
|
|
|
MacroInfo *MI) {
|
2006-07-09 08:45:31 +08:00
|
|
|
|
|
|
|
// If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
|
|
|
|
if (MI->isBuiltinMacro()) {
|
|
|
|
ExpandBuiltinMacro(Identifier);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-07-15 15:42:55 +08:00
|
|
|
/// Args - If this is a function-like macro expansion, this contains,
|
2006-07-09 08:45:31 +08:00
|
|
|
/// for each macro argument, the list of tokens that were provided to the
|
|
|
|
/// invocation.
|
2006-07-15 15:42:55 +08:00
|
|
|
MacroArgs *Args = 0;
|
2006-07-09 08:45:31 +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
|
|
|
|
// name isn't a '(', this macro should not be expanded.
|
2006-07-11 12:02:46 +08:00
|
|
|
if (!isNextPPTokenLParen())
|
2006-07-09 08:45:31 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// 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.
|
2006-07-15 15:42:55 +08:00
|
|
|
InMacroArgs = true;
|
|
|
|
Args = ReadFunctionLikeMacroArgs(Identifier, MI);
|
2006-07-09 08:45:31 +08:00
|
|
|
|
|
|
|
// Finished parsing args.
|
2006-07-15 15:42:55 +08:00
|
|
|
InMacroArgs = false;
|
2006-07-09 08:45:31 +08:00
|
|
|
|
|
|
|
// If there was an error parsing the arguments, bail out.
|
2006-07-15 15:42:55 +08:00
|
|
|
if (Args == 0) return false;
|
2006-07-09 08:45:31 +08:00
|
|
|
|
|
|
|
++NumFnMacroExpanded;
|
|
|
|
} else {
|
|
|
|
++NumMacroExpanded;
|
|
|
|
}
|
2006-07-03 13:16:44 +08:00
|
|
|
|
|
|
|
// Notice that this macro has been used.
|
|
|
|
MI->setIsUsed(true);
|
2006-07-03 04:34:39 +08:00
|
|
|
|
|
|
|
// If we started lexing a macro, enter the macro expansion body.
|
2006-06-26 14:16:29 +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) {
|
2006-07-15 15:51:24 +08:00
|
|
|
// No need for arg info.
|
2006-07-26 13:22:49 +08:00
|
|
|
if (Args) Args->destroy();
|
2006-07-09 08:45:31 +08:00
|
|
|
|
2006-06-26 14:16:29 +08:00
|
|
|
// 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()) {
|
2006-10-14 13:19:21 +08:00
|
|
|
if (IsAtStartOfLine) Identifier.setFlag(LexerToken::StartOfLine);
|
|
|
|
if (HadLeadingSpace) Identifier.setFlag(LexerToken::LeadingSpace);
|
2006-06-26 14:16:29 +08:00
|
|
|
}
|
|
|
|
++NumFastMacroExpanded;
|
2006-07-09 08:45:31 +08:00
|
|
|
return false;
|
2006-06-26 14:16:29 +08:00
|
|
|
|
2006-07-09 09:00:18 +08:00
|
|
|
} else if (MI->getNumTokens() == 1 &&
|
|
|
|
isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo())){
|
2006-06-26 14:16:29 +08:00
|
|
|
// 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.
|
2006-10-14 13:19:21 +08:00
|
|
|
Identifier.setFlagValue(LexerToken::StartOfLine , isAtStartOfLine);
|
|
|
|
Identifier.setFlagValue(LexerToken::LeadingSpace, hasLeadingSpace);
|
2006-06-26 14:16:29 +08:00
|
|
|
|
|
|
|
// Update the tokens location to include both its logical and physical
|
|
|
|
// locations.
|
|
|
|
SourceLocation Loc =
|
2006-06-30 14:10:41 +08:00
|
|
|
SourceMgr.getInstantiationLoc(Identifier.getLocation(), InstantiateLoc);
|
2006-10-14 13:19:21 +08:00
|
|
|
Identifier.setLocation(Loc);
|
2006-06-26 14:16:29 +08:00
|
|
|
|
2006-07-27 14:59:25 +08:00
|
|
|
// If this is #define X X, we must mark the result as unexpandible.
|
|
|
|
if (IdentifierInfo *NewII = Identifier.getIdentifierInfo())
|
|
|
|
if (NewII->getMacroInfo() == MI)
|
2006-10-14 13:19:21 +08:00
|
|
|
Identifier.setFlag(LexerToken::DisableExpand);
|
2006-07-27 14:59:25 +08:00
|
|
|
|
2006-06-26 14:16:29 +08:00
|
|
|
// Since this is not an identifier token, it can't be macro expanded, so
|
|
|
|
// we're done.
|
|
|
|
++NumFastMacroExpanded;
|
2006-07-09 08:45:31 +08:00
|
|
|
return false;
|
2006-06-26 14:16:29 +08:00
|
|
|
}
|
|
|
|
|
2006-07-09 08:45:31 +08:00
|
|
|
// Start expanding the macro.
|
2006-07-15 15:42:55 +08:00
|
|
|
EnterMacro(Identifier, Args);
|
2006-06-26 14:16:29 +08:00
|
|
|
|
|
|
|
// Now that the macro is at the top of the include stack, ask the
|
|
|
|
// preprocessor to read the next token from it.
|
2006-07-09 08:45:31 +08:00
|
|
|
Lex(Identifier);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-07-15 15:42:55 +08:00
|
|
|
/// ReadFunctionLikeMacroArgs - After reading "MACRO(", this method is
|
2006-07-15 15:51:24 +08:00
|
|
|
/// invoked to read all of the actual arguments specified for the macro
|
2006-07-09 08:45:31 +08:00
|
|
|
/// invocation. This returns null on error.
|
2006-07-15 15:42:55 +08:00
|
|
|
MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(LexerToken &MacroName,
|
|
|
|
MacroInfo *MI) {
|
2006-07-09 08:45:31 +08:00
|
|
|
// The number of fixed arguments to parse.
|
|
|
|
unsigned NumFixedArgsLeft = MI->getNumArgs();
|
|
|
|
bool isVariadic = MI->isVariadic();
|
|
|
|
|
|
|
|
// Outer loop, while there are more arguments, keep reading them.
|
|
|
|
LexerToken Tok;
|
2006-10-14 13:19:21 +08:00
|
|
|
Tok.setKind(tok::comma);
|
2006-07-09 08:45:31 +08:00
|
|
|
--NumFixedArgsLeft; // Start reading the first arg.
|
2006-07-21 14:38:30 +08:00
|
|
|
|
|
|
|
// ArgTokens - Build up a list of tokens that make up each argument. Each
|
2006-07-26 14:26:52 +08:00
|
|
|
// argument is separated by an EOF token. Use a SmallVector so we can avoid
|
|
|
|
// heap allocations in the common case.
|
|
|
|
SmallVector<LexerToken, 64> ArgTokens;
|
2006-07-21 14:38:30 +08:00
|
|
|
|
|
|
|
unsigned NumActuals = 0;
|
2006-07-09 08:45:31 +08:00
|
|
|
while (Tok.getKind() == tok::comma) {
|
|
|
|
// C99 6.10.3p11: Keep track of the number of l_parens we have seen.
|
|
|
|
unsigned NumParens = 0;
|
2006-07-21 14:38:30 +08:00
|
|
|
|
2006-07-09 08:45:31 +08:00
|
|
|
while (1) {
|
2006-07-11 12:02:46 +08:00
|
|
|
// Read arguments as unexpanded tokens. This avoids issues, e.g., where
|
|
|
|
// an argument value in a macro could expand to ',' or '(' or ')'.
|
2006-07-09 08:45:31 +08:00
|
|
|
LexUnexpandedToken(Tok);
|
|
|
|
|
|
|
|
if (Tok.getKind() == tok::eof) {
|
|
|
|
Diag(MacroName, diag::err_unterm_macro_invoc);
|
|
|
|
// Do not lose the EOF. Return it to the client.
|
|
|
|
MacroName = Tok;
|
|
|
|
return 0;
|
|
|
|
} else if (Tok.getKind() == tok::r_paren) {
|
|
|
|
// If we found the ) token, the macro arg list is done.
|
|
|
|
if (NumParens-- == 0)
|
|
|
|
break;
|
|
|
|
} else if (Tok.getKind() == tok::l_paren) {
|
|
|
|
++NumParens;
|
|
|
|
} else if (Tok.getKind() == tok::comma && NumParens == 0) {
|
|
|
|
// Comma ends this argument if there are more fixed arguments expected.
|
|
|
|
if (NumFixedArgsLeft)
|
|
|
|
break;
|
|
|
|
|
2006-07-15 15:51:24 +08:00
|
|
|
// If this is not a variadic macro, too many args were specified.
|
2006-07-09 08:45:31 +08:00
|
|
|
if (!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.
|
2006-07-15 15:51:24 +08:00
|
|
|
Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
|
2006-07-09 08:45:31 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// Otherwise, continue to add the tokens to this variable argument.
|
2006-07-29 14:30:25 +08:00
|
|
|
} else if (Tok.getKind() == tok::comment && !Features.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;
|
2006-07-09 08:45:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ArgTokens.push_back(Tok);
|
|
|
|
}
|
|
|
|
|
2006-07-11 12:09:02 +08:00
|
|
|
// Empty arguments are standard in C99 and supported as an extension in
|
|
|
|
// other modes.
|
|
|
|
if (ArgTokens.empty() && !Features.C99)
|
|
|
|
Diag(Tok, diag::ext_empty_fnmacro_arg);
|
2006-07-11 12:02:46 +08:00
|
|
|
|
2006-07-21 14:38:30 +08:00
|
|
|
// Add a marker EOF token to the end of the token list for this argument.
|
|
|
|
LexerToken EOFTok;
|
2006-10-14 13:19:21 +08:00
|
|
|
EOFTok.startToken();
|
|
|
|
EOFTok.setKind(tok::eof);
|
|
|
|
EOFTok.setLocation(Tok.getLocation());
|
|
|
|
EOFTok.setLength(0);
|
2006-07-21 14:38:30 +08:00
|
|
|
ArgTokens.push_back(EOFTok);
|
|
|
|
++NumActuals;
|
2006-07-09 08:45:31 +08:00
|
|
|
--NumFixedArgsLeft;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Okay, we either found the r_paren. Check to see if we parsed too few
|
|
|
|
// arguments.
|
|
|
|
unsigned MinArgsExpected = MI->getNumArgs();
|
|
|
|
|
2006-07-29 12:39:41 +08:00
|
|
|
// See MacroArgs instance var for description of this.
|
|
|
|
bool isVarargsElided = false;
|
|
|
|
|
2006-07-15 15:51:24 +08:00
|
|
|
if (NumActuals < MinArgsExpected) {
|
2006-07-09 08:45:31 +08:00
|
|
|
// There are several cases where too few arguments is ok, handle them now.
|
2006-07-15 15:51:24 +08:00
|
|
|
if (NumActuals+1 == MinArgsExpected && MI->isVariadic()) {
|
2006-07-09 08:45:31 +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);
|
2006-07-29 12:39:41 +08:00
|
|
|
|
|
|
|
// Remember this occurred if this is a C99 macro invocation with at least
|
|
|
|
// one actual argument.
|
2006-07-30 16:40:43 +08:00
|
|
|
isVarargsElided = MI->isC99Varargs() && MI->getNumArgs() > 1;
|
2006-07-09 08:45:31 +08:00
|
|
|
} else if (MI->getNumArgs() == 1) {
|
|
|
|
// #define A(x)
|
|
|
|
// A()
|
2006-07-29 09:25:12 +08:00
|
|
|
// is ok because it is an empty argument.
|
2006-07-11 12:09:02 +08:00
|
|
|
|
|
|
|
// Empty arguments are standard in C99 and supported as an extension in
|
|
|
|
// other modes.
|
|
|
|
if (ArgTokens.empty() && !Features.C99)
|
|
|
|
Diag(Tok, diag::ext_empty_fnmacro_arg);
|
2006-07-09 08:45:31 +08:00
|
|
|
} else {
|
|
|
|
// Otherwise, emit the error.
|
2006-07-15 15:51:24 +08:00
|
|
|
Diag(Tok, diag::err_too_few_args_in_macro_invoc);
|
2006-07-09 08:45:31 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2006-07-29 09:25:12 +08:00
|
|
|
|
|
|
|
// Add a marker EOF token to the end of the token list for this argument.
|
|
|
|
SourceLocation EndLoc = Tok.getLocation();
|
2006-10-14 13:19:21 +08:00
|
|
|
Tok.startToken();
|
|
|
|
Tok.setKind(tok::eof);
|
|
|
|
Tok.setLocation(EndLoc);
|
|
|
|
Tok.setLength(0);
|
2006-07-29 09:25:12 +08:00
|
|
|
ArgTokens.push_back(Tok);
|
2006-07-09 08:45:31 +08:00
|
|
|
}
|
|
|
|
|
2006-07-29 12:39:41 +08:00
|
|
|
return MacroArgs::create(MI, &ArgTokens[0], ArgTokens.size(),isVarargsElided);
|
2006-06-26 14:16:29 +08:00
|
|
|
}
|
|
|
|
|
2006-06-30 14:10:41 +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,
|
2006-07-14 14:54:10 +08:00
|
|
|
Preprocessor &PP) {
|
2006-06-30 14:10:41 +08:00
|
|
|
time_t TT = time(0);
|
|
|
|
struct tm *TM = localtime(&TT);
|
|
|
|
|
|
|
|
static const char * const Months[] = {
|
|
|
|
"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
|
|
|
|
};
|
|
|
|
|
|
|
|
char TmpBuffer[100];
|
|
|
|
sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
|
|
|
|
TM->tm_year+1900);
|
2006-07-14 14:54:10 +08:00
|
|
|
DATELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
|
2006-06-30 14:10:41 +08:00
|
|
|
|
|
|
|
sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
|
2006-07-14 14:54:10 +08:00
|
|
|
TIMELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
|
2006-06-30 14:10:41 +08:00
|
|
|
}
|
|
|
|
|
2006-06-28 14:49:17 +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'.
|
2006-07-03 04:34:39 +08:00
|
|
|
void Preprocessor::ExpandBuiltinMacro(LexerToken &Tok) {
|
2006-06-28 14:49:17 +08:00
|
|
|
// Figure out which token this is.
|
2006-07-05 01:53:21 +08:00
|
|
|
IdentifierInfo *II = Tok.getIdentifierInfo();
|
|
|
|
assert(II && "Can't be a macro without id info!");
|
2006-06-28 14:49:17 +08:00
|
|
|
|
2006-07-03 04:34:39 +08:00
|
|
|
// If this is an _Pragma directive, expand it, invoke the pragma handler, then
|
|
|
|
// lex the token after it.
|
2006-07-05 01:53:21 +08:00
|
|
|
if (II == Ident_Pragma)
|
2006-07-03 04:34:39 +08:00
|
|
|
return Handle_Pragma(Tok);
|
2006-06-30 14:10:41 +08:00
|
|
|
|
2006-07-09 08:45:31 +08:00
|
|
|
++NumBuiltinMacroExpanded;
|
|
|
|
|
2006-07-03 04:34:39 +08:00
|
|
|
char TmpBuffer[100];
|
|
|
|
|
|
|
|
// Set up the return result.
|
2006-10-14 13:19:21 +08:00
|
|
|
Tok.setIdentifierInfo(0);
|
|
|
|
Tok.clearFlag(LexerToken::NeedsCleaning);
|
2006-07-02 06:46:53 +08:00
|
|
|
|
2006-07-05 01:53:21 +08:00
|
|
|
if (II == Ident__LINE__) {
|
2006-06-28 14:49:17 +08:00
|
|
|
// __LINE__ expands to a simple numeric value.
|
|
|
|
sprintf(TmpBuffer, "%u", SourceMgr.getLineNumber(Tok.getLocation()));
|
|
|
|
unsigned Length = strlen(TmpBuffer);
|
2006-10-14 13:19:21 +08:00
|
|
|
Tok.setKind(tok::numeric_constant);
|
|
|
|
Tok.setLength(Length);
|
|
|
|
Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
|
2006-07-05 01:53:21 +08:00
|
|
|
} else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
|
2006-07-02 07:16:30 +08:00
|
|
|
SourceLocation Loc = Tok.getLocation();
|
2006-07-05 01:53:21 +08:00
|
|
|
if (II == Ident__BASE_FILE__) {
|
2006-07-02 07:16:30 +08:00
|
|
|
Diag(Tok, diag::ext_pp_base_file);
|
|
|
|
SourceLocation NextLoc = SourceMgr.getIncludeLoc(Loc.getFileID());
|
|
|
|
while (NextLoc.getFileID() != 0) {
|
|
|
|
Loc = NextLoc;
|
|
|
|
NextLoc = SourceMgr.getIncludeLoc(Loc.getFileID());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-03 09:07:01 +08:00
|
|
|
// Escape this filename. Turn '\' -> '\\' '"' -> '\"'
|
|
|
|
std::string FN = SourceMgr.getSourceName(Loc);
|
2006-07-15 13:23:31 +08:00
|
|
|
FN = '"' + Lexer::Stringify(FN) + '"';
|
2006-10-14 13:19:21 +08:00
|
|
|
Tok.setKind(tok::string_literal);
|
|
|
|
Tok.setLength(FN.size());
|
|
|
|
Tok.setLocation(CreateString(&FN[0], FN.size(), Tok.getLocation()));
|
2006-07-05 01:53:21 +08:00
|
|
|
} else if (II == Ident__DATE__) {
|
2006-06-30 14:10:41 +08:00
|
|
|
if (!DATELoc.isValid())
|
2006-07-14 14:54:10 +08:00
|
|
|
ComputeDATE_TIME(DATELoc, TIMELoc, *this);
|
2006-10-14 13:19:21 +08:00
|
|
|
Tok.setKind(tok::string_literal);
|
|
|
|
Tok.setLength(strlen("\"Mmm dd yyyy\""));
|
|
|
|
Tok.setLocation(SourceMgr.getInstantiationLoc(DATELoc, Tok.getLocation()));
|
2006-07-05 01:53:21 +08:00
|
|
|
} else if (II == Ident__TIME__) {
|
2006-06-30 14:10:41 +08:00
|
|
|
if (!TIMELoc.isValid())
|
2006-07-14 14:54:10 +08:00
|
|
|
ComputeDATE_TIME(DATELoc, TIMELoc, *this);
|
2006-10-14 13:19:21 +08:00
|
|
|
Tok.setKind(tok::string_literal);
|
|
|
|
Tok.setLength(strlen("\"hh:mm:ss\""));
|
|
|
|
Tok.setLocation(SourceMgr.getInstantiationLoc(TIMELoc, Tok.getLocation()));
|
2006-07-05 01:53:21 +08:00
|
|
|
} else if (II == Ident__INCLUDE_LEVEL__) {
|
2006-07-02 07:16:30 +08:00
|
|
|
Diag(Tok, diag::ext_pp_include_level);
|
|
|
|
|
|
|
|
// Compute the include depth of this token.
|
|
|
|
unsigned Depth = 0;
|
|
|
|
SourceLocation Loc = SourceMgr.getIncludeLoc(Tok.getLocation().getFileID());
|
|
|
|
for (; Loc.getFileID() != 0; ++Depth)
|
|
|
|
Loc = SourceMgr.getIncludeLoc(Loc.getFileID());
|
|
|
|
|
|
|
|
// __INCLUDE_LEVEL__ expands to a simple numeric value.
|
|
|
|
sprintf(TmpBuffer, "%u", Depth);
|
|
|
|
unsigned Length = strlen(TmpBuffer);
|
2006-10-14 13:19:21 +08:00
|
|
|
Tok.setKind(tok::numeric_constant);
|
|
|
|
Tok.setLength(Length);
|
|
|
|
Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
|
2006-07-05 01:53:21 +08:00
|
|
|
} else if (II == Ident__TIMESTAMP__) {
|
2006-07-02 07:49:16 +08:00
|
|
|
// 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.
|
|
|
|
Diag(Tok, diag::ext_pp_timestamp);
|
|
|
|
|
|
|
|
// 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;
|
2006-07-03 05:26:45 +08:00
|
|
|
Lexer *TheLexer = getCurrentFileLexer();
|
2006-07-02 07:49:16 +08:00
|
|
|
|
|
|
|
if (TheLexer)
|
|
|
|
CurFile = SourceMgr.getFileEntryForFileID(TheLexer->getCurFileID());
|
|
|
|
|
|
|
|
// If this file is older than the file it depends on, emit a diagnostic.
|
|
|
|
const char *Result;
|
|
|
|
if (CurFile) {
|
|
|
|
time_t TT = CurFile->getModificationTime();
|
|
|
|
struct tm *TM = localtime(&TT);
|
|
|
|
Result = asctime(TM);
|
|
|
|
} else {
|
|
|
|
Result = "??? ??? ?? ??:??:?? ????\n";
|
|
|
|
}
|
|
|
|
TmpBuffer[0] = '"';
|
|
|
|
strcpy(TmpBuffer+1, Result);
|
|
|
|
unsigned Len = strlen(TmpBuffer);
|
|
|
|
TmpBuffer[Len-1] = '"'; // Replace the newline with a quote.
|
2006-10-14 13:19:21 +08:00
|
|
|
Tok.setKind(tok::string_literal);
|
|
|
|
Tok.setLength(Len);
|
|
|
|
Tok.setLocation(CreateString(TmpBuffer, Len, Tok.getLocation()));
|
2006-06-28 14:49:17 +08:00
|
|
|
} else {
|
|
|
|
assert(0 && "Unknown identifier!");
|
|
|
|
}
|
|
|
|
}
|
2006-06-28 13:26:32 +08:00
|
|
|
|
2006-07-03 13:16:44 +08:00
|
|
|
namespace {
|
|
|
|
struct UnusedIdentifierReporter : public IdentifierVisitor {
|
|
|
|
Preprocessor &PP;
|
|
|
|
UnusedIdentifierReporter(Preprocessor &pp) : PP(pp) {}
|
|
|
|
|
2006-07-05 01:53:21 +08:00
|
|
|
void VisitIdentifier(IdentifierInfo &II) const {
|
|
|
|
if (II.getMacroInfo() && !II.getMacroInfo()->isUsed())
|
|
|
|
PP.Diag(II.getMacroInfo()->getDefinitionLoc(), diag::pp_macro_not_used);
|
2006-07-03 13:16:44 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2006-06-28 13:26:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Lexer Event Handling.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-07-08 16:28:12 +08:00
|
|
|
/// LookUpIdentifierInfo - Given a tok::identifier token, look up the
|
|
|
|
/// identifier information for the token and install it into the token.
|
|
|
|
IdentifierInfo *Preprocessor::LookUpIdentifierInfo(LexerToken &Identifier,
|
|
|
|
const char *BufPtr) {
|
|
|
|
assert(Identifier.getKind() == tok::identifier && "Not an identifier!");
|
|
|
|
assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!");
|
|
|
|
|
|
|
|
// Look up this token, see if it is a macro, or if it is a language keyword.
|
|
|
|
IdentifierInfo *II;
|
|
|
|
if (BufPtr && !Identifier.needsCleaning()) {
|
|
|
|
// No cleaning needed, just use the characters from the lexed buffer.
|
|
|
|
II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength());
|
|
|
|
} else {
|
|
|
|
// Cleaning needed, alloca a buffer, clean into it, then use the buffer.
|
|
|
|
const char *TmpBuf = (char*)alloca(Identifier.getLength());
|
|
|
|
unsigned Size = getSpelling(Identifier, TmpBuf);
|
|
|
|
II = getIdentifierInfo(TmpBuf, TmpBuf+Size);
|
|
|
|
}
|
2006-10-14 13:19:21 +08:00
|
|
|
Identifier.setIdentifierInfo(II);
|
2006-07-08 16:28:12 +08:00
|
|
|
return II;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-28 13:26:32 +08:00
|
|
|
/// 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) {
|
2006-07-20 12:16:23 +08:00
|
|
|
assert(Identifier.getIdentifierInfo() &&
|
|
|
|
"Can't handle identifiers without identifier info!");
|
|
|
|
|
2006-07-05 01:53:21 +08:00
|
|
|
IdentifierInfo &II = *Identifier.getIdentifierInfo();
|
2006-06-28 13:26:32 +08:00
|
|
|
|
|
|
|
// If this identifier was poisoned, and if it was not produced from a macro
|
|
|
|
// expansion, emit an error.
|
2006-07-06 13:17:39 +08:00
|
|
|
if (II.isPoisoned() && CurLexer) {
|
|
|
|
if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning.
|
|
|
|
Diag(Identifier, diag::err_pp_used_poisoned_id);
|
|
|
|
else
|
|
|
|
Diag(Identifier, diag::ext_pp_bad_vaargs_use);
|
|
|
|
}
|
2006-06-28 13:26:32 +08:00
|
|
|
|
2006-07-09 08:45:31 +08:00
|
|
|
// If this is a macro to be expanded, do it.
|
2006-07-05 01:53:21 +08:00
|
|
|
if (MacroInfo *MI = II.getMacroInfo())
|
2006-07-27 14:59:25 +08:00
|
|
|
if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
|
|
|
|
if (MI->isEnabled()) {
|
|
|
|
if (!HandleMacroExpandedIdentifier(Identifier, MI))
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// C99 6.10.3.4p2 says that a disabled macro may never again be
|
|
|
|
// expanded, even if it's in a context where it could be expanded in the
|
|
|
|
// future.
|
2006-10-14 13:19:21 +08:00
|
|
|
Identifier.setFlag(LexerToken::DisableExpand);
|
2006-07-27 14:59:25 +08:00
|
|
|
}
|
|
|
|
}
|
2006-06-28 13:26:32 +08:00
|
|
|
|
|
|
|
// Change the kind of this identifier to the appropriate token kind, e.g.
|
|
|
|
// turning "for" into a keyword.
|
2006-10-14 13:19:21 +08:00
|
|
|
Identifier.setKind(II.getTokenID());
|
2006-06-28 13:26:32 +08:00
|
|
|
|
|
|
|
// If this is an extension token, diagnose its use.
|
2006-07-05 01:53:21 +08:00
|
|
|
if (II.isExtensionToken()) Diag(Identifier, diag::ext_token_used);
|
2006-06-28 13:26:32 +08:00
|
|
|
}
|
|
|
|
|
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-07-18 14:36:12 +08:00
|
|
|
bool Preprocessor::HandleEndOfFile(LexerToken &Result, bool isEndOfMacro) {
|
2006-06-18 13:43:12 +08:00
|
|
|
assert(!CurMacroExpander &&
|
|
|
|
"Ending a file when currently in a macro!");
|
|
|
|
|
2006-07-04 15:11:10 +08:00
|
|
|
// See if this file had a controlling macro.
|
2006-07-04 15:26:10 +08:00
|
|
|
if (CurLexer) { // Not ending a macro, ignore it.
|
2006-07-05 01:53:21 +08:00
|
|
|
if (const IdentifierInfo *ControllingMacro =
|
2006-07-04 15:11:10 +08:00
|
|
|
CurLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
|
2006-07-04 15:26:10 +08:00
|
|
|
// Okay, this has a controlling macro, remember in PerFileInfo.
|
|
|
|
if (const FileEntry *FE =
|
|
|
|
SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
|
|
|
|
getFileInfo(FE).ControllingMacro = ControllingMacro;
|
2006-07-04 15:11:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2006-07-03 04:34:39 +08:00
|
|
|
if (!IncludeMacroStack.empty()) {
|
2006-06-18 13:43:12 +08:00
|
|
|
// We're done with the #included file.
|
2006-07-17 02:16:58 +08:00
|
|
|
RemoveTopOfLexerStack();
|
2006-06-21 14:50:18 +08:00
|
|
|
|
|
|
|
// Notify the client, if desired, that we are in a new source file.
|
2006-07-03 04:34:39 +08:00
|
|
|
if (FileChangeHandler && !isEndOfMacro && CurLexer) {
|
2006-06-22 13:52:16 +08:00
|
|
|
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-07-18 14:36:12 +08:00
|
|
|
|
|
|
|
// Client should lex another token.
|
|
|
|
return false;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
2006-10-14 13:19:21 +08:00
|
|
|
Result.startToken();
|
2006-06-19 00:22:51 +08:00
|
|
|
CurLexer->BufferPtr = CurLexer->BufferEnd;
|
|
|
|
CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
|
2006-10-14 13:19:21 +08:00
|
|
|
Result.setKind(tok::eof);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// We're done with the #included file.
|
|
|
|
delete CurLexer;
|
|
|
|
CurLexer = 0;
|
2006-07-03 13:16:44 +08:00
|
|
|
|
2006-07-10 14:16:26 +08:00
|
|
|
// This is the end of the top-level file. If the diag::pp_macro_not_used
|
|
|
|
// diagnostic is enabled, walk all of the identifiers, looking for macros that
|
|
|
|
// have not been used.
|
|
|
|
if (Diags.getDiagnosticLevel(diag::pp_macro_not_used) != Diagnostic::Ignored)
|
|
|
|
Identifiers.VisitIdentifiers(UnusedIdentifierReporter(*this));
|
2006-07-18 14:36:12 +08:00
|
|
|
|
|
|
|
return true;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// HandleEndOfMacro - This callback is invoked when the lexer hits the end of
|
2006-07-17 02:16:58 +08:00
|
|
|
/// the current macro expansion or token stream expansion.
|
2006-07-18 14:36:12 +08:00
|
|
|
bool Preprocessor::HandleEndOfMacro(LexerToken &Result) {
|
2006-06-18 13:43:12 +08:00
|
|
|
assert(CurMacroExpander && !CurLexer &&
|
|
|
|
"Ending a macro when currently in a #include file!");
|
|
|
|
|
|
|
|
delete CurMacroExpander;
|
|
|
|
|
2006-07-03 04:34:39 +08:00
|
|
|
// Handle this like a #include file being popped off the stack.
|
|
|
|
CurMacroExpander = 0;
|
|
|
|
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
|
2006-07-08 15:01:00 +08:00
|
|
|
/// of the macro line if the macro name is invalid. isDefineUndef is 1 if
|
|
|
|
/// this is due to a a #define, 2 if #undef directive, 0 if it is something
|
2006-07-03 09:27:27 +08:00
|
|
|
/// else (e.g. #ifdef).
|
2006-07-08 15:01:00 +08:00
|
|
|
void Preprocessor::ReadMacroName(LexerToken &MacroNameTok, char isDefineUndef) {
|
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);
|
|
|
|
|
2006-07-05 01:53:21 +08:00
|
|
|
IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
|
|
|
|
if (II == 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) {
|
2006-07-03 09:01:29 +08:00
|
|
|
// FIXME: C++. Error if defining a C++ named operator.
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-07-05 01:53:21 +08:00
|
|
|
} else if (isDefineUndef && II->getName()[0] == 'd' && // defined
|
|
|
|
!strcmp(II->getName()+1, "efined")) {
|
2006-07-03 09:27:27 +08:00
|
|
|
// Error if defining "defined": C99 6.10.8.4.
|
2006-07-03 09:17:59 +08:00
|
|
|
Diag(MacroNameTok, diag::err_defined_macro_name);
|
2006-07-05 01:53:21 +08:00
|
|
|
} else if (isDefineUndef && II->getMacroInfo() &&
|
|
|
|
II->getMacroInfo()->isBuiltinMacro()) {
|
2006-07-03 09:27:27 +08:00
|
|
|
// Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
|
2006-07-08 15:01:00 +08:00
|
|
|
if (isDefineUndef == 1)
|
|
|
|
Diag(MacroNameTok, diag::pp_redef_builtin_macro);
|
|
|
|
else
|
|
|
|
Diag(MacroNameTok, diag::pp_undef_builtin_macro);
|
2006-06-18 13:43:12 +08:00
|
|
|
} 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.
|
2006-10-14 13:19:21 +08:00
|
|
|
MacroNameTok.setKind(tok::eom);
|
2006-06-18 13:43:12 +08:00
|
|
|
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;
|
2006-07-03 04:34:39 +08:00
|
|
|
assert(CurMacroExpander == 0 && CurLexer &&
|
2006-06-18 13:43:12 +08:00
|
|
|
"Lexing a macro, not a file?");
|
|
|
|
|
|
|
|
CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
|
|
|
|
FoundNonSkipPortion, FoundElse);
|
|
|
|
|
2006-07-11 13:39:23 +08:00
|
|
|
// Enter raw mode to disable identifier lookup (and thus macro expansion),
|
|
|
|
// disabling warnings, etc.
|
|
|
|
CurLexer->LexingRawMode = true;
|
2006-06-18 13:43:12 +08:00
|
|
|
LexerToken Tok;
|
|
|
|
while (1) {
|
2006-06-18 14:48:37 +08:00
|
|
|
CurLexer->Lex(Tok);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-07-11 13:04:55 +08:00
|
|
|
// If this is the end of the buffer, we have an error.
|
|
|
|
if (Tok.getKind() == tok::eof) {
|
|
|
|
// Emit errors for each unterminated conditional on the stack, including
|
|
|
|
// the current one.
|
|
|
|
while (!CurLexer->ConditionalStack.empty()) {
|
|
|
|
Diag(CurLexer->ConditionalStack.back().IfLoc,
|
|
|
|
diag::err_pp_unterminated_conditional);
|
|
|
|
CurLexer->ConditionalStack.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just return and let the caller lex after this #include.
|
|
|
|
break;
|
|
|
|
}
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// 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;
|
2006-07-29 14:30:25 +08:00
|
|
|
CurLexer->KeepCommentMode = false;
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// 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;
|
2006-07-29 14:30:25 +08:00
|
|
|
// Restore comment saving mode.
|
|
|
|
CurLexer->KeepCommentMode = Features.KeepComments;
|
2006-06-18 13:43:12 +08:00
|
|
|
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;
|
2006-07-29 14:30:25 +08:00
|
|
|
// Restore comment saving mode.
|
|
|
|
CurLexer->KeepCommentMode = Features.KeepComments;
|
2006-06-18 13:43:12 +08:00
|
|
|
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;
|
2006-07-29 14:30:25 +08:00
|
|
|
// Restore comment saving mode.
|
|
|
|
CurLexer->KeepCommentMode = Features.KeepComments;
|
2006-06-22 14:36:29 +08:00
|
|
|
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 {
|
2006-07-11 13:39:23 +08:00
|
|
|
// Restore the value of LexingRawMode so that identifiers are
|
2006-06-18 13:43:12 +08:00
|
|
|
// looked up, etc, inside the #elif expression.
|
2006-07-11 13:39:23 +08:00
|
|
|
assert(CurLexer->LexingRawMode && "We have to be skipping here!");
|
|
|
|
CurLexer->LexingRawMode = false;
|
2006-07-05 01:53:21 +08:00
|
|
|
IdentifierInfo *IfNDefMacro = 0;
|
2006-07-05 01:42:08 +08:00
|
|
|
ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
|
2006-07-11 13:39:23 +08:00
|
|
|
CurLexer->LexingRawMode = true;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
2006-07-29 14:30:25 +08:00
|
|
|
// Restore comment saving mode.
|
|
|
|
CurLexer->KeepCommentMode = Features.KeepComments;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2006-07-11 13:39:23 +08:00
|
|
|
CurLexer->LexingRawMode = false;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// 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-07-03 09:01:29 +08:00
|
|
|
// FIXME: Traditional: # with whitespace before it not recognized by K&R?
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// 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
|
2006-07-09 08:45:31 +08:00
|
|
|
// EOM token (which terminates the directive).
|
2006-06-18 13:43:12 +08:00
|
|
|
CurLexer->ParsingPreprocessorDirective = true;
|
|
|
|
|
|
|
|
++NumDirectives;
|
|
|
|
|
2006-07-04 15:11:10 +08:00
|
|
|
// We are about to read a token. For the multiple-include optimization FA to
|
|
|
|
// work, we have to remember if we had read any tokens *before* this
|
|
|
|
// pp-directive.
|
|
|
|
bool ReadAnyTokensBeforeDirective = CurLexer->MIOpt.getHasReadAnyTokensVal();
|
|
|
|
|
2006-07-09 08:45:31 +08:00
|
|
|
// Read the next token, the directive flavor. This isn't expanded due to
|
|
|
|
// C99 6.10.3p8.
|
2006-06-18 14:48:37 +08:00
|
|
|
LexUnexpandedToken(Result);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-07-09 08:45:31 +08:00
|
|
|
// C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
|
|
|
|
// #define A(x) #x
|
|
|
|
// A(abc
|
|
|
|
// #warning blah
|
|
|
|
// def)
|
|
|
|
// If so, the user is relying on non-portable behavior, emit a diagnostic.
|
2006-07-15 15:42:55 +08:00
|
|
|
if (InMacroArgs)
|
2006-07-09 08:45:31 +08:00
|
|
|
Diag(Result, diag::ext_embedded_directive);
|
|
|
|
|
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:
|
2006-07-05 01:42:08 +08:00
|
|
|
return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
|
2006-06-18 13:43:12 +08:00
|
|
|
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-07-05 01:42:08 +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-07-04 06:16:27 +08:00
|
|
|
if (Directive[0] == 's' && !strcmp(Directive, "sccs"))
|
|
|
|
return HandleIdentSCCSDirective(Result);
|
2006-06-18 13:43:12 +08:00
|
|
|
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-07-04 15:11:10 +08:00
|
|
|
return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
|
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-07-04 06:16:27 +08:00
|
|
|
return HandleIdentSCCSDirective(Result);
|
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-07-04 15:11:10 +08:00
|
|
|
return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
|
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"))
|
2006-07-03 04:34:39 +08:00
|
|
|
return HandlePragmaDirective();
|
2006-06-25 05:31:03 +08:00
|
|
|
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.
|
2006-07-04 15:11:10 +08:00
|
|
|
DiscardUntilEndOfDirective();
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Okay, we're done parsing the directive.
|
|
|
|
}
|
|
|
|
|
2006-07-04 06:16:27 +08:00
|
|
|
void Preprocessor::HandleUserDiagnosticDirective(LexerToken &Tok,
|
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;
|
2006-07-04 06:16:27 +08:00
|
|
|
return Diag(Tok, DiagID, Message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
|
|
|
|
///
|
|
|
|
void Preprocessor::HandleIdentSCCSDirective(LexerToken &Tok) {
|
2006-07-04 15:11:10 +08:00
|
|
|
// Yes, this directive is an extension.
|
2006-07-04 06:16:27 +08:00
|
|
|
Diag(Tok, diag::ext_pp_ident_directive);
|
|
|
|
|
2006-07-04 15:11:10 +08:00
|
|
|
// Read the string argument.
|
2006-07-04 06:16:27 +08:00
|
|
|
LexerToken StrTok;
|
|
|
|
Lex(StrTok);
|
|
|
|
|
|
|
|
// If the token kind isn't a string, it's a malformed directive.
|
2006-10-06 13:22:26 +08:00
|
|
|
if (StrTok.getKind() != tok::string_literal &&
|
|
|
|
StrTok.getKind() != tok::wide_string_literal)
|
2006-07-04 06:16:27 +08:00
|
|
|
return Diag(StrTok, diag::err_pp_malformed_ident);
|
|
|
|
|
|
|
|
// Verify that there is nothing after the string, other than EOM.
|
|
|
|
CheckEndOfDirective("#ident");
|
|
|
|
|
|
|
|
if (IdentHandler)
|
|
|
|
IdentHandler(Tok.getLocation(), getSpelling(StrTok));
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
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;
|
2006-07-04 15:11:10 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
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.
|
2006-07-03 04:34:39 +08:00
|
|
|
if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1)
|
2006-06-18 13:43:12 +08:00
|
|
|
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
|
|
|
}
|
2006-07-04 15:26:10 +08:00
|
|
|
|
|
|
|
// Next, check to see if the file is wrapped with #ifndef guards. If so, and
|
|
|
|
// if the macro that guards it is defined, we know the #include has no effect.
|
|
|
|
if (FileInfo.ControllingMacro && FileInfo.ControllingMacro->getMacroInfo()) {
|
|
|
|
++NumMultiIncludeFileOptzn;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Look up the file, create a File ID for it.
|
2006-07-04 15:11:10 +08:00
|
|
|
unsigned FileID = 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-07-03 04:34:39 +08:00
|
|
|
if (isInPrimaryFile()) {
|
2006-06-18 13:43:12 +08:00
|
|
|
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-07-08 16:28:12 +08:00
|
|
|
/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
|
|
|
|
/// definition has just been read. Lex the rest of the arguments and the
|
|
|
|
/// closing ), updating MI with what we learn. Return true if an error occurs
|
|
|
|
/// parsing the arg list.
|
|
|
|
bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) {
|
|
|
|
LexerToken Tok;
|
|
|
|
while (1) {
|
|
|
|
LexUnexpandedToken(Tok);
|
|
|
|
switch (Tok.getKind()) {
|
|
|
|
case tok::r_paren:
|
|
|
|
// Found the end of the argument list.
|
2006-07-09 04:32:52 +08:00
|
|
|
if (MI->arg_begin() == MI->arg_end()) return false; // #define FOO()
|
2006-07-08 16:28:12 +08:00
|
|
|
// Otherwise we have #define FOO(A,)
|
|
|
|
Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
|
|
|
|
return true;
|
|
|
|
case tok::ellipsis: // #define X(... -> C99 varargs
|
|
|
|
// Warn if use of C99 feature in non-C99 mode.
|
|
|
|
if (!Features.C99) Diag(Tok, diag::ext_variadic_macro);
|
|
|
|
|
|
|
|
// Lex the token after the identifier.
|
|
|
|
LexUnexpandedToken(Tok);
|
|
|
|
if (Tok.getKind() != tok::r_paren) {
|
|
|
|
Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
|
|
|
|
return true;
|
|
|
|
}
|
2006-07-30 16:40:43 +08:00
|
|
|
// Add the __VA_ARGS__ identifier as an argument.
|
|
|
|
MI->addArgument(Ident__VA_ARGS__);
|
2006-07-08 16:28:12 +08:00
|
|
|
MI->setIsC99Varargs();
|
|
|
|
return false;
|
|
|
|
case tok::eom: // #define X(
|
|
|
|
Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
|
|
|
|
return true;
|
|
|
|
default: // #define X(1
|
|
|
|
Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
|
|
|
|
return true;
|
|
|
|
case tok::identifier:
|
2006-07-09 04:32:52 +08:00
|
|
|
IdentifierInfo *II = Tok.getIdentifierInfo();
|
|
|
|
|
|
|
|
// If this is already used as an argument, it is used multiple times (e.g.
|
|
|
|
// #define X(A,A.
|
2006-07-15 14:55:18 +08:00
|
|
|
if (MI->getArgumentNum(II) != -1) { // C99 6.10.3p6
|
2006-07-09 04:32:52 +08:00
|
|
|
Diag(Tok, diag::err_pp_duplicate_name_in_arg_list, II->getName());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the argument to the macro info.
|
|
|
|
MI->addArgument(II);
|
2006-07-08 16:28:12 +08:00
|
|
|
|
|
|
|
// Lex the token after the identifier.
|
|
|
|
LexUnexpandedToken(Tok);
|
|
|
|
|
|
|
|
switch (Tok.getKind()) {
|
|
|
|
default: // #define X(A B
|
|
|
|
Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
|
|
|
|
return true;
|
|
|
|
case tok::r_paren: // #define X(A)
|
|
|
|
return false;
|
|
|
|
case tok::comma: // #define X(A,
|
|
|
|
break;
|
|
|
|
case tok::ellipsis: // #define X(A... -> GCC extension
|
|
|
|
// Diagnose extension.
|
|
|
|
Diag(Tok, diag::ext_named_variadic_macro);
|
|
|
|
|
|
|
|
// Lex the token after the identifier.
|
|
|
|
LexUnexpandedToken(Tok);
|
|
|
|
if (Tok.getKind() != tok::r_paren) {
|
|
|
|
Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
MI->setIsGNUVarargs();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2006-07-04 15:11:10 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
LexerToken MacroNameTok;
|
2006-07-08 15:01:00 +08:00
|
|
|
ReadMacroName(MacroNameTok, 1);
|
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-07-29 14:30:25 +08:00
|
|
|
// If we are supposed to keep comments in #defines, reenable comment saving
|
|
|
|
// mode.
|
|
|
|
CurLexer->KeepCommentMode = Features.KeepMacroComments;
|
|
|
|
|
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
|
|
|
|
2006-07-09 04:32:52 +08:00
|
|
|
// If this is a function-like macro definition, parse the argument list,
|
|
|
|
// marking each of the identifiers as being used as macro arguments. Also,
|
|
|
|
// check other constraints on the first token of the macro body.
|
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()) {
|
2006-07-08 16:28:12 +08:00
|
|
|
// This is a function-like macro definition. Read the argument list.
|
|
|
|
MI->setIsFunctionLike();
|
|
|
|
if (ReadMacroDefinitionArgList(MI)) {
|
2006-07-09 04:32:52 +08:00
|
|
|
// Forget about MI.
|
2006-07-08 16:28:12 +08:00
|
|
|
delete MI;
|
2006-07-09 04:32:52 +08:00
|
|
|
// Throw away the rest of the line.
|
2006-07-08 16:28:12 +08:00
|
|
|
if (CurLexer->ParsingPreprocessorDirective)
|
|
|
|
DiscardUntilEndOfDirective();
|
|
|
|
return;
|
|
|
|
}
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-07-09 04:48:04 +08:00
|
|
|
// Read the first token after the arg list for down below.
|
|
|
|
LexUnexpandedToken(Tok);
|
2006-06-18 13:43:12 +08:00
|
|
|
} 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.
|
2006-10-14 13:19:21 +08:00
|
|
|
Tok.clearFlag(LexerToken::LeadingSpace);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
2006-07-29 11:46:57 +08:00
|
|
|
// If this is a definition of a variadic C99 function-like macro, not using
|
|
|
|
// the GNU named varargs extension, enabled __VA_ARGS__.
|
|
|
|
|
|
|
|
// "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
|
|
|
|
// This gets unpoisoned where it is allowed.
|
|
|
|
assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
|
|
|
|
if (MI->isC99Varargs())
|
|
|
|
Ident__VA_ARGS__->setIsPoisoned(false);
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Read the rest of the macro body.
|
|
|
|
while (Tok.getKind() != tok::eom) {
|
|
|
|
MI->AddTokenToBody(Tok);
|
2006-07-09 04:48:04 +08:00
|
|
|
|
|
|
|
// Check C99 6.10.3.2p1: ensure that # operators are followed by macro
|
2006-07-11 13:07:29 +08:00
|
|
|
// parameters in function-like macro expansions.
|
|
|
|
if (Tok.getKind() != tok::hash || MI->isObjectLike()) {
|
2006-07-09 04:48:04 +08:00
|
|
|
// Get the next token of the macro.
|
|
|
|
LexUnexpandedToken(Tok);
|
|
|
|
continue;
|
|
|
|
}
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-07-09 04:48:04 +08:00
|
|
|
// Get the next token of the macro.
|
|
|
|
LexUnexpandedToken(Tok);
|
|
|
|
|
|
|
|
// Not a macro arg identifier?
|
2006-07-15 14:55:18 +08:00
|
|
|
if (!Tok.getIdentifierInfo() ||
|
2006-07-30 16:40:43 +08:00
|
|
|
MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
|
2006-07-09 04:48:04 +08:00
|
|
|
Diag(Tok, diag::err_pp_stringize_not_parameter);
|
|
|
|
delete MI;
|
2006-07-29 11:46:57 +08:00
|
|
|
|
|
|
|
// Disable __VA_ARGS__ again.
|
|
|
|
Ident__VA_ARGS__->setIsPoisoned(true);
|
2006-07-09 04:48:04 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Things look ok, add the param name token to the macro.
|
|
|
|
MI->AddTokenToBody(Tok);
|
2006-07-06 12:49:18 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// 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
|
|
|
}
|
2006-07-29 11:46:57 +08:00
|
|
|
|
|
|
|
// Disable __VA_ARGS__ again.
|
|
|
|
Ident__VA_ARGS__->setIsPoisoned(true);
|
2006-07-06 12:49:18 +08:00
|
|
|
|
|
|
|
// Check that there is no paste (##) operator at the begining or end of the
|
|
|
|
// replacement list.
|
2006-07-09 08:45:31 +08:00
|
|
|
unsigned NumTokens = MI->getNumTokens();
|
2006-07-06 12:49:18 +08:00
|
|
|
if (NumTokens != 0) {
|
|
|
|
if (MI->getReplacementToken(0).getKind() == tok::hashhash) {
|
2006-07-09 04:48:04 +08:00
|
|
|
Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
|
2006-07-06 12:49:18 +08:00
|
|
|
delete MI;
|
2006-07-09 04:48:04 +08:00
|
|
|
return;
|
2006-07-06 12:49:18 +08:00
|
|
|
}
|
|
|
|
if (MI->getReplacementToken(NumTokens-1).getKind() == tok::hashhash) {
|
2006-07-09 04:48:04 +08:00
|
|
|
Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
|
2006-07-06 12:49:18 +08:00
|
|
|
delete MI;
|
2006-07-09 04:48:04 +08:00
|
|
|
return;
|
2006-07-06 12:49:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-03 13:16:44 +08:00
|
|
|
// If this is the primary source file, remember that this macro hasn't been
|
|
|
|
// used yet.
|
|
|
|
if (isInPrimaryFile())
|
|
|
|
MI->setIsUsed(false);
|
|
|
|
|
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-07-03 13:16:44 +08:00
|
|
|
if (!OtherMI->isUsed())
|
|
|
|
Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Macros must be identical. This means all tokes and whitespace separation
|
2006-07-08 15:16:08 +08:00
|
|
|
// must be the same. C99 6.10.3.2.
|
|
|
|
if (!MI->isIdenticalTo(*OtherMI, *this)) {
|
2006-07-08 15:01:00 +08:00
|
|
|
Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef,
|
|
|
|
MacroNameTok.getIdentifierInfo()->getName());
|
|
|
|
Diag(OtherMI->getDefinitionLoc(), diag::ext_pp_macro_redef2);
|
|
|
|
}
|
2006-06-18 13:43:12 +08:00
|
|
|
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;
|
2006-07-04 15:11:10 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
LexerToken MacroNameTok;
|
2006-07-08 15:01:00 +08:00
|
|
|
ReadMacroName(MacroNameTok, 2);
|
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
|
|
|
|
2006-07-03 13:16:44 +08:00
|
|
|
if (!MI->isUsed())
|
|
|
|
Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// 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
|
2006-07-04 15:11:10 +08:00
|
|
|
/// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true
|
|
|
|
/// if any tokens have been returned or pp-directives activated before this
|
|
|
|
/// #ifndef has been lexed.
|
2006-06-18 13:43:12 +08:00
|
|
|
///
|
2006-07-04 15:11:10 +08:00
|
|
|
void Preprocessor::HandleIfdefDirective(LexerToken &Result, bool isIfndef,
|
|
|
|
bool ReadAnyTokensBeforeDirective) {
|
2006-06-18 13:43:12 +08:00
|
|
|
++NumIf;
|
|
|
|
LexerToken DirectiveTok = Result;
|
2006-07-04 15:11:10 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
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-07-04 15:11:10 +08:00
|
|
|
CheckEndOfDirective(isIfndef ? "#ifndef" : "#ifdef");
|
|
|
|
|
|
|
|
// If the start of a top-level #ifdef, inform MIOpt.
|
|
|
|
if (!ReadAnyTokensBeforeDirective &&
|
|
|
|
CurLexer->getConditionalStackDepth() == 0) {
|
|
|
|
assert(isIfndef && "#ifdef shouldn't reach here");
|
|
|
|
CurLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo());
|
|
|
|
}
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-07-03 13:42:18 +08:00
|
|
|
MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo();
|
|
|
|
|
|
|
|
// If there is a macro, mark it used.
|
|
|
|
if (MI) MI->setIsUsed(true);
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Should we include the stuff contained by this directive?
|
2006-07-03 13:42:18 +08:00
|
|
|
if (!MI == isIfndef) {
|
2006-06-18 13:43:12 +08:00
|
|
|
// 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-07-05 01:42:08 +08:00
|
|
|
void Preprocessor::HandleIfDirective(LexerToken &IfToken,
|
|
|
|
bool ReadAnyTokensBeforeDirective) {
|
2006-06-18 13:43:12 +08:00
|
|
|
++NumIf;
|
2006-07-04 15:11:10 +08:00
|
|
|
|
|
|
|
// Parse and evaluation the conditional expression.
|
2006-07-05 01:53:21 +08:00
|
|
|
IdentifierInfo *IfNDefMacro = 0;
|
2006-07-05 01:42:08 +08:00
|
|
|
bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Should we include the stuff contained by this directive?
|
|
|
|
if (ConditionalTrue) {
|
2006-07-05 01:42:08 +08:00
|
|
|
// If this condition is equivalent to #ifndef X, and if this is the first
|
|
|
|
// directive seen, handle it for the multiple-include optimization.
|
|
|
|
if (!ReadAnyTokensBeforeDirective &&
|
|
|
|
CurLexer->getConditionalStackDepth() == 0 && IfNDefMacro)
|
|
|
|
CurLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// 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;
|
2006-07-04 15:11:10 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2006-07-04 15:11:10 +08:00
|
|
|
// If this the end of a top-level #endif, inform MIOpt.
|
|
|
|
if (CurLexer->getConditionalStackDepth() == 0)
|
|
|
|
CurLexer->MIOpt.ExitTopLevelConditional();
|
|
|
|
|
2006-07-20 12:31:52 +08:00
|
|
|
assert(!CondInfo.WasSkipping && !CurLexer->LexingRawMode &&
|
2006-06-18 13:43:12 +08:00
|
|
|
"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;
|
2006-07-04 15:11:10 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// #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);
|
2006-07-04 15:11:10 +08:00
|
|
|
|
|
|
|
// If this is a top-level #else, inform the MIOpt.
|
|
|
|
if (CurLexer->getConditionalStackDepth() == 0)
|
|
|
|
CurLexer->MIOpt.FoundTopLevelElse();
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// 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;
|
2006-07-04 15:11:10 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// #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);
|
|
|
|
|
2006-07-04 15:11:10 +08:00
|
|
|
// If this is a top-level #elif, inform the MIOpt.
|
|
|
|
if (CurLexer->getConditionalStackDepth() == 0)
|
|
|
|
CurLexer->MIOpt.FoundTopLevelElse();
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// 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
|
|
|
|