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