From: Ted Kremenek Date: Thu, 19 Mar 2009 22:10:38 +0000 (+0000) Subject: Store the name of the original file used to generate the PTH file in the PTH X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d5cded40432da737ef85a29a1b9de3a6dcf08793;p=clang Store the name of the original file used to generate the PTH file in the PTH file itself. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67340 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/Driver/CacheTokens.cpp b/Driver/CacheTokens.cpp index cf1ec5ac1a..4ca350e6fb 100644 --- a/Driver/CacheTokens.cpp +++ b/Driver/CacheTokens.cpp @@ -406,7 +406,7 @@ public: PTHWriter(llvm::raw_fd_ostream& out, Preprocessor& pp) : Out(out), PP(pp), idcount(0), CurStrOffset(0) {} - void GeneratePTH(); + void GeneratePTH(const std::string *MainFile = 0); StatSysCallCache *createStatListener() { return new StatListener(PM); @@ -632,7 +632,7 @@ Offset PTHWriter::EmitCachedSpellings() { return SpellingsOff; } -void PTHWriter::GeneratePTH() { +void PTHWriter::GeneratePTH(const std::string *MainFile) { // Generate the prologue. Out << "cfe-pth"; Emit32(PTHManager::Version); @@ -640,6 +640,17 @@ void PTHWriter::GeneratePTH() { // Leave 4 words for the prologue. Offset PrologueOffset = Out.tell(); for (unsigned i = 0; i < 4 * sizeof(uint32_t); ++i) Emit8(0); + + // Write the name of the MainFile. + if (MainFile && MainFile->length() > 0) { + Emit16(MainFile->length()); + EmitBuf(&((*MainFile)[0]), &((*MainFile)[0]) + MainFile->length()); + } + else { + // String with 0 bytes. + Emit16(0); + } + Emit8(0); // Iterate over all the files in SourceManager. Create a lexer // for each file and cache the tokens. @@ -690,6 +701,21 @@ void clang::CacheTokens(Preprocessor& PP, const std::string& OutFile) { llvm::errs() << "PTH error: " << ErrMsg << "\n"; return; } + + // Get the name of the main file. + const SourceManager &SrcMgr = PP.getSourceManager(); + const FileEntry *MainFile = SrcMgr.getFileEntryForID(SrcMgr.getMainFileID()); + llvm::sys::Path MainFilePath(MainFile->getName()); + std::string MainFileName; + + if (!MainFilePath.isAbsolute()) { + llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory(); + P.appendComponent(MainFilePath.toString()); + MainFileName = P.toString(); + } + else { + MainFileName = MainFilePath.toString(); + } // Create the PTHWriter. PTHWriter PW(Out, PP); @@ -703,11 +729,9 @@ void clang::CacheTokens(Preprocessor& PP, const std::string& OutFile) { PP.EnterMainSourceFile(); do { PP.Lex(Tok); } while (Tok.isNot(tok::eof)); - - // Generate the PTH file. PP.getFileManager().setStatCache(0); - PW.GeneratePTH(); + PW.GeneratePTH(&MainFileName); } //===----------------------------------------------------------------------===//