2019-09-25 18:30:22 +08:00
|
|
|
//===--- HeaderSourceSwitchTests.cpp - ---------------------------*- C++-*-===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "HeaderSourceSwitch.h"
|
|
|
|
|
2019-10-01 18:21:15 +08:00
|
|
|
#include "SyncAPI.h"
|
2019-09-25 18:30:22 +08:00
|
|
|
#include "TestFS.h"
|
|
|
|
#include "TestTU.h"
|
2019-09-30 18:48:02 +08:00
|
|
|
#include "index/MemIndex.h"
|
2022-03-09 21:55:38 +08:00
|
|
|
#include "support/Path.h"
|
2020-06-17 17:53:32 +08:00
|
|
|
#include "llvm/ADT/None.h"
|
2022-03-09 21:55:38 +08:00
|
|
|
#include "llvm/Testing/Support/SupportHelpers.h"
|
2019-09-25 18:30:22 +08:00
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
TEST(HeaderSourceSwitchTest, FileHeuristic) {
|
2020-06-17 17:53:32 +08:00
|
|
|
MockFS FS;
|
2019-09-25 18:30:22 +08:00
|
|
|
auto FooCpp = testPath("foo.cpp");
|
|
|
|
auto FooH = testPath("foo.h");
|
|
|
|
auto Invalid = testPath("main.cpp");
|
|
|
|
|
|
|
|
FS.Files[FooCpp];
|
|
|
|
FS.Files[FooH];
|
|
|
|
FS.Files[Invalid];
|
|
|
|
Optional<Path> PathResult =
|
2020-06-17 17:53:32 +08:00
|
|
|
getCorrespondingHeaderOrSource(FooCpp, FS.view(llvm::None));
|
2022-07-13 13:47:41 +08:00
|
|
|
EXPECT_TRUE(PathResult.has_value());
|
2022-07-16 11:03:13 +08:00
|
|
|
ASSERT_EQ(PathResult.value(), FooH);
|
2019-09-25 18:30:22 +08:00
|
|
|
|
2020-06-17 17:53:32 +08:00
|
|
|
PathResult = getCorrespondingHeaderOrSource(FooH, FS.view(llvm::None));
|
2022-07-13 13:47:41 +08:00
|
|
|
EXPECT_TRUE(PathResult.has_value());
|
2022-07-16 11:03:13 +08:00
|
|
|
ASSERT_EQ(PathResult.value(), FooCpp);
|
2019-09-25 18:30:22 +08:00
|
|
|
|
|
|
|
// Test with header file in capital letters and different extension, source
|
|
|
|
// file with different extension
|
|
|
|
auto FooC = testPath("bar.c");
|
|
|
|
auto FooHH = testPath("bar.HH");
|
|
|
|
|
|
|
|
FS.Files[FooC];
|
|
|
|
FS.Files[FooHH];
|
2020-06-17 17:53:32 +08:00
|
|
|
PathResult = getCorrespondingHeaderOrSource(FooC, FS.view(llvm::None));
|
2022-07-13 13:47:41 +08:00
|
|
|
EXPECT_TRUE(PathResult.has_value());
|
2022-07-16 11:03:13 +08:00
|
|
|
ASSERT_EQ(PathResult.value(), FooHH);
|
2019-09-25 18:30:22 +08:00
|
|
|
|
|
|
|
// Test with both capital letters
|
|
|
|
auto Foo2C = testPath("foo2.C");
|
|
|
|
auto Foo2HH = testPath("foo2.HH");
|
|
|
|
FS.Files[Foo2C];
|
|
|
|
FS.Files[Foo2HH];
|
2020-06-17 17:53:32 +08:00
|
|
|
PathResult = getCorrespondingHeaderOrSource(Foo2C, FS.view(llvm::None));
|
2022-07-13 13:47:41 +08:00
|
|
|
EXPECT_TRUE(PathResult.has_value());
|
2022-07-16 11:03:13 +08:00
|
|
|
ASSERT_EQ(PathResult.value(), Foo2HH);
|
2019-09-25 18:30:22 +08:00
|
|
|
|
|
|
|
// Test with source file as capital letter and .hxx header file
|
|
|
|
auto Foo3C = testPath("foo3.C");
|
|
|
|
auto Foo3HXX = testPath("foo3.hxx");
|
|
|
|
|
|
|
|
FS.Files[Foo3C];
|
|
|
|
FS.Files[Foo3HXX];
|
2020-06-17 17:53:32 +08:00
|
|
|
PathResult = getCorrespondingHeaderOrSource(Foo3C, FS.view(llvm::None));
|
2022-07-13 13:47:41 +08:00
|
|
|
EXPECT_TRUE(PathResult.has_value());
|
2022-07-16 11:03:13 +08:00
|
|
|
ASSERT_EQ(PathResult.value(), Foo3HXX);
|
2019-09-25 18:30:22 +08:00
|
|
|
|
|
|
|
// Test if asking for a corresponding file that doesn't exist returns an empty
|
|
|
|
// string.
|
2020-06-17 17:53:32 +08:00
|
|
|
PathResult = getCorrespondingHeaderOrSource(Invalid, FS.view(llvm::None));
|
2022-07-13 13:47:41 +08:00
|
|
|
EXPECT_FALSE(PathResult.has_value());
|
2019-09-25 18:30:22 +08:00
|
|
|
}
|
|
|
|
|
2022-02-01 18:14:07 +08:00
|
|
|
MATCHER_P(declNamed, Name, "") {
|
2019-09-30 18:48:02 +08:00
|
|
|
if (const NamedDecl *ND = dyn_cast<NamedDecl>(arg))
|
|
|
|
if (ND->getQualifiedNameAsString() == Name)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(HeaderSourceSwitchTest, GetLocalDecls) {
|
|
|
|
TestTU TU;
|
|
|
|
TU.HeaderCode = R"cpp(
|
|
|
|
void HeaderOnly();
|
|
|
|
)cpp";
|
|
|
|
TU.Code = R"cpp(
|
|
|
|
void MainF1();
|
|
|
|
class Foo {};
|
|
|
|
namespace ns {
|
|
|
|
class Foo {
|
|
|
|
void method();
|
|
|
|
int field;
|
|
|
|
};
|
|
|
|
} // namespace ns
|
|
|
|
|
|
|
|
// Non-indexable symbols
|
|
|
|
namespace {
|
|
|
|
void Ignore1() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
)cpp";
|
|
|
|
|
|
|
|
auto AST = TU.build();
|
|
|
|
EXPECT_THAT(getIndexableLocalDecls(AST),
|
|
|
|
testing::UnorderedElementsAre(
|
2022-02-01 18:14:07 +08:00
|
|
|
declNamed("MainF1"), declNamed("Foo"), declNamed("ns::Foo"),
|
|
|
|
declNamed("ns::Foo::method"), declNamed("ns::Foo::field")));
|
2019-09-30 18:48:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(HeaderSourceSwitchTest, FromHeaderToSource) {
|
|
|
|
// build a proper index, which contains symbols:
|
|
|
|
// A_Sym1, declared in TestTU.h, defined in a.cpp
|
|
|
|
// B_Sym[1-2], declared in TestTU.h, defined in b.cpp
|
|
|
|
SymbolSlab::Builder AllSymbols;
|
|
|
|
TestTU Testing;
|
|
|
|
Testing.HeaderFilename = "TestTU.h";
|
|
|
|
Testing.HeaderCode = "void A_Sym1();";
|
|
|
|
Testing.Filename = "a.cpp";
|
|
|
|
Testing.Code = "void A_Sym1() {};";
|
|
|
|
for (auto &Sym : Testing.headerSymbols())
|
|
|
|
AllSymbols.insert(Sym);
|
|
|
|
|
|
|
|
Testing.HeaderCode = R"cpp(
|
|
|
|
void B_Sym1();
|
|
|
|
void B_Sym2();
|
2019-10-07 19:37:25 +08:00
|
|
|
void B_Sym3_NoDef();
|
2019-09-30 18:48:02 +08:00
|
|
|
)cpp";
|
|
|
|
Testing.Filename = "b.cpp";
|
|
|
|
Testing.Code = R"cpp(
|
|
|
|
void B_Sym1() {}
|
|
|
|
void B_Sym2() {}
|
|
|
|
)cpp";
|
|
|
|
for (auto &Sym : Testing.headerSymbols())
|
|
|
|
AllSymbols.insert(Sym);
|
|
|
|
auto Index = MemIndex::build(std::move(AllSymbols).build(), {}, {});
|
|
|
|
|
2020-04-05 14:28:11 +08:00
|
|
|
// Test for switch from .h header to .cc source
|
2019-09-30 18:48:02 +08:00
|
|
|
struct {
|
|
|
|
llvm::StringRef HeaderCode;
|
|
|
|
llvm::Optional<std::string> ExpectedSource;
|
|
|
|
} TestCases[] = {
|
|
|
|
{"// empty, no header found", llvm::None},
|
|
|
|
{R"cpp(
|
|
|
|
// no definition found in the index.
|
|
|
|
void NonDefinition();
|
|
|
|
)cpp",
|
|
|
|
llvm::None},
|
|
|
|
{R"cpp(
|
|
|
|
void A_Sym1();
|
|
|
|
)cpp",
|
|
|
|
testPath("a.cpp")},
|
|
|
|
{R"cpp(
|
|
|
|
// b.cpp wins.
|
|
|
|
void A_Sym1();
|
|
|
|
void B_Sym1();
|
|
|
|
void B_Sym2();
|
|
|
|
)cpp",
|
|
|
|
testPath("b.cpp")},
|
|
|
|
{R"cpp(
|
|
|
|
// a.cpp and b.cpp have same scope, but a.cpp because "a.cpp" < "b.cpp".
|
|
|
|
void A_Sym1();
|
|
|
|
void B_Sym1();
|
|
|
|
)cpp",
|
|
|
|
testPath("a.cpp")},
|
2019-10-07 19:37:25 +08:00
|
|
|
|
|
|
|
{R"cpp(
|
|
|
|
// We don't have definition in the index, so stay in the header.
|
|
|
|
void B_Sym3_NoDef();
|
|
|
|
)cpp",
|
|
|
|
None},
|
2019-09-30 18:48:02 +08:00
|
|
|
};
|
|
|
|
for (const auto &Case : TestCases) {
|
|
|
|
TestTU TU = TestTU::withCode(Case.HeaderCode);
|
|
|
|
TU.Filename = "TestTU.h";
|
|
|
|
TU.ExtraArgs.push_back("-xc++-header"); // inform clang this is a header.
|
|
|
|
auto HeaderAST = TU.build();
|
|
|
|
EXPECT_EQ(Case.ExpectedSource,
|
|
|
|
getCorrespondingHeaderOrSource(testPath(TU.Filename), HeaderAST,
|
|
|
|
Index.get()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(HeaderSourceSwitchTest, FromSourceToHeader) {
|
|
|
|
// build a proper index, which contains symbols:
|
|
|
|
// A_Sym1, declared in a.h, defined in TestTU.cpp
|
|
|
|
// B_Sym[1-2], declared in b.h, defined in TestTU.cpp
|
|
|
|
TestTU TUForIndex = TestTU::withCode(R"cpp(
|
|
|
|
#include "a.h"
|
|
|
|
#include "b.h"
|
|
|
|
|
|
|
|
void A_Sym1() {}
|
|
|
|
|
|
|
|
void B_Sym1() {}
|
|
|
|
void B_Sym2() {}
|
|
|
|
)cpp");
|
|
|
|
TUForIndex.AdditionalFiles["a.h"] = R"cpp(
|
|
|
|
void A_Sym1();
|
|
|
|
)cpp";
|
|
|
|
TUForIndex.AdditionalFiles["b.h"] = R"cpp(
|
|
|
|
void B_Sym1();
|
|
|
|
void B_Sym2();
|
|
|
|
)cpp";
|
|
|
|
TUForIndex.Filename = "TestTU.cpp";
|
|
|
|
auto Index = TUForIndex.index();
|
|
|
|
|
|
|
|
// Test for switching from .cc source file to .h header.
|
|
|
|
struct {
|
|
|
|
llvm::StringRef SourceCode;
|
|
|
|
llvm::Optional<std::string> ExpectedResult;
|
|
|
|
} TestCases[] = {
|
|
|
|
{"// empty, no header found", llvm::None},
|
|
|
|
{R"cpp(
|
|
|
|
// symbol not in index, no header found
|
|
|
|
void Local() {}
|
|
|
|
)cpp",
|
|
|
|
llvm::None},
|
|
|
|
|
|
|
|
{R"cpp(
|
|
|
|
// a.h wins.
|
|
|
|
void A_Sym1() {}
|
|
|
|
)cpp",
|
|
|
|
testPath("a.h")},
|
|
|
|
|
|
|
|
{R"cpp(
|
|
|
|
// b.h wins.
|
|
|
|
void A_Sym1() {}
|
|
|
|
void B_Sym1() {}
|
|
|
|
void B_Sym2() {}
|
|
|
|
)cpp",
|
|
|
|
testPath("b.h")},
|
|
|
|
|
|
|
|
{R"cpp(
|
|
|
|
// a.h and b.h have same scope, but a.h wins because "a.h" < "b.h".
|
|
|
|
void A_Sym1() {}
|
|
|
|
void B_Sym1() {}
|
|
|
|
)cpp",
|
|
|
|
testPath("a.h")},
|
|
|
|
};
|
|
|
|
for (const auto &Case : TestCases) {
|
|
|
|
TestTU TU = TestTU::withCode(Case.SourceCode);
|
|
|
|
TU.Filename = "Test.cpp";
|
|
|
|
auto AST = TU.build();
|
|
|
|
EXPECT_EQ(Case.ExpectedResult,
|
|
|
|
getCorrespondingHeaderOrSource(testPath(TU.Filename), AST,
|
|
|
|
Index.get()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-01 18:21:15 +08:00
|
|
|
TEST(HeaderSourceSwitchTest, ClangdServerIntegration) {
|
|
|
|
MockCompilationDatabase CDB;
|
|
|
|
CDB.ExtraClangFlags = {"-I" +
|
|
|
|
testPath("src/include")}; // add search directory.
|
2020-06-17 17:53:32 +08:00
|
|
|
MockFS FS;
|
2019-10-01 18:21:15 +08:00
|
|
|
// File heuristic fails here, we rely on the index to find the .h file.
|
|
|
|
std::string CppPath = testPath("src/lib/test.cpp");
|
|
|
|
std::string HeaderPath = testPath("src/include/test.h");
|
|
|
|
FS.Files[HeaderPath] = "void foo();";
|
|
|
|
const std::string FileContent = R"cpp(
|
|
|
|
#include "test.h"
|
|
|
|
void foo() {};
|
|
|
|
)cpp";
|
|
|
|
FS.Files[CppPath] = FileContent;
|
|
|
|
auto Options = ClangdServer::optsForTest();
|
|
|
|
Options.BuildDynamicSymbolIndex = true;
|
2020-01-24 21:08:56 +08:00
|
|
|
ClangdServer Server(CDB, FS, Options);
|
2019-10-01 18:21:15 +08:00
|
|
|
runAddDocument(Server, CppPath, FileContent);
|
|
|
|
EXPECT_EQ(HeaderPath,
|
|
|
|
*llvm::cantFail(runSwitchHeaderSource(Server, CppPath)));
|
|
|
|
}
|
|
|
|
|
2022-03-09 21:55:38 +08:00
|
|
|
TEST(HeaderSourceSwitchTest, CaseSensitivity) {
|
|
|
|
TestTU TU = TestTU::withCode("void foo() {}");
|
|
|
|
// Define more symbols in the header than the source file to trick heuristics
|
|
|
|
// into picking the header as source file, if the matching for header file
|
|
|
|
// path fails.
|
|
|
|
TU.HeaderCode = R"cpp(
|
|
|
|
inline void bar1() {}
|
|
|
|
inline void bar2() {}
|
|
|
|
void foo();)cpp";
|
|
|
|
// Give main file and header different base names to make sure file system
|
|
|
|
// heuristics don't work.
|
|
|
|
TU.Filename = "Source.cpp";
|
|
|
|
TU.HeaderFilename = "Header.h";
|
|
|
|
|
|
|
|
auto Index = TU.index();
|
|
|
|
TU.Code = std::move(TU.HeaderCode);
|
|
|
|
TU.HeaderCode.clear();
|
|
|
|
auto AST = TU.build();
|
|
|
|
|
|
|
|
// Provide a different-cased filename in the query than what we have in the
|
|
|
|
// index, check if we can still find the source file, which defines less
|
|
|
|
// symbols than the header.
|
|
|
|
auto HeaderAbsPath = testPath("HEADER.H");
|
|
|
|
// We expect the heuristics to pick:
|
|
|
|
// - header on case sensitive file systems, because the HeaderAbsPath doesn't
|
|
|
|
// match what we've seen through index.
|
|
|
|
// - source on case insensitive file systems, as the HeaderAbsPath would match
|
|
|
|
// the filename in index.
|
|
|
|
#ifdef CLANGD_PATH_CASE_INSENSITIVE
|
|
|
|
EXPECT_THAT(getCorrespondingHeaderOrSource(HeaderAbsPath, AST, Index.get()),
|
|
|
|
llvm::ValueIs(testing::StrCaseEq(testPath(TU.Filename))));
|
|
|
|
#else
|
|
|
|
EXPECT_THAT(getCorrespondingHeaderOrSource(HeaderAbsPath, AST, Index.get()),
|
|
|
|
llvm::ValueIs(testing::StrCaseEq(testPath(TU.HeaderFilename))));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-09-25 18:30:22 +08:00
|
|
|
} // namespace
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|