clang-tools  12.0.0git
ConfigCompileTests.cpp
Go to the documentation of this file.
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 "gmock/gmock.h"
13 #include "gtest/gtest.h"
14 
15 namespace clang {
16 namespace clangd {
17 namespace config {
18 namespace {
19 using ::testing::ElementsAre;
20 using ::testing::IsEmpty;
21 using ::testing::SizeIs;
22 using ::testing::StartsWith;
23 
24 class ConfigCompileTests : public ::testing::Test {
25 protected:
26  CapturedDiags Diags;
28  Fragment Frag;
29  Params Parm;
30 
31  bool compileAndApply() {
32  Conf = Config();
33  Diags.Diagnostics.clear();
34  auto Compiled = std::move(Frag).compile(Diags.callback());
35  return Compiled(Parm, Conf);
36  }
37 };
38 
39 TEST_F(ConfigCompileTests, Condition) {
40  // No condition.
41  Frag = {};
42  Frag.CompileFlags.Add.emplace_back("X");
43  EXPECT_TRUE(compileAndApply()) << "Empty config";
44  EXPECT_THAT(Diags.Diagnostics, IsEmpty());
45  EXPECT_THAT(Conf.CompileFlags.Edits, SizeIs(1));
46 
47  // Regex with no file.
48  Frag = {};
49  Frag.If.PathMatch.emplace_back("fo*");
50  EXPECT_FALSE(compileAndApply());
51  EXPECT_THAT(Diags.Diagnostics, IsEmpty());
52  EXPECT_THAT(Conf.CompileFlags.Edits, SizeIs(0));
53 
54  // Following tests have a file path set.
55  Parm.Path = "bar";
56 
57  // Non-matching regex.
58  Frag = {};
59  Frag.If.PathMatch.emplace_back("fo*");
60  EXPECT_FALSE(compileAndApply());
61  EXPECT_THAT(Diags.Diagnostics, IsEmpty());
62 
63  // Matching regex.
64  Frag = {};
65  Frag.If.PathMatch.emplace_back("fo*");
66  Frag.If.PathMatch.emplace_back("ba*r");
67  EXPECT_TRUE(compileAndApply());
68  EXPECT_THAT(Diags.Diagnostics, IsEmpty());
69 
70  // Excluded regex.
71  Frag = {};
72  Frag.If.PathMatch.emplace_back("b.*");
73  Frag.If.PathExclude.emplace_back(".*r");
74  EXPECT_FALSE(compileAndApply()) << "Included but also excluded";
75  EXPECT_THAT(Diags.Diagnostics, IsEmpty());
76 
77  // Invalid regex.
78  Frag = {};
79  Frag.If.PathMatch.emplace_back("**]@theu");
80  EXPECT_TRUE(compileAndApply());
81  EXPECT_THAT(Diags.Diagnostics, SizeIs(1));
82  EXPECT_THAT(Diags.Diagnostics.front().Message, StartsWith("Invalid regex"));
83 
84  // Valid regex and unknown key.
85  Frag = {};
86  Frag.If.HasUnrecognizedCondition = true;
87  Frag.If.PathMatch.emplace_back("ba*r");
88  EXPECT_FALSE(compileAndApply());
89  EXPECT_THAT(Diags.Diagnostics, IsEmpty());
90 }
91 
92 TEST_F(ConfigCompileTests, CompileCommands) {
93  Frag.CompileFlags.Add.emplace_back("-foo");
94  Frag.CompileFlags.Remove.emplace_back("--include-directory=");
95  std::vector<std::string> Argv = {"clang", "-I", "bar/", "a.cc"};
96  EXPECT_TRUE(compileAndApply());
97  EXPECT_THAT(Conf.CompileFlags.Edits, SizeIs(2));
98  for (auto &Edit : Conf.CompileFlags.Edits)
99  Edit(Argv);
100  EXPECT_THAT(Argv, ElementsAre("clang", "a.cc", "-foo"));
101 }
102 
103 TEST_F(ConfigCompileTests, Index) {
104  Frag.Index.Background.emplace("Skip");
105  EXPECT_TRUE(compileAndApply());
106  EXPECT_EQ(Conf.Index.Background, Config::BackgroundPolicy::Skip);
107 
108  Frag = {};
109  Frag.Index.Background.emplace("Foo");
110  EXPECT_TRUE(compileAndApply());
111  EXPECT_EQ(Conf.Index.Background, Config::BackgroundPolicy::Build)
112  << "by default";
113  EXPECT_THAT(
114  Diags.Diagnostics,
115  ElementsAre(DiagMessage(
116  "Invalid Background value 'Foo'. Valid values are Build, Skip.")));
117 }
118 
119 } // namespace
120 } // namespace config
121 } // namespace clangd
122 } // namespace clang
static cl::opt< std::string > Config("config", cl::desc(R"( Specifies a configuration in YAML/JSON format: -config="{Checks:' *', CheckOptions:[{key:x, value:y}]}" When the value is empty, clang-tidy will attempt to find a file named .clang-tidy for each source file in its parent directories. )"), cl::init(""), cl::cat(ClangTidyCategory))
Params Parm
Config Conf
TEST_F(BackgroundIndexTest, NoCrashOnErrorFile)
CapturedDiags Diags
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::string Condition
Condition used after the preprocessor directive.
Fragment Frag
const SymbolIndex * Index
Definition: Dexp.cpp:95