1 //===--- ConfigYAML.cpp - Loading configuration fragments from YAML files -===//
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 #include "ConfigFragment.h"
9 #include "llvm/ADT/Optional.h"
10 #include "llvm/ADT/SmallSet.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/Support/MemoryBuffer.h"
13 #include "llvm/Support/SourceMgr.h"
14 #include "llvm/Support/YAMLParser.h"
15 #include <string>
16 #include <system_error>
17 
18 namespace clang {
19 namespace clangd {
20 namespace config {
21 namespace {
22 using llvm::yaml::BlockScalarNode;
23 using llvm::yaml::MappingNode;
24 using llvm::yaml::Node;
25 using llvm::yaml::ScalarNode;
26 using llvm::yaml::SequenceNode;
27 
28 llvm::Optional<llvm::StringRef>
29 bestGuess(llvm::StringRef Search,
30           llvm::ArrayRef<llvm::StringRef> AllowedValues) {
31   unsigned MaxEdit = (Search.size() + 1) / 3;
32   if (!MaxEdit)
33     return llvm::None;
34   llvm::Optional<llvm::StringRef> Result;
35   for (const auto &AllowedValue : AllowedValues) {
36     unsigned EditDistance = Search.edit_distance(AllowedValue, true, MaxEdit);
37     // We can't do better than an edit distance of 1, so just return this and
38     // save computing other values.
39     if (EditDistance == 1U)
40       return AllowedValue;
41     if (EditDistance == MaxEdit && !Result) {
42       Result = AllowedValue;
43     } else if (EditDistance < MaxEdit) {
44       Result = AllowedValue;
45       MaxEdit = EditDistance;
46     }
47   }
48   return Result;
49 }
50 
51 class Parser {
52   llvm::SourceMgr &SM;
53   bool HadError = false;
54 
55 public:
56   Parser(llvm::SourceMgr &SM) : SM(SM) {}
57 
58   // Tries to parse N into F, returning false if it failed and we couldn't
59   // meaningfully recover (YAML syntax error, or hard semantic error).
60   bool parse(Fragment &F, Node &N) {
61     DictParser Dict("Config", this);
62     Dict.handle("If", [&](Node &N) { parse(F.If, N); });
63     Dict.handle("CompileFlags", [&](Node &N) { parse(F.CompileFlags, N); });
64     Dict.handle("Index", [&](Node &N) { parse(F.Index, N); });
65     Dict.handle("Style", [&](Node &N) { parse(F.Style, N); });
66     Dict.handle("Diagnostics", [&](Node &N) { parse(F.Diagnostics, N); });
67     Dict.handle("Completion", [&](Node &N) { parse(F.Completion, N); });
68     Dict.parse(N);
69     return !(N.failed() || HadError);
70   }
71 
72 private:
73   void parse(Fragment::IfBlock &F, Node &N) {
74     DictParser Dict("If", this);
75     Dict.unrecognized([&](Located<std::string>, Node &) {
76       F.HasUnrecognizedCondition = true;
77       return true; // Emit a warning for the unrecognized key.
78     });
79     Dict.handle("PathMatch", [&](Node &N) {
80       if (auto Values = scalarValues(N))
81         F.PathMatch = std::move(*Values);
82     });
83     Dict.handle("PathExclude", [&](Node &N) {
84       if (auto Values = scalarValues(N))
85         F.PathExclude = std::move(*Values);
86     });
87     Dict.parse(N);
88   }
89 
90   void parse(Fragment::CompileFlagsBlock &F, Node &N) {
91     DictParser Dict("CompileFlags", this);
92     Dict.handle("Add", [&](Node &N) {
93       if (auto Values = scalarValues(N))
94         F.Add = std::move(*Values);
95     });
96     Dict.handle("Remove", [&](Node &N) {
97       if (auto Values = scalarValues(N))
98         F.Remove = std::move(*Values);
99     });
100     Dict.handle("CompilationDatabase", [&](Node &N) {
101       F.CompilationDatabase = scalarValue(N, "CompilationDatabase");
102     });
103     Dict.parse(N);
104   }
105 
106   void parse(Fragment::StyleBlock &F, Node &N) {
107     DictParser Dict("Style", this);
108     Dict.handle("FullyQualifiedNamespaces", [&](Node &N) {
109       if (auto Values = scalarValues(N))
110         F.FullyQualifiedNamespaces = std::move(*Values);
111     });
112     Dict.parse(N);
113   }
114 
115   void parse(Fragment::DiagnosticsBlock &F, Node &N) {
116     DictParser Dict("Diagnostics", this);
117     Dict.handle("Suppress", [&](Node &N) {
118       if (auto Values = scalarValues(N))
119         F.Suppress = std::move(*Values);
120     });
121     Dict.handle("ClangTidy", [&](Node &N) { parse(F.ClangTidy, N); });
122     Dict.parse(N);
123   }
124 
125   void parse(Fragment::DiagnosticsBlock::ClangTidyBlock &F, Node &N) {
126     DictParser Dict("ClangTidy", this);
127     Dict.handle("Add", [&](Node &N) {
128       if (auto Values = scalarValues(N))
129         F.Add = std::move(*Values);
130     });
131     Dict.handle("Remove", [&](Node &N) {
132       if (auto Values = scalarValues(N))
133         F.Remove = std::move(*Values);
134     });
135     Dict.handle("CheckOptions", [&](Node &N) {
136       DictParser CheckOptDict("CheckOptions", this);
137       CheckOptDict.unrecognized([&](Located<std::string> &&Key, Node &Val) {
138         if (auto Value = scalarValue(Val, *Key))
139           F.CheckOptions.emplace_back(std::move(Key), std::move(*Value));
140         return false; // Don't emit a warning
141       });
142       CheckOptDict.parse(N);
143     });
144     Dict.parse(N);
145   }
146 
147   void parse(Fragment::IndexBlock &F, Node &N) {
148     DictParser Dict("Index", this);
149     Dict.handle("Background",
150                 [&](Node &N) { F.Background = scalarValue(N, "Background"); });
151     Dict.handle("External", [&](Node &N) {
152       Fragment::IndexBlock::ExternalBlock External;
153       // External block can either be a mapping or a scalar value. Dispatch
154       // accordingly.
155       if (N.getType() == Node::NK_Mapping) {
156         parse(External, N);
157       } else if (N.getType() == Node::NK_Scalar ||
158                  N.getType() == Node::NK_BlockScalar) {
159         parse(External, scalarValue(N, "External").getValue());
160       } else {
161         error("External must be either a scalar or a mapping.", N);
162         return;
163       }
164       F.External.emplace(std::move(External));
165       F.External->Range = N.getSourceRange();
166     });
167     Dict.parse(N);
168   }
169 
170   void parse(Fragment::IndexBlock::ExternalBlock &F,
171              Located<std::string> ExternalVal) {
172     if (!llvm::StringRef(*ExternalVal).equals_lower("none")) {
173       error("Only scalar value supported for External is 'None'",
174             ExternalVal.Range);
175       return;
176     }
177     F.IsNone = true;
178     F.IsNone.Range = ExternalVal.Range;
179   }
180 
181   void parse(Fragment::IndexBlock::ExternalBlock &F, Node &N) {
182     DictParser Dict("External", this);
183     Dict.handle("File", [&](Node &N) { F.File = scalarValue(N, "File"); });
184     Dict.handle("Server",
185                 [&](Node &N) { F.Server = scalarValue(N, "Server"); });
186     Dict.handle("MountPoint",
187                 [&](Node &N) { F.MountPoint = scalarValue(N, "MountPoint"); });
188     Dict.parse(N);
189   }
190 
191   void parse(Fragment::CompletionBlock &F, Node &N) {
192     DictParser Dict("Completion", this);
193     Dict.handle("AllScopes", [&](Node &N) {
194       if (auto Value = scalarValue(N, "AllScopes")) {
195         if (auto AllScopes = llvm::yaml::parseBool(**Value))
196           F.AllScopes = *AllScopes;
197         else
198           warning("AllScopes should be a boolean", N);
199       }
200     });
201     Dict.parse(N);
202   }
203 
204   // Helper for parsing mapping nodes (dictionaries).
205   // We don't use YamlIO as we want to control over unknown keys.
206   class DictParser {
207     llvm::StringRef Description;
208     std::vector<std::pair<llvm::StringRef, std::function<void(Node &)>>> Keys;
209     std::function<bool(Located<std::string>, Node &)> UnknownHandler;
210     Parser *Outer;
211 
212   public:
213     DictParser(llvm::StringRef Description, Parser *Outer)
214         : Description(Description), Outer(Outer) {}
215 
216     // Parse is called when Key is encountered, and passed the associated value.
217     // It should emit diagnostics if the value is invalid (e.g. wrong type).
218     // If Key is seen twice, Parse runs only once and an error is reported.
219     void handle(llvm::StringLiteral Key, std::function<void(Node &)> Parse) {
220       for (const auto &Entry : Keys) {
221         (void) Entry;
222         assert(Entry.first != Key && "duplicate key handler");
223       }
224       Keys.emplace_back(Key, std::move(Parse));
225     }
226 
227     // Handler is called when a Key is not matched by any handle().
228     // If this is unset or the Handler returns true, a warning is emitted for
229     // the unknown key.
230     void
231     unrecognized(std::function<bool(Located<std::string>, Node &)> Handler) {
232       UnknownHandler = std::move(Handler);
233     }
234 
235     // Process a mapping node and call handlers for each key/value pair.
236     void parse(Node &N) const {
237       if (N.getType() != Node::NK_Mapping) {
238         Outer->error(Description + " should be a dictionary", N);
239         return;
240       }
241       llvm::SmallSet<std::string, 8> Seen;
242       llvm::SmallVector<Located<std::string>, 0> UnknownKeys;
243       // We *must* consume all items, even on error, or the parser will assert.
244       for (auto &KV : llvm::cast<MappingNode>(N)) {
245         auto *K = KV.getKey();
246         if (!K) // YAMLParser emitted an error.
247           continue;
248         auto Key = Outer->scalarValue(*K, "Dictionary key");
249         if (!Key)
250           continue;
251         if (!Seen.insert(**Key).second) {
252           Outer->warning("Duplicate key " + **Key + " is ignored", *K);
253           if (auto *Value = KV.getValue())
254             Value->skip();
255           continue;
256         }
257         auto *Value = KV.getValue();
258         if (!Value) // YAMLParser emitted an error.
259           continue;
260         bool Matched = false;
261         for (const auto &Handler : Keys) {
262           if (Handler.first == **Key) {
263             Matched = true;
264             Handler.second(*Value);
265             break;
266           }
267         }
268         if (!Matched) {
269           bool Warn = !UnknownHandler;
270           if (UnknownHandler)
271             Warn = UnknownHandler(
272                 Located<std::string>(**Key, K->getSourceRange()), *Value);
273           if (Warn)
274             UnknownKeys.push_back(std::move(*Key));
275         }
276       }
277       if (!UnknownKeys.empty())
278         warnUnknownKeys(UnknownKeys, Seen);
279     }
280 
281   private:
282     void warnUnknownKeys(llvm::ArrayRef<Located<std::string>> UnknownKeys,
283                          const llvm::SmallSet<std::string, 8> &SeenKeys) const {
284       llvm::SmallVector<llvm::StringRef> UnseenKeys;
285       for (const auto &KeyAndHandler : Keys)
286         if (!SeenKeys.count(KeyAndHandler.first.str()))
287           UnseenKeys.push_back(KeyAndHandler.first);
288 
289       for (const Located<std::string> &UnknownKey : UnknownKeys)
290         if (auto BestGuess = bestGuess(*UnknownKey, UnseenKeys))
291           Outer->warning("Unknown " + Description + " key '" + *UnknownKey +
292                              "'; did you mean '" + *BestGuess + "'?",
293                          UnknownKey.Range);
294         else
295           Outer->warning("Unknown " + Description + " key '" + *UnknownKey +
296                              "'",
297                          UnknownKey.Range);
298     }
299   };
300 
301   // Try to parse a single scalar value from the node, warn on failure.
302   llvm::Optional<Located<std::string>> scalarValue(Node &N,
303                                                    llvm::StringRef Desc) {
304     llvm::SmallString<256> Buf;
305     if (auto *S = llvm::dyn_cast<ScalarNode>(&N))
306       return Located<std::string>(S->getValue(Buf).str(), N.getSourceRange());
307     if (auto *BS = llvm::dyn_cast<BlockScalarNode>(&N))
308       return Located<std::string>(BS->getValue().str(), N.getSourceRange());
309     warning(Desc + " should be scalar", N);
310     return llvm::None;
311   }
312 
313   // Try to parse a list of single scalar values, or just a single value.
314   llvm::Optional<std::vector<Located<std::string>>> scalarValues(Node &N) {
315     std::vector<Located<std::string>> Result;
316     if (auto *S = llvm::dyn_cast<ScalarNode>(&N)) {
317       llvm::SmallString<256> Buf;
318       Result.emplace_back(S->getValue(Buf).str(), N.getSourceRange());
319     } else if (auto *S = llvm::dyn_cast<BlockScalarNode>(&N)) {
320       Result.emplace_back(S->getValue().str(), N.getSourceRange());
321     } else if (auto *S = llvm::dyn_cast<SequenceNode>(&N)) {
322       // We *must* consume all items, even on error, or the parser will assert.
323       for (auto &Child : *S) {
324         if (auto Value = scalarValue(Child, "List item"))
325           Result.push_back(std::move(*Value));
326       }
327     } else {
328       warning("Expected scalar or list of scalars", N);
329       return llvm::None;
330     }
331     return Result;
332   }
333 
334   // Report a "hard" error, reflecting a config file that can never be valid.
335   void error(const llvm::Twine &Msg, llvm::SMRange Range) {
336     HadError = true;
337     SM.PrintMessage(Range.Start, llvm::SourceMgr::DK_Error, Msg, Range);
338   }
339   void error(const llvm::Twine &Msg, const Node &N) {
340     return error(Msg, N.getSourceRange());
341   }
342 
343   // Report a "soft" error that could be caused by e.g. version skew.
344   void warning(const llvm::Twine &Msg, llvm::SMRange Range) {
345     SM.PrintMessage(Range.Start, llvm::SourceMgr::DK_Warning, Msg, Range);
346   }
347   void warning(const llvm::Twine &Msg, const Node &N) {
348     return warning(Msg, N.getSourceRange());
349   }
350 };
351 
352 } // namespace
353 
354 std::vector<Fragment> Fragment::parseYAML(llvm::StringRef YAML,
355                                           llvm::StringRef BufferName,
356                                           DiagnosticCallback Diags) {
357   // The YAML document may contain multiple conditional fragments.
358   // The SourceManager is shared for all of them.
359   auto SM = std::make_shared<llvm::SourceMgr>();
360   auto Buf = llvm::MemoryBuffer::getMemBufferCopy(YAML, BufferName);
361   // Adapt DiagnosticCallback to function-pointer interface.
362   // Callback receives both errors we emit and those from the YAML parser.
363   SM->setDiagHandler(
364       [](const llvm::SMDiagnostic &Diag, void *Ctx) {
365         (*reinterpret_cast<DiagnosticCallback *>(Ctx))(Diag);
366       },
367       &Diags);
368   std::vector<Fragment> Result;
369   for (auto &Doc : llvm::yaml::Stream(*Buf, *SM)) {
370     if (Node *N = Doc.getRoot()) {
371       Fragment Fragment;
372       Fragment.Source.Manager = SM;
373       Fragment.Source.Location = N->getSourceRange().Start;
374       SM->PrintMessage(Fragment.Source.Location, llvm::SourceMgr::DK_Note,
375                        "Parsing config fragment");
376       if (Parser(*SM).parse(Fragment, *N))
377         Result.push_back(std::move(Fragment));
378     }
379   }
380   SM->PrintMessage(SM->FindLocForLineAndColumn(SM->getMainFileID(), 0, 0),
381                    llvm::SourceMgr::DK_Note,
382                    "Parsed " + llvm::Twine(Result.size()) +
383                        " fragments from file");
384   // Hack: stash the buffer in the SourceMgr to keep it alive.
385   // SM has two entries: "main" non-owning buffer, and ignored owning buffer.
386   SM->AddNewSourceBuffer(std::move(Buf), llvm::SMLoc());
387   return Result;
388 }
389 
390 } // namespace config
391 } // namespace clangd
392 } // namespace clang
393