• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ConfigProvider.h"
30 #include "Features.inc"
31 #include "support/Logger.h"
32 #include "support/Trace.h"
33 #include "llvm/ADT/None.h"
34 #include "llvm/ADT/Optional.h"
35 #include "llvm/ADT/STLExtras.h"
36 #include "llvm/ADT/SmallString.h"
37 #include "llvm/ADT/StringRef.h"
38 #include "llvm/ADT/StringSwitch.h"
39 #include "llvm/Support/Error.h"
40 #include "llvm/Support/FileSystem.h"
41 #include "llvm/Support/Format.h"
42 #include "llvm/Support/FormatVariadic.h"
43 #include "llvm/Support/Path.h"
44 #include "llvm/Support/Regex.h"
45 #include "llvm/Support/SMLoc.h"
46 #include "llvm/Support/SourceMgr.h"
47 #include <string>
48 
49 namespace clang {
50 namespace clangd {
51 namespace config {
52 namespace {
53 
54 // Returns an empty stringref if Path is not under FragmentDir. Returns Path
55 // as-is when FragmentDir is empty.
configRelative(llvm::StringRef Path,llvm::StringRef FragmentDir)56 llvm::StringRef configRelative(llvm::StringRef Path,
57                                llvm::StringRef FragmentDir) {
58   if (FragmentDir.empty())
59     return Path;
60   if (!Path.consume_front(FragmentDir))
61     return llvm::StringRef();
62   return Path.empty() ? "." : Path;
63 }
64 
65 struct CompiledFragmentImpl {
66   // The independent conditions to check before using settings from this config.
67   // The following fragment has *two* conditions:
68   //   If: { Platform: [mac, linux], PathMatch: foo/.* }
69   // All of them must be satisfied: the platform and path conditions are ANDed.
70   // The OR logic for the platform condition is implemented inside the function.
71   std::vector<llvm::unique_function<bool(const Params &) const>> Conditions;
72   // Mutations that this fragment will apply to the configuration.
73   // These are invoked only if the conditions are satisfied.
74   std::vector<llvm::unique_function<void(const Params &, Config &) const>>
75       Apply;
76 
operator ()clang::clangd::config::__anonbc0c43d10111::CompiledFragmentImpl77   bool operator()(const Params &P, Config &C) const {
78     for (const auto &C : Conditions) {
79       if (!C(P)) {
80         dlog("Config fragment {0}: condition not met", this);
81         return false;
82       }
83     }
84     dlog("Config fragment {0}: applying {1} rules", this, Apply.size());
85     for (const auto &A : Apply)
86       A(P, C);
87     return true;
88   }
89 };
90 
91 // Wrapper around condition compile() functions to reduce arg-passing.
92 struct FragmentCompiler {
FragmentCompilerclang::clangd::config::__anonbc0c43d10111::FragmentCompiler93   FragmentCompiler(CompiledFragmentImpl &Out, DiagnosticCallback D,
94                    llvm::SourceMgr *SM)
95       : Out(Out), Diagnostic(D), SourceMgr(SM) {}
96   CompiledFragmentImpl &Out;
97   DiagnosticCallback Diagnostic;
98   llvm::SourceMgr *SourceMgr;
99   // Normalized Fragment::SourceInfo::Directory.
100   std::string FragmentDirectory;
101 
compileRegexclang::clangd::config::__anonbc0c43d10111::FragmentCompiler102   llvm::Optional<llvm::Regex> compileRegex(const Located<std::string> &Text) {
103     std::string Anchored = "^(" + *Text + ")$";
104     llvm::Regex Result(Anchored);
105     std::string RegexError;
106     if (!Result.isValid(RegexError)) {
107       diag(Error, "Invalid regex " + Anchored + ": " + RegexError, Text.Range);
108       return llvm::None;
109     }
110     return Result;
111   }
112 
makeAbsoluteclang::clangd::config::__anonbc0c43d10111::FragmentCompiler113   llvm::Optional<std::string> makeAbsolute(Located<std::string> Path,
114                                            llvm::StringLiteral Description,
115                                            llvm::sys::path::Style Style) {
116     if (llvm::sys::path::is_absolute(*Path))
117       return *Path;
118     if (FragmentDirectory.empty()) {
119       diag(Error,
120            llvm::formatv(
121                "{0} must be an absolute path, because this fragment is not "
122                "associated with any directory.",
123                Description)
124                .str(),
125            Path.Range);
126       return llvm::None;
127     }
128     llvm::SmallString<256> AbsPath = llvm::StringRef(*Path);
129     llvm::sys::fs::make_absolute(FragmentDirectory, AbsPath);
130     llvm::sys::path::native(AbsPath, Style);
131     return AbsPath.str().str();
132   }
133 
134   // Helper with similar API to StringSwitch, for parsing enum values.
135   template <typename T> class EnumSwitch {
136     FragmentCompiler &Outer;
137     llvm::StringRef EnumName;
138     const Located<std::string> &Input;
139     llvm::Optional<T> Result;
140     llvm::SmallVector<llvm::StringLiteral, 8> ValidValues;
141 
142   public:
EnumSwitch(llvm::StringRef EnumName,const Located<std::string> & In,FragmentCompiler & Outer)143     EnumSwitch(llvm::StringRef EnumName, const Located<std::string> &In,
144                FragmentCompiler &Outer)
145         : Outer(Outer), EnumName(EnumName), Input(In) {}
146 
map(llvm::StringLiteral Name,T Value)147     EnumSwitch &map(llvm::StringLiteral Name, T Value) {
148       assert(!llvm::is_contained(ValidValues, Name) && "Duplicate value!");
149       ValidValues.push_back(Name);
150       if (!Result && *Input == Name)
151         Result = Value;
152       return *this;
153     }
154 
value()155     llvm::Optional<T> value() {
156       if (!Result)
157         Outer.diag(
158             Warning,
159             llvm::formatv("Invalid {0} value '{1}'. Valid values are {2}.",
160                           EnumName, *Input, llvm::join(ValidValues, ", "))
161                 .str(),
162             Input.Range);
163       return Result;
164     };
165   };
166 
167   // Attempt to parse a specified string into an enum.
168   // Yields llvm::None and produces a diagnostic on failure.
169   //
170   // Optional<T> Value = compileEnum<En>("Foo", Frag.Foo)
171   //    .map("Foo", Enum::Foo)
172   //    .map("Bar", Enum::Bar)
173   //    .value();
174   template <typename T>
compileEnumclang::clangd::config::__anonbc0c43d10111::FragmentCompiler175   EnumSwitch<T> compileEnum(llvm::StringRef EnumName,
176                             const Located<std::string> &In) {
177     return EnumSwitch<T>(EnumName, In, *this);
178   }
179 
compileclang::clangd::config::__anonbc0c43d10111::FragmentCompiler180   void compile(Fragment &&F) {
181     if (!F.Source.Directory.empty()) {
182       FragmentDirectory = llvm::sys::path::convert_to_slash(F.Source.Directory);
183       if (FragmentDirectory.back() != '/')
184         FragmentDirectory += '/';
185     }
186     compile(std::move(F.If));
187     compile(std::move(F.CompileFlags));
188     compile(std::move(F.Index));
189     compile(std::move(F.ClangTidy));
190   }
191 
compileclang::clangd::config::__anonbc0c43d10111::FragmentCompiler192   void compile(Fragment::IfBlock &&F) {
193     if (F.HasUnrecognizedCondition)
194       Out.Conditions.push_back([&](const Params &) { return false; });
195 
196     auto PathMatch = std::make_unique<std::vector<llvm::Regex>>();
197     for (auto &Entry : F.PathMatch) {
198       if (auto RE = compileRegex(Entry))
199         PathMatch->push_back(std::move(*RE));
200     }
201     if (!PathMatch->empty()) {
202       Out.Conditions.push_back(
203           [PathMatch(std::move(PathMatch)),
204            FragmentDir(FragmentDirectory)](const Params &P) {
205             if (P.Path.empty())
206               return false;
207             llvm::StringRef Path = configRelative(P.Path, FragmentDir);
208             // Ignore the file if it is not nested under Fragment.
209             if (Path.empty())
210               return false;
211             return llvm::any_of(*PathMatch, [&](const llvm::Regex &RE) {
212               return RE.match(Path);
213             });
214           });
215     }
216 
217     auto PathExclude = std::make_unique<std::vector<llvm::Regex>>();
218     for (auto &Entry : F.PathExclude) {
219       if (auto RE = compileRegex(Entry))
220         PathExclude->push_back(std::move(*RE));
221     }
222     if (!PathExclude->empty()) {
223       Out.Conditions.push_back(
224           [PathExclude(std::move(PathExclude)),
225            FragmentDir(FragmentDirectory)](const Params &P) {
226             if (P.Path.empty())
227               return false;
228             llvm::StringRef Path = configRelative(P.Path, FragmentDir);
229             // Ignore the file if it is not nested under Fragment.
230             if (Path.empty())
231               return true;
232             return llvm::none_of(*PathExclude, [&](const llvm::Regex &RE) {
233               return RE.match(Path);
234             });
235           });
236     }
237   }
238 
compileclang::clangd::config::__anonbc0c43d10111::FragmentCompiler239   void compile(Fragment::CompileFlagsBlock &&F) {
240     if (!F.Remove.empty()) {
241       auto Remove = std::make_shared<ArgStripper>();
242       for (auto &A : F.Remove)
243         Remove->strip(*A);
244       Out.Apply.push_back([Remove(std::shared_ptr<const ArgStripper>(
245                               std::move(Remove)))](const Params &, Config &C) {
246         C.CompileFlags.Edits.push_back(
247             [Remove](std::vector<std::string> &Args) {
248               Remove->process(Args);
249             });
250       });
251     }
252 
253     if (!F.Add.empty()) {
254       std::vector<std::string> Add;
255       for (auto &A : F.Add)
256         Add.push_back(std::move(*A));
257       Out.Apply.push_back([Add(std::move(Add))](const Params &, Config &C) {
258         C.CompileFlags.Edits.push_back([Add](std::vector<std::string> &Args) {
259           Args.insert(Args.end(), Add.begin(), Add.end());
260         });
261       });
262     }
263   }
264 
compileclang::clangd::config::__anonbc0c43d10111::FragmentCompiler265   void compile(Fragment::IndexBlock &&F) {
266     if (F.Background) {
267       if (auto Val = compileEnum<Config::BackgroundPolicy>("Background",
268                                                            **F.Background)
269                          .map("Build", Config::BackgroundPolicy::Build)
270                          .map("Skip", Config::BackgroundPolicy::Skip)
271                          .value())
272         Out.Apply.push_back(
273             [Val](const Params &, Config &C) { C.Index.Background = *Val; });
274     }
275     if (F.External)
276       compile(std::move(**F.External), F.External->Range);
277   }
278 
compileclang::clangd::config::__anonbc0c43d10111::FragmentCompiler279   void compile(Fragment::IndexBlock::ExternalBlock &&External,
280                llvm::SMRange BlockRange) {
281 #ifndef CLANGD_ENABLE_REMOTE
282     if (External.Server) {
283       diag(Error, "Clangd isn't compiled with remote index support, ignoring "
284                   "Server." External.Server->Range);
285       External.Server.reset();
286     }
287 #endif
288     // Make sure exactly one of the Sources is set.
289     unsigned SourceCount =
290         External.File.hasValue() + External.Server.hasValue();
291     if (SourceCount != 1) {
292       diag(Error, "Exactly one of File or Server must be set.", BlockRange);
293       return;
294     }
295     Config::ExternalIndexSpec Spec;
296     if (External.Server) {
297       Spec.Kind = Config::ExternalIndexSpec::Server;
298       Spec.Location = std::move(**External.Server);
299     } else if (External.File) {
300       Spec.Kind = Config::ExternalIndexSpec::File;
301       auto AbsPath = makeAbsolute(std::move(*External.File), "File",
302                                   llvm::sys::path::Style::native);
303       if (!AbsPath)
304         return;
305       Spec.Location = std::move(*AbsPath);
306     }
307     // Make sure MountPoint is an absolute path with forward slashes.
308     if (!External.MountPoint)
309       External.MountPoint.emplace(FragmentDirectory);
310     if ((**External.MountPoint).empty()) {
311       diag(Error, "A mountpoint is required.", BlockRange);
312       return;
313     }
314     auto AbsPath = makeAbsolute(std::move(*External.MountPoint), "MountPoint",
315                                 llvm::sys::path::Style::posix);
316     if (!AbsPath)
317       return;
318     Spec.MountPoint = std::move(*AbsPath);
319     Out.Apply.push_back([Spec(std::move(Spec))](const Params &P, Config &C) {
320       if (!P.Path.startswith(Spec.MountPoint))
321         return;
322       C.Index.External = Spec;
323       // Disable background indexing for the files under the mountpoint.
324       // Note that this will overwrite statements in any previous fragments
325       // (including the current one).
326       C.Index.Background = Config::BackgroundPolicy::Skip;
327     });
328   }
329 
compileclang::clangd::config::__anonbc0c43d10111::FragmentCompiler330   void compile(Fragment::StyleBlock &&F) {
331     if (!F.FullyQualifiedNamespaces.empty()) {
332       std::vector<std::string> FullyQualifiedNamespaces;
333       for (auto &N : F.FullyQualifiedNamespaces) {
334         // Normalize the data by dropping both leading and trailing ::
335         StringRef Namespace(*N);
336         Namespace.consume_front("::");
337         Namespace.consume_back("::");
338         FullyQualifiedNamespaces.push_back(Namespace.str());
339       }
340       Out.Apply.push_back([FullyQualifiedNamespaces(
341                               std::move(FullyQualifiedNamespaces))](
342                               const Params &, Config &C) {
343         C.Style.FullyQualifiedNamespaces.insert(
344             C.Style.FullyQualifiedNamespaces.begin(),
345             FullyQualifiedNamespaces.begin(), FullyQualifiedNamespaces.end());
346       });
347     }
348   }
349 
appendTidyCheckSpecclang::clangd::config::__anonbc0c43d10111::FragmentCompiler350   void appendTidyCheckSpec(std::string &CurSpec,
351                            const Located<std::string> &Arg, bool IsPositive) {
352     StringRef Str = *Arg;
353     // Don't support negating here, its handled if the item is in the Add or
354     // Remove list.
355     if (Str.startswith("-") || Str.contains(',')) {
356       diag(Error, "Invalid clang-tidy check name", Arg.Range);
357       return;
358     }
359     CurSpec += ',';
360     if (!IsPositive)
361       CurSpec += '-';
362     CurSpec += Str;
363   }
364 
compileclang::clangd::config::__anonbc0c43d10111::FragmentCompiler365   void compile(Fragment::ClangTidyBlock &&F) {
366     std::string Checks;
367     for (auto &CheckGlob : F.Add)
368       appendTidyCheckSpec(Checks, CheckGlob, true);
369 
370     for (auto &CheckGlob : F.Remove)
371       appendTidyCheckSpec(Checks, CheckGlob, false);
372 
373     if (!Checks.empty())
374       Out.Apply.push_back(
375           [Checks = std::move(Checks)](const Params &, Config &C) {
376             C.ClangTidy.Checks.append(
377                 Checks, C.ClangTidy.Checks.empty() ? /*skip comma*/ 1 : 0,
378                 std::string::npos);
379           });
380     if (!F.CheckOptions.empty()) {
381       std::vector<std::pair<std::string, std::string>> CheckOptions;
382       for (auto &Opt : F.CheckOptions)
383         CheckOptions.emplace_back(std::move(*Opt.first),
384                                   std::move(*Opt.second));
385       Out.Apply.push_back(
386           [CheckOptions = std::move(CheckOptions)](const Params &, Config &C) {
387             for (auto &StringPair : CheckOptions)
388               C.ClangTidy.CheckOptions.insert_or_assign(StringPair.first,
389                                                         StringPair.second);
390           });
391     }
392   }
393 
394   constexpr static llvm::SourceMgr::DiagKind Error = llvm::SourceMgr::DK_Error;
395   constexpr static llvm::SourceMgr::DiagKind Warning =
396       llvm::SourceMgr::DK_Warning;
diagclang::clangd::config::__anonbc0c43d10111::FragmentCompiler397   void diag(llvm::SourceMgr::DiagKind Kind, llvm::StringRef Message,
398             llvm::SMRange Range) {
399     if (Range.isValid() && SourceMgr != nullptr)
400       Diagnostic(SourceMgr->GetMessage(Range.Start, Kind, Message, Range));
401     else
402       Diagnostic(llvm::SMDiagnostic("", Kind, Message));
403   }
404 };
405 
406 } // namespace
407 
compile(DiagnosticCallback D)408 CompiledFragment Fragment::compile(DiagnosticCallback D) && {
409   llvm::StringRef ConfigFile = "<unknown>";
410   std::pair<unsigned, unsigned> LineCol = {0, 0};
411   if (auto *SM = Source.Manager.get()) {
412     unsigned BufID = SM->getMainFileID();
413     LineCol = SM->getLineAndColumn(Source.Location, BufID);
414     ConfigFile = SM->getBufferInfo(BufID).Buffer->getBufferIdentifier();
415   }
416   trace::Span Tracer("ConfigCompile");
417   SPAN_ATTACH(Tracer, "ConfigFile", ConfigFile);
418   auto Result = std::make_shared<CompiledFragmentImpl>();
419   vlog("Config fragment: compiling {0}:{1} -> {2}", ConfigFile, LineCol.first,
420        Result.get());
421 
422   FragmentCompiler{*Result, D, Source.Manager.get()}.compile(std::move(*this));
423   // Return as cheaply-copyable wrapper.
424   return [Result(std::move(Result))](const Params &P, Config &C) {
425     return (*Result)(P, C);
426   };
427 }
428 
429 } // namespace config
430 } // namespace clangd
431 } // namespace clang
432