Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/MemoryBufferRef.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/YAMLGenerateSchema.h"
#include "llvm/Support/YAMLTraits.h"
#include <algorithm>
#include <optional>
Expand Down Expand Up @@ -87,7 +88,7 @@ struct NOptionMap {
template <>
void yamlize(IO &IO, ClangTidyOptions::OptionMap &Val, bool,
EmptyContext &Ctx) {
if (IO.outputting()) {
if (IO.getKind() == IOKind::Outputting) {
// Ensure check options are sorted
std::vector<std::pair<StringRef, StringRef>> SortedOptions;
SortedOptions.reserve(Val.size());
Expand All @@ -108,7 +109,7 @@ void yamlize(IO &IO, ClangTidyOptions::OptionMap &Val, bool,
IO.postflightKey(SaveInfo);
}
IO.endMapping();
} else {
} else if (IO.getKind() == IOKind::Inputting) {
// We need custom logic here to support the old method of specifying check
// options using a list of maps containing key and value keys.
auto &I = reinterpret_cast<Input &>(IO);
Expand All @@ -128,6 +129,11 @@ void yamlize(IO &IO, ClangTidyOptions::OptionMap &Val, bool,
} else {
IO.setError("expected a sequence or map");
}
} else {
MappingNormalization<NOptionMap, ClangTidyOptions::OptionMap> NOpts(IO,
Val);
EmptyContext Ctx;
yamlize(IO, NOpts->Options, true, Ctx);
}
}

Expand Down Expand Up @@ -184,7 +190,7 @@ struct ChecksVariant {
};

template <> void yamlize(IO &IO, ChecksVariant &Val, bool, EmptyContext &Ctx) {
if (!IO.outputting()) {
if (IO.getKind() == IOKind::Inputting) {
// Special case for reading from YAML
// Must support reading from both a string or a list
auto &I = reinterpret_cast<Input &>(IO);
Expand All @@ -197,6 +203,9 @@ template <> void yamlize(IO &IO, ChecksVariant &Val, bool, EmptyContext &Ctx) {
} else {
IO.setError("expected string or sequence");
}
} else if (IO.getKind() == IOKind::GeneratingSchema) {
Val.AsVector = std::vector<std::string>();
yamlize(IO, *Val.AsVector, true, Ctx);
}
}

Expand Down Expand Up @@ -543,6 +552,12 @@ parseConfiguration(llvm::MemoryBufferRef Config) {
return Options;
}

void dumpConfigurationYAMLSchema(llvm::raw_ostream &Stream) {
ClangTidyOptions Options;
llvm::yaml::GenerateSchema GS(Stream);
GS << Options;
}

static void diagHandlerImpl(const llvm::SMDiagnostic &Diag, void *Ctx) {
(*reinterpret_cast<DiagCallback *>(Ctx))(Diag);
}
Expand Down
3 changes: 3 additions & 0 deletions clang-tools-extra/clang-tidy/ClangTidyOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ std::error_code parseLineFilter(llvm::StringRef LineFilter,
llvm::ErrorOr<ClangTidyOptions>
parseConfiguration(llvm::MemoryBufferRef Config);

/// Dumps configuration YAML Schema to \p Stream
void dumpConfigurationYAMLSchema(llvm::raw_ostream &Stream);

using DiagCallback = llvm::function_ref<void(const llvm::SMDiagnostic &)>;

llvm::ErrorOr<ClangTidyOptions>
Expand Down
11 changes: 11 additions & 0 deletions clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,12 @@ see https://clang.llvm.org/extra/clang-tidy/QueryBasedCustomChecks.html.
cl::init(false),
cl::cat(ClangTidyCategory));

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

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

if (DumpYAMLSchema) {
dumpConfigurationYAMLSchema(llvm::outs());
return 0;
}

if (VerifyConfig) {
std::vector<ClangTidyOptionsProvider::OptionsSource> RawOptions =
OptionsProvider->getRawOptions(FileName);
Expand Down
Loading