1 //===--- ConfigCompile.cpp - Translating Fragments into Config ------------===//
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 // Fragments are applied to Configs in two steps:
10 //
11 // 1. (When the fragment is first loaded)
12 //    FragmentCompiler::compile() traverses the Fragment and creates
13 //    function objects that know how to apply the configuration.
14 // 2. (Every time a config is required)
15 //    CompiledFragment() executes these functions to populate the Config.
16 //
17 // Work could be split between these steps in different ways. We try to
18 // do as much work as possible in the first step. For example, regexes are
19 // compiled in stage 1 and captured by the apply function. This is because:
20 //
21 //  - it's more efficient, as the work done in stage 1 must only be done once
22 //  - problems can be reported in stage 1, in stage 2 we must silently recover
23 //
24 //===----------------------------------------------------------------------===//
25 
26 #include "CompileCommands.h"
27 #include "Config.h"
28 #include "ConfigFragment.h"
29 #include "support/Logger.h"
30 #include "support/Trace.h"
31 #include "llvm/ADT/STLExtras.h"
32 #include "llvm/ADT/StringRef.h"
33 #include "llvm/ADT/StringSwitch.h"
34 #include "llvm/Support/Regex.h"
35 #include "llvm/Support/SMLoc.h"
36 #include "llvm/Support/SourceMgr.h"
37 
38 namespace clang {
39 namespace clangd {
40 namespace config {
41 namespace {
42 
43 struct CompiledFragmentImpl {
44   // The independent conditions to check before using settings from this config.
45   // The following fragment has *two* conditions:
46   //   If: { Platform: [mac, linux], PathMatch: foo/.* }
47   // All of them must be satisfied: the platform and path conditions are ANDed.
48   // The OR logic for the platform condition is implemented inside the function.
49   std::vector<llvm::unique_function<bool(const Params &) const>> Conditions;
50   // Mutations that this fragment will apply to the configuration.
51   // These are invoked only if the conditions are satisfied.
52   std::vector<llvm::unique_function<void(Config &) const>> Apply;
53 
operator ()clang::clangd::config::__anon17e277c80111::CompiledFragmentImpl54   bool operator()(const Params &P, Config &C) const {
55     for (const auto &C : Conditions) {
56       if (!C(P)) {
57         dlog("Config fragment {0}: condition not met", this);
58         return false;
59       }
60     }
61     dlog("Config fragment {0}: applying {1} rules", this, Apply.size());
62     for (const auto &A : Apply)
63       A(C);
64     return true;
65   }
66 };
67 
68 // Wrapper around condition compile() functions to reduce arg-passing.
69 struct FragmentCompiler {
70   CompiledFragmentImpl &Out;
71   DiagnosticCallback Diagnostic;
72   llvm::SourceMgr *SourceMgr;
73 
compileRegexclang::clangd::config::__anon17e277c80111::FragmentCompiler74   llvm::Optional<llvm::Regex> compileRegex(const Located<std::string> &Text) {
75     std::string Anchored = "^(" + *Text + ")$";
76     llvm::Regex Result(Anchored);
77     std::string RegexError;
78     if (!Result.isValid(RegexError)) {
79       diag(Error, "Invalid regex " + Anchored + ": " + RegexError, Text.Range);
80       return llvm::None;
81     }
82     return Result;
83   }
84 
85   // Helper with similar API to StringSwitch, for parsing enum values.
86   template <typename T> class EnumSwitch {
87     FragmentCompiler &Outer;
88     llvm::StringRef EnumName;
89     const Located<std::string> &Input;
90     llvm::Optional<T> Result;
91     llvm::SmallVector<llvm::StringLiteral, 8> ValidValues;
92 
93   public:
EnumSwitch(llvm::StringRef EnumName,const Located<std::string> & In,FragmentCompiler & Outer)94     EnumSwitch(llvm::StringRef EnumName, const Located<std::string> &In,
95                FragmentCompiler &Outer)
96         : Outer(Outer), EnumName(EnumName), Input(In) {}
97 
map(llvm::StringLiteral Name,T Value)98     EnumSwitch &map(llvm::StringLiteral Name, T Value) {
99       assert(!llvm::is_contained(ValidValues, Name) && "Duplicate value!");
100       ValidValues.push_back(Name);
101       if (!Result && *Input == Name)
102         Result = Value;
103       return *this;
104     }
105 
value()106     llvm::Optional<T> value() {
107       if (!Result)
108         Outer.diag(
109             Warning,
110             llvm::formatv("Invalid {0} value '{1}'. Valid values are {2}.",
111                           EnumName, *Input, llvm::join(ValidValues, ", "))
112                 .str(),
113             Input.Range);
114       return Result;
115     };
116   };
117 
118   // Attempt to parse a specified string into an enum.
119   // Yields llvm::None and produces a diagnostic on failure.
120   //
121   // Optional<T> Value = compileEnum<En>("Foo", Frag.Foo)
122   //    .map("Foo", Enum::Foo)
123   //    .map("Bar", Enum::Bar)
124   //    .value();
125   template <typename T>
compileEnumclang::clangd::config::__anon17e277c80111::FragmentCompiler126   EnumSwitch<T> compileEnum(llvm::StringRef EnumName,
127                             const Located<std::string> &In) {
128     return EnumSwitch<T>(EnumName, In, *this);
129   }
130 
compileclang::clangd::config::__anon17e277c80111::FragmentCompiler131   void compile(Fragment &&F) {
132     compile(std::move(F.If));
133     compile(std::move(F.CompileFlags));
134     compile(std::move(F.Index));
135   }
136 
compileclang::clangd::config::__anon17e277c80111::FragmentCompiler137   void compile(Fragment::IfBlock &&F) {
138     if (F.HasUnrecognizedCondition)
139       Out.Conditions.push_back([&](const Params &) { return false; });
140 
141     auto PathMatch = std::make_unique<std::vector<llvm::Regex>>();
142     for (auto &Entry : F.PathMatch) {
143       if (auto RE = compileRegex(Entry))
144         PathMatch->push_back(std::move(*RE));
145     }
146     if (!PathMatch->empty()) {
147       Out.Conditions.push_back(
148           [PathMatch(std::move(PathMatch))](const Params &P) {
149             if (P.Path.empty())
150               return false;
151             return llvm::any_of(*PathMatch, [&](const llvm::Regex &RE) {
152               return RE.match(P.Path);
153             });
154           });
155     }
156 
157     auto PathExclude = std::make_unique<std::vector<llvm::Regex>>();
158     for (auto &Entry : F.PathExclude) {
159       if (auto RE = compileRegex(Entry))
160         PathExclude->push_back(std::move(*RE));
161     }
162     if (!PathExclude->empty()) {
163       Out.Conditions.push_back(
164           [PathExclude(std::move(PathExclude))](const Params &P) {
165             if (P.Path.empty())
166               return false;
167             return llvm::none_of(*PathExclude, [&](const llvm::Regex &RE) {
168               return RE.match(P.Path);
169             });
170           });
171     }
172   }
173 
compileclang::clangd::config::__anon17e277c80111::FragmentCompiler174   void compile(Fragment::CompileFlagsBlock &&F) {
175     if (!F.Remove.empty()) {
176       auto Remove = std::make_shared<ArgStripper>();
177       for (auto &A : F.Remove)
178         Remove->strip(*A);
179       Out.Apply.push_back([Remove(std::shared_ptr<const ArgStripper>(
180                               std::move(Remove)))](Config &C) {
181         C.CompileFlags.Edits.push_back(
182             [Remove](std::vector<std::string> &Args) {
183               Remove->process(Args);
184             });
185       });
186     }
187 
188     if (!F.Add.empty()) {
189       std::vector<std::string> Add;
190       for (auto &A : F.Add)
191         Add.push_back(std::move(*A));
192       Out.Apply.push_back([Add(std::move(Add))](Config &C) {
193         C.CompileFlags.Edits.push_back([Add](std::vector<std::string> &Args) {
194           Args.insert(Args.end(), Add.begin(), Add.end());
195         });
196       });
197     }
198   }
199 
compileclang::clangd::config::__anon17e277c80111::FragmentCompiler200   void compile(Fragment::IndexBlock &&F) {
201     if (F.Background) {
202       if (auto Val = compileEnum<Config::BackgroundPolicy>("Background",
203                                                            **F.Background)
204                          .map("Build", Config::BackgroundPolicy::Build)
205                          .map("Skip", Config::BackgroundPolicy::Skip)
206                          .value())
207         Out.Apply.push_back([Val](Config &C) { C.Index.Background = *Val; });
208     }
209   }
210 
211   constexpr static llvm::SourceMgr::DiagKind Error = llvm::SourceMgr::DK_Error;
212   constexpr static llvm::SourceMgr::DiagKind Warning =
213       llvm::SourceMgr::DK_Warning;
diagclang::clangd::config::__anon17e277c80111::FragmentCompiler214   void diag(llvm::SourceMgr::DiagKind Kind, llvm::StringRef Message,
215             llvm::SMRange Range) {
216     if (Range.isValid() && SourceMgr != nullptr)
217       Diagnostic(SourceMgr->GetMessage(Range.Start, Kind, Message, Range));
218     else
219       Diagnostic(llvm::SMDiagnostic("", Kind, Message));
220   }
221 };
222 
223 } // namespace
224 
compile(DiagnosticCallback D)225 CompiledFragment Fragment::compile(DiagnosticCallback D) && {
226   llvm::StringRef ConfigFile = "<unknown>";
227   std::pair<unsigned, unsigned> LineCol = {0, 0};
228   if (auto *SM = Source.Manager.get()) {
229     unsigned BufID = SM->getMainFileID();
230     LineCol = SM->getLineAndColumn(Source.Location, BufID);
231     ConfigFile = SM->getBufferInfo(BufID).Buffer->getBufferIdentifier();
232   }
233   trace::Span Tracer("ConfigCompile");
234   SPAN_ATTACH(Tracer, "ConfigFile", ConfigFile);
235   auto Result = std::make_shared<CompiledFragmentImpl>();
236   vlog("Config fragment: compiling {0}:{1} -> {2}", ConfigFile, LineCol.first,
237        Result.get());
238 
239   FragmentCompiler{*Result, D, Source.Manager.get()}.compile(std::move(*this));
240   // Return as cheaply-copyable wrapper.
241   return [Result(std::move(Result))](const Params &P, Config &C) {
242     return (*Result)(P, C);
243   };
244 }
245 
246 } // namespace config
247 } // namespace clangd
248 } // namespace clang
249