Skip to content

Commit 9f1e81f

Browse files
committed
[ASTImporter] Also import overwritten file buffers
Summary: Overwritten file buffers are at the moment ignored when importing and instead only the underlying file buffer is imported. This patch fixes this by not going to the underlying file entry if the file has an overwritten buffer. Reviewers: martong, a.sidorin, shafik Reviewed By: martong, shafik Subscribers: rnkovacs Differential Revision: https://reviews.llvm.org/D78086
1 parent 03f419f commit 9f1e81f

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

clang/lib/AST/ASTImporter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8560,7 +8560,7 @@ Expected<FileID> ASTImporter::Import(FileID FromID, bool IsBuiltin) {
85608560
} else {
85618561
const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
85628562

8563-
if (!IsBuiltin) {
8563+
if (!IsBuiltin && !Cache->BufferOverridden) {
85648564
// Include location of this file.
85658565
ExpectedSLoc ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
85668566
if (!ToIncludeLoc)

clang/unittests/AST/ASTImporterTest.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "clang/ASTMatchers/ASTMatchers.h"
1414
#include "llvm/ADT/StringMap.h"
15+
#include "llvm/Support/SmallVectorMemoryBuffer.h"
1516

1617
#include "clang/AST/DeclContextInternals.h"
1718
#include "gtest/gtest.h"
@@ -5896,6 +5897,61 @@ TEST_P(ImportSourceLocations, PreserveFileIDTreeStructure) {
58965897
EXPECT_FALSE(ToSM.isBeforeInTranslationUnit(Location2, Location1));
58975898
}
58985899

5900+
TEST_P(ImportSourceLocations, NormalFileBuffer) {
5901+
// Test importing normal file buffers.
5902+
5903+
std::string Path = "input0.c";
5904+
std::string Source = "int X;";
5905+
TranslationUnitDecl *FromTU = getTuDecl(Source, Lang_C, Path);
5906+
5907+
SourceLocation ImportedLoc;
5908+
{
5909+
// Import the VarDecl to trigger the importing of the FileID.
5910+
auto Pattern = varDecl(hasName("X"));
5911+
VarDecl *FromD = FirstDeclMatcher<VarDecl>().match(FromTU, Pattern);
5912+
ImportedLoc = Import(FromD, Lang_C)->getLocation();
5913+
}
5914+
5915+
// Make sure the imported buffer has the original contents.
5916+
SourceManager &ToSM = ToAST->getSourceManager();
5917+
FileID ImportedID = ToSM.getFileID(ImportedLoc);
5918+
EXPECT_EQ(Source, ToSM.getBuffer(ImportedID, SourceLocation())->getBuffer());
5919+
}
5920+
5921+
TEST_P(ImportSourceLocations, OverwrittenFileBuffer) {
5922+
// Test importing overwritten file buffers.
5923+
5924+
std::string Path = "input0.c";
5925+
TranslationUnitDecl *FromTU = getTuDecl("int X;", Lang_C, Path);
5926+
5927+
// Overwrite the file buffer for our input file with new content.
5928+
const std::string Contents = "overwritten contents";
5929+
SourceLocation ImportedLoc;
5930+
{
5931+
SourceManager &FromSM = FromTU->getASTContext().getSourceManager();
5932+
clang::FileManager &FM = FromSM.getFileManager();
5933+
const clang::FileEntry &FE =
5934+
*FM.getVirtualFile(Path, static_cast<off_t>(Contents.size()), 0);
5935+
5936+
llvm::SmallVector<char, 64> Buffer;
5937+
Buffer.append(Contents.begin(), Contents.end());
5938+
auto FileContents =
5939+
std::make_unique<llvm::SmallVectorMemoryBuffer>(std::move(Buffer), Path);
5940+
FromSM.overrideFileContents(&FE, std::move(FileContents));
5941+
5942+
// Import the VarDecl to trigger the importing of the FileID.
5943+
auto Pattern = varDecl(hasName("X"));
5944+
VarDecl *FromD = FirstDeclMatcher<VarDecl>().match(FromTU, Pattern);
5945+
ImportedLoc = Import(FromD, Lang_C)->getLocation();
5946+
}
5947+
5948+
// Make sure the imported buffer has the overwritten contents.
5949+
SourceManager &ToSM = ToAST->getSourceManager();
5950+
FileID ImportedID = ToSM.getFileID(ImportedLoc);
5951+
EXPECT_EQ(Contents,
5952+
ToSM.getBuffer(ImportedID, SourceLocation())->getBuffer());
5953+
}
5954+
58995955
TEST_P(ASTImporterOptionSpecificTestBase, ImportExprOfAlignmentAttr) {
59005956
// Test if import of these packed and aligned attributes does not trigger an
59015957
// error situation where source location from 'From' context is referenced in

0 commit comments

Comments
 (0)