|
| 1 | +//===--- InvalidEnumDefaultInitializationCheck.cpp - clang-tidy -----------===// |
| 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 | + |
| 9 | +#include "InvalidEnumDefaultInitializationCheck.h" |
| 10 | +#include "clang/AST/ASTContext.h" |
| 11 | +#include "clang/AST/TypeVisitor.h" |
| 12 | +#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 13 | +#include <algorithm> |
| 14 | + |
| 15 | +using namespace clang::ast_matchers; |
| 16 | + |
| 17 | +namespace clang::tidy::bugprone { |
| 18 | + |
| 19 | +namespace { |
| 20 | + |
| 21 | +bool isCompleteAndHasNoZeroValue(const EnumDecl *D) { |
| 22 | + const EnumDecl *Definition = D->getDefinition(); |
| 23 | + return Definition && Definition->isComplete() && |
| 24 | + !Definition->enumerators().empty() && |
| 25 | + std::none_of(Definition->enumerator_begin(), |
| 26 | + Definition->enumerator_end(), |
| 27 | + [](const EnumConstantDecl *Value) { |
| 28 | + return Value->getInitVal().isZero(); |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +AST_MATCHER(EnumDecl, isCompleteAndHasNoZeroValue) { |
| 33 | + return isCompleteAndHasNoZeroValue(&Node); |
| 34 | +} |
| 35 | + |
| 36 | +// Find an initialization which initializes the value (if it has enum type) to a |
| 37 | +// default zero value. |
| 38 | +AST_MATCHER(Expr, isEmptyInit) { |
| 39 | + if (isa<CXXScalarValueInitExpr, ImplicitValueInitExpr>(&Node)) |
| 40 | + return true; |
| 41 | + if (const auto *Init = dyn_cast<InitListExpr>(&Node)) { |
| 42 | + if (Init->getNumInits() == 0) |
| 43 | + return true; |
| 44 | + } |
| 45 | + return false; |
| 46 | +} |
| 47 | + |
| 48 | +AST_MATCHER(InitListExpr, hasArrayFiller) { return Node.hasArrayFiller(); } |
| 49 | + |
| 50 | +// Check if any type has a "child" type that is an enum without zero value. |
| 51 | +// The "child" type can be an array element type or member type of a record |
| 52 | +// type (or a recursive combination of these). In this case, if the "root" type |
| 53 | +// is statically initialized, the enum component is initialized to zero. |
| 54 | +class FindEnumMember : public TypeVisitor<FindEnumMember, bool> { |
| 55 | +public: |
| 56 | + const EnumType *FoundEnum = nullptr; |
| 57 | + |
| 58 | + bool VisitType(const Type *T) { |
| 59 | + const Type *DesT = T->getUnqualifiedDesugaredType(); |
| 60 | + if (DesT != T) |
| 61 | + return Visit(DesT); |
| 62 | + return false; |
| 63 | + } |
| 64 | + bool VisitArrayType(const ArrayType *T) { |
| 65 | + return Visit(T->getElementType().getTypePtr()); |
| 66 | + } |
| 67 | + bool VisitConstantArrayType(const ConstantArrayType *T) { |
| 68 | + return Visit(T->getElementType().getTypePtr()); |
| 69 | + } |
| 70 | + bool VisitEnumType(const EnumType *T) { |
| 71 | + if (isCompleteAndHasNoZeroValue(T->getDecl())) { |
| 72 | + FoundEnum = T; |
| 73 | + return true; |
| 74 | + } |
| 75 | + return false; |
| 76 | + } |
| 77 | + bool VisitRecordType(const RecordType *T) { |
| 78 | + const RecordDecl *RD = T->getDecl(); |
| 79 | + if (RD->isUnion()) |
| 80 | + return false; |
| 81 | + auto VisitField = [this](const FieldDecl *F) { |
| 82 | + return Visit(F->getType().getTypePtr()); |
| 83 | + }; |
| 84 | + return llvm::any_of(RD->fields(), VisitField); |
| 85 | + } |
| 86 | +}; |
| 87 | + |
| 88 | +} // namespace |
| 89 | + |
| 90 | +InvalidEnumDefaultInitializationCheck::InvalidEnumDefaultInitializationCheck( |
| 91 | + StringRef Name, ClangTidyContext *Context) |
| 92 | + : ClangTidyCheck(Name, Context) {} |
| 93 | + |
| 94 | +void InvalidEnumDefaultInitializationCheck::registerMatchers( |
| 95 | + MatchFinder *Finder) { |
| 96 | + auto EnumWithoutZeroValue = enumType( |
| 97 | + hasDeclaration(enumDecl(isCompleteAndHasNoZeroValue()).bind("enum"))); |
| 98 | + auto EnumOrArrayOfEnum = qualType(hasUnqualifiedDesugaredType( |
| 99 | + anyOf(EnumWithoutZeroValue, |
| 100 | + arrayType(hasElementType(qualType( |
| 101 | + hasUnqualifiedDesugaredType(EnumWithoutZeroValue))))))); |
| 102 | + Finder->addMatcher( |
| 103 | + expr(isEmptyInit(), hasType(EnumOrArrayOfEnum)).bind("expr"), this); |
| 104 | + |
| 105 | + // Array initialization can contain an "array filler" for the (syntactically) |
| 106 | + // unspecified elements. This expression is not found by AST matchers and can |
| 107 | + // have any type (the array's element type). This is an implicitly generated |
| 108 | + // initialization, so if the type contains somewhere an enum without zero |
| 109 | + // enumerator, the zero initialization applies here. We search this array |
| 110 | + // element type for the specific enum type manually when this matcher matches. |
| 111 | + Finder->addMatcher(initListExpr(hasArrayFiller()).bind("array_filler_expr"), |
| 112 | + this); |
| 113 | +} |
| 114 | + |
| 115 | +void InvalidEnumDefaultInitializationCheck::check( |
| 116 | + const MatchFinder::MatchResult &Result) { |
| 117 | + const auto *InitExpr = Result.Nodes.getNodeAs<Expr>("expr"); |
| 118 | + const auto *Enum = Result.Nodes.getNodeAs<EnumDecl>("enum"); |
| 119 | + if (!InitExpr) { |
| 120 | + const auto *InitList = |
| 121 | + Result.Nodes.getNodeAs<InitListExpr>("array_filler_expr"); |
| 122 | + // Initialization of omitted array elements with array filler was found. |
| 123 | + // Check the type for enum without zero value. |
| 124 | + // FIXME: In this way only one enum-typed value is found, not all of these. |
| 125 | + FindEnumMember Finder; |
| 126 | + if (!Finder.Visit(InitList->getArrayFiller()->getType().getTypePtr())) |
| 127 | + return; |
| 128 | + InitExpr = InitList; |
| 129 | + Enum = Finder.FoundEnum->getDecl(); |
| 130 | + } |
| 131 | + |
| 132 | + if (!InitExpr || !Enum) |
| 133 | + return; |
| 134 | + |
| 135 | + ASTContext &ACtx = Enum->getASTContext(); |
| 136 | + SourceLocation Loc = InitExpr->getExprLoc(); |
| 137 | + if (Loc.isInvalid()) { |
| 138 | + if (isa<ImplicitValueInitExpr, InitListExpr>(InitExpr)) { |
| 139 | + DynTypedNodeList Parents = ACtx.getParents(*InitExpr); |
| 140 | + if (Parents.empty()) |
| 141 | + return; |
| 142 | + |
| 143 | + if (const auto *Ctor = Parents[0].get<CXXConstructorDecl>()) { |
| 144 | + // Try to find member initializer with the found expression and get the |
| 145 | + // source ___location from it. |
| 146 | + CXXCtorInitializer *const *CtorInit = std::find_if( |
| 147 | + Ctor->init_begin(), Ctor->init_end(), |
| 148 | + [InitExpr](const CXXCtorInitializer *Init) { |
| 149 | + return Init->isMemberInitializer() && Init->getInit() == InitExpr; |
| 150 | + }); |
| 151 | + if (!CtorInit) |
| 152 | + return; |
| 153 | + Loc = (*CtorInit)->getLParenLoc(); |
| 154 | + } else if (const auto *InitList = Parents[0].get<InitListExpr>()) { |
| 155 | + // The expression may be implicitly generated for an initialization. |
| 156 | + // Search for a parent initialization list with valid source ___location. |
| 157 | + while (InitList->getExprLoc().isInvalid()) { |
| 158 | + DynTypedNodeList Parents = ACtx.getParents(*InitList); |
| 159 | + if (Parents.empty()) |
| 160 | + return; |
| 161 | + InitList = Parents[0].get<InitListExpr>(); |
| 162 | + if (!InitList) |
| 163 | + return; |
| 164 | + } |
| 165 | + Loc = InitList->getExprLoc(); |
| 166 | + } |
| 167 | + } |
| 168 | + // If still not found a source ___location, omit the warning. |
| 169 | + // Ideally all such cases (if they exist) should be handled to make the |
| 170 | + // check more precise. |
| 171 | + if (Loc.isInvalid()) |
| 172 | + return; |
| 173 | + } |
| 174 | + diag(Loc, "enum value of type %0 initialized with invalid value of 0, " |
| 175 | + "enum doesn't have a zero-value enumerator") |
| 176 | + << Enum; |
| 177 | + diag(Enum->getLocation(), "enum is defined here", DiagnosticIDs::Note); |
| 178 | +} |
| 179 | + |
| 180 | +} // namespace clang::tidy::bugprone |
0 commit comments