From 2f3e65f35906044e5f7dcd39bcd119e63db29fc7 Mon Sep 17 00:00:00 2001 From: Shafik Yaghmour Date: Mon, 4 Mar 2019 20:25:54 +0000 Subject: [PATCH] [ASTImporter] Handle built-in when importing SourceLocation and FileID Summary: Currently when we see a built-in we try and import the include location. Instead what we do now is find the buffer like we do for the invalid case and copy that over to the to context. Differential Revision: https://reviews.llvm.org/D58743 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@355332 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/ASTImporter.h | 4 +-- lib/AST/ASTImporter.cpp | 47 ++++++++++++++++++--------------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/include/clang/AST/ASTImporter.h b/include/clang/AST/ASTImporter.h index f11944d9f1..ee8194ab3c 100644 --- a/include/clang/AST/ASTImporter.h +++ b/include/clang/AST/ASTImporter.h @@ -337,9 +337,9 @@ class TypeSourceInfo; /// /// \returns The equivalent file ID in the source manager of the "to" /// context, or the import error. - llvm::Expected Import_New(FileID); + llvm::Expected Import_New(FileID, bool IsBuiltin = false); // FIXME: Remove this version. - FileID Import(FileID); + FileID Import(FileID, bool IsBuiltin = false); /// Import the given C++ constructor initializer from the "from" /// context into the "to" context. diff --git a/lib/AST/ASTImporter.cpp b/lib/AST/ASTImporter.cpp index 5ab57ecd31..e2cfe9a347 100644 --- a/lib/AST/ASTImporter.cpp +++ b/lib/AST/ASTImporter.cpp @@ -8229,9 +8229,10 @@ SourceLocation ASTImporter::Import(SourceLocation FromLoc) { return {}; SourceManager &FromSM = FromContext.getSourceManager(); + bool IsBuiltin = FromSM.isWrittenInBuiltinFile(FromLoc); std::pair Decomposed = FromSM.getDecomposedLoc(FromLoc); - FileID ToFileID = Import(Decomposed.first); + FileID ToFileID = Import(Decomposed.first, IsBuiltin); if (ToFileID.isInvalid()) return {}; SourceManager &ToSM = ToContext.getSourceManager(); @@ -8246,13 +8247,13 @@ SourceRange ASTImporter::Import(SourceRange FromRange) { return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd())); } -Expected ASTImporter::Import_New(FileID FromID) { - FileID ToID = Import(FromID); +Expected ASTImporter::Import_New(FileID FromID, bool IsBuiltin) { + FileID ToID = Import(FromID, IsBuiltin); if (ToID.isInvalid() && FromID.isValid()) return llvm::make_error(); return ToID; } -FileID ASTImporter::Import(FileID FromID) { +FileID ASTImporter::Import(FileID FromID, bool IsBuiltin) { llvm::DenseMap::iterator Pos = ImportedFileIDs.find(FromID); if (Pos != ImportedFileIDs.end()) return Pos->second; @@ -8278,25 +8279,29 @@ FileID ASTImporter::Import(FileID FromID) { } ToID = ToSM.getFileID(MLoc); } else { - // Include location of this file. - SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc()); - const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache(); - if (Cache->OrigEntry && Cache->OrigEntry->getDir()) { - // FIXME: We probably want to use getVirtualFile(), so we don't hit the - // disk again - // FIXME: We definitely want to re-use the existing MemoryBuffer, rather - // than mmap the files several times. - const FileEntry *Entry = - ToFileManager.getFile(Cache->OrigEntry->getName()); - // FIXME: The filename may be a virtual name that does probably not - // point to a valid file and we get no Entry here. In this case try with - // the memory buffer below. - if (Entry) - ToID = ToSM.createFileID(Entry, ToIncludeLoc, - FromSLoc.getFile().getFileCharacteristic()); + + if (!IsBuiltin) { + // Include location of this file. + SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc()); + + if (Cache->OrigEntry && Cache->OrigEntry->getDir()) { + // FIXME: We probably want to use getVirtualFile(), so we don't hit the + // disk again + // FIXME: We definitely want to re-use the existing MemoryBuffer, rather + // than mmap the files several times. + const FileEntry *Entry = + ToFileManager.getFile(Cache->OrigEntry->getName()); + // FIXME: The filename may be a virtual name that does probably not + // point to a valid file and we get no Entry here. In this case try with + // the memory buffer below. + if (Entry) + ToID = ToSM.createFileID(Entry, ToIncludeLoc, + FromSLoc.getFile().getFileCharacteristic()); + } } - if (ToID.isInvalid()) { + + if (ToID.isInvalid() || IsBuiltin) { // FIXME: We want to re-use the existing MemoryBuffer! bool Invalid = true; const llvm::MemoryBuffer *FromBuf = Cache->getBuffer( -- 2.50.1