Skip to content

Commit db6b315

Browse files
committed
extend func
1 parent 6ab7149 commit db6b315

File tree

5 files changed

+49
-5
lines changed

5 files changed

+49
-5
lines changed

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "ClangTidyOptions.h"
1010
#include "ClangTidyModuleRegistry.h"
11+
#include "clang/Basic/DiagnosticIDs.h"
1112
#include "clang/Basic/LLVM.h"
1213
#include "llvm/ADT/SmallString.h"
1314
#include "llvm/ADT/StringExtras.h"
@@ -71,7 +72,8 @@ struct NOptionMap {
7172
NOptionMap(IO &, const ClangTidyOptions::OptionMap &OptionMap) {
7273
Options.reserve(OptionMap.size());
7374
for (const auto &KeyValue : OptionMap)
74-
Options.emplace_back(std::string(KeyValue.getKey()), KeyValue.getValue().Value);
75+
Options.emplace_back(std::string(KeyValue.getKey()),
76+
KeyValue.getValue().Value);
7577
}
7678
ClangTidyOptions::OptionMap denormalize(IO &) {
7779
ClangTidyOptions::OptionMap Map;
@@ -135,13 +137,30 @@ template <> struct BlockScalarTraits<MultiLineString> {
135137
static void output(const MultiLineString &S, void *Ctxt, raw_ostream &OS) {
136138
OS << S.S;
137139
}
138-
139140
static StringRef input(StringRef Str, void *Ctxt, MultiLineString &S) {
140141
S.S = Str;
141142
return "";
142143
}
143144
};
144145

146+
template <> struct ScalarEnumerationTraits<clang::DiagnosticIDs::Level> {
147+
static void enumeration(IO &IO, clang::DiagnosticIDs::Level &Level) {
148+
IO.enumCase(Level, "Error", clang::DiagnosticIDs::Level::Error);
149+
IO.enumCase(Level, "Warning", clang::DiagnosticIDs::Level::Warning);
150+
IO.enumCase(Level, "Note", clang::DiagnosticIDs::Level::Note);
151+
}
152+
};
153+
template <> struct SequenceElementTraits<ClangTidyOptions::CustomCheckDiag> {
154+
static const bool flow = false;
155+
};
156+
template <> struct MappingTraits<ClangTidyOptions::CustomCheckDiag> {
157+
static void mapping(IO &IO, ClangTidyOptions::CustomCheckDiag &D) {
158+
IO.mapRequired("BindName", D.BindName);
159+
MultiLineString MLS{D.Message};
160+
IO.mapRequired("Message", MLS);
161+
IO.mapOptional("Level", D.Level);
162+
}
163+
};
145164
template <> struct SequenceElementTraits<ClangTidyOptions::CustomCheckValue> {
146165
static const bool flow = false;
147166
};
@@ -150,6 +169,7 @@ template <> struct MappingTraits<ClangTidyOptions::CustomCheckValue> {
150169
IO.mapRequired("Name", V.Name);
151170
MultiLineString MLS{V.Query};
152171
IO.mapRequired("Query", MLS);
172+
IO.mapRequired("Diagnostic", V.Diags);
153173
}
154174
};
155175

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONS_H
1010
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONS_H
1111

12+
#include "clang/Basic/DiagnosticIDs.h"
1213
#include "llvm/ADT/IntrusiveRefCntPtr.h"
1314
#include "llvm/ADT/SmallString.h"
1415
#include "llvm/ADT/StringMap.h"
@@ -17,6 +18,7 @@
1718
#include "llvm/Support/MemoryBufferRef.h"
1819
#include "llvm/Support/VirtualFileSystem.h"
1920
#include <functional>
21+
#include <map>
2022
#include <optional>
2123
#include <string>
2224
#include <system_error>
@@ -129,10 +131,15 @@ struct ClangTidyOptions {
129131
/// Key-value mapping used to store check-specific options.
130132
OptionMap CheckOptions;
131133

134+
struct CustomCheckDiag {
135+
std::string BindName;
136+
std::string Message;
137+
std::optional<DiagnosticIDs::Level> Level;
138+
};
132139
struct CustomCheckValue {
133140
std::string Name;
134141
std::string Query;
135-
// FIXME: extend more features here (e.g. isLanguageVersionSupported, Level)
142+
llvm::SmallVector<CustomCheckDiag> Diags;
136143
};
137144
using CustomCheckValueList = llvm::SmallVector<CustomCheckValue>;
138145
std::optional<CustomCheckValueList> CustomChecks;

clang-tools-extra/clang-tidy/custom/CustomTidyModule.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class CustomModule : public ClangTidyModule {
2222
for (const ClangTidyOptions::CustomCheckValue &V :
2323
Options->CustomChecks.value()) {
2424
CheckFactories.registerCheckFactory(
25+
// add custom- prefix to avoid conflicts with builtin checks
2526
"custom-" + V.Name,
2627
[&V](llvm::StringRef Name, ClangTidyContext *Context) {
2728
return std::make_unique<custom::QueryCheck>(Name, V, Context);

clang-tools-extra/clang-tidy/custom/QueryCheck.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include "../../clang-query/Query.h"
1111
#include "../../clang-query/QueryParser.h"
1212
#include "clang/ASTMatchers/Dynamic/VariantValue.h"
13+
#include "clang/Basic/DiagnosticIDs.h"
14+
#include "llvm/ADT/SmallVector.h"
1315
#include "llvm/ADT/StringRef.h"
1416

1517
using namespace clang::ast_matchers;
@@ -19,7 +21,13 @@ namespace clang::tidy::custom {
1921
QueryCheck::QueryCheck(llvm::StringRef Name,
2022
const ClangTidyOptions::CustomCheckValue &V,
2123
ClangTidyContext *Context)
22-
: ClangTidyCheck(Name, Context), Matchers{} {
24+
: ClangTidyCheck(Name, Context) {
25+
for (const ClangTidyOptions::CustomCheckDiag &D : V.Diags) {
26+
auto It = Diags.try_emplace(D.BindName, llvm::SmallVector<Diag>{}).first;
27+
It->second.emplace_back(
28+
Diag{D.Message, D.Level.value_or(DiagnosticIDs::Warning)});
29+
}
30+
2331
clang::query::QuerySession QS({});
2432
llvm::StringRef QueryStringRef{V.Query};
2533
while (!QueryStringRef.empty()) {
@@ -64,7 +72,9 @@ void QueryCheck::registerMatchers(MatchFinder *Finder) {
6472

6573
void QueryCheck::check(const MatchFinder::MatchResult &Result) {
6674
for (auto &[Name, Node] : Result.Nodes.getMap())
67-
diag(Node.getSourceRange().getBegin(), Name) << Node.getSourceRange();
75+
if (Diags.contains(Name))
76+
for (const Diag &D : Diags[Name])
77+
diag(D.Message, D.Level) << Node.getSourceRange();
6878
}
6979

7080
} // namespace clang::tidy::custom

clang-tools-extra/clang-tidy/custom/QueryCheck.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "../ClangTidyCheck.h"
1313
#include "clang/ASTMatchers/Dynamic/VariantValue.h"
1414
#include "llvm/ADT/SmallVector.h"
15+
#include "llvm/ADT/StringMap.h"
1516

1617
namespace clang::tidy::custom {
1718

@@ -28,6 +29,11 @@ class QueryCheck : public ClangTidyCheck {
2829

2930
private:
3031
llvm::SmallVector<ast_matchers::dynamic::DynTypedMatcher> Matchers{};
32+
struct Diag {
33+
std::string Message;
34+
DiagnosticIDs::Level Level;
35+
};
36+
llvm::StringMap<llvm::SmallVector<Diag>> Diags{};
3137
};
3238

3339
} // namespace clang::tidy::custom

0 commit comments

Comments
 (0)