clang-tools 22.0.0git
InvalidEnumDefaultInitializationCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "../utils/Matchers.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/AST/TypeVisitor.h"
14#include "clang/ASTMatchers/ASTMatchFinder.h"
15#include <algorithm>
16
17using namespace clang::ast_matchers;
18
19namespace clang::tidy::bugprone {
20
21namespace {
22
23bool isCompleteAndHasNoZeroValue(const EnumDecl *D) {
24 const EnumDecl *Definition = D->getDefinition();
25 return Definition && Definition->isComplete() &&
26 !Definition->enumerators().empty() &&
27 llvm::none_of(Definition->enumerators(),
28 [](const EnumConstantDecl *Value) {
29 return Value->getInitVal().isZero();
30 });
31}
32
33AST_MATCHER(EnumDecl, isCompleteAndHasNoZeroValue) {
34 return isCompleteAndHasNoZeroValue(&Node);
35}
36
37// Find an initialization which initializes the value (if it has enum type) to a
38// default zero value.
39AST_MATCHER(Expr, isEmptyInit) {
40 if (isa<CXXScalarValueInitExpr, ImplicitValueInitExpr>(&Node))
41 return true;
42 if (const auto *Init = dyn_cast<InitListExpr>(&Node)) {
43 if (Init->getNumInits() == 0)
44 return true;
45 }
46 return false;
47}
48
49AST_MATCHER(InitListExpr, hasArrayFiller) { return Node.hasArrayFiller(); }
50
51// Check if any type has a "child" type that is an enum without zero value.
52// The "child" type can be an array element type or member type of a record
53// type (or a recursive combination of these). In this case, if the "root" type
54// is statically initialized, the enum component is initialized to zero.
55class FindEnumMember : public TypeVisitor<FindEnumMember, bool> {
56public:
57 const EnumType *FoundEnum = nullptr;
58
59 bool VisitType(const Type *T) {
60 const Type *DesT = T->getUnqualifiedDesugaredType();
61 if (DesT != T)
62 return Visit(DesT);
63 return false;
64 }
65 bool VisitArrayType(const ArrayType *T) {
66 return Visit(T->getElementType().getTypePtr());
67 }
68 bool VisitConstantArrayType(const ConstantArrayType *T) {
69 return Visit(T->getElementType().getTypePtr());
70 }
71 bool VisitEnumType(const EnumType *T) {
72 if (isCompleteAndHasNoZeroValue(T->getOriginalDecl())) {
73 FoundEnum = T;
74 return true;
75 }
76 return false;
77 }
78 bool VisitRecordType(const RecordType *T) {
79 const RecordDecl *RD = T->getOriginalDecl()->getDefinition();
80 if (!RD || RD->isUnion())
81 return false;
82 auto VisitField = [this](const FieldDecl *F) {
83 return Visit(F->getType().getTypePtr());
84 };
85 return llvm::any_of(RD->fields(), VisitField);
86 }
87};
88
89} // namespace
90
92 StringRef Name, ClangTidyContext *Context)
93 : ClangTidyCheck(Name, Context),
94 IgnoredEnums(
95 utils::options::parseStringList(Options.get("IgnoredEnums", ""))) {
96 IgnoredEnums.emplace_back("::std::errc");
97}
98
101 Options.store(Opts, "IgnoredEnums",
103}
104
106 MatchFinder *Finder) {
107 auto EnumWithoutZeroValue = enumType(hasDeclaration(
108 enumDecl(isCompleteAndHasNoZeroValue(),
109 unless(matchers::matchesAnyListedName(IgnoredEnums)))
110 .bind("enum")));
111 auto EnumOrArrayOfEnum = qualType(hasUnqualifiedDesugaredType(
112 anyOf(EnumWithoutZeroValue,
113 arrayType(hasElementType(qualType(
114 hasUnqualifiedDesugaredType(EnumWithoutZeroValue)))))));
115 Finder->addMatcher(
116 expr(isEmptyInit(), hasType(EnumOrArrayOfEnum)).bind("expr"), this);
117
118 // Array initialization can contain an "array filler" for the (syntactically)
119 // unspecified elements. This expression is not found by AST matchers and can
120 // have any type (the array's element type). This is an implicitly generated
121 // initialization, so if the type contains somewhere an enum without zero
122 // enumerator, the zero initialization applies here. We search this array
123 // element type for the specific enum type manually when this matcher matches.
124 Finder->addMatcher(initListExpr(hasArrayFiller()).bind("array_filler_expr"),
125 this);
126}
127
129 const MatchFinder::MatchResult &Result) {
130 const auto *InitExpr = Result.Nodes.getNodeAs<Expr>("expr");
131 const auto *Enum = Result.Nodes.getNodeAs<EnumDecl>("enum");
132 if (!InitExpr) {
133 const auto *InitList =
134 Result.Nodes.getNodeAs<InitListExpr>("array_filler_expr");
135 // Initialization of omitted array elements with array filler was found.
136 // Check the type for enum without zero value.
137 // FIXME: In this way only one enum-typed value is found, not all of these.
138 FindEnumMember Finder;
139 if (!Finder.Visit(InitList->getArrayFiller()->getType().getTypePtr()))
140 return;
141 InitExpr = InitList;
142 Enum = Finder.FoundEnum->getOriginalDecl();
143 }
144
145 if (!InitExpr || !Enum)
146 return;
147
148 ASTContext &ACtx = Enum->getASTContext();
149 SourceLocation Loc = InitExpr->getExprLoc();
150 if (Loc.isInvalid()) {
151 if (isa<ImplicitValueInitExpr, InitListExpr>(InitExpr)) {
152 DynTypedNodeList Parents = ACtx.getParents(*InitExpr);
153 if (Parents.empty())
154 return;
155
156 if (const auto *Ctor = Parents[0].get<CXXConstructorDecl>()) {
157 // Try to find member initializer with the found expression and get the
158 // source location from it.
159 CXXCtorInitializer *const *CtorInit = std::find_if(
160 Ctor->init_begin(), Ctor->init_end(),
161 [InitExpr](const CXXCtorInitializer *Init) {
162 return Init->isMemberInitializer() && Init->getInit() == InitExpr;
163 });
164 if (!CtorInit)
165 return;
166 Loc = (*CtorInit)->getLParenLoc();
167 } else if (const auto *InitList = Parents[0].get<InitListExpr>()) {
168 // The expression may be implicitly generated for an initialization.
169 // Search for a parent initialization list with valid source location.
170 while (InitList->getExprLoc().isInvalid()) {
171 DynTypedNodeList Parents = ACtx.getParents(*InitList);
172 if (Parents.empty())
173 return;
174 InitList = Parents[0].get<InitListExpr>();
175 if (!InitList)
176 return;
177 }
178 Loc = InitList->getExprLoc();
179 }
180 }
181 // If still not found a source location, omit the warning.
182 // Ideally all such cases (if they exist) should be handled to make the
183 // check more precise.
184 if (Loc.isInvalid())
185 return;
186 }
187 diag(Loc, "enum value of type %0 initialized with invalid value of 0, "
188 "enum doesn't have a zero-value enumerator")
189 << Enum;
190 diag(Enum->getLocation(), "enum is defined here", DiagnosticIDs::Note);
191}
192
193} // namespace clang::tidy::bugprone
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
@ Type
An inlay hint that for a type annotation.
Definition Protocol.h:1672
AST_MATCHER(BinaryOperator, isRelationalOperator)
inline ::clang::ast_matchers::internal::Matcher< NamedDecl > matchesAnyListedName(llvm::ArrayRef< StringRef > NameList)
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
llvm::StringMap< ClangTidyValue > OptionMap