11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "llvm/ADT/STLExtras.h"
13#include "llvm/ADT/SmallSet.h"
21 "EnableCountingEnumHeuristic";
23 "CountingEnumPrefixes";
25 "CountingEnumSuffixes";
40AST_MATCHER_P2(RecordDecl, fieldCountOfKindIsOne,
41 ast_matchers::internal::Matcher<FieldDecl>, InnerMatcher,
42 StringRef, BindName) {
48 clang::ast_matchers::internal::BoundNodesTreeBuilder TempBuilder;
50 const FieldDecl *FirstMatch =
nullptr;
51 for (
const FieldDecl *Field : Node.fields()) {
52 if (InnerMatcher.matches(*Field, Finder, &TempBuilder)) {
61 Builder->setBinding(BindName, clang::DynTypedNode::create(*FirstMatch));
74 EnableCountingEnumHeuristic(
77 CountingEnumPrefixes(
utils::options::parseStringList(
80 CountingEnumSuffixes(
utils::options::parseStringList(
83 if (!EnableCountingEnumHeuristic) {
85 configurationDiag(
"%0: Counting enum heuristic is disabled but "
89 configurationDiag(
"%0: Counting enum heuristic is disabled but "
99 EnableCountingEnumHeuristic);
108 auto NotFromSystemHeaderOrStdNamespace =
109 unless(anyOf(isExpansionInSystemHeader(), isInStdNamespace()));
112 fieldDecl(hasType(qualType(hasCanonicalType(recordType(hasDeclaration(
113 recordDecl(isUnion(), NotFromSystemHeaderOrStdNamespace)))))));
115 auto EnumField = fieldDecl(hasType(qualType(hasCanonicalType(
116 enumType(hasDeclaration(enumDecl(NotFromSystemHeaderOrStdNamespace)))))));
121 Finder->addMatcher(recordDecl(anyOf(isStruct(), isClass()), HasOneUnionField,
122 HasOneEnumField, unless(isImplicit()))
127bool TaggedUnionMemberCountCheck::isCountingEnumLikeName(StringRef Name)
const {
128 if (llvm::any_of(CountingEnumPrefixes, [Name](StringRef Prefix) ->
bool {
129 return Name.starts_with_insensitive(Prefix);
132 if (llvm::any_of(CountingEnumSuffixes, [Name](StringRef Suffix) ->
bool {
133 return Name.ends_with_insensitive(Suffix);
139std::pair<const std::size_t, const EnumConstantDecl *>
140TaggedUnionMemberCountCheck::getNumberOfEnumValues(
const EnumDecl *ED) {
141 llvm::SmallSet<llvm::APSInt, 16> EnumValues;
143 const EnumConstantDecl *LastEnumConstant =
nullptr;
144 for (
const EnumConstantDecl *Enumerator : ED->enumerators()) {
145 EnumValues.insert(Enumerator->getInitVal());
146 LastEnumConstant = Enumerator;
149 if (EnableCountingEnumHeuristic && LastEnumConstant &&
150 isCountingEnumLikeName(LastEnumConstant->getName()) &&
151 llvm::APSInt::isSameValue(LastEnumConstant->getInitVal(),
152 llvm::APSInt::get(EnumValues.size() - 1))) {
153 return {EnumValues.size() - 1, LastEnumConstant};
156 return {EnumValues.size(),
nullptr};
160 const MatchFinder::MatchResult &Result) {
162 const auto *UnionField =
166 assert(Root &&
"Root is missing!");
167 assert(UnionField &&
"UnionField is missing!");
168 assert(TagField &&
"TagField is missing!");
169 if (!Root || !UnionField || !TagField)
172 const auto *UnionDef = UnionField->getType()->castAsRecordDecl();
173 const auto *EnumDef = TagField->getType()->castAsEnumDecl();
175 const std::size_t UnionMemberCount = llvm::range_size(UnionDef->fields());
176 auto [TagCount, CountingEnumConstantDecl] = getNumberOfEnumValues(EnumDef);
178 if (UnionMemberCount > TagCount) {
179 diag(Root->getLocation(),
180 "tagged union has more data members (%0) than tags (%1)!")
181 << UnionMemberCount << TagCount;
182 }
else if (StrictMode && UnionMemberCount < TagCount) {
183 diag(Root->getLocation(),
184 "tagged union has fewer data members (%0) than tags (%1)!")
185 << UnionMemberCount << TagCount;
188 if (CountingEnumConstantDecl) {
189 diag(CountingEnumConstantDecl->getLocation(),
190 "assuming that this constant is just an auxiliary value and not "
191 "used for indicating a valid union data member",
192 DiagnosticIDs::Note);
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
TaggedUnionMemberCountCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
static constexpr llvm::StringLiteral CountingEnumSuffixesOptionDefaultValue
static constexpr llvm::StringLiteral CountingEnumSuffixesOptionName
static constexpr bool EnableCountingEnumHeuristicOptionDefaultValue
static constexpr llvm::StringLiteral RootMatchBindName
static constexpr llvm::StringLiteral StrictModeOptionName
static constexpr llvm::StringLiteral EnableCountingEnumHeuristicOptionName
static constexpr bool StrictModeOptionDefaultValue
static constexpr llvm::StringLiteral UnionMatchBindName
static constexpr llvm::StringLiteral TagMatchBindName
static constexpr llvm::StringLiteral CountingEnumPrefixesOptionName
static constexpr llvm::StringLiteral CountingEnumPrefixesOptionDefaultValue
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
llvm::StringMap< ClangTidyValue > OptionMap