Changeset View
Changeset View
Standalone View
Standalone View
clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
Show First 20 Lines • Show All 1,194 Lines • ▼ Show 20 Lines | |||||
// it's included by another open file, then we parse it using that files flags. | // it's included by another open file, then we parse it using that files flags. | ||||
TEST_F(TUSchedulerTests, IncluderCache) { | TEST_F(TUSchedulerTests, IncluderCache) { | ||||
static std::string Main = testPath("main.cpp"), Main2 = testPath("main2.cpp"), | static std::string Main = testPath("main.cpp"), Main2 = testPath("main2.cpp"), | ||||
Main3 = testPath("main3.cpp"), | Main3 = testPath("main3.cpp"), | ||||
NoCmd = testPath("no_cmd.h"), | NoCmd = testPath("no_cmd.h"), | ||||
Unreliable = testPath("unreliable.h"), | Unreliable = testPath("unreliable.h"), | ||||
OK = testPath("ok.h"), | OK = testPath("ok.h"), | ||||
NotIncluded = testPath("not_included.h"); | NotIncluded = testPath("not_included.h"); | ||||
class NoHeadersCDB : public GlobalCompilationDatabase { | struct NoHeadersCDB : public GlobalCompilationDatabase { | ||||
llvm::Optional<tooling::CompileCommand> | llvm::Optional<tooling::CompileCommand> | ||||
getCompileCommand(PathRef File) const override { | getCompileCommand(PathRef File) const override { | ||||
if (File == NoCmd || File == NotIncluded) | if (File == NoCmd || File == NotIncluded || FailAll) | ||||
return llvm::None; | return llvm::None; | ||||
auto Basic = getFallbackCommand(File); | auto Basic = getFallbackCommand(File); | ||||
Basic.Heuristic.clear(); | Basic.Heuristic.clear(); | ||||
if (File == Unreliable) { | if (File == Unreliable) { | ||||
Basic.Heuristic = "not reliable"; | Basic.Heuristic = "not reliable"; | ||||
} else if (File == Main) { | } else if (File == Main) { | ||||
Basic.CommandLine.push_back("-DMAIN"); | Basic.CommandLine.push_back("-DMAIN"); | ||||
} else if (File == Main2) { | } else if (File == Main2) { | ||||
Basic.CommandLine.push_back("-DMAIN2"); | Basic.CommandLine.push_back("-DMAIN2"); | ||||
} else if (File == Main3) { | } else if (File == Main3) { | ||||
Basic.CommandLine.push_back("-DMAIN3"); | Basic.CommandLine.push_back("-DMAIN3"); | ||||
} | } | ||||
return Basic; | return Basic; | ||||
} | } | ||||
std::atomic<bool> FailAll{false}; | |||||
} CDB; | } CDB; | ||||
TUScheduler S(CDB, optsForTest()); | TUScheduler S(CDB, optsForTest()); | ||||
auto GetFlags = [&](PathRef Header) { | auto GetFlags = [&](PathRef Header) { | ||||
S.update(Header, getInputs(Header, ";"), WantDiagnostics::Yes); | S.update(Header, getInputs(Header, ";"), WantDiagnostics::Yes); | ||||
EXPECT_TRUE(S.blockUntilIdle(timeoutSeconds(10))); | EXPECT_TRUE(S.blockUntilIdle(timeoutSeconds(10))); | ||||
tooling::CompileCommand Cmd; | tooling::CompileCommand Cmd; | ||||
S.runWithPreamble("GetFlags", Header, TUScheduler::StaleOrAbsent, | S.runWithPreamble("GetFlags", Header, TUScheduler::StaleOrAbsent, | ||||
[&](llvm::Expected<InputsAndPreamble> Inputs) { | [&](llvm::Expected<InputsAndPreamble> Inputs) { | ||||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | TEST_F(TUSchedulerTests, IncluderCache) { | ||||
S.update(Main3, getInputs(Main3, SomeIncludes), WantDiagnostics::Yes); | S.update(Main3, getInputs(Main3, SomeIncludes), WantDiagnostics::Yes); | ||||
EXPECT_TRUE(S.blockUntilIdle(timeoutSeconds(10))); | EXPECT_TRUE(S.blockUntilIdle(timeoutSeconds(10))); | ||||
EXPECT_THAT(GetFlags(NoCmd), Contains("-DMAIN3")) | EXPECT_THAT(GetFlags(NoCmd), Contains("-DMAIN3")) | ||||
<< "association invalidated and then claimed by main3"; | << "association invalidated and then claimed by main3"; | ||||
EXPECT_THAT(GetFlags(Unreliable), Contains("-DMAIN")) | EXPECT_THAT(GetFlags(Unreliable), Contains("-DMAIN")) | ||||
<< "association invalidated but not reclaimed"; | << "association invalidated but not reclaimed"; | ||||
EXPECT_THAT(GetFlags(NotIncluded), Contains("-DMAIN2")) | EXPECT_THAT(GetFlags(NotIncluded), Contains("-DMAIN2")) | ||||
<< "association still valid"; | << "association still valid"; | ||||
// Delete the file from CDB, it should invalidate the associations. | |||||
CDB.FailAll = true; | |||||
EXPECT_THAT(GetFlags(NoCmd), Not(Contains("-DMAIN3"))) | |||||
<< "association should've been invalidated."; | |||||
// Also run update for Main3 to invalidate the preeamble to make sure next | |||||
// update populates include cache associations. | |||||
S.update(Main3, getInputs(Main3, SomeIncludes), WantDiagnostics::Yes); | |||||
EXPECT_TRUE(S.blockUntilIdle(timeoutSeconds(10))); | |||||
// Re-add the file and make sure nothing crashes. | |||||
CDB.FailAll = false; | |||||
S.update(Main3, getInputs(Main3, SomeIncludes), WantDiagnostics::Yes); | |||||
EXPECT_TRUE(S.blockUntilIdle(timeoutSeconds(10))); | |||||
EXPECT_THAT(GetFlags(NoCmd), Contains("-DMAIN3")) | |||||
<< "association invalidated and then claimed by main3"; | |||||
} | } | ||||
TEST_F(TUSchedulerTests, PreservesLastActiveFile) { | TEST_F(TUSchedulerTests, PreservesLastActiveFile) { | ||||
for (bool Sync : {false, true}) { | for (bool Sync : {false, true}) { | ||||
auto Opts = optsForTest(); | auto Opts = optsForTest(); | ||||
if (Sync) | if (Sync) | ||||
Opts.AsyncThreadsCount = 0; | Opts.AsyncThreadsCount = 0; | ||||
TUScheduler S(CDB, Opts); | TUScheduler S(CDB, Opts); | ||||
▲ Show 20 Lines • Show All 60 Lines • Show Last 20 Lines |