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"
|
2009-12-15 09:51:03 +08:00
|
|
|
#include "MacroArgs.h"
|
2010-01-05 03:18:44 +08:00
|
|
|
#include "clang/Lex/ExternalPreprocessorSource.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"
|
2010-03-20 00:15:56 +08:00
|
|
|
#include "clang/Lex/PreprocessingRecord.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"
|
2010-08-25 03:08:16 +08:00
|
|
|
#include "clang/Lex/CodeCompletionHandler.h"
|
2011-08-27 07:56:07 +08:00
|
|
|
#include "clang/Lex/ModuleLoader.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"
|
2012-02-04 21:45:25 +08:00
|
|
|
#include "llvm/ADT/SmallString.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"
|
2011-07-28 02:41:23 +08:00
|
|
|
#include "llvm/Support/Capacity.h"
|
2006-06-18 13:43:12 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2010-01-05 03:18:44 +08:00
|
|
|
ExternalPreprocessorSource::~ExternalPreprocessorSource() { }
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2011-09-26 07:23:43 +08:00
|
|
|
Preprocessor::Preprocessor(DiagnosticsEngine &diags, LangOptions &opts,
|
2011-09-02 07:39:15 +08:00
|
|
|
const TargetInfo *target, SourceManager &SM,
|
2011-08-27 07:56:07 +08:00
|
|
|
HeaderSearch &Headers, ModuleLoader &TheModuleLoader,
|
2009-11-12 05:44:21 +08:00
|
|
|
IdentifierInfoLookup* IILookup,
|
2011-09-02 07:39:15 +08:00
|
|
|
bool OwnsHeaders,
|
|
|
|
bool DelayInitialization)
|
2009-03-14 05:17:43 +08:00
|
|
|
: Diags(&diags), Features(opts), Target(target),FileMgr(Headers.getFileMgr()),
|
2011-08-27 07:56:07 +08:00
|
|
|
SourceMgr(SM), HeaderInfo(Headers), TheModuleLoader(TheModuleLoader),
|
|
|
|
ExternalSource(0),
|
2011-09-02 07:39:15 +08:00
|
|
|
Identifiers(opts, IILookup), CodeComplete(0),
|
2011-09-04 11:32:15 +08:00
|
|
|
CodeCompletionFile(0), CodeCompletionOffset(0), CodeCompletionReached(0),
|
|
|
|
SkipMainFilePreamble(0, true), CurPPLexer(0),
|
2011-09-26 11:37:43 +08:00
|
|
|
CurDirLookup(0), CurLexerKind(CLK_Lexer), Callbacks(0), MacroArgCache(0),
|
|
|
|
Record(0), MIChainHead(0), MICache(0)
|
2011-09-02 07:39:15 +08:00
|
|
|
{
|
2009-11-12 05:44:21 +08:00
|
|
|
OwnsHeaderSearch = OwnsHeaders;
|
2011-09-02 07:39:15 +08:00
|
|
|
|
|
|
|
if (!DelayInitialization) {
|
|
|
|
assert(Target && "Must provide target information for PP initialization");
|
|
|
|
Initialize(*Target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Preprocessor::~Preprocessor() {
|
|
|
|
assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!");
|
|
|
|
|
|
|
|
while (!IncludeMacroStack.empty()) {
|
|
|
|
delete IncludeMacroStack.back().TheLexer;
|
|
|
|
delete IncludeMacroStack.back().TheTokenLexer;
|
|
|
|
IncludeMacroStack.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free any macro definitions.
|
|
|
|
for (MacroInfoChain *I = MIChainHead ; I ; I = I->Next)
|
|
|
|
I->MI.Destroy();
|
|
|
|
|
|
|
|
// Free any cached macro expanders.
|
|
|
|
for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i)
|
|
|
|
delete TokenLexerCache[i];
|
|
|
|
|
|
|
|
// Free any cached MacroArgs.
|
|
|
|
for (MacroArgs *ArgList = MacroArgCache; ArgList; )
|
|
|
|
ArgList = ArgList->deallocate();
|
|
|
|
|
|
|
|
// Release pragma information.
|
|
|
|
delete PragmaHandlers;
|
|
|
|
|
|
|
|
// Delete the scratch buffer info.
|
|
|
|
delete ScratchBuf;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-09-02 07:39:15 +08:00
|
|
|
// Delete the header search info, if we own it.
|
|
|
|
if (OwnsHeaderSearch)
|
|
|
|
delete &HeaderInfo;
|
|
|
|
|
|
|
|
delete Callbacks;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Preprocessor::Initialize(const TargetInfo &Target) {
|
|
|
|
assert((!this->Target || this->Target == &Target) &&
|
|
|
|
"Invalid override of target information");
|
|
|
|
this->Target = &Target;
|
|
|
|
|
|
|
|
// Initialize information about built-ins.
|
|
|
|
BuiltinInfo.InitializeTarget(Target);
|
|
|
|
|
|
|
|
ScratchBuf = new ScratchBuffer(SourceMgr);
|
|
|
|
CounterValue = 0; // __COUNTER__ starts at 0.
|
|
|
|
|
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;
|
2011-09-02 07:39:15 +08:00
|
|
|
|
2006-11-21 14:17:10 +08:00
|
|
|
// Default to discarding comments.
|
|
|
|
KeepComments = false;
|
|
|
|
KeepMacroComments = false;
|
2011-09-01 02:45:31 +08:00
|
|
|
SuppressIncludeNotFoundError = false;
|
2011-09-02 07:39:15 +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;
|
2011-09-02 07:39:15 +08:00
|
|
|
|
2008-08-10 21:15:22 +08:00
|
|
|
CachedLexPos = 0;
|
2011-09-02 07:39:15 +08:00
|
|
|
|
2010-01-05 03:18:44 +08:00
|
|
|
// We haven't read anything from the external source.
|
|
|
|
ReadMacrosFromExternalSource = false;
|
2011-09-02 07:39:15 +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();
|
2011-04-28 09:08:34 +08:00
|
|
|
SetPoisonReason(Ident__VA_ARGS__,diag::ext_pp_bad_vaargs_use);
|
2011-09-02 07:39:15 +08:00
|
|
|
|
2006-06-25 05:31:03 +08:00
|
|
|
// Initialize the pragma handlers.
|
2011-07-23 18:55:15 +08:00
|
|
|
PragmaHandlers = new PragmaNamespace(StringRef());
|
2006-06-25 05:31:03 +08:00
|
|
|
RegisterBuiltinPragmas();
|
2011-09-02 07:39:15 +08:00
|
|
|
|
2006-06-28 13:26:32 +08:00
|
|
|
// Initialize builtin macros like __LINE__ and friends.
|
|
|
|
RegisterBuiltinMacros();
|
2011-09-02 07:39:15 +08:00
|
|
|
|
2011-04-28 09:08:34 +08:00
|
|
|
if(Features.Borland) {
|
|
|
|
Ident__exception_info = getIdentifierInfo("_exception_info");
|
|
|
|
Ident___exception_info = getIdentifierInfo("__exception_info");
|
|
|
|
Ident_GetExceptionInfo = getIdentifierInfo("GetExceptionInformation");
|
|
|
|
Ident__exception_code = getIdentifierInfo("_exception_code");
|
|
|
|
Ident___exception_code = getIdentifierInfo("__exception_code");
|
|
|
|
Ident_GetExceptionCode = getIdentifierInfo("GetExceptionCode");
|
|
|
|
Ident__abnormal_termination = getIdentifierInfo("_abnormal_termination");
|
|
|
|
Ident___abnormal_termination = getIdentifierInfo("__abnormal_termination");
|
|
|
|
Ident_AbnormalTermination = getIdentifierInfo("AbnormalTermination");
|
|
|
|
} else {
|
|
|
|
Ident__exception_info = Ident__exception_code = Ident__abnormal_termination = 0;
|
|
|
|
Ident___exception_info = Ident___exception_code = Ident___abnormal_termination = 0;
|
|
|
|
Ident_GetExceptionInfo = Ident_GetExceptionCode = Ident_AbnormalTermination = 0;
|
2012-01-30 14:01:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
HeaderInfo.setTarget(Target);
|
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());
|
2011-07-23 18:55:15 +08:00
|
|
|
llvm::errs() << " [UnClean='" << StringRef(Start, Tok.getLength())
|
2009-08-23 20:08:50 +08:00
|
|
|
<< "']";
|
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
|
|
|
}
|
|
|
|
|
2010-03-13 18:17:05 +08:00
|
|
|
Preprocessor::macro_iterator
|
|
|
|
Preprocessor::macro_begin(bool IncludeExternalMacros) const {
|
|
|
|
if (IncludeExternalMacros && ExternalSource &&
|
2010-01-05 03:18:44 +08:00
|
|
|
!ReadMacrosFromExternalSource) {
|
|
|
|
ReadMacrosFromExternalSource = true;
|
|
|
|
ExternalSource->ReadDefinedMacros();
|
|
|
|
}
|
2010-03-13 18:17:05 +08:00
|
|
|
|
|
|
|
return Macros.begin();
|
2010-01-05 03:18:44 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 06:20:04 +08:00
|
|
|
size_t Preprocessor::getTotalMemory() const {
|
2011-07-27 05:17:24 +08:00
|
|
|
return BP.getTotalMemory()
|
2011-07-28 02:41:23 +08:00
|
|
|
+ llvm::capacity_in_bytes(MacroExpandedTokens)
|
2011-07-27 05:17:24 +08:00
|
|
|
+ Predefines.capacity() /* Predefines buffer. */
|
2011-07-28 02:41:23 +08:00
|
|
|
+ llvm::capacity_in_bytes(Macros)
|
|
|
|
+ llvm::capacity_in_bytes(PragmaPushMacroInfo)
|
|
|
|
+ llvm::capacity_in_bytes(PoisonReasons)
|
|
|
|
+ llvm::capacity_in_bytes(CommentHandlers);
|
2011-06-30 06:20:04 +08:00
|
|
|
}
|
|
|
|
|
2010-03-13 18:17:05 +08:00
|
|
|
Preprocessor::macro_iterator
|
|
|
|
Preprocessor::macro_end(bool IncludeExternalMacros) const {
|
|
|
|
if (IncludeExternalMacros && ExternalSource &&
|
2010-01-05 03:18:44 +08:00
|
|
|
!ReadMacrosFromExternalSource) {
|
|
|
|
ReadMacrosFromExternalSource = true;
|
|
|
|
ExternalSource->ReadDefinedMacros();
|
|
|
|
}
|
2010-03-13 18:17:05 +08:00
|
|
|
|
|
|
|
return Macros.end();
|
2010-01-05 03:18:44 +08:00
|
|
|
}
|
|
|
|
|
2012-01-04 14:20:15 +08:00
|
|
|
void Preprocessor::recomputeCurLexerKind() {
|
|
|
|
if (CurLexer)
|
|
|
|
CurLexerKind = CLK_Lexer;
|
|
|
|
else if (CurPTHLexer)
|
|
|
|
CurLexerKind = CLK_PTHLexer;
|
|
|
|
else if (CurTokenLexer)
|
|
|
|
CurLexerKind = CLK_TokenLexer;
|
|
|
|
else
|
|
|
|
CurLexerKind = CLK_CachingLexer;
|
|
|
|
}
|
|
|
|
|
2010-03-13 18:17:05 +08:00
|
|
|
bool Preprocessor::SetCodeCompletionPoint(const FileEntry *File,
|
2011-09-04 11:32:15 +08:00
|
|
|
unsigned CompleteLine,
|
|
|
|
unsigned CompleteColumn) {
|
|
|
|
assert(File);
|
|
|
|
assert(CompleteLine && CompleteColumn && "Starts from 1:1");
|
|
|
|
assert(!CodeCompletionFile && "Already set");
|
2009-12-02 14:49:09 +08:00
|
|
|
|
2011-09-04 11:32:15 +08:00
|
|
|
using llvm::MemoryBuffer;
|
2009-12-02 14:49:09 +08:00
|
|
|
|
|
|
|
// Load the actual file's contents.
|
2010-03-17 03:49:24 +08:00
|
|
|
bool Invalid = false;
|
|
|
|
const MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File, &Invalid);
|
|
|
|
if (Invalid)
|
2009-12-02 14:49:09 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Find the byte position of the truncation point.
|
|
|
|
const char *Position = Buffer->getBufferStart();
|
2011-09-04 11:32:15 +08:00
|
|
|
for (unsigned Line = 1; Line < CompleteLine; ++Line) {
|
2009-12-02 14:49:09 +08:00
|
|
|
for (; *Position; ++Position) {
|
|
|
|
if (*Position != '\r' && *Position != '\n')
|
|
|
|
continue;
|
2010-03-13 18:17:05 +08:00
|
|
|
|
2009-12-02 14:49:09 +08:00
|
|
|
// Eat \r\n or \n\r as a single line.
|
|
|
|
if ((Position[1] == '\r' || Position[1] == '\n') &&
|
|
|
|
Position[0] != Position[1])
|
|
|
|
++Position;
|
|
|
|
++Position;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-03-13 18:17:05 +08:00
|
|
|
|
2011-09-04 11:32:15 +08:00
|
|
|
Position += CompleteColumn - 1;
|
2010-03-13 18:17:05 +08:00
|
|
|
|
2011-09-04 11:32:15 +08:00
|
|
|
// Insert '\0' at the code-completion point.
|
2009-12-09 05:45:46 +08:00
|
|
|
if (Position < Buffer->getBufferEnd()) {
|
2011-09-04 11:32:15 +08:00
|
|
|
CodeCompletionFile = File;
|
|
|
|
CodeCompletionOffset = Position - Buffer->getBufferStart();
|
|
|
|
|
|
|
|
MemoryBuffer *NewBuffer =
|
|
|
|
MemoryBuffer::getNewUninitMemBuffer(Buffer->getBufferSize() + 1,
|
|
|
|
Buffer->getBufferIdentifier());
|
2011-09-05 04:26:28 +08:00
|
|
|
char *NewBuf = const_cast<char*>(NewBuffer->getBufferStart());
|
2011-09-04 11:32:15 +08:00
|
|
|
char *NewPos = std::copy(Buffer->getBufferStart(), Position, NewBuf);
|
|
|
|
*NewPos = '\0';
|
|
|
|
std::copy(Position, Buffer->getBufferEnd(), NewPos+1);
|
|
|
|
SourceMgr.overrideFileContents(File, NewBuffer);
|
2009-12-02 14:49:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-08-26 01:04:25 +08:00
|
|
|
void Preprocessor::CodeCompleteNaturalLanguage() {
|
|
|
|
if (CodeComplete)
|
|
|
|
CodeComplete->CodeCompleteNaturalLanguage();
|
2011-09-04 11:32:15 +08:00
|
|
|
setCodeCompletionReached();
|
2010-08-26 01:04:25 +08:00
|
|
|
}
|
|
|
|
|
2010-02-28 01:05:45 +08:00
|
|
|
/// getSpelling - This method is used to get the spelling of a token into a
|
|
|
|
/// SmallVector. Note that the returned StringRef may not point to the
|
|
|
|
/// supplied buffer if a copy can be avoided.
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Preprocessor::getSpelling(const Token &Tok,
|
|
|
|
SmallVectorImpl<char> &Buffer,
|
2010-03-16 13:20:39 +08:00
|
|
|
bool *Invalid) const {
|
2010-12-22 16:23:18 +08:00
|
|
|
// NOTE: this has to be checked *before* testing for an IdentifierInfo.
|
|
|
|
if (Tok.isNot(tok::raw_identifier)) {
|
|
|
|
// Try the fast path.
|
|
|
|
if (const IdentifierInfo *II = Tok.getIdentifierInfo())
|
|
|
|
return II->getName();
|
|
|
|
}
|
2010-02-28 01:05:45 +08:00
|
|
|
|
|
|
|
// Resize the buffer if we need to copy into it.
|
|
|
|
if (Tok.needsCleaning())
|
|
|
|
Buffer.resize(Tok.getLength());
|
|
|
|
|
|
|
|
const char *Ptr = Buffer.data();
|
2010-03-16 13:20:39 +08:00
|
|
|
unsigned Len = getSpelling(Tok, Ptr, Invalid);
|
2011-07-23 18:55:15 +08:00
|
|
|
return StringRef(Ptr, Len);
|
2010-02-28 01:05:45 +08:00
|
|
|
}
|
|
|
|
|
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,
|
2011-10-04 02:39:03 +08:00
|
|
|
SourceLocation ExpansionLocStart,
|
|
|
|
SourceLocation ExpansionLocEnd) {
|
2009-01-27 03:29:26 +08:00
|
|
|
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
|
|
|
|
2011-10-04 02:39:03 +08:00
|
|
|
if (ExpansionLocStart.isValid())
|
|
|
|
Loc = SourceMgr.createExpansionLoc(Loc, ExpansionLocStart,
|
|
|
|
ExpansionLocEnd, Len);
|
2009-01-27 03:29:26 +08:00
|
|
|
Tok.setLocation(Loc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-12-22 16:23:18 +08:00
|
|
|
// If this is a raw identifier or a literal token, set the pointer data.
|
|
|
|
if (Tok.is(tok::raw_identifier))
|
|
|
|
Tok.setRawIdentifierData(DestPtr);
|
|
|
|
else if (Tok.isLiteral())
|
2009-01-27 03:29:26 +08:00
|
|
|
Tok.setLiteralData(DestPtr);
|
2006-07-14 14:54:10 +08:00
|
|
|
}
|
|
|
|
|
2011-12-02 09:47:07 +08:00
|
|
|
Module *Preprocessor::getCurrentModule() {
|
|
|
|
if (getLangOptions().CurrentModule.empty())
|
|
|
|
return 0;
|
|
|
|
|
2012-01-30 01:08:11 +08:00
|
|
|
return getHeaderSearchInfo().lookupModule(getLangOptions().CurrentModule);
|
2011-12-02 09:47:07 +08:00
|
|
|
}
|
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.
|
2010-04-21 04:35:58 +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
|
|
|
|
2012-01-06 05:36:25 +08:00
|
|
|
// If MainFileID is loaded it means we loaded an AST file, no need to enter
|
|
|
|
// a main file.
|
|
|
|
if (!SourceMgr.isLoadedFileID(MainFileID)) {
|
|
|
|
// Enter the main file source buffer.
|
|
|
|
EnterSourceFile(MainFileID, 0, SourceLocation());
|
Introduce basic support for loading a precompiled preamble while
reparsing an ASTUnit. When saving a preamble, create a buffer larger
than the actual file we're working with but fill everything from the
end of the preamble to the end of the file with spaces (so the lexer
will quickly skip them). When we load the file, create a buffer of the
same size, filling it with the file and then spaces. Then, instruct
the lexer to start lexing after the preamble, therefore continuing the
parse from the spot where the preamble left off.
It's now possible to perform a simple preamble build + parse (+
reparse) with ASTUnit. However, one has to disable a bunch of checking
in the PCH reader to do so. That part isn't committed; it will likely
be handled with some other kind of flag (e.g., -fno-validate-pch).
As part of this, fix some issues with null termination of the memory
buffers created for the preamble; we were trying to explicitly
NULL-terminate them, even though they were also getting implicitly
NULL terminated, leading to excess warnings about NULL characters in
source files.
llvm-svn: 109445
2010-07-27 05:36:20 +08:00
|
|
|
|
2012-01-06 05:36:25 +08:00
|
|
|
// If we've been asked to skip bytes in the main file (e.g., as part of a
|
|
|
|
// precompiled preamble), do so now.
|
|
|
|
if (SkipMainFilePreamble.first > 0)
|
|
|
|
CurLexer->SkipBytes(SkipMainFilePreamble.first,
|
|
|
|
SkipMainFilePreamble.second);
|
|
|
|
|
|
|
|
// Tell the header info that the main file was entered. If the file is later
|
|
|
|
// #imported, it won't be re-entered.
|
|
|
|
if (const FileEntry *FE = SourceMgr.getFileEntryForID(MainFileID))
|
|
|
|
HeaderInfo.IncrementIncludeCount(FE);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-12-31 23:33:09 +08:00
|
|
|
// Preprocess Predefines to populate the initial preprocessor state.
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::MemoryBuffer *SB =
|
2010-04-06 06:42:27 +08:00
|
|
|
llvm::MemoryBuffer::getMemBufferCopy(Predefines, "<built-in>");
|
2010-08-26 22:07:34 +08:00
|
|
|
assert(SB && "Cannot 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.
|
2010-04-21 04:35:58 +08:00
|
|
|
EnterSourceFile(FID, 0, SourceLocation());
|
2007-10-10 06:10:18 +08:00
|
|
|
}
|
2007-07-16 14:48:38 +08:00
|
|
|
|
2010-03-23 13:09:10 +08:00
|
|
|
void Preprocessor::EndSourceFile() {
|
|
|
|
// Notify the client that we reached the end of the source file.
|
|
|
|
if (Callbacks)
|
|
|
|
Callbacks->EndOfMainFile();
|
|
|
|
}
|
2006-06-28 13:26:32 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Lexer Event Handling.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-12-22 16:23:18 +08:00
|
|
|
/// LookUpIdentifierInfo - Given a tok::raw_identifier token, look up the
|
|
|
|
/// identifier information for the token and install it into the token,
|
|
|
|
/// updating the token kind accordingly.
|
|
|
|
IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier) const {
|
|
|
|
assert(Identifier.getRawIdentifierData() != 0 && "No raw identifier data!");
|
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;
|
2010-12-22 16:23:18 +08:00
|
|
|
if (!Identifier.needsCleaning()) {
|
2006-07-08 16:28:12 +08:00
|
|
|
// No cleaning needed, just use the characters from the lexed buffer.
|
2011-07-23 18:55:15 +08:00
|
|
|
II = getIdentifierInfo(StringRef(Identifier.getRawIdentifierData(),
|
2010-12-22 16:23:18 +08:00
|
|
|
Identifier.getLength()));
|
2006-07-08 16:28:12 +08:00
|
|
|
} else {
|
|
|
|
// Cleaning needed, alloca a buffer, clean into it, then use the buffer.
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<64> IdentifierBuffer;
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef CleanedStr = getSpelling(Identifier, IdentifierBuffer);
|
2010-02-27 21:44:12 +08:00
|
|
|
II = getIdentifierInfo(CleanedStr);
|
2006-07-08 16:28:12 +08:00
|
|
|
}
|
2010-12-22 16:23:18 +08:00
|
|
|
|
|
|
|
// Update the token info (identifier info and appropriate token kind).
|
2006-10-14 13:19:21 +08:00
|
|
|
Identifier.setIdentifierInfo(II);
|
2010-12-22 16:23:18 +08:00
|
|
|
Identifier.setKind(II->getTokenID());
|
|
|
|
|
2006-07-08 16:28:12 +08:00
|
|
|
return II;
|
|
|
|
}
|
|
|
|
|
2011-04-28 09:08:34 +08:00
|
|
|
void Preprocessor::SetPoisonReason(IdentifierInfo *II, unsigned DiagID) {
|
|
|
|
PoisonReasons[II] = DiagID;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Preprocessor::PoisonSEHIdentifiers(bool Poison) {
|
|
|
|
assert(Ident__exception_code && Ident__exception_info);
|
|
|
|
assert(Ident___exception_code && Ident___exception_info);
|
|
|
|
Ident__exception_code->setIsPoisoned(Poison);
|
|
|
|
Ident___exception_code->setIsPoisoned(Poison);
|
|
|
|
Ident_GetExceptionCode->setIsPoisoned(Poison);
|
|
|
|
Ident__exception_info->setIsPoisoned(Poison);
|
|
|
|
Ident___exception_info->setIsPoisoned(Poison);
|
|
|
|
Ident_GetExceptionInfo->setIsPoisoned(Poison);
|
|
|
|
Ident__abnormal_termination->setIsPoisoned(Poison);
|
|
|
|
Ident___abnormal_termination->setIsPoisoned(Poison);
|
|
|
|
Ident_AbnormalTermination->setIsPoisoned(Poison);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Preprocessor::HandlePoisonedIdentifier(Token & Identifier) {
|
|
|
|
assert(Identifier.getIdentifierInfo() &&
|
|
|
|
"Can't handle identifiers without identifier info!");
|
|
|
|
llvm::DenseMap<IdentifierInfo*,unsigned>::const_iterator it =
|
|
|
|
PoisonReasons.find(Identifier.getIdentifierInfo());
|
|
|
|
if(it == PoisonReasons.end())
|
|
|
|
Diag(Identifier, diag::err_pp_used_poisoned_id);
|
|
|
|
else
|
|
|
|
Diag(Identifier,it->second) << Identifier.getIdentifierInfo();
|
|
|
|
}
|
2006-07-08 16:28:12 +08:00
|
|
|
|
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
|
|
|
|
Make the loading of information attached to an IdentifierInfo from an
AST file more lazy, so that we don't eagerly load that information for
all known identifiers each time a new AST file is loaded. The eager
reloading made some sense in the context of precompiled headers, since
very few identifiers were defined before PCH load time. With modules,
however, a huge amount of code can get parsed before we see an
@import, so laziness becomes important here.
The approach taken to make this information lazy is fairly simple:
when we load a new AST file, we mark all of the existing identifiers
as being out-of-date. Whenever we want to access information that may
come from an AST (e.g., whether the identifier has a macro definition,
or what top-level declarations have that name), we check the
out-of-date bit and, if it's set, ask the AST reader to update the
IdentifierInfo from the AST files. The update is a merge, and we now
take care to merge declarations before/after imports with declarations
from multiple imports.
The results of this optimization are fairly dramatic. On a small
application that brings in 14 non-trivial modules, this takes modules
from being > 3x slower than a "perfect" PCH file down to 30% slower
for a full rebuild. A partial rebuild (where the PCH file or modules
can be re-used) is down to 7% slower. Making the PCH file just a
little imperfect (e.g., adding two smallish modules used by a bunch of
.m files that aren't in the PCH file) tips the scales in favor of the
modules approach, with 24% faster partial rebuilds.
This is just a first step; the lazy scheme could possibly be improved
by adding versioning, so we don't search into modules we already
searched. Moreover, we'll need similar lazy schemes for all of the
other lookup data structures, such as DeclContexts.
llvm-svn: 143100
2011-10-27 17:33:13 +08:00
|
|
|
// If the information about this identifier is out of date, update it from
|
|
|
|
// the external source.
|
|
|
|
if (II.isOutOfDate()) {
|
|
|
|
ExternalSource->updateOutOfDateIdentifier(II);
|
|
|
|
Identifier.setKind(II.getTokenID());
|
|
|
|
}
|
|
|
|
|
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) {
|
2011-04-28 09:08:34 +08:00
|
|
|
HandlePoisonedIdentifier(Identifier);
|
2006-07-06 13:17:39 +08:00
|
|
|
}
|
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)) {
|
2012-01-02 06:01:04 +08:00
|
|
|
if (!DisableMacroExpansion) {
|
|
|
|
if (Identifier.isExpandDisabled()) {
|
|
|
|
Diag(Identifier, diag::pp_disabled_macro_expansion);
|
|
|
|
} else if (MI->isEnabled()) {
|
2006-07-27 14:59:25 +08:00
|
|
|
if (!HandleMacroExpandedIdentifier(Identifier, MI))
|
2011-08-27 14:37:51 +08:00
|
|
|
return;
|
2006-07-27 14:59:25 +08:00
|
|
|
} 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);
|
2012-01-02 06:01:04 +08:00
|
|
|
Diag(Identifier, diag::pp_disabled_macro_expansion);
|
2006-07-27 14:59:25 +08:00
|
|
|
}
|
|
|
|
}
|
2006-10-15 03:54:15 +08:00
|
|
|
}
|
2006-06-28 13:26:32 +08:00
|
|
|
|
2011-10-12 03:57:52 +08:00
|
|
|
// If this identifier is a keyword in C++11, produce a warning. Don't warn if
|
|
|
|
// we're not considering macro expansion, since this identifier might be the
|
|
|
|
// name of a macro.
|
|
|
|
// FIXME: This warning is disabled in cases where it shouldn't be, like
|
|
|
|
// "#define constexpr constexpr", "int constexpr;"
|
|
|
|
if (II.isCXX11CompatKeyword() & !DisableMacroExpansion) {
|
|
|
|
Diag(Identifier, diag::warn_cxx11_keyword) << II.getName();
|
|
|
|
// Don't diagnose this keyword again in this translation unit.
|
|
|
|
II.setIsCXX11CompatKeyword(false);
|
|
|
|
}
|
|
|
|
|
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.
|
2010-09-04 01:33:04 +08:00
|
|
|
if (II.isCPlusPlusOperatorKeyword())
|
2006-11-22 01:23:33 +08:00
|
|
|
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);
|
2011-09-08 07:11:54 +08:00
|
|
|
|
2012-03-02 06:07:04 +08:00
|
|
|
// If this is the '__experimental_modules_import' contextual keyword, note
|
|
|
|
// that the next token indicates a module name.
|
2012-01-04 14:20:15 +08:00
|
|
|
//
|
2012-03-02 06:07:04 +08:00
|
|
|
// Note that we do not treat '__experimental_modules_import' as a contextual
|
|
|
|
// keyword when we're in a caching lexer, because caching lexers only get
|
|
|
|
// used in contexts where import declarations are disallowed.
|
|
|
|
if (II.isModulesImport() && !InMacroArgs && !DisableMacroExpansion &&
|
2012-01-04 14:20:15 +08:00
|
|
|
getLangOptions().Modules && CurLexerKind != CLK_CachingLexer) {
|
2011-09-08 07:11:54 +08:00
|
|
|
ModuleImportLoc = Identifier.getLocation();
|
2011-11-30 12:26:53 +08:00
|
|
|
ModuleImportPath.clear();
|
|
|
|
ModuleImportExpectsIdentifier = true;
|
2011-09-08 07:11:54 +08:00
|
|
|
CurLexerKind = CLK_LexAfterModuleImport;
|
|
|
|
}
|
2011-08-27 07:56:07 +08:00
|
|
|
}
|
|
|
|
|
2012-01-04 03:32:59 +08:00
|
|
|
/// \brief Lex a token following the 'import' contextual keyword.
|
2012-01-04 02:04:46 +08:00
|
|
|
///
|
2011-09-08 07:11:54 +08:00
|
|
|
void Preprocessor::LexAfterModuleImport(Token &Result) {
|
|
|
|
// Figure out what kind of lexer we actually have.
|
2012-01-04 14:20:15 +08:00
|
|
|
recomputeCurLexerKind();
|
2011-09-08 07:11:54 +08:00
|
|
|
|
|
|
|
// Lex the next token.
|
|
|
|
Lex(Result);
|
|
|
|
|
2011-08-27 07:56:07 +08:00
|
|
|
// The token sequence
|
|
|
|
//
|
2012-01-04 02:04:46 +08:00
|
|
|
// import identifier (. identifier)*
|
|
|
|
//
|
2012-01-04 03:32:59 +08:00
|
|
|
// indicates a module import directive. We already saw the 'import'
|
|
|
|
// contextual keyword, so now we're looking for the identifiers.
|
2011-11-30 12:26:53 +08:00
|
|
|
if (ModuleImportExpectsIdentifier && Result.getKind() == tok::identifier) {
|
|
|
|
// We expected to see an identifier here, and we did; continue handling
|
|
|
|
// identifiers.
|
|
|
|
ModuleImportPath.push_back(std::make_pair(Result.getIdentifierInfo(),
|
|
|
|
Result.getLocation()));
|
|
|
|
ModuleImportExpectsIdentifier = false;
|
|
|
|
CurLexerKind = CLK_LexAfterModuleImport;
|
2011-08-27 07:56:07 +08:00
|
|
|
return;
|
2011-11-30 12:26:53 +08:00
|
|
|
}
|
2011-08-27 07:56:07 +08:00
|
|
|
|
2011-11-30 12:26:53 +08:00
|
|
|
// If we're expecting a '.' or a ';', and we got a '.', then wait until we
|
|
|
|
// see the next identifier.
|
|
|
|
if (!ModuleImportExpectsIdentifier && Result.getKind() == tok::period) {
|
|
|
|
ModuleImportExpectsIdentifier = true;
|
|
|
|
CurLexerKind = CLK_LexAfterModuleImport;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a non-empty module path, load the named module.
|
|
|
|
if (!ModuleImportPath.empty())
|
2011-12-02 01:11:21 +08:00
|
|
|
(void)TheModuleLoader.loadModule(ModuleImportLoc, ModuleImportPath,
|
2011-12-03 07:42:12 +08:00
|
|
|
Module::MacrosVisible,
|
|
|
|
/*IsIncludeDirective=*/false);
|
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);
|
|
|
|
}
|
|
|
|
|
2010-01-19 06:35:47 +08:00
|
|
|
bool Preprocessor::HandleComment(Token &result, SourceRange Comment) {
|
|
|
|
bool AnyPendingTokens = false;
|
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
|
|
|
for (std::vector<CommentHandler *>::iterator H = CommentHandlers.begin(),
|
|
|
|
HEnd = CommentHandlers.end();
|
2010-01-19 06:35:47 +08:00
|
|
|
H != HEnd; ++H) {
|
|
|
|
if ((*H)->HandleComment(*this, Comment))
|
|
|
|
AnyPendingTokens = true;
|
|
|
|
}
|
|
|
|
if (!AnyPendingTokens || getCommentRetentionState())
|
|
|
|
return false;
|
|
|
|
Lex(result);
|
|
|
|
return true;
|
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
|
|
|
}
|
|
|
|
|
2011-08-27 07:56:07 +08:00
|
|
|
ModuleLoader::~ModuleLoader() { }
|
|
|
|
|
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
|
|
|
CommentHandler::~CommentHandler() { }
|
2010-03-20 00:15:56 +08:00
|
|
|
|
2010-08-25 03:08:16 +08:00
|
|
|
CodeCompletionHandler::~CodeCompletionHandler() { }
|
|
|
|
|
2012-02-25 10:41:16 +08:00
|
|
|
void Preprocessor::createPreprocessingRecord() {
|
2010-03-20 00:15:56 +08:00
|
|
|
if (Record)
|
|
|
|
return;
|
|
|
|
|
2012-02-25 10:41:16 +08:00
|
|
|
Record = new PreprocessingRecord(getSourceManager());
|
2010-03-20 01:12:43 +08:00
|
|
|
addPPCallbacks(Record);
|
2010-03-20 00:15:56 +08:00
|
|
|
}
|