llvm-project

Форк
0
495 строк · 17.7 Кб
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/SmallSet.h"
10
#include "llvm/ADT/SmallString.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 <optional>
16
#include <string>
17
#include <system_error>
18

19
namespace clang {
20
namespace clangd {
21
namespace config {
22
namespace {
23
using llvm::yaml::BlockScalarNode;
24
using llvm::yaml::MappingNode;
25
using llvm::yaml::Node;
26
using llvm::yaml::ScalarNode;
27
using llvm::yaml::SequenceNode;
28

29
std::optional<llvm::StringRef>
30
bestGuess(llvm::StringRef Search,
31
          llvm::ArrayRef<llvm::StringRef> AllowedValues) {
32
  unsigned MaxEdit = (Search.size() + 1) / 3;
33
  if (!MaxEdit)
34
    return std::nullopt;
35
  std::optional<llvm::StringRef> Result;
36
  for (const auto &AllowedValue : AllowedValues) {
37
    unsigned EditDistance = Search.edit_distance(AllowedValue, true, MaxEdit);
38
    // We can't do better than an edit distance of 1, so just return this and
39
    // save computing other values.
40
    if (EditDistance == 1U)
41
      return AllowedValue;
42
    if (EditDistance == MaxEdit && !Result) {
43
      Result = AllowedValue;
44
    } else if (EditDistance < MaxEdit) {
45
      Result = AllowedValue;
46
      MaxEdit = EditDistance;
47
    }
48
  }
49
  return Result;
50
}
51

52
class Parser {
53
  llvm::SourceMgr &SM;
54
  bool HadError = false;
55

56
public:
57
  Parser(llvm::SourceMgr &SM) : SM(SM) {}
58

59
  // Tries to parse N into F, returning false if it failed and we couldn't
60
  // meaningfully recover (YAML syntax error, or hard semantic error).
61
  bool parse(Fragment &F, Node &N) {
62
    DictParser Dict("Config", this);
63
    Dict.handle("If", [&](Node &N) { parse(F.If, N); });
64
    Dict.handle("CompileFlags", [&](Node &N) { parse(F.CompileFlags, N); });
65
    Dict.handle("Index", [&](Node &N) { parse(F.Index, N); });
66
    Dict.handle("Style", [&](Node &N) { parse(F.Style, N); });
67
    Dict.handle("Diagnostics", [&](Node &N) { parse(F.Diagnostics, N); });
68
    Dict.handle("Completion", [&](Node &N) { parse(F.Completion, N); });
69
    Dict.handle("Hover", [&](Node &N) { parse(F.Hover, N); });
70
    Dict.handle("InlayHints", [&](Node &N) { parse(F.InlayHints, N); });
71
    Dict.handle("SemanticTokens", [&](Node &N) { parse(F.SemanticTokens, N); });
72
    Dict.parse(N);
73
    return !(N.failed() || HadError);
74
  }
75

76
private:
77
  void parse(Fragment::IfBlock &F, Node &N) {
78
    DictParser Dict("If", this);
79
    Dict.unrecognized([&](Located<std::string>, Node &) {
80
      F.HasUnrecognizedCondition = true;
81
      return true; // Emit a warning for the unrecognized key.
82
    });
83
    Dict.handle("PathMatch", [&](Node &N) {
84
      if (auto Values = scalarValues(N))
85
        F.PathMatch = std::move(*Values);
86
    });
87
    Dict.handle("PathExclude", [&](Node &N) {
88
      if (auto Values = scalarValues(N))
89
        F.PathExclude = std::move(*Values);
90
    });
91
    Dict.parse(N);
92
  }
93

94
  void parse(Fragment::CompileFlagsBlock &F, Node &N) {
95
    DictParser Dict("CompileFlags", this);
96
    Dict.handle("Compiler", [&](Node &N) {
97
      if (auto Value = scalarValue(N, "Compiler"))
98
        F.Compiler = std::move(*Value);
99
    });
100
    Dict.handle("Add", [&](Node &N) {
101
      if (auto Values = scalarValues(N))
102
        F.Add = std::move(*Values);
103
    });
104
    Dict.handle("Remove", [&](Node &N) {
105
      if (auto Values = scalarValues(N))
106
        F.Remove = std::move(*Values);
107
    });
108
    Dict.handle("CompilationDatabase", [&](Node &N) {
109
      F.CompilationDatabase = scalarValue(N, "CompilationDatabase");
110
    });
111
    Dict.parse(N);
112
  }
113

114
  void parse(Fragment::StyleBlock &F, Node &N) {
115
    DictParser Dict("Style", this);
116
    Dict.handle("FullyQualifiedNamespaces", [&](Node &N) {
117
      if (auto Values = scalarValues(N))
118
        F.FullyQualifiedNamespaces = std::move(*Values);
119
    });
120
    Dict.parse(N);
121
  }
122

123
  void parse(Fragment::DiagnosticsBlock &F, Node &N) {
124
    DictParser Dict("Diagnostics", this);
125
    Dict.handle("Suppress", [&](Node &N) {
126
      if (auto Values = scalarValues(N))
127
        F.Suppress = std::move(*Values);
128
    });
129
    Dict.handle("UnusedIncludes", [&](Node &N) {
130
      F.UnusedIncludes = scalarValue(N, "UnusedIncludes");
131
    });
132
    Dict.handle("MissingIncludes", [&](Node &N) {
133
      F.MissingIncludes = scalarValue(N, "MissingIncludes");
134
    });
135
    Dict.handle("Includes", [&](Node &N) { parse(F.Includes, N); });
136
    Dict.handle("ClangTidy", [&](Node &N) { parse(F.ClangTidy, N); });
137
    Dict.parse(N);
138
  }
139

140
  void parse(Fragment::DiagnosticsBlock::ClangTidyBlock &F, Node &N) {
141
    DictParser Dict("ClangTidy", this);
142
    Dict.handle("Add", [&](Node &N) {
143
      if (auto Values = scalarValues(N))
144
        F.Add = std::move(*Values);
145
    });
146
    Dict.handle("Remove", [&](Node &N) {
147
      if (auto Values = scalarValues(N))
148
        F.Remove = std::move(*Values);
149
    });
150
    Dict.handle("CheckOptions", [&](Node &N) {
151
      DictParser CheckOptDict("CheckOptions", this);
152
      CheckOptDict.unrecognized([&](Located<std::string> &&Key, Node &Val) {
153
        if (auto Value = scalarValue(Val, *Key))
154
          F.CheckOptions.emplace_back(std::move(Key), std::move(*Value));
155
        return false; // Don't emit a warning
156
      });
157
      CheckOptDict.parse(N);
158
    });
159
    Dict.handle("FastCheckFilter", [&](Node &N) {
160
      if (auto FastCheckFilter = scalarValue(N, "FastCheckFilter"))
161
        F.FastCheckFilter = *FastCheckFilter;
162
    });
163
    Dict.parse(N);
164
  }
165

166
  void parse(Fragment::DiagnosticsBlock::IncludesBlock &F, Node &N) {
167
    DictParser Dict("Includes", this);
168
    Dict.handle("IgnoreHeader", [&](Node &N) {
169
      if (auto Values = scalarValues(N))
170
        F.IgnoreHeader = std::move(*Values);
171
    });
172
    Dict.handle("AnalyzeAngledIncludes", [&](Node &N) {
173
      if (auto Value = boolValue(N, "AnalyzeAngledIncludes"))
174
        F.AnalyzeAngledIncludes = *Value;
175
    });
176
    Dict.parse(N);
177
  }
178

179
  void parse(Fragment::IndexBlock &F, Node &N) {
180
    DictParser Dict("Index", this);
181
    Dict.handle("Background",
182
                [&](Node &N) { F.Background = scalarValue(N, "Background"); });
183
    Dict.handle("External", [&](Node &N) {
184
      Fragment::IndexBlock::ExternalBlock External;
185
      // External block can either be a mapping or a scalar value. Dispatch
186
      // accordingly.
187
      if (N.getType() == Node::NK_Mapping) {
188
        parse(External, N);
189
      } else if (N.getType() == Node::NK_Scalar ||
190
                 N.getType() == Node::NK_BlockScalar) {
191
        parse(External, *scalarValue(N, "External"));
192
      } else {
193
        error("External must be either a scalar or a mapping.", N);
194
        return;
195
      }
196
      F.External.emplace(std::move(External));
197
      F.External->Range = N.getSourceRange();
198
    });
199
    Dict.handle("StandardLibrary", [&](Node &N) {
200
      if (auto StandardLibrary = boolValue(N, "StandardLibrary"))
201
        F.StandardLibrary = *StandardLibrary;
202
    });
203
    Dict.parse(N);
204
  }
205

206
  void parse(Fragment::IndexBlock::ExternalBlock &F,
207
             Located<std::string> ExternalVal) {
208
    if (!llvm::StringRef(*ExternalVal).equals_insensitive("none")) {
209
      error("Only scalar value supported for External is 'None'",
210
            ExternalVal.Range);
211
      return;
212
    }
213
    F.IsNone = true;
214
    F.IsNone.Range = ExternalVal.Range;
215
  }
216

217
  void parse(Fragment::IndexBlock::ExternalBlock &F, Node &N) {
218
    DictParser Dict("External", this);
219
    Dict.handle("File", [&](Node &N) { F.File = scalarValue(N, "File"); });
220
    Dict.handle("Server",
221
                [&](Node &N) { F.Server = scalarValue(N, "Server"); });
222
    Dict.handle("MountPoint",
223
                [&](Node &N) { F.MountPoint = scalarValue(N, "MountPoint"); });
224
    Dict.parse(N);
225
  }
226

227
  void parse(Fragment::CompletionBlock &F, Node &N) {
228
    DictParser Dict("Completion", this);
229
    Dict.handle("AllScopes", [&](Node &N) {
230
      if (auto AllScopes = boolValue(N, "AllScopes"))
231
        F.AllScopes = *AllScopes;
232
    });
233
    Dict.parse(N);
234
  }
235

236
  void parse(Fragment::HoverBlock &F, Node &N) {
237
    DictParser Dict("Hover", this);
238
    Dict.handle("ShowAKA", [&](Node &N) {
239
      if (auto ShowAKA = boolValue(N, "ShowAKA"))
240
        F.ShowAKA = *ShowAKA;
241
    });
242
    Dict.parse(N);
243
  }
244

245
  void parse(Fragment::InlayHintsBlock &F, Node &N) {
246
    DictParser Dict("InlayHints", this);
247
    Dict.handle("Enabled", [&](Node &N) {
248
      if (auto Value = boolValue(N, "Enabled"))
249
        F.Enabled = *Value;
250
    });
251
    Dict.handle("ParameterNames", [&](Node &N) {
252
      if (auto Value = boolValue(N, "ParameterNames"))
253
        F.ParameterNames = *Value;
254
    });
255
    Dict.handle("DeducedTypes", [&](Node &N) {
256
      if (auto Value = boolValue(N, "DeducedTypes"))
257
        F.DeducedTypes = *Value;
258
    });
259
    Dict.handle("Designators", [&](Node &N) {
260
      if (auto Value = boolValue(N, "Designators"))
261
        F.Designators = *Value;
262
    });
263
    Dict.handle("BlockEnd", [&](Node &N) {
264
      if (auto Value = boolValue(N, "BlockEnd"))
265
        F.BlockEnd = *Value;
266
    });
267
    Dict.handle("TypeNameLimit", [&](Node &N) {
268
      if (auto Value = uint32Value(N, "TypeNameLimit"))
269
        F.TypeNameLimit = *Value;
270
    });
271
    Dict.parse(N);
272
  }
273

274
  void parse(Fragment::SemanticTokensBlock &F, Node &N) {
275
    DictParser Dict("SemanticTokens", this);
276
    Dict.handle("DisabledKinds", [&](Node &N) {
277
      if (auto Values = scalarValues(N))
278
        F.DisabledKinds = std::move(*Values);
279
    });
280
    Dict.handle("DisabledModifiers", [&](Node &N) {
281
      if (auto Values = scalarValues(N))
282
        F.DisabledModifiers = std::move(*Values);
283
    });
284
    Dict.parse(N);
285
  }
286

287
  // Helper for parsing mapping nodes (dictionaries).
288
  // We don't use YamlIO as we want to control over unknown keys.
289
  class DictParser {
290
    llvm::StringRef Description;
291
    std::vector<std::pair<llvm::StringRef, std::function<void(Node &)>>> Keys;
292
    std::function<bool(Located<std::string>, Node &)> UnknownHandler;
293
    Parser *Outer;
294

295
  public:
296
    DictParser(llvm::StringRef Description, Parser *Outer)
297
        : Description(Description), Outer(Outer) {}
298

299
    // Parse is called when Key is encountered, and passed the associated value.
300
    // It should emit diagnostics if the value is invalid (e.g. wrong type).
301
    // If Key is seen twice, Parse runs only once and an error is reported.
302
    void handle(llvm::StringLiteral Key, std::function<void(Node &)> Parse) {
303
      for (const auto &Entry : Keys) {
304
        (void)Entry;
305
        assert(Entry.first != Key && "duplicate key handler");
306
      }
307
      Keys.emplace_back(Key, std::move(Parse));
308
    }
309

310
    // Handler is called when a Key is not matched by any handle().
311
    // If this is unset or the Handler returns true, a warning is emitted for
312
    // the unknown key.
313
    void
314
    unrecognized(std::function<bool(Located<std::string>, Node &)> Handler) {
315
      UnknownHandler = std::move(Handler);
316
    }
317

318
    // Process a mapping node and call handlers for each key/value pair.
319
    void parse(Node &N) const {
320
      if (N.getType() != Node::NK_Mapping) {
321
        Outer->error(Description + " should be a dictionary", N);
322
        return;
323
      }
324
      llvm::SmallSet<std::string, 8> Seen;
325
      llvm::SmallVector<Located<std::string>, 0> UnknownKeys;
326
      // We *must* consume all items, even on error, or the parser will assert.
327
      for (auto &KV : llvm::cast<MappingNode>(N)) {
328
        auto *K = KV.getKey();
329
        if (!K) // YAMLParser emitted an error.
330
          continue;
331
        auto Key = Outer->scalarValue(*K, "Dictionary key");
332
        if (!Key)
333
          continue;
334
        if (!Seen.insert(**Key).second) {
335
          Outer->warning("Duplicate key " + **Key + " is ignored", *K);
336
          if (auto *Value = KV.getValue())
337
            Value->skip();
338
          continue;
339
        }
340
        auto *Value = KV.getValue();
341
        if (!Value) // YAMLParser emitted an error.
342
          continue;
343
        bool Matched = false;
344
        for (const auto &Handler : Keys) {
345
          if (Handler.first == **Key) {
346
            Matched = true;
347
            Handler.second(*Value);
348
            break;
349
          }
350
        }
351
        if (!Matched) {
352
          bool Warn = !UnknownHandler;
353
          if (UnknownHandler)
354
            Warn = UnknownHandler(
355
                Located<std::string>(**Key, K->getSourceRange()), *Value);
356
          if (Warn)
357
            UnknownKeys.push_back(std::move(*Key));
358
        }
359
      }
360
      if (!UnknownKeys.empty())
361
        warnUnknownKeys(UnknownKeys, Seen);
362
    }
363

364
  private:
365
    void warnUnknownKeys(llvm::ArrayRef<Located<std::string>> UnknownKeys,
366
                         const llvm::SmallSet<std::string, 8> &SeenKeys) const {
367
      llvm::SmallVector<llvm::StringRef> UnseenKeys;
368
      for (const auto &KeyAndHandler : Keys)
369
        if (!SeenKeys.count(KeyAndHandler.first.str()))
370
          UnseenKeys.push_back(KeyAndHandler.first);
371

372
      for (const Located<std::string> &UnknownKey : UnknownKeys)
373
        if (auto BestGuess = bestGuess(*UnknownKey, UnseenKeys))
374
          Outer->warning("Unknown " + Description + " key '" + *UnknownKey +
375
                             "'; did you mean '" + *BestGuess + "'?",
376
                         UnknownKey.Range);
377
        else
378
          Outer->warning("Unknown " + Description + " key '" + *UnknownKey +
379
                             "'",
380
                         UnknownKey.Range);
381
    }
382
  };
383

384
  // Try to parse a single scalar value from the node, warn on failure.
385
  std::optional<Located<std::string>> scalarValue(Node &N,
386
                                                  llvm::StringRef Desc) {
387
    llvm::SmallString<256> Buf;
388
    if (auto *S = llvm::dyn_cast<ScalarNode>(&N))
389
      return Located<std::string>(S->getValue(Buf).str(), N.getSourceRange());
390
    if (auto *BS = llvm::dyn_cast<BlockScalarNode>(&N))
391
      return Located<std::string>(BS->getValue().str(), N.getSourceRange());
392
    warning(Desc + " should be scalar", N);
393
    return std::nullopt;
394
  }
395

396
  std::optional<Located<bool>> boolValue(Node &N, llvm::StringRef Desc) {
397
    if (auto Scalar = scalarValue(N, Desc)) {
398
      if (auto Bool = llvm::yaml::parseBool(**Scalar))
399
        return Located<bool>(*Bool, Scalar->Range);
400
      warning(Desc + " should be a boolean", N);
401
    }
402
    return std::nullopt;
403
  }
404

405
  std::optional<Located<uint32_t>> uint32Value(Node &N, llvm::StringRef Desc) {
406
    if (auto Scalar = scalarValue(N, Desc)) {
407
      unsigned long long Num;
408
      if (!llvm::getAsUnsignedInteger(**Scalar, 0, Num)) {
409
        return Located<uint32_t>(Num, Scalar->Range);
410
      }
411
    }
412
    warning(Desc + " invalid number", N);
413
    return std::nullopt;
414
  }
415

416
  // Try to parse a list of single scalar values, or just a single value.
417
  std::optional<std::vector<Located<std::string>>> scalarValues(Node &N) {
418
    std::vector<Located<std::string>> Result;
419
    if (auto *S = llvm::dyn_cast<ScalarNode>(&N)) {
420
      llvm::SmallString<256> Buf;
421
      Result.emplace_back(S->getValue(Buf).str(), N.getSourceRange());
422
    } else if (auto *S = llvm::dyn_cast<BlockScalarNode>(&N)) {
423
      Result.emplace_back(S->getValue().str(), N.getSourceRange());
424
    } else if (auto *S = llvm::dyn_cast<SequenceNode>(&N)) {
425
      // We *must* consume all items, even on error, or the parser will assert.
426
      for (auto &Child : *S) {
427
        if (auto Value = scalarValue(Child, "List item"))
428
          Result.push_back(std::move(*Value));
429
      }
430
    } else {
431
      warning("Expected scalar or list of scalars", N);
432
      return std::nullopt;
433
    }
434
    return Result;
435
  }
436

437
  // Report a "hard" error, reflecting a config file that can never be valid.
438
  void error(const llvm::Twine &Msg, llvm::SMRange Range) {
439
    HadError = true;
440
    SM.PrintMessage(Range.Start, llvm::SourceMgr::DK_Error, Msg, Range);
441
  }
442
  void error(const llvm::Twine &Msg, const Node &N) {
443
    return error(Msg, N.getSourceRange());
444
  }
445

446
  // Report a "soft" error that could be caused by e.g. version skew.
447
  void warning(const llvm::Twine &Msg, llvm::SMRange Range) {
448
    SM.PrintMessage(Range.Start, llvm::SourceMgr::DK_Warning, Msg, Range);
449
  }
450
  void warning(const llvm::Twine &Msg, const Node &N) {
451
    return warning(Msg, N.getSourceRange());
452
  }
453
};
454

455
} // namespace
456

457
std::vector<Fragment> Fragment::parseYAML(llvm::StringRef YAML,
458
                                          llvm::StringRef BufferName,
459
                                          DiagnosticCallback Diags) {
460
  // The YAML document may contain multiple conditional fragments.
461
  // The SourceManager is shared for all of them.
462
  auto SM = std::make_shared<llvm::SourceMgr>();
463
  auto Buf = llvm::MemoryBuffer::getMemBufferCopy(YAML, BufferName);
464
  // Adapt DiagnosticCallback to function-pointer interface.
465
  // Callback receives both errors we emit and those from the YAML parser.
466
  SM->setDiagHandler(
467
      [](const llvm::SMDiagnostic &Diag, void *Ctx) {
468
        (*reinterpret_cast<DiagnosticCallback *>(Ctx))(Diag);
469
      },
470
      &Diags);
471
  std::vector<Fragment> Result;
472
  for (auto &Doc : llvm::yaml::Stream(*Buf, *SM)) {
473
    if (Node *N = Doc.getRoot()) {
474
      Fragment Fragment;
475
      Fragment.Source.Manager = SM;
476
      Fragment.Source.Location = N->getSourceRange().Start;
477
      SM->PrintMessage(Fragment.Source.Location, llvm::SourceMgr::DK_Note,
478
                       "Parsing config fragment");
479
      if (Parser(*SM).parse(Fragment, *N))
480
        Result.push_back(std::move(Fragment));
481
    }
482
  }
483
  SM->PrintMessage(SM->FindLocForLineAndColumn(SM->getMainFileID(), 0, 0),
484
                   llvm::SourceMgr::DK_Note,
485
                   "Parsed " + llvm::Twine(Result.size()) +
486
                       " fragments from file");
487
  // Hack: stash the buffer in the SourceMgr to keep it alive.
488
  // SM has two entries: "main" non-owning buffer, and ignored owning buffer.
489
  SM->AddNewSourceBuffer(std::move(Buf), llvm::SMLoc());
490
  return Result;
491
}
492

493
} // namespace config
494
} // namespace clangd
495
} // namespace clang
496

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.