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"
28using ::testing::AllOf;
29using ::testing::Contains;
30using ::testing::ElementsAre;
31using ::testing::IsEmpty;
32using ::testing::SizeIs;
33using ::testing::StartsWith;
34using ::testing::UnorderedElementsAre;
36class ConfigCompileTests :
public ::testing::Test {
43 bool compileAndApply() {
45 Diags.Diagnostics.clear();
46 auto Compiled = std::move(Frag).compile(Diags.callback());
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));
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));
71 Frag.If.PathMatch.emplace_back(
"fo*");
72 EXPECT_FALSE(compileAndApply());
73 EXPECT_THAT(Diags.Diagnostics, IsEmpty());
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());
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());
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"));
98 Frag.If.HasUnrecognizedCondition =
true;
99 Frag.If.PathMatch.emplace_back(
"ba*r");
100 EXPECT_FALSE(compileAndApply());
101 EXPECT_THAT(Diags.Diagnostics, IsEmpty());
105 Frag.If.PathMatch.emplace_back(
"B.*R");
106 EXPECT_THAT(Diags.Diagnostics, IsEmpty());
107#ifdef CLANGD_PATH_CASE_INSENSITIVE
108 EXPECT_TRUE(compileAndApply());
110 EXPECT_FALSE(compileAndApply());
114 Frag.If.PathExclude.emplace_back(
"B.*R");
115 EXPECT_THAT(Diags.Diagnostics, IsEmpty());
116#ifdef CLANGD_PATH_CASE_INSENSITIVE
117 EXPECT_FALSE(compileAndApply());
119 EXPECT_TRUE(compileAndApply());
123TEST_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)
132 EXPECT_THAT(Argv, ElementsAre(
"tpc.exe",
"-foo",
"--",
"a.cc"));
135TEST_F(ConfigCompileTests, CompilationDatabase) {
136 Frag.CompileFlags.CompilationDatabase.emplace(
"None");
137 EXPECT_TRUE(compileAndApply());
138 EXPECT_EQ(
Conf.CompileFlags.CDBSearch.Policy,
141 Frag.CompileFlags.CompilationDatabase.emplace(
"Ancestors");
142 EXPECT_TRUE(compileAndApply());
143 EXPECT_EQ(
Conf.CompileFlags.CDBSearch.Policy,
147 Frag.CompileFlags.CompilationDatabase.emplace(
"Something");
148 EXPECT_TRUE(compileAndApply());
149 EXPECT_EQ(
Conf.CompileFlags.CDBSearch.Policy,
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.")));
159 EXPECT_TRUE(compileAndApply());
160 EXPECT_EQ(
Conf.CompileFlags.CDBSearch.Policy,
162 EXPECT_EQ(
Conf.CompileFlags.CDBSearch.FixedCDBPath,
testPath(
"Something"));
163 EXPECT_THAT(Diags.Diagnostics, IsEmpty());
166 Frag.Source.Directory.clear();
167 Frag.CompileFlags.CompilationDatabase.emplace(
testPath(
"Something2"));
168 EXPECT_TRUE(compileAndApply());
169 EXPECT_EQ(
Conf.CompileFlags.CDBSearch.Policy,
171 EXPECT_EQ(
Conf.CompileFlags.CDBSearch.FixedCDBPath,
testPath(
"Something2"));
172 EXPECT_THAT(Diags.Diagnostics, IsEmpty());
175TEST_F(ConfigCompileTests, Index) {
176 Frag.Index.Background.emplace(
"Skip");
177 EXPECT_TRUE(compileAndApply());
181 Frag.Index.Background.emplace(
"Foo");
182 EXPECT_TRUE(compileAndApply());
187 ElementsAre(diagMessage(
188 "Invalid Background value 'Foo'. Valid values are Build, Skip.")));
191TEST_F(ConfigCompileTests, PathSpecMatch) {
192 auto BarPath = llvm::sys::path::convert_to_slash(
testPath(
"foo/bar.h"));
197 std::string PathSpec;
203 llvm::sys::path::convert_to_slash(
testPath(
"foo/bar.h")),
209 llvm::sys::path::convert_to_slash(
testPath(
"bar/bar.h")),
228 for (
const auto &Case : Cases) {
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());
237 for (
const auto &Case : Cases) {
238 SCOPED_TRACE(Case.Directory);
239 SCOPED_TRACE(Case.PathSpec);
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());
248TEST_F(ConfigCompileTests, DiagnosticsIncludeCleaner) {
250 EXPECT_TRUE(compileAndApply());
254 Frag.Diagnostics.UnusedIncludes.emplace(
"None");
255 EXPECT_TRUE(compileAndApply());
259 Frag.Diagnostics.UnusedIncludes.emplace(
"Strict");
260 EXPECT_TRUE(compileAndApply());
264 EXPECT_TRUE(
Conf.Diagnostics.Includes.IgnoreHeader.empty())
265 <<
Conf.Diagnostics.Includes.IgnoreHeader.size();
266 Frag.Diagnostics.Includes.IgnoreHeader.push_back(
267 Located<std::string>(
"foo.h"));
268 Frag.Diagnostics.Includes.IgnoreHeader.push_back(
269 Located<std::string>(
".*inc"));
270 EXPECT_TRUE(compileAndApply());
272 for (
auto &Filter :
Conf.Diagnostics.Includes.IgnoreHeader) {
282TEST_F(ConfigCompileTests, DiagnosticSuppression) {
283 Frag.Diagnostics.Suppress.emplace_back(
"bugprone-use-after-move");
284 Frag.Diagnostics.Suppress.emplace_back(
"unreachable-code");
285 Frag.Diagnostics.Suppress.emplace_back(
"-Wunused-variable");
286 Frag.Diagnostics.Suppress.emplace_back(
"typecheck_bool_condition");
287 Frag.Diagnostics.Suppress.emplace_back(
"err_unexpected_friend");
288 Frag.Diagnostics.Suppress.emplace_back(
"warn_alloca");
289 EXPECT_TRUE(compileAndApply());
290 EXPECT_THAT(
Conf.Diagnostics.Suppress.keys(),
291 UnorderedElementsAre(
"bugprone-use-after-move",
292 "unreachable-code",
"unused-variable",
293 "typecheck_bool_condition",
294 "unexpected_friend",
"warn_alloca"));
296 diag::warn_unreachable,
Conf.Diagnostics.Suppress, LangOptions()));
299 diag::warn_unreachable_break,
Conf.Diagnostics.Suppress, LangOptions()));
301 diag::warn_unused_variable,
Conf.Diagnostics.Suppress, LangOptions()));
303 Conf.Diagnostics.Suppress,
306 diag::err_unexpected_friend,
Conf.Diagnostics.Suppress, LangOptions()));
308 diag::warn_alloca,
Conf.Diagnostics.Suppress, LangOptions()));
310 Frag.Diagnostics.Suppress.emplace_back(
"*");
311 EXPECT_TRUE(compileAndApply());
312 EXPECT_TRUE(
Conf.Diagnostics.SuppressAll);
313 EXPECT_THAT(
Conf.Diagnostics.Suppress, IsEmpty());
316TEST_F(ConfigCompileTests, Tidy) {
317 auto &Tidy = Frag.Diagnostics.ClangTidy;
318 Tidy.Add.emplace_back(
"bugprone-use-after-move");
319 Tidy.Add.emplace_back(
"llvm-*");
320 Tidy.Remove.emplace_back(
"llvm-include-order");
321 Tidy.Remove.emplace_back(
"readability-*");
322 Tidy.CheckOptions.emplace_back(
323 std::make_pair(std::string(
"StrictMode"), std::string(
"true")));
324 Tidy.CheckOptions.emplace_back(std::make_pair(
325 std::string(
"example-check.ExampleOption"), std::string(
"0")));
326 EXPECT_TRUE(compileAndApply());
327 EXPECT_EQ(
Conf.Diagnostics.ClangTidy.CheckOptions.size(), 2U);
328 EXPECT_EQ(
Conf.Diagnostics.ClangTidy.CheckOptions.lookup(
"StrictMode"),
330 EXPECT_EQ(
Conf.Diagnostics.ClangTidy.CheckOptions.lookup(
331 "example-check.ExampleOption"),
333#if CLANGD_TIDY_CHECKS
335 Conf.Diagnostics.ClangTidy.Checks,
336 "bugprone-use-after-move,llvm-*,-llvm-include-order,-readability-*");
337 EXPECT_THAT(Diags.Diagnostics, IsEmpty());
339 EXPECT_EQ(
Conf.Diagnostics.ClangTidy.Checks,
"llvm-*,-readability-*");
344 "clang-tidy check 'bugprone-use-after-move' was not found"),
345 diagMessage(
"clang-tidy check 'llvm-include-order' was not found")));
349TEST_F(ConfigCompileTests, TidyBadChecks) {
350 auto &Tidy = Frag.Diagnostics.ClangTidy;
351 Tidy.Add.emplace_back(
"unknown-check");
352 Tidy.Remove.emplace_back(
"*");
353 Tidy.Remove.emplace_back(
"llvm-includeorder");
354 EXPECT_TRUE(compileAndApply());
356 EXPECT_EQ(
Conf.Diagnostics.ClangTidy.Checks,
"-*");
360 AllOf(diagMessage(
"clang-tidy check 'unknown-check' was not found"),
361 diagKind(llvm::SourceMgr::DK_Warning)),
363 diagMessage(
"clang-tidy check 'llvm-includeorder' was not found"),
364 diagKind(llvm::SourceMgr::DK_Warning))));
367TEST_F(ConfigCompileTests, ExternalServerNeedsTrusted) {
368 Fragment::IndexBlock::ExternalBlock External;
369 External.Server.emplace(
"xxx");
370 Frag.Index.External = std::move(External);
374 ElementsAre(diagMessage(
375 "Remote index may not be specified by untrusted configuration. "
376 "Copy this into user config to use it.")));
380TEST_F(ConfigCompileTests, ExternalBlockWarnOnMultipleSource) {
381 Frag.Source.Trusted =
true;
382 Fragment::IndexBlock::ExternalBlock External;
383 External.File.emplace(
"");
384 External.Server.emplace(
"");
385 Frag.Index.External = std::move(External);
387#ifdef CLANGD_ENABLE_REMOTE
391 AllOf(diagMessage(
"Exactly one of File, Server or None must be set."),
392 diagKind(llvm::SourceMgr::DK_Error))));
394 ASSERT_TRUE(
Conf.Index.External.hasValue());
399TEST_F(ConfigCompileTests, ExternalBlockDisableWithNone) {
403 Fragment::IndexBlock::ExternalBlock External;
404 External.IsNone =
true;
405 Frag.Index.External = std::move(External);
410TEST_F(ConfigCompileTests, ExternalBlockErrOnNoSource) {
411 Frag.Index.External.emplace(Fragment::IndexBlock::ExternalBlock{});
416 AllOf(diagMessage(
"Exactly one of File, Server or None must be set."),
417 diagKind(llvm::SourceMgr::DK_Error))));
420TEST_F(ConfigCompileTests, ExternalBlockDisablesBackgroundIndex) {
421 auto BazPath =
testPath(
"foo/bar/baz.h", llvm::sys::path::Style::posix);
423 Frag.Index.Background.emplace(
"Build");
424 Fragment::IndexBlock::ExternalBlock External;
425 External.File.emplace(
testPath(
"foo"));
426 External.MountPoint.emplace(
427 testPath(
"foo/bar", llvm::sys::path::Style::posix));
428 Frag.Index.External = std::move(External);
433TEST_F(ConfigCompileTests, ExternalBlockMountPoint) {
434 auto GetFrag = [](llvm::StringRef
Directory,
435 std::optional<const char *> MountPoint) {
438 Fragment::IndexBlock::ExternalBlock External;
439 External.File.emplace(
testPath(
"foo"));
441 External.MountPoint.emplace(*MountPoint);
442 Frag.Index.External = std::move(External);
446 auto BarPath =
testPath(
"foo/bar.h", llvm::sys::path::Style::posix);
447 BarPath = llvm::sys::path::convert_to_slash(BarPath);
450 Frag = GetFrag(
"",
"foo");
455 AllOf(diagMessage(
"MountPoint must be an absolute path, because this "
456 "fragment is not associated with any directory."),
457 diagKind(llvm::SourceMgr::DK_Error))));
460 auto FooPath =
testPath(
"foo/", llvm::sys::path::Style::posix);
461 FooPath = llvm::sys::path::convert_to_slash(FooPath);
465 ASSERT_THAT(Diags.Diagnostics, IsEmpty());
467 EXPECT_THAT(
Conf.Index.External.MountPoint, FooPath);
470 Frag = GetFrag(FooPath, std::nullopt);
472 ASSERT_THAT(Diags.Diagnostics, IsEmpty());
474 EXPECT_THAT(
Conf.Index.External.MountPoint, FooPath);
478 Frag = GetFrag(
"", FooPath.c_str());
480 ASSERT_THAT(Diags.Diagnostics, IsEmpty());
484 auto BazPath =
testPath(
"bar/baz.h", llvm::sys::path::Style::posix);
485 BazPath = llvm::sys::path::convert_to_slash(BazPath);
487 Frag = GetFrag(
"", FooPath.c_str());
489 ASSERT_THAT(Diags.Diagnostics, IsEmpty());
493 BazPath =
testPath(
"foo/baz.h", llvm::sys::path::Style::posix);
494 BazPath = llvm::sys::path::convert_to_slash(BazPath);
496 Frag = GetFrag(
"", FooPath.c_str());
498 ASSERT_THAT(Diags.Diagnostics, IsEmpty());
500 EXPECT_THAT(
Conf.Index.External.MountPoint, FooPath);
503 BazPath =
testPath(
"fOo/baz.h", llvm::sys::path::Style::posix);
504 BazPath = llvm::sys::path::convert_to_slash(BazPath);
507 FooPath =
testPath(
"FOO/", llvm::sys::path::Style::posix);
508 FooPath = llvm::sys::path::convert_to_slash(FooPath);
509 Frag = GetFrag(
"", FooPath.c_str());
511 ASSERT_THAT(Diags.Diagnostics, IsEmpty());
512#ifdef CLANGD_PATH_CASE_INSENSITIVE
514 EXPECT_THAT(
Conf.Index.External.MountPoint, FooPath);
520TEST_F(ConfigCompileTests, AllScopes) {
522 EXPECT_TRUE(compileAndApply());
523 EXPECT_TRUE(
Conf.Completion.AllScopes);
526 Frag.Completion.AllScopes =
false;
527 EXPECT_TRUE(compileAndApply());
528 EXPECT_FALSE(
Conf.Completion.AllScopes);
531 Frag.Completion.AllScopes =
true;
532 EXPECT_TRUE(compileAndApply());
533 EXPECT_TRUE(
Conf.Completion.AllScopes);
536TEST_F(ConfigCompileTests, Style) {
538 Frag.Style.FullyQualifiedNamespaces.push_back(std::string(
"foo"));
539 Frag.Style.FullyQualifiedNamespaces.push_back(std::string(
"bar"));
540 EXPECT_TRUE(compileAndApply());
541 EXPECT_THAT(
Conf.Style.FullyQualifiedNamespaces, ElementsAre(
"foo",
"bar"));
std::string Condition
Condition used after the preprocessor directive.
llvm::StringRef Directory
TEST_F(BackgroundIndexTest, NoCrashOnErrorFile)
std::string Path
A typedef to represent a file path.
bool isBuiltinDiagnosticSuppressed(unsigned ID, const llvm::StringSet<> &Suppress, const LangOptions &LangOpts)
Determine whether a (non-clang-tidy) diagnostic is suppressed by config.
std::string testPath(PathRef File, llvm::sys::path::Style Style)
llvm::ArrayRef< std::function< bool(llvm::StringRef)> > HeaderFilter
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
@ Strict
Diagnose missing and unused includes.
llvm::StringRef Path
Absolute path to a source file we're applying the config to.