When indexing a module file, for the ppIncludedFile callback give

an invalid location if the location points to the synthetic buffer
for the module input.

llvm-svn: 165592
This commit is contained in:
Argyrios Kyrtzidis 2012-10-10 02:12:47 +00:00
parent 130190ffff
commit e445c7236e
5 changed files with 46 additions and 22 deletions

View File

@ -626,6 +626,10 @@ public:
/// \brief Get the PCH file if one was included.
const FileEntry *getPCHFile();
/// \brief Returns true if the ASTUnit was constructed from a serialized
/// module file.
bool isModuleFile();
llvm::MemoryBuffer *getBufferForFile(StringRef Filename,
std::string *ErrorStr = 0);

View File

@ -105,14 +105,16 @@ public:
/// \brief Receives the language options.
///
/// \returns true to indicate the options are invalid or false otherwise.
virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
virtual bool ReadLanguageOptions(const serialization::ModuleFile &M,
const LangOptions &LangOpts) {
return false;
}
/// \brief Receives the target triple.
///
/// \returns true to indicate the target triple is invalid or false otherwise.
virtual bool ReadTargetTriple(StringRef Triple) {
virtual bool ReadTargetTriple(const serialization::ModuleFile &M,
StringRef Triple) {
return false;
}
@ -138,7 +140,8 @@ public:
virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) {}
/// \brief Receives __COUNTER__ value.
virtual void ReadCounter(unsigned Value) {}
virtual void ReadCounter(const serialization::ModuleFile &M,
unsigned Value) {}
};
/// \brief ASTReaderListener implementation to validate the information of
@ -153,14 +156,16 @@ public:
PCHValidator(Preprocessor &PP, ASTReader &Reader)
: PP(PP), Reader(Reader), NumHeaderInfos(0) {}
virtual bool ReadLanguageOptions(const LangOptions &LangOpts);
virtual bool ReadTargetTriple(StringRef Triple);
virtual bool ReadLanguageOptions(const serialization::ModuleFile &M,
const LangOptions &LangOpts);
virtual bool ReadTargetTriple(const serialization::ModuleFile &M,
StringRef Triple);
virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
StringRef OriginalFileName,
std::string &SuggestedPredefines,
FileManager &FileMgr);
virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID);
virtual void ReadCounter(unsigned Value);
virtual void ReadCounter(const serialization::ModuleFile &M, unsigned Value);
private:
void Error(const char *Msg);
@ -834,7 +839,7 @@ private:
llvm::BitstreamCursor &SLocCursorForID(int ID);
SourceLocation getImportLocation(ModuleFile *F);
ASTReadResult ReadSubmoduleBlock(ModuleFile &F);
bool ParseLanguageOptions(const RecordData &Record);
bool ParseLanguageOptions(const ModuleFile &M, const RecordData &Record);
struct RecordLocation {
RecordLocation(ModuleFile *M, uint64_t O)

View File

@ -512,8 +512,9 @@ public:
Predefines(Predefines), Counter(Counter), NumHeaderInfos(0),
InitializedLanguage(false) {}
virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
if (InitializedLanguage)
virtual bool ReadLanguageOptions(const serialization::ModuleFile &M,
const LangOptions &LangOpts) {
if (InitializedLanguage || M.Kind != serialization::MK_MainFile)
return false;
LangOpt = LangOpts;
@ -530,9 +531,10 @@ public:
return false;
}
virtual bool ReadTargetTriple(StringRef Triple) {
virtual bool ReadTargetTriple(const serialization::ModuleFile &M,
StringRef Triple) {
// If we've already initialized the target, don't do it again.
if (Target)
if (Target || M.Kind != serialization::MK_MainFile)
return false;
// FIXME: This is broken, we should store the TargetOptions in the AST file.
@ -563,7 +565,7 @@ public:
HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
}
virtual void ReadCounter(unsigned Value) {
virtual void ReadCounter(const serialization::ModuleFile &M, unsigned Value) {
Counter = Value;
}
@ -2852,6 +2854,10 @@ const FileEntry *ASTUnit::getPCHFile() {
return 0;
}
bool ASTUnit::isModuleFile() {
return isMainFileAST() && !ASTFileLangOpts.CurrentModule.empty();
}
void ASTUnit::PreambleData::countLines() const {
NumLines = 0;
if (empty())

View File

@ -63,7 +63,8 @@ using namespace clang::serialization::reader;
ASTReaderListener::~ASTReaderListener() {}
bool
PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
PCHValidator::ReadLanguageOptions(const ModuleFile &M,
const LangOptions &LangOpts) {
const LangOptions &PPLangOpts = PP.getLangOpts();
#define LANGOPT(Name, Bits, Default, Description) \
@ -100,7 +101,7 @@ PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
return false;
}
bool PCHValidator::ReadTargetTriple(StringRef Triple) {
bool PCHValidator::ReadTargetTriple(const ModuleFile &M, StringRef Triple) {
if (Triple == PP.getTargetInfo().getTriple().str())
return false;
@ -408,7 +409,7 @@ void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
++NumHeaderInfos;
}
void PCHValidator::ReadCounter(unsigned Value) {
void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
PP.setCounterValue(Value);
}
@ -1828,7 +1829,7 @@ ASTReader::ReadASTBlock(ModuleFile &F) {
RelocatablePCH = Record[4];
if (Listener) {
std::string TargetTriple(BlobStart, BlobLen);
if (Listener->ReadTargetTriple(TargetTriple))
if (Listener->ReadTargetTriple(F, TargetTriple))
return IgnorePCH;
}
break;
@ -1938,7 +1939,7 @@ ASTReader::ReadASTBlock(ModuleFile &F) {
}
case LANGUAGE_OPTIONS:
if (ParseLanguageOptions(Record) && !DisableValidation)
if (ParseLanguageOptions(F, Record) && !DisableValidation)
return IgnorePCH;
break;
@ -2083,7 +2084,7 @@ ASTReader::ReadASTBlock(ModuleFile &F) {
case PP_COUNTER_VALUE:
if (!Record.empty() && Listener)
Listener->ReadCounter(Record[0]);
Listener->ReadCounter(F, Record[0]);
break;
case FILE_SORTED_DECLS:
@ -3430,7 +3431,8 @@ ASTReader::ASTReadResult ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
/// them to the AST listener if one is set.
///
/// \returns true if the listener deems the file unacceptable, false otherwise.
bool ASTReader::ParseLanguageOptions(const RecordData &Record) {
bool ASTReader::ParseLanguageOptions(const ModuleFile &M,
const RecordData &Record) {
if (Listener) {
LangOptions LangOpts;
unsigned Idx = 0;
@ -3447,7 +3449,7 @@ bool ASTReader::ParseLanguageOptions(const RecordData &Record) {
unsigned Length = Record[Idx++];
LangOpts.CurrentModule.assign(Record.begin() + Idx,
Record.begin() + Idx + Length);
return Listener->ReadLanguageOptions(LangOpts);
return Listener->ReadLanguageOptions(M, LangOpts);
}
return false;

View File

@ -450,14 +450,21 @@ static void indexPreprocessingRecord(ASTUnit &Unit, IndexingContext &IdxCtx) {
PreprocessingRecord::iterator I, E;
llvm::tie(I, E) = Unit.getLocalPreprocessingEntities();
bool isModuleFile = Unit.isModuleFile();
for (; I != E; ++I) {
PreprocessedEntity *PPE = *I;
if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) {
if (!ID->importedModule())
IdxCtx.ppIncludedFile(ID->getSourceRange().getBegin(),ID->getFileName(),
if (!ID->importedModule()) {
SourceLocation Loc = ID->getSourceRange().getBegin();
// Modules have synthetic main files as input, give an invalid location
// if the location points to such a file.
if (isModuleFile && Unit.isInMainFileID(Loc))
Loc = SourceLocation();
IdxCtx.ppIncludedFile(Loc, ID->getFileName(),
ID->getFile(), ID->getKind() == InclusionDirective::Import,
!ID->wasInQuotes());
}
}
}
}