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 
9 #include "ConfigFragment.h"
10 #include "llvm/ADT/Optional.h"
11 #include "llvm/ADT/SmallSet.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/Support/MemoryBuffer.h"
14 #include "llvm/Support/SourceMgr.h"
15 #include "llvm/Support/YAMLParser.h"
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 class Parser {
29   llvm::SourceMgr &SM;
30   bool HadError = false;
31 
32 public:
Parser(llvm::SourceMgr & SM)33   Parser(llvm::SourceMgr &SM) : SM(SM) {}
34 
35   // Tries to parse N into F, returning false if it failed and we couldn't
36   // meaningfully recover (YAML syntax error, or hard semantic error).
parse(Fragment & F,Node & N)37   bool parse(Fragment &F, Node &N) {
38     DictParser Dict("Config", this);
39     Dict.handle("If", [&](Node &N) { parse(F.If, N); });
40     Dict.handle("CompileFlags", [&](Node &N) { parse(F.CompileFlags, N); });
41     Dict.parse(N);
42     return !(N.failed() || HadError);
43   }
44 
45 private:
parse(Fragment::IfBlock & F,Node & N)46   void parse(Fragment::IfBlock &F, Node &N) {
47     DictParser Dict("If", this);
48     Dict.unrecognized(
49         [&](llvm::StringRef) { F.HasUnrecognizedCondition = true; });
50     Dict.handle("PathMatch", [&](Node &N) {
51       if (auto Values = scalarValues(N))
52         F.PathMatch = std::move(*Values);
53     });
54     Dict.handle("PathExclude", [&](Node &N) {
55       if (auto Values = scalarValues(N))
56         F.PathExclude = std::move(*Values);
57     });
58     Dict.parse(N);
59   }
60 
parse(Fragment::CompileFlagsBlock & F,Node & N)61   void parse(Fragment::CompileFlagsBlock &F, Node &N) {
62     DictParser Dict("CompileFlags", this);
63     Dict.handle("Add", [&](Node &N) {
64       if (auto Values = scalarValues(N))
65         F.Add = std::move(*Values);
66     });
67     Dict.handle("Remove", [&](Node &N) {
68       if (auto Values = scalarValues(N))
69         F.Remove = std::move(*Values);
70     });
71     Dict.parse(N);
72   }
73 
parse(Fragment::IndexBlock & F,Node & N)74   void parse(Fragment::IndexBlock &F, Node &N) {
75     DictParser Dict("Index", this);
76     Dict.handle("Background",
77                 [&](Node &N) { F.Background = scalarValue(N, "Background"); });
78     Dict.parse(N);
79   }
80 
81   // Helper for parsing mapping nodes (dictionaries).
82   // We don't use YamlIO as we want to control over unknown keys.
83   class DictParser {
84     llvm::StringRef Description;
85     std::vector<std::pair<llvm::StringRef, std::function<void(Node &)>>> Keys;
86     std::function<void(llvm::StringRef)> Unknown;
87     Parser *Outer;
88 
89   public:
DictParser(llvm::StringRef Description,Parser * Outer)90     DictParser(llvm::StringRef Description, Parser *Outer)
91         : Description(Description), Outer(Outer) {}
92 
93     // Parse is called when Key is encountered, and passed the associated value.
94     // It should emit diagnostics if the value is invalid (e.g. wrong type).
95     // If Key is seen twice, Parse runs only once and an error is reported.
handle(llvm::StringLiteral Key,std::function<void (Node &)> Parse)96     void handle(llvm::StringLiteral Key, std::function<void(Node &)> Parse) {
97       for (const auto &Entry : Keys) {
98         (void) Entry;
99         assert(Entry.first != Key && "duplicate key handler");
100       }
101       Keys.emplace_back(Key, std::move(Parse));
102     }
103 
104     // Fallback is called when a Key is not matched by any handle().
105     // A warning is also automatically emitted.
unrecognized(std::function<void (llvm::StringRef)> Fallback)106     void unrecognized(std::function<void(llvm::StringRef)> Fallback) {
107       Unknown = std::move(Fallback);
108     }
109 
110     // Process a mapping node and call handlers for each key/value pair.
parse(Node & N) const111     void parse(Node &N) const {
112       if (N.getType() != Node::NK_Mapping) {
113         Outer->error(Description + " should be a dictionary", N);
114         return;
115       }
116       llvm::SmallSet<std::string, 8> Seen;
117       // We *must* consume all items, even on error, or the parser will assert.
118       for (auto &KV : llvm::cast<MappingNode>(N)) {
119         auto *K = KV.getKey();
120         if (!K) // YAMLParser emitted an error.
121           continue;
122         auto Key = Outer->scalarValue(*K, "Dictionary key");
123         if (!Key)
124           continue;
125         if (!Seen.insert(**Key).second) {
126           Outer->warning("Duplicate key " + **Key + " is ignored", *K);
127           continue;
128         }
129         auto *Value = KV.getValue();
130         if (!Value) // YAMLParser emitted an error.
131           continue;
132         bool Matched = false;
133         for (const auto &Handler : Keys) {
134           if (Handler.first == **Key) {
135             Matched = true;
136             Handler.second(*Value);
137             break;
138           }
139         }
140         if (!Matched) {
141           Outer->warning("Unknown " + Description + " key " + **Key, *K);
142           if (Unknown)
143             Unknown(**Key);
144         }
145       }
146     }
147   };
148 
149   // Try to parse a single scalar value from the node, warn on failure.
scalarValue(Node & N,llvm::StringRef Desc)150   llvm::Optional<Located<std::string>> scalarValue(Node &N,
151                                                    llvm::StringRef Desc) {
152     llvm::SmallString<256> Buf;
153     if (auto *S = llvm::dyn_cast<ScalarNode>(&N))
154       return Located<std::string>(S->getValue(Buf).str(), N.getSourceRange());
155     if (auto *BS = llvm::dyn_cast<BlockScalarNode>(&N))
156       return Located<std::string>(BS->getValue().str(), N.getSourceRange());
157     warning(Desc + " should be scalar", N);
158     return llvm::None;
159   }
160 
161   // Try to parse a list of single scalar values, or just a single value.
scalarValues(Node & N)162   llvm::Optional<std::vector<Located<std::string>>> scalarValues(Node &N) {
163     std::vector<Located<std::string>> Result;
164     if (auto *S = llvm::dyn_cast<ScalarNode>(&N)) {
165       llvm::SmallString<256> Buf;
166       Result.emplace_back(S->getValue(Buf).str(), N.getSourceRange());
167     } else if (auto *S = llvm::dyn_cast<BlockScalarNode>(&N)) {
168       Result.emplace_back(S->getValue().str(), N.getSourceRange());
169     } else if (auto *S = llvm::dyn_cast<SequenceNode>(&N)) {
170       // We *must* consume all items, even on error, or the parser will assert.
171       for (auto &Child : *S) {
172         if (auto Value = scalarValue(Child, "List item"))
173           Result.push_back(std::move(*Value));
174       }
175     } else {
176       warning("Expected scalar or list of scalars", N);
177       return llvm::None;
178     }
179     return Result;
180   }
181 
182   // Report a "hard" error, reflecting a config file that can never be valid.
error(const llvm::Twine & Msg,const Node & N)183   void error(const llvm::Twine &Msg, const Node &N) {
184     HadError = true;
185     SM.PrintMessage(N.getSourceRange().Start, llvm::SourceMgr::DK_Error, Msg,
186                     N.getSourceRange());
187   }
188 
189   // Report a "soft" error that could be caused by e.g. version skew.
warning(const llvm::Twine & Msg,const Node & N)190   void warning(const llvm::Twine &Msg, const Node &N) {
191     SM.PrintMessage(N.getSourceRange().Start, llvm::SourceMgr::DK_Warning, Msg,
192                     N.getSourceRange());
193   }
194 };
195 
196 } // namespace
197 
parseYAML(llvm::StringRef YAML,llvm::StringRef BufferName,DiagnosticCallback Diags)198 std::vector<Fragment> Fragment::parseYAML(llvm::StringRef YAML,
199                                           llvm::StringRef BufferName,
200                                           DiagnosticCallback Diags) {
201   // The YAML document may contain multiple conditional fragments.
202   // The SourceManager is shared for all of them.
203   auto SM = std::make_shared<llvm::SourceMgr>();
204   auto Buf = llvm::MemoryBuffer::getMemBufferCopy(YAML, BufferName);
205   // Adapt DiagnosticCallback to function-pointer interface.
206   // Callback receives both errors we emit and those from the YAML parser.
207   SM->setDiagHandler(
208       [](const llvm::SMDiagnostic &Diag, void *Ctx) {
209         (*reinterpret_cast<DiagnosticCallback *>(Ctx))(Diag);
210       },
211       &Diags);
212   std::vector<Fragment> Result;
213   for (auto &Doc : llvm::yaml::Stream(*Buf, *SM)) {
214     if (Node *N = Doc.getRoot()) {
215       Fragment Fragment;
216       Fragment.Source.Manager = SM;
217       Fragment.Source.Location = N->getSourceRange().Start;
218       if (Parser(*SM).parse(Fragment, *N))
219         Result.push_back(std::move(Fragment));
220     }
221   }
222   // Hack: stash the buffer in the SourceMgr to keep it alive.
223   // SM has two entries: "main" non-owning buffer, and ignored owning buffer.
224   SM->AddNewSourceBuffer(std::move(Buf), llvm::SMLoc());
225   return Result;
226 }
227 
228 } // namespace config
229 } // namespace clangd
230 } // namespace clang
231