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