[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 Apr 24 14:51:26 PDT 2024
================
@@ -0,0 +1,125 @@
+//===--- 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 "clang/AST/ASTContext.h"
+#include "clang/AST/Expr.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+#include "clang/AST/PrettyPrinter.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/Casting.h"
+#include <limits>
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+void TaggedUnionMemberCountCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(
+ recordDecl(
+ allOf(isStruct(),
+ has(fieldDecl(hasType(recordDecl(isUnion()).bind("union")))),
+ has(fieldDecl(hasType(enumDecl().bind("tags"))))))
+ .bind("root"),
+ this);
----------------
isuckatcs wrote:
This only matches the following form, right?
```c++
struct {
<enum> e;
<union> u;
};
```
What happens if an anonymous union is used, as it can be seen in the [wikipedia article](https://en.wikipedia.org/wiki/Tagged_union#1970s_&_1980s) for tagged union?
```c++
enum Tags { INT, FLOAT };
struct TU {
Tags tags;
union {
int i;
float f;
};
};
```
Also `isStruct()` won't match `class`. Is it intentional?
I think the checker would benefit from more matchers, or a more generalized one, so that the above cases are detected too.
https://github.com/llvm/llvm-project/pull/89925
More information about the cfe-commits
mailing list