2006-06-18 13:43:12 +08:00
|
|
|
//===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-06-18 13:43:12 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Preprocessor interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Options to support:
|
|
|
|
// -H - Print the name of each header file used.
|
2009-02-06 14:45:26 +08:00
|
|
|
// -d[DNI] - Dump various things.
|
2006-06-18 13:43:12 +08:00
|
|
|
// -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"
|
2006-10-22 15:28:56 +08:00
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
2006-06-18 13:43:12 +08:00
|
|
|
#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"
|
2009-01-29 13:15:15 +08:00
|
|
|
#include "clang/Lex/LexDiagnostic.h"
|
2006-06-18 13:43:12 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2009-02-12 11:26:59 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2006-10-15 03:03:49 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2008-10-06 04:40:30 +08:00
|
|
|
#include "llvm/ADT/APFloat.h"
|
2006-07-26 14:26:52 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2007-07-16 14:48:38 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2009-08-23 20:08:50 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-03-03 06:20:04 +08:00
|
|
|
#include <cstdio>
|
2006-06-18 13:43:12 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-10-14 15:50:21 +08:00
|
|
|
Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
|
2009-09-09 23:08:12 +08:00
|
|
|
TargetInfo &target, SourceManager &SM,
|
2009-01-16 02:47:46 +08:00
|
|
|
HeaderSearch &Headers,
|
|
|
|
IdentifierInfoLookup* IILookup)
|
2009-03-14 05:17:43 +08:00
|
|
|
: Diags(&diags), Features(opts), Target(target),FileMgr(Headers.getFileMgr()),
|
2009-01-16 02:47:46 +08:00
|
|
|
SourceMgr(SM), HeaderInfo(Headers), Identifiers(opts, IILookup),
|
2009-06-17 00:18:48 +08:00
|
|
|
BuiltinInfo(Target), CurPPLexer(0), CurDirLookup(0), Callbacks(0) {
|
2006-06-28 14:49:17 +08:00
|
|
|
ScratchBuf = new ScratchBuffer(SourceMgr);
|
2009-04-13 09:29:17 +08:00
|
|
|
CounterValue = 0; // __COUNTER__ starts at 0.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Clear stats.
|
2006-10-18 13:34:33 +08:00
|
|
|
NumDirectives = NumDefined = NumUndefined = NumPragma = 0;
|
2006-06-18 13:43:12 +08:00
|
|
|
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;
|
2009-09-09 23:08:12 +08:00
|
|
|
MaxIncludeStackDepth = 0;
|
2006-06-18 13:43:12 +08:00
|
|
|
NumSkipped = 0;
|
2006-11-21 14:17:10 +08:00
|
|
|
|
|
|
|
// Default to discarding comments.
|
|
|
|
KeepComments = false;
|
|
|
|
KeepMacroComments = false;
|
2009-09-09 23:08:12 +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;
|
2008-03-09 10:26:03 +08:00
|
|
|
NumCachedTokenLexers = 0;
|
2006-06-21 14:50:18 +08:00
|
|
|
|
2008-08-10 21:15:22 +08:00
|
|
|
CachedLexPos = 0;
|
|
|
|
|
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();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-25 05:31:03 +08:00
|
|
|
// Initialize the pragma handlers.
|
|
|
|
PragmaHandlers = new PragmaNamespace(0);
|
|
|
|
RegisterBuiltinPragmas();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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() {
|
2008-08-23 20:12:06 +08:00
|
|
|
assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!");
|
|
|
|
|
2006-07-03 04:34:39 +08:00
|
|
|
while (!IncludeMacroStack.empty()) {
|
|
|
|
delete IncludeMacroStack.back().TheLexer;
|
2008-03-09 10:26:03 +08:00
|
|
|
delete IncludeMacroStack.back().TheTokenLexer;
|
2006-07-03 04:34:39 +08:00
|
|
|
IncludeMacroStack.pop_back();
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
2007-10-07 16:44:20 +08:00
|
|
|
|
|
|
|
// Free any macro definitions.
|
|
|
|
for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I =
|
|
|
|
Macros.begin(), E = Macros.end(); I != E; ++I) {
|
2008-12-16 03:56:42 +08:00
|
|
|
// We don't need to free the MacroInfo objects directly. These
|
|
|
|
// will be released when the BumpPtrAllocator 'BP' object gets
|
2009-01-19 15:45:44 +08:00
|
|
|
// destroyed. We still need to run the dstor, however, to free
|
|
|
|
// memory alocated by MacroInfo.
|
2009-02-21 06:46:43 +08:00
|
|
|
I->second->Destroy(BP);
|
2007-10-07 16:44:20 +08:00
|
|
|
I->first->setHasMacroDefinition(false);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-07-15 08:25:26 +08:00
|
|
|
// Free any cached macro expanders.
|
2008-03-09 10:26:03 +08:00
|
|
|
for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i)
|
|
|
|
delete TokenLexerCache[i];
|
2009-09-09 23:08: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;
|
2008-03-14 14:07:05 +08:00
|
|
|
|
|
|
|
delete Callbacks;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
2009-02-12 11:26:59 +08:00
|
|
|
void Preprocessor::setPTHManager(PTHManager* pm) {
|
|
|
|
PTH.reset(pm);
|
2009-10-17 02:18:30 +08:00
|
|
|
FileMgr.addStatCache(PTH->createStatCache());
|
2009-02-12 11:26:59 +08:00
|
|
|
}
|
|
|
|
|
2007-07-21 00:59:19 +08:00
|
|
|
void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const {
|
2009-08-23 20:08:50 +08:00
|
|
|
llvm::errs() << tok::getTokenName(Tok.getKind()) << " '"
|
|
|
|
<< getSpelling(Tok) << "'";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-19 00:22:51 +08:00
|
|
|
if (!DumpFlags) return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-23 20:08:50 +08:00
|
|
|
llvm::errs() << "\t";
|
2006-06-19 00:22:51 +08:00
|
|
|
if (Tok.isAtStartOfLine())
|
2009-08-23 20:08:50 +08:00
|
|
|
llvm::errs() << " [StartOfLine]";
|
2006-06-19 00:22:51 +08:00
|
|
|
if (Tok.hasLeadingSpace())
|
2009-08-23 20:08:50 +08:00
|
|
|
llvm::errs() << " [LeadingSpace]";
|
2006-07-27 14:59:25 +08:00
|
|
|
if (Tok.isExpandDisabled())
|
2009-08-23 20:08:50 +08:00
|
|
|
llvm::errs() << " [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());
|
2009-08-23 20:08:50 +08:00
|
|
|
llvm::errs() << " [UnClean='" << std::string(Start, Start+Tok.getLength())
|
|
|
|
<< "']";
|
2006-06-19 00:22:51 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-23 20:08:50 +08:00
|
|
|
llvm::errs() << "\tLoc=<";
|
2007-12-10 04:31:55 +08:00
|
|
|
DumpLocation(Tok.getLocation());
|
2009-08-23 20:08:50 +08:00
|
|
|
llvm::errs() << ">";
|
2007-12-10 04:31:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Preprocessor::DumpLocation(SourceLocation Loc) const {
|
2009-01-27 15:57:44 +08:00
|
|
|
Loc.dump(SourceMgr);
|
2006-06-19 00:22:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Preprocessor::DumpMacro(const MacroInfo &MI) const {
|
2009-08-23 20:08:50 +08:00
|
|
|
llvm::errs() << "MACRO: ";
|
2006-06-19 00:22:51 +08:00
|
|
|
for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
|
|
|
|
DumpToken(MI.getReplacementToken(i));
|
2009-08-23 20:08:50 +08:00
|
|
|
llvm::errs() << " ";
|
2006-06-19 00:22:51 +08:00
|
|
|
}
|
2009-08-23 20:08:50 +08:00
|
|
|
llvm::errs() << "\n";
|
2006-06-19 00:22:51 +08:00
|
|
|
}
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
void Preprocessor::PrintStats() {
|
2009-08-23 20:08:50 +08:00
|
|
|
llvm::errs() << "\n*** Preprocessor Stats:\n";
|
|
|
|
llvm::errs() << NumDirectives << " directives found:\n";
|
|
|
|
llvm::errs() << " " << NumDefined << " #define.\n";
|
|
|
|
llvm::errs() << " " << NumUndefined << " #undef.\n";
|
|
|
|
llvm::errs() << " #include/#include_next/#import:\n";
|
|
|
|
llvm::errs() << " " << NumEnteredSourceFiles << " source files entered.\n";
|
|
|
|
llvm::errs() << " " << MaxIncludeStackDepth << " max include stack depth\n";
|
|
|
|
llvm::errs() << " " << NumIf << " #if/#ifndef/#ifdef.\n";
|
|
|
|
llvm::errs() << " " << NumElse << " #else/#elif.\n";
|
|
|
|
llvm::errs() << " " << NumEndif << " #endif.\n";
|
|
|
|
llvm::errs() << " " << NumPragma << " #pragma.\n";
|
|
|
|
llvm::errs() << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
|
|
|
|
|
|
|
|
llvm::errs() << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
|
2008-01-15 00:44:48 +08:00
|
|
|
<< NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
|
|
|
|
<< NumFastMacroExpanded << " on the fast path.\n";
|
2009-08-23 20:08:50 +08:00
|
|
|
llvm::errs() << (NumFastTokenPaste+NumTokenPaste)
|
2008-01-15 00:44:48 +08:00
|
|
|
<< " 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.
|
2007-07-21 00:59:19 +08:00
|
|
|
std::string Preprocessor::getSpelling(const Token &Tok) const {
|
2006-06-19 00:22:51 +08:00
|
|
|
assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
|
2009-01-27 08:01:05 +08:00
|
|
|
|
2006-06-19 00:22:51 +08:00
|
|
|
// If this token contains nothing interesting, return it directly.
|
2009-01-27 08:01:05 +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());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-19 00:22:51 +08:00
|
|
|
std::string Result;
|
|
|
|
Result.reserve(Tok.getLength());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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.
|
2007-07-21 00:59:19 +08:00
|
|
|
unsigned Preprocessor::getSpelling(const Token &Tok,
|
2006-07-05 06:33:12 +08:00
|
|
|
const char *&Buffer) const {
|
2006-06-19 00:22:51 +08:00
|
|
|
assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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()) {
|
2009-10-19 04:26:12 +08:00
|
|
|
Buffer = II->getNameStart();
|
2009-01-06 03:44:41 +08:00
|
|
|
return II->getLength();
|
2006-07-05 07:01:03 +08:00
|
|
|
}
|
2009-01-08 10:47:16 +08:00
|
|
|
|
2006-07-05 07:01:03 +08:00
|
|
|
// Otherwise, compute the start of the token in the input lexer buffer.
|
2009-01-27 03:29:26 +08:00
|
|
|
const char *TokStart = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-27 03:29:26 +08:00
|
|
|
if (Tok.isLiteral())
|
|
|
|
TokStart = Tok.getLiteralData();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-27 03:29:26 +08:00
|
|
|
if (TokStart == 0)
|
|
|
|
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
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-19 00:22:51 +08:00
|
|
|
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.
|
2009-01-27 03:29:26 +08:00
|
|
|
void Preprocessor::CreateString(const char *Buf, unsigned Len, Token &Tok,
|
|
|
|
SourceLocation InstantiationLoc) {
|
|
|
|
Tok.setLength(Len);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-27 03:29:26 +08:00
|
|
|
const char *DestPtr;
|
|
|
|
SourceLocation Loc = ScratchBuf->getToken(Buf, Len, DestPtr);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-27 03:29:26 +08:00
|
|
|
if (InstantiationLoc.isValid())
|
2009-02-16 04:52:18 +08:00
|
|
|
Loc = SourceMgr.createInstantiationLoc(Loc, InstantiationLoc,
|
|
|
|
InstantiationLoc, Len);
|
2009-01-27 03:29:26 +08:00
|
|
|
Tok.setLocation(Loc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-27 03:29:26 +08:00
|
|
|
// If this is a literal token, set the pointer data.
|
|
|
|
if (Tok.isLiteral())
|
|
|
|
Tok.setLiteralData(DestPtr);
|
2006-07-14 14:54:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-16 14:48:38 +08:00
|
|
|
/// AdvanceToTokenCharacter - Given a location that specifies the start of a
|
|
|
|
/// token, return a new location that specifies a character within the token.
|
2009-09-09 23:08:12 +08:00
|
|
|
SourceLocation Preprocessor::AdvanceToTokenCharacter(SourceLocation TokStart,
|
2007-07-16 14:48:38 +08:00
|
|
|
unsigned CharNo) {
|
2009-01-16 15:36:28 +08:00
|
|
|
// Figure out how many physical characters away the specified instantiation
|
2007-07-16 14:48:38 +08:00
|
|
|
// character is. This needs to take into consideration newlines and
|
|
|
|
// trigraphs.
|
2007-07-21 00:37:10 +08:00
|
|
|
const char *TokPtr = SourceMgr.getCharacterData(TokStart);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-19 06:28:58 +08:00
|
|
|
// If they request the first char of the token, we're trivially done.
|
|
|
|
if (CharNo == 0 && Lexer::isObviouslySimpleCharacter(*TokPtr))
|
|
|
|
return TokStart;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-07-21 00:37:10 +08:00
|
|
|
unsigned PhysOffset = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-07-16 14:48:38 +08:00
|
|
|
// The usual case is that tokens don't contain anything interesting. Skip
|
|
|
|
// over the uninteresting characters. If a token only consists of simple
|
|
|
|
// chars, this method is extremely fast.
|
2009-04-19 06:28:58 +08:00
|
|
|
while (Lexer::isObviouslySimpleCharacter(*TokPtr)) {
|
|
|
|
if (CharNo == 0)
|
|
|
|
return TokStart.getFileLocWithOffset(PhysOffset);
|
2007-07-21 00:37:10 +08:00
|
|
|
++TokPtr, --CharNo, ++PhysOffset;
|
2009-04-19 06:28:58 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-17 15:57:25 +08:00
|
|
|
// If we have a character that may be a trigraph or escaped newline, use a
|
2007-07-16 14:48:38 +08:00
|
|
|
// lexer to parse it correctly.
|
2009-04-19 06:28:58 +08:00
|
|
|
for (; CharNo; --CharNo) {
|
|
|
|
unsigned Size;
|
|
|
|
Lexer::getCharAndSizeNoWarn(TokPtr, Size, Features);
|
|
|
|
TokPtr += Size;
|
|
|
|
PhysOffset += Size;
|
2007-07-16 14:48:38 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-19 06:28:58 +08:00
|
|
|
// Final detail: if we end up on an escaped newline, we want to return the
|
|
|
|
// location of the actual byte of the token. For example foo\<newline>bar
|
|
|
|
// advanced by 3 should return the location of b, not of \\. One compounding
|
|
|
|
// detail of this is that the escape may be made by a trigraph.
|
|
|
|
if (!Lexer::isObviouslySimpleCharacter(*TokPtr))
|
|
|
|
PhysOffset = Lexer::SkipEscapedNewLines(TokPtr)-TokPtr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-07-21 00:37:10 +08:00
|
|
|
return TokStart.getFileLocWithOffset(PhysOffset);
|
2007-07-16 14:48:38 +08:00
|
|
|
}
|
|
|
|
|
2009-02-28 01:53:17 +08:00
|
|
|
/// \brief Computes the source location just past the end of the
|
|
|
|
/// token at this source location.
|
|
|
|
///
|
|
|
|
/// This routine can be used to produce a source location that
|
|
|
|
/// points just past the end of the token referenced by \p Loc, and
|
|
|
|
/// is generally used when a diagnostic needs to point just after a
|
|
|
|
/// token where it expected something different that it received. If
|
|
|
|
/// the returned source location would not be meaningful (e.g., if
|
|
|
|
/// it points into a macro), this routine returns an invalid
|
|
|
|
/// source location.
|
|
|
|
SourceLocation Preprocessor::getLocForEndOfToken(SourceLocation Loc) {
|
|
|
|
if (Loc.isInvalid() || !Loc.isFileID())
|
|
|
|
return SourceLocation();
|
|
|
|
|
2009-04-15 07:22:57 +08:00
|
|
|
unsigned Len = Lexer::MeasureTokenLength(Loc, getSourceManager(), Features);
|
2009-02-28 01:53:17 +08:00
|
|
|
return AdvanceToTokenCharacter(Loc, Len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-16 14:48:38 +08:00
|
|
|
|
2007-10-10 06:10:18 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Preprocessor Initialization Methods
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
|
|
/// EnterMainSourceFile - Enter the specified FileID as the main source file,
|
2008-01-07 12:01:26 +08:00
|
|
|
/// which implicitly adds the builtin defines etc.
|
2007-12-20 06:51:13 +08:00
|
|
|
void Preprocessor::EnterMainSourceFile() {
|
2009-02-14 03:33:24 +08:00
|
|
|
// We do not allow the preprocessor to reenter the main file. Doing so will
|
|
|
|
// cause FileID's to accumulate information from both runs (e.g. #line
|
|
|
|
// information) and predefined macros aren't guaranteed to be set properly.
|
|
|
|
assert(NumEnteredSourceFiles == 0 && "Cannot reenter the main file!");
|
2009-01-17 14:22:33 +08:00
|
|
|
FileID MainFileID = SourceMgr.getMainFileID();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-10 06:10:18 +08:00
|
|
|
// Enter the main file source buffer.
|
|
|
|
EnterSourceFile(MainFileID, 0);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-16 03:07:47 +08:00
|
|
|
// Tell the header info that the main file was entered. If the file is later
|
|
|
|
// #imported, it won't be re-entered.
|
2009-01-17 14:22:33 +08:00
|
|
|
if (const FileEntry *FE = SourceMgr.getFileEntryForID(MainFileID))
|
2007-11-16 03:07:47 +08:00
|
|
|
HeaderInfo.IncrementIncludeCount(FE);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-10 06:10:18 +08:00
|
|
|
std::vector<char> PrologFile;
|
|
|
|
PrologFile.reserve(4080);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-11 05:58:23 +08:00
|
|
|
// FIXME: Don't make a copy.
|
2008-04-20 07:09:31 +08:00
|
|
|
PrologFile.insert(PrologFile.end(), Predefines.begin(), Predefines.end());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-10 06:10:18 +08:00
|
|
|
// Memory buffer must end with a null byte!
|
|
|
|
PrologFile.push_back(0);
|
|
|
|
|
|
|
|
// Now that we have emitted the predefined macros, #includes, etc into
|
|
|
|
// PrologFile, preprocess it to populate the initial preprocessor state.
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::MemoryBuffer *SB =
|
2007-10-10 06:10:18 +08:00
|
|
|
llvm::MemoryBuffer::getMemBufferCopy(&PrologFile.front(),&PrologFile.back(),
|
2009-03-21 04:16:10 +08:00
|
|
|
"<built-in>");
|
2007-10-10 06:10:18 +08:00
|
|
|
assert(SB && "Cannot fail to create predefined source buffer");
|
2009-01-17 14:22:33 +08:00
|
|
|
FileID FID = SourceMgr.createFileIDForMemBuffer(SB);
|
|
|
|
assert(!FID.isInvalid() && "Could not create FileID for predefines?");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-10 06:10:18 +08:00
|
|
|
// Start parsing the predefines.
|
2009-01-17 14:22:33 +08:00
|
|
|
EnterSourceFile(FID, 0);
|
2007-10-10 06:10:18 +08:00
|
|
|
}
|
2007-07-16 14:48:38 +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.
|
2007-07-21 00:59:19 +08:00
|
|
|
IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier,
|
2006-07-08 16:28:12 +08:00
|
|
|
const char *BufPtr) {
|
2007-10-10 02:02:16 +08:00
|
|
|
assert(Identifier.is(tok::identifier) && "Not an identifier!");
|
2006-07-08 16:28:12 +08:00
|
|
|
assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-08 16:28:12 +08:00
|
|
|
// 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.
|
2007-07-14 01:10:38 +08:00
|
|
|
llvm::SmallVector<char, 64> IdentifierBuffer;
|
|
|
|
IdentifierBuffer.resize(Identifier.getLength());
|
|
|
|
const char *TmpBuf = &IdentifierBuffer[0];
|
2006-07-08 16:28:12 +08:00
|
|
|
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').
|
2009-01-21 15:43:11 +08:00
|
|
|
///
|
|
|
|
/// Note that callers of this method are guarded by checking the
|
|
|
|
/// IdentifierInfo's 'isHandleIdentifierCase' bit. If this method changes, the
|
|
|
|
/// IdentifierInfo methods that compute these properties will need to change to
|
|
|
|
/// match.
|
2007-07-21 00:59:19 +08:00
|
|
|
void Preprocessor::HandleIdentifier(Token &Identifier) {
|
2006-07-20 12:16:23 +08:00
|
|
|
assert(Identifier.getIdentifierInfo() &&
|
|
|
|
"Can't handle identifiers without identifier info!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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.
|
2008-11-20 06:43:49 +08:00
|
|
|
if (II.isPoisoned() && CurPPLexer) {
|
2006-07-06 13:17:39 +08:00
|
|
|
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);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-09 08:45:31 +08:00
|
|
|
// If this is a macro to be expanded, do it.
|
2007-10-07 16:44:20 +08:00
|
|
|
if (MacroInfo *MI = getMacroInfo(&II)) {
|
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.
|
2007-07-21 00:59:19 +08:00
|
|
|
Identifier.setFlag(Token::DisableExpand);
|
2006-07-27 14:59:25 +08:00
|
|
|
}
|
|
|
|
}
|
2006-10-15 03:54:15 +08:00
|
|
|
}
|
2006-06-28 13:26:32 +08:00
|
|
|
|
2006-11-22 01:23:33 +08:00
|
|
|
// C++ 2.11p2: If this is an alternative representation of a C++ operator,
|
|
|
|
// then we act as if it is the actual operator and not the textual
|
|
|
|
// representation of it.
|
|
|
|
if (II.isCPlusPlusOperatorKeyword())
|
|
|
|
Identifier.setIdentifierInfo(0);
|
|
|
|
|
2006-06-28 13:26:32 +08:00
|
|
|
// If this is an extension token, diagnose its use.
|
2008-09-03 02:50:17 +08:00
|
|
|
// We avoid diagnosing tokens that originate from macro definitions.
|
2009-04-28 11:59:15 +08:00
|
|
|
// FIXME: This warning is disabled in cases where it shouldn't be,
|
|
|
|
// like "#define TY typeof", "TY(1) x".
|
|
|
|
if (II.isExtensionToken() && !DisableMacroExpansion)
|
2007-06-14 04:44:40 +08:00
|
|
|
Diag(Identifier, diag::ext_token_used);
|
2006-06-28 13:26:32 +08:00
|
|
|
}
|
Add support for retrieving the Doxygen comment associated with a given
declaration in the AST.
The new ASTContext::getCommentForDecl function searches for a comment
that is attached to the given declaration, and returns that comment,
which may be composed of several comment blocks.
Comments are always available in an AST. However, to avoid harming
performance, we don't actually parse the comments. Rather, we keep the
source ranges of all of the comments within a large, sorted vector,
then lazily extract comments via a binary search in that vector only
when needed (which never occurs in a "normal" compile).
Comments are written to a precompiled header/AST file as a blob of
source ranges. That blob is only lazily loaded when one requests a
comment for a declaration (this never occurs in a "normal" compile).
The indexer testbed now supports comment extraction. When the
-point-at location points to a declaration with a Doxygen-style
comment, the indexer testbed prints the associated comment
block(s). See test/Index/comments.c for an example.
Some notes:
- We don't actually attempt to parse the comment blocks themselves,
beyond identifying them as Doxygen comment blocks to associate them
with a declaration.
- We won't find comment blocks that aren't adjacent to the
declaration, because we start our search based on the location of
the declaration.
- We don't go through the necessary hops to find, for example,
whether some redeclaration of a declaration has comments when our
current declaration does not. Similarly, we don't attempt to
associate a \param Foo marker in a function body comment with the
parameter named Foo (although that is certainly possible).
- Verification of my "no performance impact" claims is still "to be
done".
llvm-svn: 74704
2009-07-03 01:08:52 +08:00
|
|
|
|
|
|
|
void Preprocessor::AddCommentHandler(CommentHandler *Handler) {
|
|
|
|
assert(Handler && "NULL comment handler");
|
|
|
|
assert(std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler) ==
|
|
|
|
CommentHandlers.end() && "Comment handler already registered");
|
|
|
|
CommentHandlers.push_back(Handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Preprocessor::RemoveCommentHandler(CommentHandler *Handler) {
|
|
|
|
std::vector<CommentHandler *>::iterator Pos
|
|
|
|
= std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler);
|
|
|
|
assert(Pos != CommentHandlers.end() && "Comment handler not registered");
|
|
|
|
CommentHandlers.erase(Pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Preprocessor::HandleComment(SourceRange Comment) {
|
|
|
|
for (std::vector<CommentHandler *>::iterator H = CommentHandlers.begin(),
|
|
|
|
HEnd = CommentHandlers.end();
|
|
|
|
H != HEnd; ++H)
|
|
|
|
(*H)->HandleComment(*this, Comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
CommentHandler::~CommentHandler() { }
|