Skip to content

Commit 263582f

Browse files
committed
[clang-tidy] Introduced new option --dump-yaml-schema
Since YAML Generate Schema was added, it can be used to dump current clang-tidy's YAML's schema.
1 parent b7e0f9c commit 263582f

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

clang-tools-extra/clang-tidy/ClangTidyOptions.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "llvm/Support/ErrorOr.h"
1717
#include "llvm/Support/MemoryBufferRef.h"
1818
#include "llvm/Support/Path.h"
19+
#include "llvm/Support/YAMLGenerateSchema.h"
1920
#include "llvm/Support/YAMLTraits.h"
2021
#include <algorithm>
2122
#include <optional>
@@ -87,7 +88,7 @@ struct NOptionMap {
8788
template <>
8889
void yamlize(IO &IO, ClangTidyOptions::OptionMap &Val, bool,
8990
EmptyContext &Ctx) {
90-
if (IO.outputting()) {
91+
if (IO.getKind() == IOKind::Outputting) {
9192
// Ensure check options are sorted
9293
std::vector<std::pair<StringRef, StringRef>> SortedOptions;
9394
SortedOptions.reserve(Val.size());
@@ -108,7 +109,7 @@ void yamlize(IO &IO, ClangTidyOptions::OptionMap &Val, bool,
108109
IO.postflightKey(SaveInfo);
109110
}
110111
IO.endMapping();
111-
} else {
112+
} else if (IO.getKind() == IOKind::Inputting) {
112113
// We need custom logic here to support the old method of specifying check
113114
// options using a list of maps containing key and value keys.
114115
auto &I = reinterpret_cast<Input &>(IO);
@@ -128,6 +129,11 @@ void yamlize(IO &IO, ClangTidyOptions::OptionMap &Val, bool,
128129
} else {
129130
IO.setError("expected a sequence or map");
130131
}
132+
} else {
133+
MappingNormalization<NOptionMap, ClangTidyOptions::OptionMap> NOpts(IO,
134+
Val);
135+
EmptyContext Ctx;
136+
yamlize(IO, NOpts->Options, true, Ctx);
131137
}
132138
}
133139

@@ -184,7 +190,7 @@ struct ChecksVariant {
184190
};
185191

186192
template <> void yamlize(IO &IO, ChecksVariant &Val, bool, EmptyContext &Ctx) {
187-
if (!IO.outputting()) {
193+
if (IO.getKind() == IOKind::Inputting) {
188194
// Special case for reading from YAML
189195
// Must support reading from both a string or a list
190196
auto &I = reinterpret_cast<Input &>(IO);
@@ -197,6 +203,9 @@ template <> void yamlize(IO &IO, ChecksVariant &Val, bool, EmptyContext &Ctx) {
197203
} else {
198204
IO.setError("expected string or sequence");
199205
}
206+
} else if (IO.getKind() == IOKind::GeneratingSchema) {
207+
Val.AsVector = std::vector<std::string>();
208+
yamlize(IO, *Val.AsVector, true, Ctx);
200209
}
201210
}
202211

@@ -543,6 +552,12 @@ parseConfiguration(llvm::MemoryBufferRef Config) {
543552
return Options;
544553
}
545554

555+
void dumpConfigurationYAMLSchema(llvm::raw_fd_ostream &Stream) {
556+
ClangTidyOptions Options;
557+
llvm::yaml::GenerateSchema GS(Stream);
558+
GS << Options;
559+
}
560+
546561
static void diagHandlerImpl(const llvm::SMDiagnostic &Diag, void *Ctx) {
547562
(*reinterpret_cast<DiagCallback *>(Ctx))(Diag);
548563
}

clang-tools-extra/clang-tidy/ClangTidyOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,9 @@ std::error_code parseLineFilter(llvm::StringRef LineFilter,
343343
llvm::ErrorOr<ClangTidyOptions>
344344
parseConfiguration(llvm::MemoryBufferRef Config);
345345

346+
/// Dumps configuration YAML Schema to \p Stream
347+
void dumpConfigurationYAMLSchema(llvm::raw_fd_ostream &Stream);
348+
346349
using DiagCallback = llvm::function_ref<void(const llvm::SMDiagnostic &)>;
347350

348351
llvm::ErrorOr<ClangTidyOptions>

clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,12 @@ see https://clang.llvm.org/extra/clang-tidy/QueryBasedCustomChecks.html.
355355
cl::init(false),
356356
cl::cat(ClangTidyCategory));
357357

358+
static cl::opt<bool> DumpYAMLSchema("dump-yaml-schema", desc(R"(
359+
Dumps configuration YAML Schema in JSON format to
360+
stdout.
361+
)"),
362+
cl::init(false),
363+
cl::cat(ClangTidyCategory));
358364
namespace clang::tidy {
359365

360366
static void printStats(const ClangTidyStats &Stats) {
@@ -684,6 +690,11 @@ int clangTidyMain(int argc, const char **argv) {
684690
return 0;
685691
}
686692

693+
if (DumpYAMLSchema) {
694+
dumpConfigurationYAMLSchema(llvm::outs());
695+
return 0;
696+
}
697+
687698
if (VerifyConfig) {
688699
std::vector<ClangTidyOptionsProvider::OptionsSource> RawOptions =
689700
OptionsProvider->getRawOptions(FileName);

0 commit comments

Comments
 (0)