10#include "../utils/OptionsUtils.h"
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)) {
62 Builder->setBinding(BindName, clang::DynTypedNode::create(*FirstMatch));
75 EnableCountingEnumHeuristic(
78 CountingEnumPrefixes(utils::options::parseStringList(
81 CountingEnumSuffixes(utils::options::parseStringList(
84 if (!EnableCountingEnumHeuristic) {
100 EnableCountingEnumHeuristic);
109 auto UnionField = fieldDecl(hasType(qualType(
110 hasCanonicalType(recordType(hasDeclaration(recordDecl(isUnion())))))));
112 auto EnumField = fieldDecl(hasType(
113 qualType(hasCanonicalType(enumType(hasDeclaration(enumDecl()))))));
118 Finder->addMatcher(recordDecl(anyOf(isStruct(), isClass()), hasOneUnionField,
119 hasOneEnumField, unless(isImplicit()))
124bool TaggedUnionMemberCountCheck::isCountingEnumLikeName(StringRef
Name)
const {
125 if (llvm::any_of(CountingEnumPrefixes, [
Name](StringRef Prefix) ->
bool {
126 return Name.starts_with_insensitive(Prefix);
129 if (llvm::any_of(CountingEnumSuffixes, [
Name](StringRef
Suffix) ->
bool {
136std::pair<const std::size_t, const EnumConstantDecl *>
137TaggedUnionMemberCountCheck::getNumberOfEnumValues(
const EnumDecl *ED) {
138 llvm::SmallSet<llvm::APSInt, 16> EnumValues;
140 const EnumConstantDecl *LastEnumConstant =
nullptr;
141 for (
const EnumConstantDecl *Enumerator : ED->enumerators()) {
142 EnumValues.insert(Enumerator->getInitVal());
143 LastEnumConstant = Enumerator;
146 if (EnableCountingEnumHeuristic && LastEnumConstant &&
147 isCountingEnumLikeName(LastEnumConstant->getName()) &&
148 (LastEnumConstant->getInitVal() == (EnumValues.size() - 1))) {
149 return {EnumValues.size() - 1, LastEnumConstant};
152 return {EnumValues.size(),
nullptr};
156 const MatchFinder::MatchResult &Result) {
158 const auto *UnionField =
162 assert(
Root &&
"Root is missing!");
163 assert(UnionField &&
"UnionField is missing!");
164 assert(TagField &&
"TagField is missing!");
165 if (!
Root || !UnionField || !TagField)
168 const auto *UnionDef =
169 UnionField->getType().getCanonicalType().getTypePtr()->getAsRecordDecl();
170 const auto *EnumDef = llvm::dyn_cast<EnumDecl>(
171 TagField->getType().getCanonicalType().getTypePtr()->getAsTagDecl());
173 assert(UnionDef &&
"UnionDef is missing!");
174 assert(EnumDef &&
"EnumDef is missing!");
175 if (!UnionDef || !EnumDef)
178 const std::size_t UnionMemberCount = llvm::range_size(UnionDef->fields());
179 auto [TagCount, CountingEnumConstantDecl] = getNumberOfEnumValues(EnumDef);
181 if (UnionMemberCount > TagCount) {
183 "tagged union has more data members (%0) than tags (%1)!")
184 << UnionMemberCount << TagCount;
185 }
else if (StrictMode && UnionMemberCount < TagCount) {
187 "tagged union has fewer data members (%0) than tags (%1)!")
188 << UnionMemberCount << TagCount;
191 if (CountingEnumConstantDecl) {
192 diag(CountingEnumConstantDecl->getLocation(),
193 "assuming that this constant is just an auxiliary value and not "
194 "used for indicating a valid union data member",
195 DiagnosticIDs::Note);
llvm::SmallString< 256U > Name
CodeCompletionBuilder Builder
::clang::DynTypedNode Node
std::optional< StringRef > get(StringRef LocalName) const
Read a named option from the Context.
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
DiagnosticBuilder configurationDiag(StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning) const
Adds a diagnostic to report errors in the check's configuration.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
TaggedUnionMemberCountCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
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