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