[clang-tools-extra] [clang-tidy] Add new check bugprone-tagged-union-member-count (PR #89925)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Aug 30 07:08:15 PDT 2024
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?= <tigbrcode at protonmail.com>,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?= <tigbrcode at protonmail.com>,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?= <tigbrcode at protonmail.com>,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?= <tigbrcode at protonmail.com>,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?= <tigbrcode at protonmail.com>,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?= <tigbrcode at protonmail.com>,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?= <tigbrcode at protonmail.com>,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?= <tigbrcode at protonmail.com>,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?= <tigbrcode at protonmail.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/89925 at github.com>
================
@@ -0,0 +1,214 @@
+//===--- TaggedUnionMemberCountCheck.cpp - clang-tidy ---------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "TaggedUnionMemberCountCheck.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallSet.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+const char StrictModeOptionName[] = "StrictMode";
+const char EnableCountingEnumHeuristicOptionName[] =
+ "EnableCountingEnumHeuristic";
+const char CountingEnumPrefixesOptionName[] = "CountingEnumPrefixes";
+const char CountingEnumSuffixesOptionName[] = "CountingEnumSuffixes";
+
+TaggedUnionMemberCountCheck::TaggedUnionMemberCountCheck(
+ StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context),
+ StrictMode(Options.get(StrictModeOptionName, false)),
+ EnableCountingEnumHeuristic(
+ Options.get(EnableCountingEnumHeuristicOptionName, true)),
+ RawCountingEnumPrefixes(Options.get(CountingEnumPrefixesOptionName, "")),
+ RawCountingEnumSuffixes(
+ Options.get(CountingEnumSuffixesOptionName, "count")),
+ ParsedCountingEnumPrefixes(
+ utils::options::parseStringList(RawCountingEnumPrefixes)),
+ ParsedCountingEnumSuffixes(
+ utils::options::parseStringList(RawCountingEnumSuffixes)),
+ CountingEnumPrefixesSet(
+ Options.get(CountingEnumPrefixesOptionName).has_value()),
+ CountingEnumSuffixesSet(
+ Options.get(CountingEnumSuffixesOptionName).has_value()),
+ CountingEnumConstantDecl(nullptr) {
+ if (!EnableCountingEnumHeuristic) {
+ if (CountingEnumPrefixesSet)
+ configurationDiag("%0: Counting enum heuristic is disabled but "
+ "CountingEnumPrefixes is set")
+ << Name;
+ if (CountingEnumSuffixesSet)
+ configurationDiag("%0: Counting enum heuristic is disabled but "
+ "CountingEnumSuffixes is set")
+ << Name;
+ }
+}
+
+void TaggedUnionMemberCountCheck::storeOptions(
+ ClangTidyOptions::OptionMap &Opts) {
+ Options.store(Opts, StrictModeOptionName, StrictMode);
+ Options.store(Opts, EnableCountingEnumHeuristicOptionName,
+ EnableCountingEnumHeuristic);
+ Options.store(Opts, CountingEnumPrefixesOptionName, RawCountingEnumPrefixes);
+ Options.store(Opts, CountingEnumSuffixesOptionName, RawCountingEnumSuffixes);
+}
+
+void TaggedUnionMemberCountCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(
+ recordDecl(
+ anyOf(isStruct(), isClass()),
+ has(fieldDecl(hasType(qualType(hasCanonicalType(recordType()))))
+ .bind("union")),
+ has(fieldDecl(hasType(qualType(hasCanonicalType(enumType()))))
+ .bind("tags")))
+ .bind("root"),
+ this);
+}
+
+static bool isUnion(const FieldDecl *R) {
+ return R->getType().getCanonicalType().getTypePtr()->isUnionType();
+}
+
+static bool isEnum(const FieldDecl *R) {
+ return R->getType().getCanonicalType().getTypePtr()->isEnumeralType();
+}
+
+static bool hasMultipleUnionsOrEnums(const RecordDecl *Rec) {
+ return llvm::count_if(Rec->fields(), isUnion) > 1 ||
+ llvm::count_if(Rec->fields(), isEnum) > 1;
+}
+
+static bool signEquals(const llvm::APSInt &A, const llvm::APSInt &B) {
+ return (A.isNegative() && B.isNegative()) ||
+ (A.isStrictlyPositive() && B.isStrictlyPositive()) ||
+ (A.isZero() && B.isZero());
+}
+
+static bool greaterBySign(const llvm::APSInt &A, const llvm::APSInt &B) {
+ return (A.isNonNegative() && B.isNegative()) ||
+ (A.isStrictlyPositive() && B.isNonPositive());
+}
+
+bool TaggedUnionMemberCountCheck::isCountingEnumLikeName(
+ StringRef Name) const noexcept {
+ if (llvm::any_of(ParsedCountingEnumPrefixes,
+ [&Name](const StringRef &Prefix) -> bool {
+ return Name.starts_with_insensitive(Prefix);
+ }))
+ return true;
+ if (llvm::any_of(ParsedCountingEnumSuffixes,
+ [&Name](const StringRef &Suffix) -> bool {
+ return Name.ends_with_insensitive(Suffix);
+ }))
+ return true;
+ return false;
+}
+
+size_t TaggedUnionMemberCountCheck::getNumberOfValidEnumValues(
+ const EnumDecl *Ed) noexcept {
+ bool FoundMax = false;
+ llvm::APSInt MaxTagValue;
+ llvm::SmallSet<llvm::APSInt, 32> EnumValues;
----------------
tigbr wrote:
I tested the check on several open source projects and the number of enum values in the warnings were `[3, 3, 4, 4, 6, 8, 9, 10, 11, 32, 33, 46]`. The average of these is about 14, so maybe the value of 16 covers most cases without extra allocation.
https://github.com/llvm/llvm-project/pull/89925
More information about the cfe-commits
mailing list