=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?= <tigbrc...@protonmail.com>,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?= <tigbrc...@protonmail.com>,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?= <tigbrc...@protonmail.com>,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?= <tigbrc...@protonmail.com>,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?= <tigbrc...@protonmail.com>,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?= <tigbrc...@protonmail.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/89...@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()))))
----------------
whisperity wrote:

```suggestion       
has(fieldDecl(hasType(qualType(hasCanonicalType(recordType(hasDeclaration(recordDecl(isUnion())))))))
```

`recordType()` alone will likely also match other record/tag members such as 
structs or classes.

https://github.com/llvm/llvm-project/pull/89925
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to