| 1 |
//===-- ConfigCompileTests.cpp --------------------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "Config.h" |
| 10 |
#include "ConfigFragment.h" |
| 11 |
#include "ConfigTesting.h" |
| 12 |
#include "Diagnostics.h" |
| 13 |
#include "Feature.h" |
| 14 |
#include "TestFS.h" |
| 15 |
#include "clang/Basic/DiagnosticSema.h" |
| 16 |
#include "llvm/ADT/StringRef.h" |
| 17 |
#include "llvm/Support/Path.h" |
| 18 |
#include "llvm/Support/SourceMgr.h" |
| 19 |
#include "gmock/gmock.h" |
| 20 |
#include "gtest/gtest.h" |
| 21 |
#include <optional> |
| 22 |
#include <string> |
| 23 |
|
| 24 |
namespace clang { |
| 25 |
namespace clangd { |
| 26 |
namespace config { |
| 27 |
namespace { |
| 28 |
using ::testing::AllOf; |
| 29 |
using ::testing::Contains; |
| 30 |
using ::testing::ElementsAre; |
| 31 |
using ::testing::IsEmpty; |
| 32 |
using ::testing::SizeIs; |
| 33 |
using ::testing::StartsWith; |
| 34 |
using ::testing::UnorderedElementsAre; |
| 35 |
|
| 36 |
class ConfigCompileTests : public ::testing::Test { |
| 37 |
protected: |
| 38 |
CapturedDiags Diags; |
| 39 |
Config Conf; |
| 40 |
Fragment Frag; |
| 41 |
Params Parm; |
| 42 |
|
| 43 |
bool compileAndApply() { |
| 44 |
Conf = Config(); |
| 45 |
Diags.Diagnostics.clear(); |
| 46 |
auto Compiled = std::move(Frag).compile(Diags.callback()); |
| 47 |
return Compiled(Parm, Conf); |
| 48 |
} |
| 49 |
}; |
| 50 |
|
| 51 |
TEST_F(ConfigCompileTests, Condition) { |
| 52 |
// No condition. |
| 53 |
Frag = {}; |
| 54 |
Frag.CompileFlags.Add.emplace_back("X"); |
| 55 |
EXPECT_TRUE(compileAndApply()) << "Empty config"; |
| 56 |
EXPECT_THAT(Diags.Diagnostics, IsEmpty()); |
| 57 |
EXPECT_THAT(Conf.CompileFlags.Edits, SizeIs(1)); |
| 58 |
|
| 59 |
// Regex with no file. |
| 60 |
Frag = {}; |
| 61 |
Frag.If.PathMatch.emplace_back("fo*"); |
| 62 |
EXPECT_FALSE(compileAndApply()); |
| 63 |
EXPECT_THAT(Diags.Diagnostics, IsEmpty()); |
| 64 |
EXPECT_THAT(Conf.CompileFlags.Edits, SizeIs(0)); |
| 65 |
|
| 66 |
// Following tests have a file path set. |
| 67 |
Parm.Path = "bar"; |
| 68 |
|
| 69 |
// Non-matching regex. |
| 70 |
Frag = {}; |
| 71 |
Frag.If.PathMatch.emplace_back("fo*"); |
| 72 |
EXPECT_FALSE(compileAndApply()); |
| 73 |
EXPECT_THAT(Diags.Diagnostics, IsEmpty()); |
| 74 |
|
| 75 |
// Matching regex. |
| 76 |
Frag = {}; |
| 77 |
Frag.If.PathMatch.emplace_back("fo*"); |
| 78 |
Frag.If.PathMatch.emplace_back("ba*r"); |
| 79 |
EXPECT_TRUE(compileAndApply()); |
| 80 |
EXPECT_THAT(Diags.Diagnostics, IsEmpty()); |
| 81 |
|
| 82 |
// Excluded regex. |
| 83 |
Frag = {}; |
| 84 |
Frag.If.PathMatch.emplace_back("b.*"); |
| 85 |
Frag.If.PathExclude.emplace_back(".*r"); |
| 86 |
EXPECT_FALSE(compileAndApply()) << "Included but also excluded"; |
| 87 |
EXPECT_THAT(Diags.Diagnostics, IsEmpty()); |
| 88 |
|
| 89 |
// Invalid regex. |
| 90 |
Frag = {}; |
| 91 |
Frag.If.PathMatch.emplace_back("**]@theu"); |
| 92 |
EXPECT_TRUE(compileAndApply()); |
| 93 |
EXPECT_THAT(Diags.Diagnostics, SizeIs(1)); |
| 94 |
EXPECT_THAT(Diags.Diagnostics.front().Message, StartsWith("Invalid regex")); |
| 95 |
|
| 96 |
// Valid regex and unknown key. |
| 97 |
Frag = {}; |
| 98 |
Frag.If.HasUnrecognizedCondition = true; |
| 99 |
Frag.If.PathMatch.emplace_back("ba*r"); |
| 100 |
EXPECT_FALSE(compileAndApply()); |
| 101 |
EXPECT_THAT(Diags.Diagnostics, IsEmpty()); |
| 102 |
|
| 103 |
// Only matches case-insensitively. |
| 104 |
Frag = {}; |
| 105 |
Frag.If.PathMatch.emplace_back("B.*R"); |
| 106 |
EXPECT_THAT(Diags.Diagnostics, IsEmpty()); |
| 107 |
#ifdef CLANGD_PATH_CASE_INSENSITIVE |
| 108 |
EXPECT_TRUE(compileAndApply()); |
| 109 |
#else |
| 110 |
EXPECT_FALSE(compileAndApply()); |
| 111 |
#endif |
| 112 |
|
| 113 |
Frag = {}; |
| 114 |
Frag.If.PathExclude.emplace_back("B.*R"); |
| 115 |
EXPECT_THAT(Diags.Diagnostics, IsEmpty()); |
| 116 |
#ifdef CLANGD_PATH_CASE_INSENSITIVE |
| 117 |
EXPECT_FALSE(compileAndApply()); |
| 118 |
#else |
| 119 |
EXPECT_TRUE(compileAndApply()); |
| 120 |
#endif |
| 121 |
} |
| 122 |
|
| 123 |
TEST_F(ConfigCompileTests, CompileCommands) { |
| 124 |
Frag.CompileFlags.Compiler.emplace("tpc.exe"); |
| 125 |
Frag.CompileFlags.Add.emplace_back("-foo"); |
| 126 |
Frag.CompileFlags.Remove.emplace_back("--include-directory="); |
| 127 |
std::vector<std::string> Argv = {"clang", "-I", "bar/", "--", "a.cc"}; |
| 128 |
EXPECT_TRUE(compileAndApply()); |
| 129 |
EXPECT_THAT(Conf.CompileFlags.Edits, SizeIs(3)); |
| 130 |
for (auto &Edit : Conf.CompileFlags.Edits) |
| 131 |
Edit(Argv); |
| 132 |
EXPECT_THAT(Argv, ElementsAre("tpc.exe", "-foo", "--", "a.cc")); |
| 133 |
} |
| 134 |
|
| 135 |
TEST_F(ConfigCompileTests, CompilationDatabase) { |
| 136 |
Frag.CompileFlags.CompilationDatabase.emplace("None"); |
| 137 |
EXPECT_TRUE(compileAndApply()); |
| 138 |
EXPECT_EQ(Conf.CompileFlags.CDBSearch.Policy, |
| 139 |
Config::CDBSearchSpec::NoCDBSearch); |
| 140 |
|
| 141 |
Frag.CompileFlags.CompilationDatabase.emplace("Ancestors"); |
| 142 |
EXPECT_TRUE(compileAndApply()); |
| 143 |
EXPECT_EQ(Conf.CompileFlags.CDBSearch.Policy, |
| 144 |
Config::CDBSearchSpec::Ancestors); |
| 145 |
|
| 146 |
// Relative path not allowed without directory set. |
| 147 |
Frag.CompileFlags.CompilationDatabase.emplace("Something"); |
| 148 |
EXPECT_TRUE(compileAndApply()); |
| 149 |
EXPECT_EQ(Conf.CompileFlags.CDBSearch.Policy, |
| 150 |
Config::CDBSearchSpec::Ancestors) |
| 151 |
<< "default value"; |
| 152 |
EXPECT_THAT(Diags.Diagnostics, |
| 153 |
ElementsAre(diagMessage( |
| 154 |
"CompilationDatabase must be an absolute path, because this " |
| 155 |
"fragment is not associated with any directory."))); |
| 156 |
|
| 157 |
// Relative path allowed if directory is set. |
| 158 |
Frag.Source.Directory = testRoot(); |
| 159 |
EXPECT_TRUE(compileAndApply()); |
| 160 |
EXPECT_EQ(Conf.CompileFlags.CDBSearch.Policy, |
| 161 |
Config::CDBSearchSpec::FixedDir); |
| 162 |
EXPECT_EQ(Conf.CompileFlags.CDBSearch.FixedCDBPath, testPath("Something")); |
| 163 |
EXPECT_THAT(Diags.Diagnostics, IsEmpty()); |
| 164 |
|
| 165 |
// Absolute path allowed. |
| 166 |
Frag.Source.Directory.clear(); |
| 167 |
Frag.CompileFlags.CompilationDatabase.emplace(testPath("Something2")); |
| 168 |
EXPECT_TRUE(compileAndApply()); |
| 169 |
EXPECT_EQ(Conf.CompileFlags.CDBSearch.Policy, |
| 170 |
Config::CDBSearchSpec::FixedDir); |
| 171 |
EXPECT_EQ(Conf.CompileFlags.CDBSearch.FixedCDBPath, testPath("Something2")); |
| 172 |
EXPECT_THAT(Diags.Diagnostics, IsEmpty()); |
| 173 |
} |
| 174 |
|
| 175 |
TEST_F(ConfigCompileTests, Index) { |
| 176 |
Frag.Index.Background.emplace("Skip"); |
| 177 |
EXPECT_TRUE(compileAndApply()); |
| 178 |
EXPECT_EQ(Conf.Index.Background, Config::BackgroundPolicy::Skip); |
| 179 |
|
| 180 |
Frag = {}; |
| 181 |
Frag.Index.Background.emplace("Foo"); |
| 182 |
EXPECT_TRUE(compileAndApply()); |
| 183 |
EXPECT_EQ(Conf.Index.Background, Config::BackgroundPolicy::Build) |
| 184 |
<< "by default"; |
| 185 |
EXPECT_THAT( |
| 186 |
Diags.Diagnostics, |
| 187 |
ElementsAre(diagMessage( |
| 188 |
"Invalid Background value 'Foo'. Valid values are Build, Skip."))); |
| 189 |
} |
| 190 |
|
| 191 |
TEST_F(ConfigCompileTests, PathSpecMatch) { |
| 192 |
auto BarPath = llvm::sys::path::convert_to_slash(testPath("foo/bar.h")); |
| 193 |
Parm.Path = BarPath; |
| 194 |
|
| 195 |
struct { |
| 196 |
std::string Directory; |
| 197 |
std::string PathSpec; |
| 198 |
bool ShouldMatch; |
| 199 |
} Cases[] = { |
| 200 |
{ |
| 201 |
// Absolute path matches. |
| 202 |
"", |
| 203 |
llvm::sys::path::convert_to_slash(testPath("foo/bar.h")), |
| 204 |
true, |
| 205 |
}, |
| 206 |
{ |
| 207 |
// Absolute path fails. |
| 208 |
"", |
| 209 |
llvm::sys::path::convert_to_slash(testPath("bar/bar.h")), |
| 210 |
false, |
| 211 |
}, |
| 212 |
{ |
| 213 |
// Relative should fail to match as /foo/bar.h doesn't reside under |
| 214 |
// /baz/. |
| 215 |
testPath("baz"), |
| 216 |
"bar\\.h", |
| 217 |
false, |
| 218 |
}, |
| 219 |
{ |
| 220 |
// Relative should pass with /foo as directory. |
| 221 |
testPath("foo"), |
| 222 |
"bar\\.h", |
| 223 |
true, |
| 224 |
}, |
| 225 |
}; |
| 226 |
|
| 227 |
// PathMatch |
| 228 |
for (const auto &Case : Cases) { |
| 229 |
Frag = {}; |
| 230 |
Frag.If.PathMatch.emplace_back(Case.PathSpec); |
| 231 |
Frag.Source.Directory = Case.Directory; |
| 232 |
EXPECT_EQ(compileAndApply(), Case.ShouldMatch); |
| 233 |
ASSERT_THAT(Diags.Diagnostics, IsEmpty()); |
| 234 |
} |
| 235 |
|
| 236 |
// PathEclude |
| 237 |
for (const auto &Case : Cases) { |
| 238 |
SCOPED_TRACE(Case.Directory); |
| 239 |
SCOPED_TRACE(Case.PathSpec); |
| 240 |
Frag = {}; |
| 241 |
Frag.If.PathExclude.emplace_back(Case.PathSpec); |
| 242 |
Frag.Source.Directory = Case.Directory; |
| 243 |
EXPECT_NE(compileAndApply(), Case.ShouldMatch); |
| 244 |
ASSERT_THAT(Diags.Diagnostics, IsEmpty()); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
TEST_F(ConfigCompileTests, DiagnosticsIncludeCleaner) { |
| 249 |
// Defaults to None. |
| 250 |
EXPECT_TRUE(compileAndApply()); |
| 251 |
EXPECT_EQ(Conf.Diagnostics.UnusedIncludes, |
| 252 |
Config::UnusedIncludesPolicy::None); |
| 253 |
|
| 254 |
Frag = {}; |
| 255 |
Frag.Diagnostics.UnusedIncludes.emplace("None"); |
| 256 |
EXPECT_TRUE(compileAndApply()); |
| 257 |
EXPECT_EQ(Conf.Diagnostics.UnusedIncludes, |
| 258 |
Config::UnusedIncludesPolicy::None); |
| 259 |
|
| 260 |
Frag = {}; |
| 261 |
Frag.Diagnostics.UnusedIncludes.emplace("Strict"); |
| 262 |
EXPECT_TRUE(compileAndApply()); |
| 263 |
EXPECT_EQ(Conf.Diagnostics.UnusedIncludes, |
| 264 |
Config::UnusedIncludesPolicy::Strict); |
| 265 |
|
| 266 |
Frag = {}; |
| 267 |
EXPECT_TRUE(Conf.Diagnostics.Includes.IgnoreHeader.empty()) |
| 268 |
<< Conf.Diagnostics.Includes.IgnoreHeader.size(); |
| 269 |
Frag.Diagnostics.Includes.IgnoreHeader.push_back( |
| 270 |
Located<std::string>("foo.h")); |
| 271 |
Frag.Diagnostics.Includes.IgnoreHeader.push_back( |
| 272 |
Located<std::string>(".*inc")); |
| 273 |
EXPECT_TRUE(compileAndApply()); |
| 274 |
auto HeaderFilter = [this](llvm::StringRef Path) { |
| 275 |
for (auto &Filter : Conf.Diagnostics.Includes.IgnoreHeader) { |
| 276 |
if (Filter(Path)) |
| 277 |
return true; |
| 278 |
} |
| 279 |
return false; |
| 280 |
}; |
| 281 |
EXPECT_TRUE(HeaderFilter("foo.h")); |
| 282 |
EXPECT_FALSE(HeaderFilter("bar.h")); |
| 283 |
} |
| 284 |
|
| 285 |
TEST_F(ConfigCompileTests, DiagnosticSuppression) { |
| 286 |
Frag.Diagnostics.Suppress.emplace_back("bugprone-use-after-move"); |
| 287 |
Frag.Diagnostics.Suppress.emplace_back("unreachable-code"); |
| 288 |
Frag.Diagnostics.Suppress.emplace_back("-Wunused-variable"); |
| 289 |
Frag.Diagnostics.Suppress.emplace_back("typecheck_bool_condition"); |
| 290 |
Frag.Diagnostics.Suppress.emplace_back("err_unexpected_friend"); |
| 291 |
Frag.Diagnostics.Suppress.emplace_back("warn_alloca"); |
| 292 |
EXPECT_TRUE(compileAndApply()); |
| 293 |
EXPECT_THAT(Conf.Diagnostics.Suppress.keys(), |
| 294 |
UnorderedElementsAre("bugprone-use-after-move", |
| 295 |
"unreachable-code", "unused-variable", |
| 296 |
"typecheck_bool_condition", |
| 297 |
"unexpected_friend", "warn_alloca")); |
| 298 |
EXPECT_TRUE(isBuiltinDiagnosticSuppressed( |
| 299 |
diag::warn_unreachable, Conf.Diagnostics.Suppress, LangOptions())); |
| 300 |
// Subcategory not respected/suppressed. |
| 301 |
EXPECT_FALSE(isBuiltinDiagnosticSuppressed( |
| 302 |
diag::warn_unreachable_break, Conf.Diagnostics.Suppress, LangOptions())); |
| 303 |
EXPECT_TRUE(isBuiltinDiagnosticSuppressed( |
| 304 |
diag::warn_unused_variable, Conf.Diagnostics.Suppress, LangOptions())); |
| 305 |
EXPECT_TRUE(isBuiltinDiagnosticSuppressed(diag::err_typecheck_bool_condition, |
| 306 |
Conf.Diagnostics.Suppress, |
| 307 |
LangOptions())); |
| 308 |
EXPECT_TRUE(isBuiltinDiagnosticSuppressed( |
| 309 |
diag::err_unexpected_friend, Conf.Diagnostics.Suppress, LangOptions())); |
| 310 |
EXPECT_TRUE(isBuiltinDiagnosticSuppressed( |
| 311 |
diag::warn_alloca, Conf.Diagnostics.Suppress, LangOptions())); |
| 312 |
|
| 313 |
Frag.Diagnostics.Suppress.emplace_back("*"); |
| 314 |
EXPECT_TRUE(compileAndApply()); |
| 315 |
EXPECT_TRUE(Conf.Diagnostics.SuppressAll); |
| 316 |
EXPECT_THAT(Conf.Diagnostics.Suppress, IsEmpty()); |
| 317 |
} |
| 318 |
|
| 319 |
TEST_F(ConfigCompileTests, Tidy) { |
| 320 |
auto &Tidy = Frag.Diagnostics.ClangTidy; |
| 321 |
Tidy.Add.emplace_back("bugprone-use-after-move"); |
| 322 |
Tidy.Add.emplace_back("llvm-*"); |
| 323 |
Tidy.Remove.emplace_back("llvm-include-order"); |
| 324 |
Tidy.Remove.emplace_back("readability-*"); |
| 325 |
Tidy.CheckOptions.emplace_back( |
| 326 |
std::make_pair(std::string("StrictMode"), std::string("true"))); |
| 327 |
Tidy.CheckOptions.emplace_back(std::make_pair( |
| 328 |
std::string("example-check.ExampleOption"), std::string("0"))); |
| 329 |
EXPECT_TRUE(compileAndApply()); |
| 330 |
EXPECT_EQ(Conf.Diagnostics.ClangTidy.CheckOptions.size(), 2U); |
| 331 |
EXPECT_EQ(Conf.Diagnostics.ClangTidy.CheckOptions.lookup("StrictMode"), |
| 332 |
"true"); |
| 333 |
EXPECT_EQ(Conf.Diagnostics.ClangTidy.CheckOptions.lookup( |
| 334 |
"example-check.ExampleOption"), |
| 335 |
"0"); |
| 336 |
#if CLANGD_TIDY_CHECKS |
| 337 |
EXPECT_EQ( |
| 338 |
Conf.Diagnostics.ClangTidy.Checks, |
| 339 |
"bugprone-use-after-move,llvm-*,-llvm-include-order,-readability-*"); |
| 340 |
EXPECT_THAT(Diags.Diagnostics, IsEmpty()); |
| 341 |
#else // !CLANGD_TIDY_CHECKS |
| 342 |
EXPECT_EQ(Conf.Diagnostics.ClangTidy.Checks, "llvm-*,-readability-*"); |
| 343 |
EXPECT_THAT( |
| 344 |
Diags.Diagnostics, |
| 345 |
ElementsAre( |
| 346 |
diagMessage( |
| 347 |
"clang-tidy check 'bugprone-use-after-move' was not found"), |
| 348 |
diagMessage("clang-tidy check 'llvm-include-order' was not found"))); |
| 349 |
#endif |
| 350 |
} |
| 351 |
|
| 352 |
TEST_F(ConfigCompileTests, TidyBadChecks) { |
| 353 |
auto &Tidy = Frag.Diagnostics.ClangTidy; |
| 354 |
Tidy.Add.emplace_back("unknown-check"); |
| 355 |
Tidy.Remove.emplace_back("*"); |
| 356 |
Tidy.Remove.emplace_back("llvm-includeorder"); |
| 357 |
EXPECT_TRUE(compileAndApply()); |
| 358 |
// Ensure bad checks are stripped from the glob. |
| 359 |
EXPECT_EQ(Conf.Diagnostics.ClangTidy.Checks, "-*"); |
| 360 |
EXPECT_THAT( |
| 361 |
Diags.Diagnostics, |
| 362 |
ElementsAre( |
| 363 |
AllOf(diagMessage("clang-tidy check 'unknown-check' was not found"), |
| 364 |
diagKind(llvm::SourceMgr::DK_Warning)), |
| 365 |
AllOf( |
| 366 |
diagMessage("clang-tidy check 'llvm-includeorder' was not found"), |
| 367 |
diagKind(llvm::SourceMgr::DK_Warning)))); |
| 368 |
} |
| 369 |
|
| 370 |
TEST_F(ConfigCompileTests, ExternalServerNeedsTrusted) { |
| 371 |
Fragment::IndexBlock::ExternalBlock External; |
| 372 |
External.Server.emplace("xxx"); |
| 373 |
Frag.Index.External = std::move(External); |
| 374 |
compileAndApply(); |
| 375 |
EXPECT_THAT( |
| 376 |
Diags.Diagnostics, |
| 377 |
ElementsAre(diagMessage( |
| 378 |
"Remote index may not be specified by untrusted configuration. " |
| 379 |
"Copy this into user config to use it."))); |
| 380 |
EXPECT_EQ(Conf.Index.External.Kind, Config::ExternalIndexSpec::None); |
| 381 |
} |
| 382 |
|
| 383 |
TEST_F(ConfigCompileTests, ExternalBlockWarnOnMultipleSource) { |
| 384 |
Frag.Source.Trusted = true; |
| 385 |
Fragment::IndexBlock::ExternalBlock External; |
| 386 |
External.File.emplace(""); |
| 387 |
External.Server.emplace(""); |
| 388 |
Frag.Index.External = std::move(External); |
| 389 |
compileAndApply(); |
| 390 |
#ifdef CLANGD_ENABLE_REMOTE |
| 391 |
EXPECT_THAT( |
| 392 |
Diags.Diagnostics, |
| 393 |
Contains( |
| 394 |
AllOf(diagMessage("Exactly one of File, Server or None must be set."), |
| 395 |
diagKind(llvm::SourceMgr::DK_Error)))); |
| 396 |
#else |
| 397 |
ASSERT_TRUE(Conf.Index.External.hasValue()); |
| 398 |
EXPECT_EQ(Conf.Index.External->Kind, Config::ExternalIndexSpec::File); |
| 399 |
#endif |
| 400 |
} |
| 401 |
|
| 402 |
TEST_F(ConfigCompileTests, ExternalBlockDisableWithNone) { |
| 403 |
compileAndApply(); |
| 404 |
EXPECT_EQ(Conf.Index.External.Kind, Config::ExternalIndexSpec::None); |
| 405 |
|
| 406 |
Fragment::IndexBlock::ExternalBlock External; |
| 407 |
External.IsNone = true; |
| 408 |
Frag.Index.External = std::move(External); |
| 409 |
compileAndApply(); |
| 410 |
EXPECT_EQ(Conf.Index.External.Kind, Config::ExternalIndexSpec::None); |
| 411 |
} |
| 412 |
|
| 413 |
TEST_F(ConfigCompileTests, ExternalBlockErrOnNoSource) { |
| 414 |
Frag.Index.External.emplace(Fragment::IndexBlock::ExternalBlock{}); |
| 415 |
compileAndApply(); |
| 416 |
EXPECT_THAT( |
| 417 |
Diags.Diagnostics, |
| 418 |
Contains( |
| 419 |
AllOf(diagMessage("Exactly one of File, Server or None must be set."), |
| 420 |
diagKind(llvm::SourceMgr::DK_Error)))); |
| 421 |
} |
| 422 |
|
| 423 |
TEST_F(ConfigCompileTests, ExternalBlockDisablesBackgroundIndex) { |
| 424 |
auto BazPath = testPath("foo/bar/baz.h", llvm::sys::path::Style::posix); |
| 425 |
Parm.Path = BazPath; |
| 426 |
Frag.Index.Background.emplace("Build"); |
| 427 |
Fragment::IndexBlock::ExternalBlock External; |
| 428 |
External.File.emplace(testPath("foo")); |
| 429 |
External.MountPoint.emplace( |
| 430 |
testPath("foo/bar", llvm::sys::path::Style::posix)); |
| 431 |
Frag.Index.External = std::move(External); |
| 432 |
compileAndApply(); |
| 433 |
EXPECT_EQ(Conf.Index.Background, Config::BackgroundPolicy::Skip); |
| 434 |
} |
| 435 |
|
| 436 |
TEST_F(ConfigCompileTests, ExternalBlockMountPoint) { |
| 437 |
auto GetFrag = [](llvm::StringRef Directory, |
| 438 |
std::optional<const char *> MountPoint) { |
| 439 |
Fragment Frag; |
| 440 |
Frag.Source.Directory = Directory.str(); |
| 441 |
Fragment::IndexBlock::ExternalBlock External; |
| 442 |
External.File.emplace(testPath("foo")); |
| 443 |
if (MountPoint) |
| 444 |
External.MountPoint.emplace(*MountPoint); |
| 445 |
Frag.Index.External = std::move(External); |
| 446 |
return Frag; |
| 447 |
}; |
| 448 |
|
| 449 |
auto BarPath = testPath("foo/bar.h", llvm::sys::path::Style::posix); |
| 450 |
BarPath = llvm::sys::path::convert_to_slash(BarPath); |
| 451 |
Parm.Path = BarPath; |
| 452 |
// Non-absolute MountPoint without a directory raises an error. |
| 453 |
Frag = GetFrag("", "foo"); |
| 454 |
compileAndApply(); |
| 455 |
ASSERT_THAT( |
| 456 |
Diags.Diagnostics, |
| 457 |
ElementsAre( |
| 458 |
AllOf(diagMessage("MountPoint must be an absolute path, because this " |
| 459 |
"fragment is not associated with any directory."), |
| 460 |
diagKind(llvm::SourceMgr::DK_Error)))); |
| 461 |
EXPECT_EQ(Conf.Index.External.Kind, Config::ExternalIndexSpec::None); |
| 462 |
|
| 463 |
auto FooPath = testPath("foo/", llvm::sys::path::Style::posix); |
| 464 |
FooPath = llvm::sys::path::convert_to_slash(FooPath); |
| 465 |
// Ok when relative. |
| 466 |
Frag = GetFrag(testRoot(), "foo/"); |
| 467 |
compileAndApply(); |
| 468 |
ASSERT_THAT(Diags.Diagnostics, IsEmpty()); |
| 469 |
ASSERT_EQ(Conf.Index.External.Kind, Config::ExternalIndexSpec::File); |
| 470 |
EXPECT_THAT(Conf.Index.External.MountPoint, FooPath); |
| 471 |
|
| 472 |
// None defaults to ".". |
| 473 |
Frag = GetFrag(FooPath, std::nullopt); |
| 474 |
compileAndApply(); |
| 475 |
ASSERT_THAT(Diags.Diagnostics, IsEmpty()); |
| 476 |
ASSERT_EQ(Conf.Index.External.Kind, Config::ExternalIndexSpec::File); |
| 477 |
EXPECT_THAT(Conf.Index.External.MountPoint, FooPath); |
| 478 |
|
| 479 |
// Without a file, external index is empty. |
| 480 |
Parm.Path = ""; |
| 481 |
Frag = GetFrag("", FooPath.c_str()); |
| 482 |
compileAndApply(); |
| 483 |
ASSERT_THAT(Diags.Diagnostics, IsEmpty()); |
| 484 |
ASSERT_EQ(Conf.Index.External.Kind, Config::ExternalIndexSpec::None); |
| 485 |
|
| 486 |
// File outside MountPoint, no index. |
| 487 |
auto BazPath = testPath("bar/baz.h", llvm::sys::path::Style::posix); |
| 488 |
BazPath = llvm::sys::path::convert_to_slash(BazPath); |
| 489 |
Parm.Path = BazPath; |
| 490 |
Frag = GetFrag("", FooPath.c_str()); |
| 491 |
compileAndApply(); |
| 492 |
ASSERT_THAT(Diags.Diagnostics, IsEmpty()); |
| 493 |
ASSERT_EQ(Conf.Index.External.Kind, Config::ExternalIndexSpec::None); |
| 494 |
|
| 495 |
// File under MountPoint, index should be set. |
| 496 |
BazPath = testPath("foo/baz.h", llvm::sys::path::Style::posix); |
| 497 |
BazPath = llvm::sys::path::convert_to_slash(BazPath); |
| 498 |
Parm.Path = BazPath; |
| 499 |
Frag = GetFrag("", FooPath.c_str()); |
| 500 |
compileAndApply(); |
| 501 |
ASSERT_THAT(Diags.Diagnostics, IsEmpty()); |
| 502 |
ASSERT_EQ(Conf.Index.External.Kind, Config::ExternalIndexSpec::File); |
| 503 |
EXPECT_THAT(Conf.Index.External.MountPoint, FooPath); |
| 504 |
|
| 505 |
// Only matches case-insensitively. |
| 506 |
BazPath = testPath("fOo/baz.h", llvm::sys::path::Style::posix); |
| 507 |
BazPath = llvm::sys::path::convert_to_slash(BazPath); |
| 508 |
Parm.Path = BazPath; |
| 509 |
|
| 510 |
FooPath = testPath("FOO/", llvm::sys::path::Style::posix); |
| 511 |
FooPath = llvm::sys::path::convert_to_slash(FooPath); |
| 512 |
Frag = GetFrag("", FooPath.c_str()); |
| 513 |
compileAndApply(); |
| 514 |
ASSERT_THAT(Diags.Diagnostics, IsEmpty()); |
| 515 |
#ifdef CLANGD_PATH_CASE_INSENSITIVE |
| 516 |
ASSERT_EQ(Conf.Index.External.Kind, Config::ExternalIndexSpec::File); |
| 517 |
EXPECT_THAT(Conf.Index.External.MountPoint, FooPath); |
| 518 |
#else |
| 519 |
ASSERT_EQ(Conf.Index.External.Kind, Config::ExternalIndexSpec::None); |
| 520 |
#endif |
| 521 |
} |
| 522 |
|
| 523 |
TEST_F(ConfigCompileTests, AllScopes) { |
| 524 |
// Defaults to true. |
| 525 |
EXPECT_TRUE(compileAndApply()); |
| 526 |
EXPECT_TRUE(Conf.Completion.AllScopes); |
| 527 |
|
| 528 |
Frag = {}; |
| 529 |
Frag.Completion.AllScopes = false; |
| 530 |
EXPECT_TRUE(compileAndApply()); |
| 531 |
EXPECT_FALSE(Conf.Completion.AllScopes); |
| 532 |
|
| 533 |
Frag = {}; |
| 534 |
Frag.Completion.AllScopes = true; |
| 535 |
EXPECT_TRUE(compileAndApply()); |
| 536 |
EXPECT_TRUE(Conf.Completion.AllScopes); |
| 537 |
} |
| 538 |
|
| 539 |
TEST_F(ConfigCompileTests, Style) { |
| 540 |
Frag = {}; |
| 541 |
Frag.Style.FullyQualifiedNamespaces.push_back(std::string("foo")); |
| 542 |
Frag.Style.FullyQualifiedNamespaces.push_back(std::string("bar")); |
| 543 |
EXPECT_TRUE(compileAndApply()); |
| 544 |
EXPECT_THAT(Conf.Style.FullyQualifiedNamespaces, ElementsAre("foo", "bar")); |
| 545 |
} |
| 546 |
} // namespace |
| 547 |
} // namespace config |
| 548 |
} // namespace clangd |
| 549 |
} // namespace clang |