]> granicus.if.org Git - clang/commitdiff
When loading a precompiled preamble, use the file ID of the
authorDouglas Gregor <dgregor@apple.com>
Tue, 30 Nov 2010 05:23:00 +0000 (05:23 +0000)
committerDouglas Gregor <dgregor@apple.com>
Tue, 30 Nov 2010 05:23:00 +0000 (05:23 +0000)
precompiled preamble as the "main" source file's file ID within the
source manager. This makes compiling with a precompiled preamble
produce the same source locations as when compiling without the
precompiled preamble; prior to this change, we ended up with different
file IDs for source locations within the precompiled preamble
vs. those after the precompiled preamble, even for entities (e.g.,
preprocessing entities) in the same file.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@120390 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/SourceManager.h
lib/Frontend/CompilerInstance.cpp
lib/Serialization/ASTReader.cpp

index b8811b42c4890967b0730fe453bbc364a131977d..a19272f29dd28402fbb4372fbbe0912b64f4a28e 100644 (file)
@@ -452,6 +452,13 @@ public:
     return MainFileID;
   }
 
+  /// \brief Set the file ID for the precompiled preamble, which is also the
+  /// main file.
+  void SetPreambleFileID(FileID Preamble) {
+    assert(MainFileID.isInvalid() && "MainFileID already set!");
+    MainFileID = Preamble;
+  }
+  
   //===--------------------------------------------------------------------===//
   // Methods to create new FileID's and instantiations.
   //===--------------------------------------------------------------------===//
index b5feadb96dec1dc25e7846a432211446548618de..30c3b62c19157b785a8b40f547ee81b53ca461d7 100644 (file)
@@ -474,8 +474,11 @@ bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile,
                                                FileManager &FileMgr,
                                                SourceManager &SourceMgr,
                                                const FrontendOptions &Opts) {
-  // Figure out where to get and map in the main file.
-  if (InputFile != "-") {
+  // Figure out where to get and map in the main file, unless it's already
+  // been created (e.g., by a precompiled preamble).
+  if (!SourceMgr.getMainFileID().isInvalid()) {
+    // Do nothing: the main file has already been set.
+  } else if (InputFile != "-") {
     const FileEntry *File = FileMgr.getFile(InputFile);
     if (!File) {
       Diags.Report(diag::err_fe_error_reading) << InputFile;
index 86732484bcdc71c270b81d34c59c1978e9a8a887..1cac948d032cdd399edb2abdcff86ae8d1c4dd13 100644 (file)
@@ -2239,6 +2239,18 @@ ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
   if (DeserializationListener)
     DeserializationListener->ReaderInitialized(this);
 
+  // If this AST file is a precompiled preamble, then set the main file ID of 
+  // the source manager to the file source file from which the preamble was
+  // built. This is the only valid way to use a precompiled preamble.
+  if (Type == Preamble) {
+    SourceLocation Loc
+      = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
+    if (Loc.isValid()) {
+      std::pair<FileID, unsigned> Decomposed = SourceMgr.getDecomposedLoc(Loc);
+      SourceMgr.SetPreambleFileID(Decomposed.first);
+    }
+  }
+  
   return Success;
 }