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