2011-12-22 00:56:29 +08:00
|
|
|
//===- unittests/Basic/SourceManagerTest.cpp ------ SourceManager tests ---===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2012-10-24 06:38:58 +08:00
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
2012-12-04 17:45:34 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2011-12-22 00:56:29 +08:00
|
|
|
#include "clang/Basic/LangOptions.h"
|
Reapply "Modules: Cache PCMs in memory and avoid a use-after-free"
This reverts commit r298185, effectively reapplying r298165, after fixing the
new unit tests (PR32338). The memory buffer generator doesn't null-terminate
the MemoryBuffer it creates; this version of the commit informs getMemBuffer
about that to avoid the assert.
Original commit message follows:
----
Clang's internal build system for implicit modules uses lock files to
ensure that after a process writes a PCM it will read the same one back
in (without contention from other -cc1 commands). Since PCMs are read
from disk repeatedly while invalidating, building, and importing, the
lock is not released quickly. Furthermore, the LockFileManager is not
robust in every environment. Other -cc1 commands can stall until
timeout (after about eight minutes).
This commit changes the lock file from being necessary for correctness
to a (possibly dubious) performance hack. The remaining benefit is to
reduce duplicate work in competing -cc1 commands which depend on the
same module. Follow-up commits will change the internal build system to
continue after a timeout, and reduce the timeout. Perhaps we should
reconsider blocking at all.
This also fixes a use-after-free, when one part of a compilation
validates a PCM and starts using it, and another tries to swap out the
PCM for something new.
The PCMCache is a new type called MemoryBufferCache, which saves memory
buffers based on their filename. Its ownership is shared by the
CompilerInstance and ModuleManager.
- The ModuleManager stores PCMs there that it loads from disk, never
touching the disk if the cache is hot.
- When modules fail to validate, they're removed from the cache.
- When a CompilerInstance is spawned to build a new module, each
already-loaded PCM is assumed to be valid, and is frozen to avoid
the use-after-free.
- Any newly-built module is written directly to the cache to avoid the
round-trip to the filesystem, making lock files unnecessary for
correctness.
Original patch by Manman Ren; most testcases by Adrian Prantl!
llvm-svn: 298278
2017-03-21 01:58:26 +08:00
|
|
|
#include "clang/Basic/MemoryBufferCache.h"
|
2011-12-22 00:56:29 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2012-12-04 17:45:34 +08:00
|
|
|
#include "clang/Basic/TargetOptions.h"
|
2011-12-22 00:56:29 +08:00
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
2012-10-25 00:24:38 +08:00
|
|
|
#include "clang/Lex/HeaderSearchOptions.h"
|
2012-12-04 17:45:34 +08:00
|
|
|
#include "clang/Lex/ModuleLoader.h"
|
2011-12-22 00:56:29 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2012-10-25 01:46:57 +08:00
|
|
|
#include "clang/Lex/PreprocessorOptions.h"
|
2012-02-04 21:45:25 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2014-06-04 11:28:55 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2011-12-22 00:56:29 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// The test fixture.
|
|
|
|
class SourceManagerTest : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
SourceManagerTest()
|
|
|
|
: FileMgr(FileMgrOpts),
|
|
|
|
DiagID(new DiagnosticIDs()),
|
2012-10-24 06:31:51 +08:00
|
|
|
Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
|
2012-10-17 08:11:35 +08:00
|
|
|
SourceMgr(Diags, FileMgr),
|
|
|
|
TargetOpts(new TargetOptions) {
|
|
|
|
TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
|
2014-07-06 13:26:44 +08:00
|
|
|
Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
|
2011-12-22 00:56:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FileSystemOptions FileMgrOpts;
|
|
|
|
FileManager FileMgr;
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
|
2011-12-22 00:56:29 +08:00
|
|
|
DiagnosticsEngine Diags;
|
|
|
|
SourceManager SourceMgr;
|
|
|
|
LangOptions LangOpts;
|
2014-07-06 13:26:44 +08:00
|
|
|
std::shared_ptr<TargetOptions> TargetOpts;
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<TargetInfo> Target;
|
2011-12-22 00:56:29 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(SourceManagerTest, isBeforeInTranslationUnit) {
|
|
|
|
const char *source =
|
|
|
|
"#define M(x) [x]\n"
|
|
|
|
"M(foo)";
|
2016-03-05 03:00:41 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> Buf =
|
|
|
|
llvm::MemoryBuffer::getMemBuffer(source);
|
2014-08-29 15:59:55 +08:00
|
|
|
FileID mainFileID = SourceMgr.createFileID(std::move(Buf));
|
2014-05-21 09:12:41 +08:00
|
|
|
SourceMgr.setMainFileID(mainFileID);
|
2011-12-22 00:56:29 +08:00
|
|
|
|
2017-06-10 03:22:32 +08:00
|
|
|
TrivialModuleLoader ModLoader;
|
Reapply "Modules: Cache PCMs in memory and avoid a use-after-free"
This reverts commit r298185, effectively reapplying r298165, after fixing the
new unit tests (PR32338). The memory buffer generator doesn't null-terminate
the MemoryBuffer it creates; this version of the commit informs getMemBuffer
about that to avoid the assert.
Original commit message follows:
----
Clang's internal build system for implicit modules uses lock files to
ensure that after a process writes a PCM it will read the same one back
in (without contention from other -cc1 commands). Since PCMs are read
from disk repeatedly while invalidating, building, and importing, the
lock is not released quickly. Furthermore, the LockFileManager is not
robust in every environment. Other -cc1 commands can stall until
timeout (after about eight minutes).
This commit changes the lock file from being necessary for correctness
to a (possibly dubious) performance hack. The remaining benefit is to
reduce duplicate work in competing -cc1 commands which depend on the
same module. Follow-up commits will change the internal build system to
continue after a timeout, and reduce the timeout. Perhaps we should
reconsider blocking at all.
This also fixes a use-after-free, when one part of a compilation
validates a PCM and starts using it, and another tries to swap out the
PCM for something new.
The PCMCache is a new type called MemoryBufferCache, which saves memory
buffers based on their filename. Its ownership is shared by the
CompilerInstance and ModuleManager.
- The ModuleManager stores PCMs there that it loads from disk, never
touching the disk if the cache is hot.
- When modules fail to validate, they're removed from the cache.
- When a CompilerInstance is spawned to build a new module, each
already-loaded PCM is assumed to be valid, and is frozen to avoid
the use-after-free.
- Any newly-built module is written directly to the cache to avoid the
round-trip to the filesystem, making lock files unnecessary for
correctness.
Original patch by Manman Ren; most testcases by Adrian Prantl!
llvm-svn: 298278
2017-03-21 01:58:26 +08:00
|
|
|
MemoryBufferCache PCMCache;
|
2017-01-06 09:04:46 +08:00
|
|
|
HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
|
|
|
|
Diags, LangOpts, &*Target);
|
2017-01-06 03:11:36 +08:00
|
|
|
Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
|
Reapply "Modules: Cache PCMs in memory and avoid a use-after-free"
This reverts commit r298185, effectively reapplying r298165, after fixing the
new unit tests (PR32338). The memory buffer generator doesn't null-terminate
the MemoryBuffer it creates; this version of the commit informs getMemBuffer
about that to avoid the assert.
Original commit message follows:
----
Clang's internal build system for implicit modules uses lock files to
ensure that after a process writes a PCM it will read the same one back
in (without contention from other -cc1 commands). Since PCMs are read
from disk repeatedly while invalidating, building, and importing, the
lock is not released quickly. Furthermore, the LockFileManager is not
robust in every environment. Other -cc1 commands can stall until
timeout (after about eight minutes).
This commit changes the lock file from being necessary for correctness
to a (possibly dubious) performance hack. The remaining benefit is to
reduce duplicate work in competing -cc1 commands which depend on the
same module. Follow-up commits will change the internal build system to
continue after a timeout, and reduce the timeout. Perhaps we should
reconsider blocking at all.
This also fixes a use-after-free, when one part of a compilation
validates a PCM and starts using it, and another tries to swap out the
PCM for something new.
The PCMCache is a new type called MemoryBufferCache, which saves memory
buffers based on their filename. Its ownership is shared by the
CompilerInstance and ModuleManager.
- The ModuleManager stores PCMs there that it loads from disk, never
touching the disk if the cache is hot.
- When modules fail to validate, they're removed from the cache.
- When a CompilerInstance is spawned to build a new module, each
already-loaded PCM is assumed to be valid, and is frozen to avoid
the use-after-free.
- Any newly-built module is written directly to the cache to avoid the
round-trip to the filesystem, making lock files unnecessary for
correctness.
Original patch by Manman Ren; most testcases by Adrian Prantl!
llvm-svn: 298278
2017-03-21 01:58:26 +08:00
|
|
|
SourceMgr, PCMCache, HeaderInfo, ModLoader,
|
2014-06-08 16:38:12 +08:00
|
|
|
/*IILookup =*/nullptr,
|
2014-05-02 11:43:30 +08:00
|
|
|
/*OwnsHeaderSearch =*/false);
|
|
|
|
PP.Initialize(*Target);
|
2011-12-22 00:56:29 +08:00
|
|
|
PP.EnterMainSourceFile();
|
|
|
|
|
|
|
|
std::vector<Token> toks;
|
|
|
|
while (1) {
|
|
|
|
Token tok;
|
|
|
|
PP.Lex(tok);
|
|
|
|
if (tok.is(tok::eof))
|
|
|
|
break;
|
|
|
|
toks.push_back(tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we got the tokens that we expected.
|
|
|
|
ASSERT_EQ(3U, toks.size());
|
|
|
|
ASSERT_EQ(tok::l_square, toks[0].getKind());
|
|
|
|
ASSERT_EQ(tok::identifier, toks[1].getKind());
|
|
|
|
ASSERT_EQ(tok::r_square, toks[2].getKind());
|
|
|
|
|
|
|
|
SourceLocation lsqrLoc = toks[0].getLocation();
|
|
|
|
SourceLocation idLoc = toks[1].getLocation();
|
|
|
|
SourceLocation rsqrLoc = toks[2].getLocation();
|
|
|
|
|
|
|
|
SourceLocation macroExpStartLoc = SourceMgr.translateLineCol(mainFileID, 2, 1);
|
|
|
|
SourceLocation macroExpEndLoc = SourceMgr.translateLineCol(mainFileID, 2, 6);
|
|
|
|
ASSERT_TRUE(macroExpStartLoc.isFileID());
|
|
|
|
ASSERT_TRUE(macroExpEndLoc.isFileID());
|
|
|
|
|
2012-02-13 20:32:26 +08:00
|
|
|
SmallString<32> str;
|
2011-12-22 00:56:29 +08:00
|
|
|
ASSERT_EQ("M", PP.getSpelling(macroExpStartLoc, str));
|
|
|
|
ASSERT_EQ(")", PP.getSpelling(macroExpEndLoc, str));
|
|
|
|
|
|
|
|
EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(lsqrLoc, idLoc));
|
|
|
|
EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, rsqrLoc));
|
|
|
|
EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(macroExpStartLoc, idLoc));
|
|
|
|
EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, macroExpEndLoc));
|
|
|
|
}
|
|
|
|
|
2012-06-19 11:09:38 +08:00
|
|
|
TEST_F(SourceManagerTest, getColumnNumber) {
|
|
|
|
const char *Source =
|
|
|
|
"int x;\n"
|
|
|
|
"int y;";
|
|
|
|
|
2016-03-05 03:00:41 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> Buf =
|
|
|
|
llvm::MemoryBuffer::getMemBuffer(Source);
|
2014-08-29 15:59:55 +08:00
|
|
|
FileID MainFileID = SourceMgr.createFileID(std::move(Buf));
|
2014-05-21 09:12:41 +08:00
|
|
|
SourceMgr.setMainFileID(MainFileID);
|
2012-06-19 11:09:38 +08:00
|
|
|
|
|
|
|
bool Invalid;
|
|
|
|
|
|
|
|
Invalid = false;
|
|
|
|
EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, &Invalid));
|
|
|
|
EXPECT_TRUE(!Invalid);
|
|
|
|
|
|
|
|
Invalid = false;
|
|
|
|
EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 4, &Invalid));
|
|
|
|
EXPECT_TRUE(!Invalid);
|
|
|
|
|
|
|
|
Invalid = false;
|
|
|
|
EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 7, &Invalid));
|
|
|
|
EXPECT_TRUE(!Invalid);
|
|
|
|
|
|
|
|
Invalid = false;
|
|
|
|
EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 11, &Invalid));
|
|
|
|
EXPECT_TRUE(!Invalid);
|
|
|
|
|
|
|
|
Invalid = false;
|
|
|
|
EXPECT_EQ(7U, SourceMgr.getColumnNumber(MainFileID, strlen(Source),
|
|
|
|
&Invalid));
|
|
|
|
EXPECT_TRUE(!Invalid);
|
|
|
|
|
|
|
|
Invalid = false;
|
|
|
|
SourceMgr.getColumnNumber(MainFileID, strlen(Source)+1, &Invalid);
|
|
|
|
EXPECT_TRUE(Invalid);
|
|
|
|
|
|
|
|
// Test invalid files
|
|
|
|
Invalid = false;
|
|
|
|
SourceMgr.getColumnNumber(FileID(), 0, &Invalid);
|
|
|
|
EXPECT_TRUE(Invalid);
|
|
|
|
|
|
|
|
Invalid = false;
|
|
|
|
SourceMgr.getColumnNumber(FileID(), 1, &Invalid);
|
|
|
|
EXPECT_TRUE(Invalid);
|
|
|
|
|
|
|
|
// Test with no invalid flag.
|
2014-06-08 16:38:12 +08:00
|
|
|
EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, nullptr));
|
2012-06-19 11:09:38 +08:00
|
|
|
}
|
|
|
|
|
2018-08-31 07:10:52 +08:00
|
|
|
TEST_F(SourceManagerTest, locationPrintTest) {
|
|
|
|
const char *header = "#define IDENTITY(x) x\n";
|
|
|
|
|
|
|
|
const char *Source = "int x;\n"
|
|
|
|
"include \"test-header.h\"\n"
|
|
|
|
"IDENTITY(int y);\n"
|
|
|
|
"int z;";
|
|
|
|
|
|
|
|
std::unique_ptr<llvm::MemoryBuffer> HeaderBuf =
|
|
|
|
llvm::MemoryBuffer::getMemBuffer(header);
|
|
|
|
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));
|
|
|
|
|
|
|
|
const FileEntry *HeaderFile =
|
|
|
|
FileMgr.getVirtualFile("/test-header.h", HeaderBuf->getBufferSize(), 0);
|
|
|
|
SourceMgr.overrideFileContents(HeaderFile, std::move(HeaderBuf));
|
|
|
|
|
|
|
|
FileID MainFileID = SourceMgr.getOrCreateFileID(SourceFile, SrcMgr::C_User);
|
|
|
|
FileID HeaderFileID = SourceMgr.getOrCreateFileID(HeaderFile, SrcMgr::C_User);
|
|
|
|
SourceMgr.setMainFileID(MainFileID);
|
|
|
|
|
|
|
|
auto BeginLoc = SourceMgr.getLocForStartOfFile(MainFileID);
|
|
|
|
auto EndLoc = SourceMgr.getLocForEndOfFile(MainFileID);
|
|
|
|
|
|
|
|
auto BeginEOLLoc = SourceMgr.translateLineCol(MainFileID, 1, 7);
|
|
|
|
|
|
|
|
auto HeaderLoc = SourceMgr.getLocForStartOfFile(HeaderFileID);
|
|
|
|
|
|
|
|
EXPECT_EQ(BeginLoc.printToString(SourceMgr), "/mainFile.cpp:1:1");
|
|
|
|
EXPECT_EQ(EndLoc.printToString(SourceMgr), "/mainFile.cpp:4:7");
|
|
|
|
|
|
|
|
EXPECT_EQ(BeginEOLLoc.printToString(SourceMgr), "/mainFile.cpp:1:7");
|
|
|
|
EXPECT_EQ(HeaderLoc.printToString(SourceMgr), "/test-header.h:1:1");
|
|
|
|
|
|
|
|
EXPECT_EQ(SourceRange(BeginLoc, BeginLoc).printToString(SourceMgr),
|
|
|
|
"</mainFile.cpp:1:1>");
|
|
|
|
EXPECT_EQ(SourceRange(BeginLoc, BeginEOLLoc).printToString(SourceMgr),
|
|
|
|
"</mainFile.cpp:1:1, col:7>");
|
|
|
|
EXPECT_EQ(SourceRange(BeginLoc, EndLoc).printToString(SourceMgr),
|
|
|
|
"</mainFile.cpp:1:1, line:4:7>");
|
|
|
|
EXPECT_EQ(SourceRange(BeginLoc, HeaderLoc).printToString(SourceMgr),
|
|
|
|
"</mainFile.cpp:1:1, /test-header.h:1:1>");
|
|
|
|
}
|
|
|
|
|
2011-12-22 00:56:35 +08:00
|
|
|
#if defined(LLVM_ON_UNIX)
|
|
|
|
|
|
|
|
TEST_F(SourceManagerTest, getMacroArgExpandedLocation) {
|
|
|
|
const char *header =
|
|
|
|
"#define FM(x,y) x\n";
|
|
|
|
|
|
|
|
const char *main =
|
|
|
|
"#include \"/test-header.h\"\n"
|
|
|
|
"#define VAL 0\n"
|
|
|
|
"FM(VAL,0)\n"
|
|
|
|
"FM(0,VAL)\n"
|
|
|
|
"FM(FM(0,VAL),0)\n"
|
|
|
|
"#define CONCAT(X, Y) X##Y\n"
|
|
|
|
"CONCAT(1,1)\n";
|
|
|
|
|
2016-03-05 03:00:41 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> HeaderBuf =
|
|
|
|
llvm::MemoryBuffer::getMemBuffer(header);
|
|
|
|
std::unique_ptr<llvm::MemoryBuffer> MainBuf =
|
|
|
|
llvm::MemoryBuffer::getMemBuffer(main);
|
2014-08-29 15:59:55 +08:00
|
|
|
FileID mainFileID = SourceMgr.createFileID(std::move(MainBuf));
|
2014-05-21 09:12:41 +08:00
|
|
|
SourceMgr.setMainFileID(mainFileID);
|
2011-12-22 00:56:35 +08:00
|
|
|
|
|
|
|
const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
|
2014-08-28 04:03:29 +08:00
|
|
|
HeaderBuf->getBufferSize(), 0);
|
2014-08-28 04:54:45 +08:00
|
|
|
SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf));
|
2011-12-22 00:56:35 +08:00
|
|
|
|
2017-06-10 03:22:32 +08:00
|
|
|
TrivialModuleLoader ModLoader;
|
Reapply "Modules: Cache PCMs in memory and avoid a use-after-free"
This reverts commit r298185, effectively reapplying r298165, after fixing the
new unit tests (PR32338). The memory buffer generator doesn't null-terminate
the MemoryBuffer it creates; this version of the commit informs getMemBuffer
about that to avoid the assert.
Original commit message follows:
----
Clang's internal build system for implicit modules uses lock files to
ensure that after a process writes a PCM it will read the same one back
in (without contention from other -cc1 commands). Since PCMs are read
from disk repeatedly while invalidating, building, and importing, the
lock is not released quickly. Furthermore, the LockFileManager is not
robust in every environment. Other -cc1 commands can stall until
timeout (after about eight minutes).
This commit changes the lock file from being necessary for correctness
to a (possibly dubious) performance hack. The remaining benefit is to
reduce duplicate work in competing -cc1 commands which depend on the
same module. Follow-up commits will change the internal build system to
continue after a timeout, and reduce the timeout. Perhaps we should
reconsider blocking at all.
This also fixes a use-after-free, when one part of a compilation
validates a PCM and starts using it, and another tries to swap out the
PCM for something new.
The PCMCache is a new type called MemoryBufferCache, which saves memory
buffers based on their filename. Its ownership is shared by the
CompilerInstance and ModuleManager.
- The ModuleManager stores PCMs there that it loads from disk, never
touching the disk if the cache is hot.
- When modules fail to validate, they're removed from the cache.
- When a CompilerInstance is spawned to build a new module, each
already-loaded PCM is assumed to be valid, and is frozen to avoid
the use-after-free.
- Any newly-built module is written directly to the cache to avoid the
round-trip to the filesystem, making lock files unnecessary for
correctness.
Original patch by Manman Ren; most testcases by Adrian Prantl!
llvm-svn: 298278
2017-03-21 01:58:26 +08:00
|
|
|
MemoryBufferCache PCMCache;
|
2017-01-06 09:04:46 +08:00
|
|
|
HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
|
|
|
|
Diags, LangOpts, &*Target);
|
2017-01-06 03:11:36 +08:00
|
|
|
Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
|
Reapply "Modules: Cache PCMs in memory and avoid a use-after-free"
This reverts commit r298185, effectively reapplying r298165, after fixing the
new unit tests (PR32338). The memory buffer generator doesn't null-terminate
the MemoryBuffer it creates; this version of the commit informs getMemBuffer
about that to avoid the assert.
Original commit message follows:
----
Clang's internal build system for implicit modules uses lock files to
ensure that after a process writes a PCM it will read the same one back
in (without contention from other -cc1 commands). Since PCMs are read
from disk repeatedly while invalidating, building, and importing, the
lock is not released quickly. Furthermore, the LockFileManager is not
robust in every environment. Other -cc1 commands can stall until
timeout (after about eight minutes).
This commit changes the lock file from being necessary for correctness
to a (possibly dubious) performance hack. The remaining benefit is to
reduce duplicate work in competing -cc1 commands which depend on the
same module. Follow-up commits will change the internal build system to
continue after a timeout, and reduce the timeout. Perhaps we should
reconsider blocking at all.
This also fixes a use-after-free, when one part of a compilation
validates a PCM and starts using it, and another tries to swap out the
PCM for something new.
The PCMCache is a new type called MemoryBufferCache, which saves memory
buffers based on their filename. Its ownership is shared by the
CompilerInstance and ModuleManager.
- The ModuleManager stores PCMs there that it loads from disk, never
touching the disk if the cache is hot.
- When modules fail to validate, they're removed from the cache.
- When a CompilerInstance is spawned to build a new module, each
already-loaded PCM is assumed to be valid, and is frozen to avoid
the use-after-free.
- Any newly-built module is written directly to the cache to avoid the
round-trip to the filesystem, making lock files unnecessary for
correctness.
Original patch by Manman Ren; most testcases by Adrian Prantl!
llvm-svn: 298278
2017-03-21 01:58:26 +08:00
|
|
|
SourceMgr, PCMCache, HeaderInfo, ModLoader,
|
2014-06-08 16:38:12 +08:00
|
|
|
/*IILookup =*/nullptr,
|
2014-05-02 11:43:30 +08:00
|
|
|
/*OwnsHeaderSearch =*/false);
|
|
|
|
PP.Initialize(*Target);
|
2011-12-22 00:56:35 +08:00
|
|
|
PP.EnterMainSourceFile();
|
|
|
|
|
|
|
|
std::vector<Token> toks;
|
|
|
|
while (1) {
|
|
|
|
Token tok;
|
|
|
|
PP.Lex(tok);
|
|
|
|
if (tok.is(tok::eof))
|
|
|
|
break;
|
|
|
|
toks.push_back(tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we got the tokens that we expected.
|
|
|
|
ASSERT_EQ(4U, toks.size());
|
|
|
|
ASSERT_EQ(tok::numeric_constant, toks[0].getKind());
|
|
|
|
ASSERT_EQ(tok::numeric_constant, toks[1].getKind());
|
|
|
|
ASSERT_EQ(tok::numeric_constant, toks[2].getKind());
|
|
|
|
ASSERT_EQ(tok::numeric_constant, toks[3].getKind());
|
|
|
|
|
|
|
|
SourceLocation defLoc = SourceMgr.translateLineCol(mainFileID, 2, 13);
|
|
|
|
SourceLocation loc1 = SourceMgr.translateLineCol(mainFileID, 3, 8);
|
|
|
|
SourceLocation loc2 = SourceMgr.translateLineCol(mainFileID, 4, 4);
|
|
|
|
SourceLocation loc3 = SourceMgr.translateLineCol(mainFileID, 5, 7);
|
|
|
|
SourceLocation defLoc2 = SourceMgr.translateLineCol(mainFileID, 6, 22);
|
|
|
|
defLoc = SourceMgr.getMacroArgExpandedLocation(defLoc);
|
|
|
|
loc1 = SourceMgr.getMacroArgExpandedLocation(loc1);
|
|
|
|
loc2 = SourceMgr.getMacroArgExpandedLocation(loc2);
|
|
|
|
loc3 = SourceMgr.getMacroArgExpandedLocation(loc3);
|
|
|
|
defLoc2 = SourceMgr.getMacroArgExpandedLocation(defLoc2);
|
|
|
|
|
|
|
|
EXPECT_TRUE(defLoc.isFileID());
|
|
|
|
EXPECT_TRUE(loc1.isFileID());
|
|
|
|
EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc2));
|
|
|
|
EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc3));
|
|
|
|
EXPECT_EQ(loc2, toks[1].getLocation());
|
|
|
|
EXPECT_EQ(loc3, toks[2].getLocation());
|
|
|
|
EXPECT_TRUE(defLoc2.isFileID());
|
|
|
|
}
|
|
|
|
|
2012-03-28 02:47:48 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct MacroAction {
|
2017-04-27 05:05:44 +08:00
|
|
|
enum Kind { kExpansion, kDefinition, kUnDefinition};
|
|
|
|
|
2012-03-28 02:47:48 +08:00
|
|
|
SourceLocation Loc;
|
|
|
|
std::string Name;
|
2017-04-27 05:05:44 +08:00
|
|
|
unsigned MAKind : 3;
|
|
|
|
|
|
|
|
MacroAction(SourceLocation Loc, StringRef Name, unsigned K)
|
|
|
|
: Loc(Loc), Name(Name), MAKind(K) { }
|
|
|
|
|
|
|
|
bool isExpansion() const { return MAKind == kExpansion; }
|
|
|
|
bool isDefinition() const { return MAKind & kDefinition; }
|
|
|
|
bool isUnDefinition() const { return MAKind & kUnDefinition; }
|
2012-03-28 02:47:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class MacroTracker : public PPCallbacks {
|
|
|
|
std::vector<MacroAction> &Macros;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit MacroTracker(std::vector<MacroAction> &Macros) : Macros(Macros) { }
|
2015-04-11 10:00:23 +08:00
|
|
|
|
|
|
|
void MacroDefined(const Token &MacroNameTok,
|
|
|
|
const MacroDirective *MD) override {
|
2013-02-24 08:05:14 +08:00
|
|
|
Macros.push_back(MacroAction(MD->getLocation(),
|
2012-03-28 02:47:48 +08:00
|
|
|
MacroNameTok.getIdentifierInfo()->getName(),
|
2017-04-27 05:05:44 +08:00
|
|
|
MacroAction::kDefinition));
|
|
|
|
}
|
|
|
|
void MacroUndefined(const Token &MacroNameTok,
|
|
|
|
const MacroDefinition &MD,
|
|
|
|
const MacroDirective *UD) override {
|
|
|
|
Macros.push_back(
|
|
|
|
MacroAction(UD ? UD->getLocation() : SourceLocation(),
|
|
|
|
MacroNameTok.getIdentifierInfo()->getName(),
|
|
|
|
UD ? MacroAction::kDefinition | MacroAction::kUnDefinition
|
|
|
|
: MacroAction::kUnDefinition));
|
2012-03-28 02:47:48 +08:00
|
|
|
}
|
2015-05-04 11:15:40 +08:00
|
|
|
void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
|
2015-04-11 10:00:23 +08:00
|
|
|
SourceRange Range, const MacroArgs *Args) override {
|
2012-03-28 02:47:48 +08:00
|
|
|
Macros.push_back(MacroAction(MacroNameTok.getLocation(),
|
|
|
|
MacroNameTok.getIdentifierInfo()->getName(),
|
2017-04-27 05:05:44 +08:00
|
|
|
MacroAction::kExpansion));
|
2012-03-28 02:47:48 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2012-03-28 02:47:48 +08:00
|
|
|
|
|
|
|
TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithMacroInInclude) {
|
|
|
|
const char *header =
|
2017-04-27 05:05:44 +08:00
|
|
|
"#define MACRO_IN_INCLUDE 0\n"
|
|
|
|
"#define MACRO_DEFINED\n"
|
|
|
|
"#undef MACRO_DEFINED\n"
|
|
|
|
"#undef MACRO_UNDEFINED\n";
|
2012-03-28 02:47:48 +08:00
|
|
|
|
|
|
|
const char *main =
|
|
|
|
"#define M(x) x\n"
|
|
|
|
"#define INC \"/test-header.h\"\n"
|
|
|
|
"#include M(INC)\n"
|
|
|
|
"#define INC2 </test-header.h>\n"
|
|
|
|
"#include M(INC2)\n";
|
|
|
|
|
2016-03-05 03:00:41 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> HeaderBuf =
|
|
|
|
llvm::MemoryBuffer::getMemBuffer(header);
|
|
|
|
std::unique_ptr<llvm::MemoryBuffer> MainBuf =
|
|
|
|
llvm::MemoryBuffer::getMemBuffer(main);
|
2014-08-29 15:59:55 +08:00
|
|
|
SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(MainBuf)));
|
2012-03-28 02:47:48 +08:00
|
|
|
|
|
|
|
const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
|
2014-08-28 04:03:29 +08:00
|
|
|
HeaderBuf->getBufferSize(), 0);
|
2014-08-28 04:54:45 +08:00
|
|
|
SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf));
|
2012-03-28 02:47:48 +08:00
|
|
|
|
2017-06-10 03:22:32 +08:00
|
|
|
TrivialModuleLoader ModLoader;
|
Reapply "Modules: Cache PCMs in memory and avoid a use-after-free"
This reverts commit r298185, effectively reapplying r298165, after fixing the
new unit tests (PR32338). The memory buffer generator doesn't null-terminate
the MemoryBuffer it creates; this version of the commit informs getMemBuffer
about that to avoid the assert.
Original commit message follows:
----
Clang's internal build system for implicit modules uses lock files to
ensure that after a process writes a PCM it will read the same one back
in (without contention from other -cc1 commands). Since PCMs are read
from disk repeatedly while invalidating, building, and importing, the
lock is not released quickly. Furthermore, the LockFileManager is not
robust in every environment. Other -cc1 commands can stall until
timeout (after about eight minutes).
This commit changes the lock file from being necessary for correctness
to a (possibly dubious) performance hack. The remaining benefit is to
reduce duplicate work in competing -cc1 commands which depend on the
same module. Follow-up commits will change the internal build system to
continue after a timeout, and reduce the timeout. Perhaps we should
reconsider blocking at all.
This also fixes a use-after-free, when one part of a compilation
validates a PCM and starts using it, and another tries to swap out the
PCM for something new.
The PCMCache is a new type called MemoryBufferCache, which saves memory
buffers based on their filename. Its ownership is shared by the
CompilerInstance and ModuleManager.
- The ModuleManager stores PCMs there that it loads from disk, never
touching the disk if the cache is hot.
- When modules fail to validate, they're removed from the cache.
- When a CompilerInstance is spawned to build a new module, each
already-loaded PCM is assumed to be valid, and is frozen to avoid
the use-after-free.
- Any newly-built module is written directly to the cache to avoid the
round-trip to the filesystem, making lock files unnecessary for
correctness.
Original patch by Manman Ren; most testcases by Adrian Prantl!
llvm-svn: 298278
2017-03-21 01:58:26 +08:00
|
|
|
MemoryBufferCache PCMCache;
|
2017-01-06 09:04:46 +08:00
|
|
|
HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
|
|
|
|
Diags, LangOpts, &*Target);
|
2017-01-06 03:11:36 +08:00
|
|
|
Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
|
Reapply "Modules: Cache PCMs in memory and avoid a use-after-free"
This reverts commit r298185, effectively reapplying r298165, after fixing the
new unit tests (PR32338). The memory buffer generator doesn't null-terminate
the MemoryBuffer it creates; this version of the commit informs getMemBuffer
about that to avoid the assert.
Original commit message follows:
----
Clang's internal build system for implicit modules uses lock files to
ensure that after a process writes a PCM it will read the same one back
in (without contention from other -cc1 commands). Since PCMs are read
from disk repeatedly while invalidating, building, and importing, the
lock is not released quickly. Furthermore, the LockFileManager is not
robust in every environment. Other -cc1 commands can stall until
timeout (after about eight minutes).
This commit changes the lock file from being necessary for correctness
to a (possibly dubious) performance hack. The remaining benefit is to
reduce duplicate work in competing -cc1 commands which depend on the
same module. Follow-up commits will change the internal build system to
continue after a timeout, and reduce the timeout. Perhaps we should
reconsider blocking at all.
This also fixes a use-after-free, when one part of a compilation
validates a PCM and starts using it, and another tries to swap out the
PCM for something new.
The PCMCache is a new type called MemoryBufferCache, which saves memory
buffers based on their filename. Its ownership is shared by the
CompilerInstance and ModuleManager.
- The ModuleManager stores PCMs there that it loads from disk, never
touching the disk if the cache is hot.
- When modules fail to validate, they're removed from the cache.
- When a CompilerInstance is spawned to build a new module, each
already-loaded PCM is assumed to be valid, and is frozen to avoid
the use-after-free.
- Any newly-built module is written directly to the cache to avoid the
round-trip to the filesystem, making lock files unnecessary for
correctness.
Original patch by Manman Ren; most testcases by Adrian Prantl!
llvm-svn: 298278
2017-03-21 01:58:26 +08:00
|
|
|
SourceMgr, PCMCache, HeaderInfo, ModLoader,
|
2014-06-08 16:38:12 +08:00
|
|
|
/*IILookup =*/nullptr,
|
2014-05-02 11:43:30 +08:00
|
|
|
/*OwnsHeaderSearch =*/false);
|
|
|
|
PP.Initialize(*Target);
|
2012-03-28 02:47:48 +08:00
|
|
|
|
|
|
|
std::vector<MacroAction> Macros;
|
2014-09-10 12:53:53 +08:00
|
|
|
PP.addPPCallbacks(llvm::make_unique<MacroTracker>(Macros));
|
2012-03-28 02:47:48 +08:00
|
|
|
|
|
|
|
PP.EnterMainSourceFile();
|
|
|
|
|
|
|
|
std::vector<Token> toks;
|
|
|
|
while (1) {
|
|
|
|
Token tok;
|
|
|
|
PP.Lex(tok);
|
|
|
|
if (tok.is(tok::eof))
|
|
|
|
break;
|
|
|
|
toks.push_back(tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we got the tokens that we expected.
|
|
|
|
ASSERT_EQ(0U, toks.size());
|
|
|
|
|
2017-04-27 05:05:44 +08:00
|
|
|
ASSERT_EQ(15U, Macros.size());
|
2012-03-28 02:47:48 +08:00
|
|
|
// #define M(x) x
|
2017-04-27 05:05:44 +08:00
|
|
|
ASSERT_TRUE(Macros[0].isDefinition());
|
2012-03-28 02:47:48 +08:00
|
|
|
ASSERT_EQ("M", Macros[0].Name);
|
|
|
|
// #define INC "/test-header.h"
|
2017-04-27 05:05:44 +08:00
|
|
|
ASSERT_TRUE(Macros[1].isDefinition());
|
2012-03-28 02:47:48 +08:00
|
|
|
ASSERT_EQ("INC", Macros[1].Name);
|
|
|
|
// M expansion in #include M(INC)
|
2017-04-27 05:05:44 +08:00
|
|
|
ASSERT_FALSE(Macros[2].isDefinition());
|
2012-03-28 02:47:48 +08:00
|
|
|
ASSERT_EQ("M", Macros[2].Name);
|
|
|
|
// INC expansion in #include M(INC)
|
2017-04-27 05:05:44 +08:00
|
|
|
ASSERT_TRUE(Macros[3].isExpansion());
|
2012-03-28 02:47:48 +08:00
|
|
|
ASSERT_EQ("INC", Macros[3].Name);
|
|
|
|
// #define MACRO_IN_INCLUDE 0
|
2017-04-27 05:05:44 +08:00
|
|
|
ASSERT_TRUE(Macros[4].isDefinition());
|
2012-03-28 02:47:48 +08:00
|
|
|
ASSERT_EQ("MACRO_IN_INCLUDE", Macros[4].Name);
|
2017-04-27 05:05:44 +08:00
|
|
|
// #define MACRO_DEFINED
|
|
|
|
ASSERT_TRUE(Macros[5].isDefinition());
|
|
|
|
ASSERT_FALSE(Macros[5].isUnDefinition());
|
|
|
|
ASSERT_EQ("MACRO_DEFINED", Macros[5].Name);
|
|
|
|
// #undef MACRO_DEFINED
|
|
|
|
ASSERT_TRUE(Macros[6].isDefinition());
|
|
|
|
ASSERT_TRUE(Macros[6].isUnDefinition());
|
|
|
|
ASSERT_EQ("MACRO_DEFINED", Macros[6].Name);
|
|
|
|
// #undef MACRO_UNDEFINED
|
|
|
|
ASSERT_FALSE(Macros[7].isDefinition());
|
|
|
|
ASSERT_TRUE(Macros[7].isUnDefinition());
|
|
|
|
ASSERT_EQ("MACRO_UNDEFINED", Macros[7].Name);
|
2012-03-28 02:47:48 +08:00
|
|
|
// #define INC2 </test-header.h>
|
2017-04-27 05:05:44 +08:00
|
|
|
ASSERT_TRUE(Macros[8].isDefinition());
|
|
|
|
ASSERT_EQ("INC2", Macros[8].Name);
|
2012-03-28 02:47:48 +08:00
|
|
|
// M expansion in #include M(INC2)
|
2017-04-27 05:05:44 +08:00
|
|
|
ASSERT_FALSE(Macros[9].isDefinition());
|
|
|
|
ASSERT_EQ("M", Macros[9].Name);
|
2012-03-28 02:47:48 +08:00
|
|
|
// INC2 expansion in #include M(INC2)
|
2017-04-27 05:05:44 +08:00
|
|
|
ASSERT_TRUE(Macros[10].isExpansion());
|
|
|
|
ASSERT_EQ("INC2", Macros[10].Name);
|
2012-03-28 02:47:48 +08:00
|
|
|
// #define MACRO_IN_INCLUDE 0
|
2017-04-27 05:05:44 +08:00
|
|
|
ASSERT_TRUE(Macros[11].isDefinition());
|
|
|
|
ASSERT_EQ("MACRO_IN_INCLUDE", Macros[11].Name);
|
2012-03-28 02:47:48 +08:00
|
|
|
|
|
|
|
// The INC expansion in #include M(INC) comes before the first
|
|
|
|
// MACRO_IN_INCLUDE definition of the included file.
|
|
|
|
EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[3].Loc, Macros[4].Loc));
|
|
|
|
|
|
|
|
// The INC2 expansion in #include M(INC2) comes before the second
|
|
|
|
// MACRO_IN_INCLUDE definition of the included file.
|
2017-04-27 05:05:44 +08:00
|
|
|
EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[10].Loc, Macros[11].Loc));
|
2012-03-28 02:47:48 +08:00
|
|
|
}
|
|
|
|
|
2011-12-22 00:56:35 +08:00
|
|
|
#endif
|
|
|
|
|
2011-12-22 00:56:29 +08:00
|
|
|
} // anonymous namespace
|