1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
//===--- 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"
#include "SyncAPI.h"
#include "TestFS.h"
#include "TestTU.h"
#include "index/MemIndex.h"
#include "llvm/ADT/None.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace clang {
namespace clangd {
namespace {
TEST(HeaderSourceSwitchTest, FileHeuristic) {
MockFS FS;
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 =
getCorrespondingHeaderOrSource(FooCpp, FS.view(llvm::None));
EXPECT_TRUE(PathResult.hasValue());
ASSERT_EQ(PathResult.getValue(), FooH);
PathResult = getCorrespondingHeaderOrSource(FooH, FS.view(llvm::None));
EXPECT_TRUE(PathResult.hasValue());
ASSERT_EQ(PathResult.getValue(), FooCpp);
// 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];
PathResult = getCorrespondingHeaderOrSource(FooC, FS.view(llvm::None));
EXPECT_TRUE(PathResult.hasValue());
ASSERT_EQ(PathResult.getValue(), FooHH);
// Test with both capital letters
auto Foo2C = testPath("foo2.C");
auto Foo2HH = testPath("foo2.HH");
FS.Files[Foo2C];
FS.Files[Foo2HH];
PathResult = getCorrespondingHeaderOrSource(Foo2C, FS.view(llvm::None));
EXPECT_TRUE(PathResult.hasValue());
ASSERT_EQ(PathResult.getValue(), Foo2HH);
// 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];
PathResult = getCorrespondingHeaderOrSource(Foo3C, FS.view(llvm::None));
EXPECT_TRUE(PathResult.hasValue());
ASSERT_EQ(PathResult.getValue(), Foo3HXX);
// Test if asking for a corresponding file that doesn't exist returns an empty
// string.
PathResult = getCorrespondingHeaderOrSource(Invalid, FS.view(llvm::None));
EXPECT_FALSE(PathResult.hasValue());
}
MATCHER_P(DeclNamed, Name, "") {
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(
DeclNamed("MainF1"), DeclNamed("Foo"), DeclNamed("ns::Foo"),
DeclNamed("ns::Foo::method"), DeclNamed("ns::Foo::field")));
}
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();
void B_Sym3_NoDef();
)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(), {}, {});
// Test for switch from .h header to .cc source
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")},
{R"cpp(
// We don't have definition in the index, so stay in the header.
void B_Sym3_NoDef();
)cpp",
None},
};
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()));
}
}
TEST(HeaderSourceSwitchTest, ClangdServerIntegration) {
MockCompilationDatabase CDB;
CDB.ExtraClangFlags = {"-I" +
testPath("src/include")}; // add search directory.
MockFS FS;
// 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;
ClangdServer Server(CDB, FS, Options);
runAddDocument(Server, CppPath, FileContent);
EXPECT_EQ(HeaderPath,
*llvm::cantFail(runSwitchHeaderSource(Server, CppPath)));
}
} // namespace
} // namespace clangd
} // namespace clang
|