[clangd] Make system header mappings available for PreambleParsedCallback

Summary:
SystemHeaderMappings were added only after takeIncludes call, which
resulted in getting mapping on main file ast updates but not on preamble ast
updates.
Fixes https://github.com/clangd/clangd/issues/8

Reviewers: hokein

Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, arphaman, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D58029

llvm-svn: 353687
This commit is contained in:
Kadir Cetinkaya 2019-02-11 10:31:13 +00:00
parent cfc3f751aa
commit db7fbcb038
2 changed files with 37 additions and 5 deletions

View File

@ -96,14 +96,13 @@ private:
class CppFilePreambleCallbacks : public PreambleCallbacks {
public:
CppFilePreambleCallbacks(PathRef File, PreambleParsedCallback ParsedCallback)
: File(File), ParsedCallback(ParsedCallback) {}
: File(File), ParsedCallback(ParsedCallback) {
addSystemHeadersMapping(&CanonIncludes);
}
IncludeStructure takeIncludes() { return std::move(Includes); }
CanonicalIncludes takeCanonicalIncludes() {
addSystemHeadersMapping(&CanonIncludes);
return std::move(CanonIncludes);
}
CanonicalIncludes takeCanonicalIncludes() { return std::move(CanonIncludes); }
void AfterExecute(CompilerInstance &CI) override {
if (!ParsedCallback)

View File

@ -212,6 +212,39 @@ TEST(FileIndexTest, IncludeCollected) {
"<the/good/header.h>");
}
TEST(FileIndexTest, HasSystemHeaderMappingsInPreamble) {
FileIndex Index;
const std::string Header = R"cpp(
class Foo {};
)cpp";
auto MainFile = testPath("foo.cpp");
auto HeaderFile = testPath("bits/alloc_traits.h");
std::vector<const char *> Cmd = {"clang", "-xc++", MainFile.c_str(),
"-include", HeaderFile.c_str()};
// Preparse ParseInputs.
ParseInputs PI;
PI.CompileCommand.Directory = testRoot();
PI.CompileCommand.Filename = MainFile;
PI.CompileCommand.CommandLine = {Cmd.begin(), Cmd.end()};
PI.Contents = "";
PI.FS = buildTestFS({{MainFile, ""}, {HeaderFile, Header}});
// Prepare preamble.
auto CI = buildCompilerInvocation(PI);
auto PreambleData = buildPreamble(
MainFile, *buildCompilerInvocation(PI), /*OldPreamble=*/nullptr,
tooling::CompileCommand(), PI, std::make_shared<PCHContainerOperations>(),
/*StoreInMemory=*/true,
[&](ASTContext &Ctx, std::shared_ptr<Preprocessor> PP,
const CanonicalIncludes &Includes) {
Index.updatePreamble(MainFile, Ctx, PP, Includes);
});
auto Symbols = runFuzzyFind(Index, "");
EXPECT_THAT(Symbols, ElementsAre(_));
EXPECT_THAT(Symbols.begin()->IncludeHeaders.front().IncludeHeader,
"<memory>");
}
TEST(FileIndexTest, TemplateParamsInLabel) {
auto Source = R"cpp(
template <class Ty>