From 62288edde26ff4af9fc079c979a0e1bdc577ce9d Mon Sep 17 00:00:00 2001 From: Argyrios Kyrtzidis Date: Wed, 10 Oct 2012 02:12:47 +0000 Subject: [PATCH] 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@165592 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Frontend/ASTUnit.h | 4 ++++ include/clang/Serialization/ASTReader.h | 19 ++++++++++++------- lib/Frontend/ASTUnit.cpp | 16 +++++++++++----- lib/Serialization/ASTReader.cpp | 18 ++++++++++-------- tools/libclang/Indexing.cpp | 11 +++++++++-- 5 files changed, 46 insertions(+), 22 deletions(-) diff --git a/include/clang/Frontend/ASTUnit.h b/include/clang/Frontend/ASTUnit.h index cadf2810b9..d1e0edbeab 100644 --- a/include/clang/Frontend/ASTUnit.h +++ b/include/clang/Frontend/ASTUnit.h @@ -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); diff --git a/include/clang/Serialization/ASTReader.h b/include/clang/Serialization/ASTReader.h index a5fb396a50..c7e3bb8ba9 100644 --- a/include/clang/Serialization/ASTReader.h +++ b/include/clang/Serialization/ASTReader.h @@ -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) diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp index 136e72a6c6..6a4f0eb6d7 100644 --- a/lib/Frontend/ASTUnit.cpp +++ b/lib/Frontend/ASTUnit.cpp @@ -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()) diff --git a/lib/Serialization/ASTReader.cpp b/lib/Serialization/ASTReader.cpp index d060e795de..4e6673bedd 100644 --- a/lib/Serialization/ASTReader.cpp +++ b/lib/Serialization/ASTReader.cpp @@ -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; diff --git a/tools/libclang/Indexing.cpp b/tools/libclang/Indexing.cpp index 6ea87eb95e..d2b0ab3c8d 100644 --- a/tools/libclang/Indexing.cpp +++ b/tools/libclang/Indexing.cpp @@ -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(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()); + } } } } -- 2.40.0