aboutsummaryrefslogtreecommitdiff
path: root/clang/unittests/Basic/SourceManagerTest.cpp
diff options
context:
space:
mode:
authorAlex Lorenz <arphaman@gmail.com>2020-05-14 14:11:31 -0700
committerAlex Lorenz <arphaman@gmail.com>2020-05-14 14:13:34 -0700
commit11d612ac99a621c762c2cc8f7bacbb8ae32d7fe9 (patch)
tree2af435bc475fbc930c9fd4516e9f755a182a3e43 /clang/unittests/Basic/SourceManagerTest.cpp
parent428d0b6f77986efd944df01bb4ae7888c6262c2f (diff)
downloadllvm-11d612ac99a621c762c2cc8f7bacbb8ae32d7fe9.zip
llvm-11d612ac99a621c762c2cc8f7bacbb8ae32d7fe9.tar.gz
llvm-11d612ac99a621c762c2cc8f7bacbb8ae32d7fe9.tar.bz2
[clang][Preprocessor] Replace the slow translateFile call by a new, faster isMainFile check
The commit 3c28a2dc6bdc331e5a0d8097a5fa59d06682b9d0 introduced the check that checks if we're trying to re-enter a main file when building a preamble. Unfortunately this slowed down the preamble compilation by 80-90% in some test cases, as translateFile is really slow. This change checks to see if the FileEntry is the main file without calling translateFile, but by using the new isMainFile check instead. This speeds up preamble building by 1.5-2x for certain test cases that we have. rdar://59361291 Differential Revision: https://reviews.llvm.org/D79834
Diffstat (limited to 'clang/unittests/Basic/SourceManagerTest.cpp')
-rw-r--r--clang/unittests/Basic/SourceManagerTest.cpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/clang/unittests/Basic/SourceManagerTest.cpp b/clang/unittests/Basic/SourceManagerTest.cpp
index 850a08b..5fd7607f 100644
--- a/clang/unittests/Basic/SourceManagerTest.cpp
+++ b/clang/unittests/Basic/SourceManagerTest.cpp
@@ -491,6 +491,30 @@ TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithMacroInInclude) {
EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[10].Loc, Macros[11].Loc));
}
+TEST_F(SourceManagerTest, isMainFile) {
+ const char *Source = "int x;";
+
+ std::unique_ptr<llvm::MemoryBuffer> Buf =
+ llvm::MemoryBuffer::getMemBuffer(Source);
+ const FileEntry *SourceFile =
+ FileMgr.getVirtualFile("mainFile.cpp", Buf->getBufferSize(), 0);
+ SourceMgr.overrideFileContents(SourceFile, std::move(Buf));
+
+ std::unique_ptr<llvm::MemoryBuffer> Buf2 =
+ llvm::MemoryBuffer::getMemBuffer(Source);
+ const FileEntry *SecondFile =
+ FileMgr.getVirtualFile("file2.cpp", Buf2->getBufferSize(), 0);
+ SourceMgr.overrideFileContents(SecondFile, std::move(Buf2));
+
+ FileID MainFileID = SourceMgr.getOrCreateFileID(SourceFile, SrcMgr::C_User);
+ SourceMgr.setMainFileID(MainFileID);
+
+ EXPECT_TRUE(SourceMgr.isMainFile(FileEntryRef("mainFile.cpp", *SourceFile)));
+ EXPECT_TRUE(
+ SourceMgr.isMainFile(FileEntryRef("anotherName.cpp", *SourceFile)));
+ EXPECT_FALSE(SourceMgr.isMainFile(FileEntryRef("mainFile.cpp", *SecondFile)));
+}
+
#endif
} // anonymous namespace