[clang-tools-extra] [clang-tidy] Add new check bugprone-tagged-union-member-count (PR #89925)

via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 7 07:00:27 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>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/89925 at github.com>


================
@@ -16,39 +16,74 @@ using namespace clang::ast_matchers;
 
 namespace clang::tidy::bugprone {
 
-const char StrictModeOptionName[] = "StrictMode";
-const char EnableCountingEnumHeuristicOptionName[] =
+static constexpr llvm::StringLiteral StrictModeOptionName = "StrictMode";
+static constexpr llvm::StringLiteral EnableCountingEnumHeuristicOptionName =
     "EnableCountingEnumHeuristic";
-const char CountingEnumPrefixesOptionName[] = "CountingEnumPrefixes";
-const char CountingEnumSuffixesOptionName[] = "CountingEnumSuffixes";
+static constexpr llvm::StringLiteral CountingEnumPrefixesOptionName =
+    "CountingEnumPrefixes";
+static constexpr llvm::StringLiteral CountingEnumSuffixesOptionName =
+    "CountingEnumSuffixes";
+
+static constexpr bool StrictModeOptionDefaultValue = false;
+static constexpr bool EnableCountingEnumHeuristicOptionDefaultValue = true;
+static constexpr llvm::StringLiteral CountingEnumPrefixesOptionDefaultValue =
+    "";
+static constexpr llvm::StringLiteral CountingEnumSuffixesOptionDefaultValue =
+    "count";
+
+static constexpr llvm::StringLiteral RootMatchBindName = "root";
+static constexpr llvm::StringLiteral UnionMatchBindName = "union";
+static constexpr llvm::StringLiteral TagMatchBindName = "tags";
+
+namespace {
+AST_MATCHER(FieldDecl, isUnion) {
+  const Type *T = Node.getType().getCanonicalType().getTypePtr();
+  assert(T);
+  return T->isUnionType();
+}
+
+AST_MATCHER(FieldDecl, isEnum) {
+  const Type *T = Node.getType().getCanonicalType().getTypePtr();
+  assert(T);
+  return T->isEnumeralType();
+}
+
+AST_MATCHER_P2(RecordDecl, fieldCountOfKindIsGT,
+               ast_matchers::internal::Matcher<FieldDecl>, InnerMatcher,
+               unsigned, N) {
+  unsigned matchCount = 0;
+  for (const auto field : Node.fields()) {
+    if (InnerMatcher.matches(*field, Finder, Builder)) {
+      matchCount += 1;
+    }
+  }
+  return matchCount > N;
+}
+}; // namespace
 
 TaggedUnionMemberCountCheck::TaggedUnionMemberCountCheck(
     StringRef Name, ClangTidyContext *Context)
     : ClangTidyCheck(Name, Context),
-      StrictMode(Options.get(StrictModeOptionName, false)),
+      StrictMode(
+          Options.get(StrictModeOptionName, StrictModeOptionDefaultValue)),
       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) {
+          Options.get(EnableCountingEnumHeuristicOptionName,
+                      EnableCountingEnumHeuristicOptionDefaultValue)),
+      CountingEnumPrefixes(utils::options::parseStringList(
+          Options.get(CountingEnumPrefixesOptionName,
+                      CountingEnumPrefixesOptionDefaultValue))),
+      CountingEnumSuffixes(utils::options::parseStringList(
+          Options.get(CountingEnumSuffixesOptionName,
+                      CountingEnumSuffixesOptionDefaultValue))) {
   if (!EnableCountingEnumHeuristic) {
-    if (CountingEnumPrefixesSet)
+    if (Options.get(CountingEnumPrefixesOptionName).has_value())
----------------
isuckatcs wrote:

```suggestion
    if (Options.get(CountingEnumPrefixesOptionName))
```
`has_value()` is equivalent to `operator bool()`

https://github.com/llvm/llvm-project/pull/89925


More information about the cfe-commits mailing list