[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:25 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>
================
@@ -153,61 +178,57 @@ size_t TaggedUnionMemberCountCheck::getNumberOfValidEnumValues(
CeIsLast = true;
CeValue = Val;
CeCount += 1;
- CountingEnumConstantDecl = Enumerator;
+ OutCountingEnumConstantDecl = Enumerator;
} else {
CeIsLast = false;
}
}
}
- size_t ValidValuesCount = EnumValues.size();
+ std::size_t ValidValuesCount = EnumValues.size();
if (CeCount == 1 && CeIsLast && CeValue == MaxTagValue) {
ValidValuesCount -= 1;
} else {
- CountingEnumConstantDecl = nullptr;
+ OutCountingEnumConstantDecl = nullptr;
}
return ValidValuesCount;
}
void TaggedUnionMemberCountCheck::check(
const MatchFinder::MatchResult &Result) {
- const auto *Root = Result.Nodes.getNodeAs<RecordDecl>("root");
- const auto *UnionField = Result.Nodes.getNodeAs<FieldDecl>("union");
- const auto *TagField = Result.Nodes.getNodeAs<FieldDecl>("tags");
-
- // The matcher can only narrow down the type to recordType()
- if (!isUnion(UnionField))
- return;
-
- if (hasMultipleUnionsOrEnums(Root))
- return;
+ const auto *Root = Result.Nodes.getNodeAs<RecordDecl>(RootMatchBindName);
+ const auto *UnionField =
+ Result.Nodes.getNodeAs<FieldDecl>(UnionMatchBindName);
+ const auto *TagField = Result.Nodes.getNodeAs<FieldDecl>(TagMatchBindName);
+ assert(Root && UnionField && TagField);
----------------
isuckatcs wrote:
How about adding a return if one of these is missing, so we don't crash in release mode? Also it's a good idea to separate the assertions, so that we know which of the 3 conditions fails on a crash.
https://github.com/llvm/llvm-project/pull/89925
More information about the cfe-commits
mailing list