clang-tools 10.0.0

SuspiciousEnumUsageCheck.cpp
Go to the documentation of this file.
1 //===--- SuspiciousEnumUsageCheck.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 "SuspiciousEnumUsageCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include <algorithm>
13
14 using namespace clang::ast_matchers;
15
16 namespace clang {
17 namespace tidy {
18 namespace bugprone {
19
20 static const char DifferentEnumErrorMessage[] =
21 "enum values are from different enum types";
22
23 static const char BitmaskErrorMessage[] =
24 "enum type seems like a bitmask (contains mostly "
25 "power-of-2 literals), but this literal is not a "
26 "power-of-2";
27
28 static const char BitmaskVarErrorMessage[] =
29 "enum type seems like a bitmask (contains mostly "
30 "power-of-2 literals) but %plural{1:a literal is|:some literals are}0 not "
31 "power-of-2";
32
33 static const char BitmaskNoteMessage[] = "used here as a bitmask";
34
35 /// Stores a min and a max value which describe an interval.
36 struct ValueRange {
37 llvm::APSInt MinVal;
38 llvm::APSInt MaxVal;
39
40 ValueRange(const EnumDecl *EnumDec) {
41 const auto MinMaxVal = std::minmax_element(
42 EnumDec->enumerator_begin(), EnumDec->enumerator_end(),
43 [](const EnumConstantDecl *E1, const EnumConstantDecl *E2) {
44 return llvm::APSInt::compareValues(E1->getInitVal(),
45 E2->getInitVal()) < 0;
46 });
47 MinVal = MinMaxVal.first->getInitVal();
48 MaxVal = MinMaxVal.second->getInitVal();
49 }
50 };
51
52 /// Return the number of EnumConstantDecls in an EnumDecl.
53 static int enumLength(const EnumDecl *EnumDec) {
54 return std::distance(EnumDec->enumerator_begin(), EnumDec->enumerator_end());
55 }
56
57 static bool hasDisjointValueRange(const EnumDecl *Enum1,
58 const EnumDecl *Enum2) {
59 ValueRange Range1(Enum1), Range2(Enum2);
60 return llvm::APSInt::compareValues(Range1.MaxVal, Range2.MinVal) < 0 ||
61 llvm::APSInt::compareValues(Range2.MaxVal, Range1.MinVal) < 0;
62 }
63
64 static bool isNonPowerOf2NorNullLiteral(const EnumConstantDecl *EnumConst) {
65 llvm::APSInt Val = EnumConst->getInitVal();
66 if (Val.isPowerOf2() || !Val.getBoolValue())
67 return false;
68 const Expr *InitExpr = EnumConst->getInitExpr();
69 if (!InitExpr)
70 return true;
71 return isa<IntegerLiteral>(InitExpr->IgnoreImpCasts());
72 }
73
74 static bool isMaxValAllBitSetLiteral(const EnumDecl *EnumDec) {
75 auto EnumConst = std::max_element(
76 EnumDec->enumerator_begin(), EnumDec->enumerator_end(),
77 [](const EnumConstantDecl *E1, const EnumConstantDecl *E2) {
78 return E1->getInitVal() < E2->getInitVal();
79 });
80
81 if (const Expr *InitExpr = EnumConst->getInitExpr()) {
82 return EnumConst->getInitVal().countTrailingOnes() ==
83 EnumConst->getInitVal().getActiveBits() &&
84 isa<IntegerLiteral>(InitExpr->IgnoreImpCasts());
85 }
86 return false;
87 }
88
89 static int countNonPowOfTwoLiteralNum(const EnumDecl *EnumDec) {
90 return std::count_if(
91 EnumDec->enumerator_begin(), EnumDec->enumerator_end(),
92 [](const EnumConstantDecl *E) { return isNonPowerOf2NorNullLiteral(E); });
93 }
94
95 /// Check if there is one or two enumerators that are not a power of 2 and are
96 /// initialized by a literal in the enum type, and that the enumeration contains
97 /// enough elements to reasonably act as a bitmask. Exclude the case where the
98 /// last enumerator is the sum of the lesser values (and initialized by a
99 /// literal) or when it could contain consecutive values.
100 static bool isPossiblyBitMask(const EnumDecl *EnumDec) {
101 ValueRange VR(EnumDec);
102 int EnumLen = enumLength(EnumDec);
103 int NonPowOfTwoCounter = countNonPowOfTwoLiteralNum(EnumDec);
104 return NonPowOfTwoCounter >= 1 && NonPowOfTwoCounter <= 2 &&
105 NonPowOfTwoCounter < EnumLen / 2 &&
106 (VR.MaxVal - VR.MinVal != EnumLen - 1) &&
107 !(NonPowOfTwoCounter == 1 && isMaxValAllBitSetLiteral(EnumDec));
108 }
109
110 SuspiciousEnumUsageCheck::SuspiciousEnumUsageCheck(StringRef Name,
111 ClangTidyContext *Context)
112 : ClangTidyCheck(Name, Context),
113 StrictMode(Options.getLocalOrGlobal("StrictMode", 0)) {}
114
115 void SuspiciousEnumUsageCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
116 Options.store(Opts, "StrictMode", StrictMode);
117 }
118
119 void SuspiciousEnumUsageCheck::registerMatchers(MatchFinder *Finder) {
120 const auto enumExpr = [](StringRef RefName, StringRef DeclName) {
121 return expr(ignoringImpCasts(expr().bind(RefName)),
122 ignoringImpCasts(hasType(enumDecl().bind(DeclName))));
123 };
124
125 Finder->addMatcher(
126 binaryOperator(hasOperatorName("|"), hasLHS(enumExpr("", "enumDecl")),
127 hasRHS(expr(enumExpr("", "otherEnumDecl"),
128 ignoringImpCasts(hasType(enumDecl(
129 unless(equalsBoundNode("enumDecl"))))))))
130 .bind("diffEnumOp"),
131 this);
132
133 Finder->addMatcher(
134 binaryOperator(anyOf(hasOperatorName("+"), hasOperatorName("|")),
135 hasLHS(enumExpr("lhsExpr", "enumDecl")),
136 hasRHS(expr(enumExpr("rhsExpr", ""),
137 ignoringImpCasts(hasType(
138 enumDecl(equalsBoundNode("enumDecl"))))))),
139 this);
140
141 Finder->addMatcher(
142 binaryOperator(anyOf(hasOperatorName("+"), hasOperatorName("|")),
143 hasEitherOperand(
144 expr(hasType(isInteger()), unless(enumExpr("", "")))),
145 hasEitherOperand(enumExpr("enumExpr", "enumDecl"))),
146 this);
147
148 Finder->addMatcher(
149 binaryOperator(anyOf(hasOperatorName("|="), hasOperatorName("+=")),
150 hasRHS(enumExpr("enumExpr", "enumDecl"))),
151 this);
152 }
153
154 void SuspiciousEnumUsageCheck::checkSuspiciousBitmaskUsage(
155 const Expr *NodeExpr, const EnumDecl *EnumDec) {
156 const auto *EnumExpr = dyn_cast<DeclRefExpr>(NodeExpr);
157 const auto *EnumConst =
158 EnumExpr ? dyn_cast<EnumConstantDecl>(EnumExpr->getDecl()) : nullptr;
159
160 // Report the parameter if neccessary.
161 if (!EnumConst) {
162 diag(EnumDec->getInnerLocStart(), BitmaskVarErrorMessage)
163 << countNonPowOfTwoLiteralNum(EnumDec);
164 diag(EnumExpr->getExprLoc(), BitmaskNoteMessage, DiagnosticIDs::Note);
165 } else if (isNonPowerOf2NorNullLiteral(EnumConst)) {
166 diag(EnumConst->getSourceRange().getBegin(), BitmaskErrorMessage);
167 diag(EnumExpr->getExprLoc(), BitmaskNoteMessage, DiagnosticIDs::Note);
168 }
169 }
170
171 void SuspiciousEnumUsageCheck::check(const MatchFinder::MatchResult &Result) {
172 // Case 1: The two enum values come from different types.
173 if (const auto *DiffEnumOp =
174 Result.Nodes.getNodeAs<BinaryOperator>("diffEnumOp")) {
175 const auto *EnumDec = Result.Nodes.getNodeAs<EnumDecl>("enumDecl");
176 const auto *OtherEnumDec =
177 Result.Nodes.getNodeAs<EnumDecl>("otherEnumDecl");
178 // Skip when one of the parameters is an empty enum. The
179 // hasDisjointValueRange function could not decide the values properly in
180 // case of an empty enum.
181 if (EnumDec->enumerator_begin() == EnumDec->enumerator_end() ||
182 OtherEnumDec->enumerator_begin() == OtherEnumDec->enumerator_end())
183 return;
184
185 if (!hasDisjointValueRange(EnumDec, OtherEnumDec))
186 diag(DiffEnumOp->getOperatorLoc(), DifferentEnumErrorMessage);
187 return;
188 }
189
190 // Case 2 and 3 only checked in strict mode. The checker tries to detect
191 // suspicious bitmasks which contains values initialized by non power-of-2
192 // literals.
193 if (!StrictMode)
194 return;
195 const auto *EnumDec = Result.Nodes.getNodeAs<EnumDecl>("enumDecl");
196 if (!isPossiblyBitMask(EnumDec))
197 return;
198
199 // Case 2:
200 // a. Investigating the right hand side of `+=` or `|=` operator.
201 // b. When the operator is `|` or `+` but only one of them is an EnumExpr
202 if (const auto *EnumExpr = Result.Nodes.getNodeAs<Expr>("enumExpr")) {
203 checkSuspiciousBitmaskUsage(EnumExpr, EnumDec);
204 return;
205 }
206
207 // Case 3:
208 // '|' or '+' operator where both argument comes from the same enum type
209 const auto *LhsExpr = Result.Nodes.getNodeAs<Expr>("lhsExpr");
210 checkSuspiciousBitmaskUsage(LhsExpr, EnumDec);
211
212 const auto *RhsExpr = Result.Nodes.getNodeAs<Expr>("rhsExpr");
213 checkSuspiciousBitmaskUsage(RhsExpr, EnumDec);
214 }
215
216 } // namespace bugprone
217 } // namespace tidy
218 } // namespace clang
SuspiciousEnumUsageCheck.h
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::tidy::bugprone::enumLength
static int enumLength(const EnumDecl *EnumDec)
Return the number of EnumConstantDecls in an EnumDecl.
Definition: SuspiciousEnumUsageCheck.cpp:53
clang::tidy::bugprone::countNonPowOfTwoLiteralNum
static int countNonPowOfTwoLiteralNum(const EnumDecl *EnumDec)
Definition: SuspiciousEnumUsageCheck.cpp:89
clang::tidy::bugprone::isPossiblyBitMask
static bool isPossiblyBitMask(const EnumDecl *EnumDec)
Check if there is one or two enumerators that are not a power of 2 and are initialized by a literal i...
Definition: SuspiciousEnumUsageCheck.cpp:100
clang::tidy::bugprone::ValueRange::MaxVal
llvm::APSInt MaxVal
Definition: SuspiciousEnumUsageCheck.cpp:38
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:47
clang::tidy::bugprone::ValueRange::ValueRange
ValueRange(const EnumDecl *EnumDec)
Definition: SuspiciousEnumUsageCheck.cpp:40
clang::tidy::bugprone::ValueRange
Stores a min and a max value which describe an interval.
Definition: SuspiciousEnumUsageCheck.cpp:36
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:184
clang::tidy::bugprone::BitmaskErrorMessage
static const char BitmaskErrorMessage[]
Definition: SuspiciousEnumUsageCheck.cpp:23
clang::tidy::ClangTidyCheck::OptionsView::store
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Definition: ClangTidyCheck.cpp:58
clang::tidy::bugprone::isMaxValAllBitSetLiteral
static bool isMaxValAllBitSetLiteral(const EnumDecl *EnumDec)
Definition: SuspiciousEnumUsageCheck.cpp:74
clang::tidy::bugprone::BitmaskVarErrorMessage
static const char BitmaskVarErrorMessage[]
Definition: SuspiciousEnumUsageCheck.cpp:28
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:97
clang::tidy::bugprone::BitmaskNoteMessage
static const char BitmaskNoteMessage[]
Definition: SuspiciousEnumUsageCheck.cpp:33
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::bugprone::isNonPowerOf2NorNullLiteral
static bool isNonPowerOf2NorNullLiteral(const EnumConstantDecl *EnumConst)
Definition: SuspiciousEnumUsageCheck.cpp:64
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77
E
const Expr * E
Definition: AvoidBindCheck.cpp:82
clang::tidy::bugprone::SuspiciousEnumUsageCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: SuspiciousEnumUsageCheck.cpp:115
clang::tidy::bugprone::SuspiciousEnumUsageCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: SuspiciousEnumUsageCheck.cpp:119
clang::tidy::bugprone::DifferentEnumErrorMessage
static const char DifferentEnumErrorMessage[]
Definition: SuspiciousEnumUsageCheck.cpp:20
clang::tidy::bugprone::ValueRange::MinVal
llvm::APSInt MinVal
Definition: SuspiciousEnumUsageCheck.cpp:37
clang::tidy::bugprone::SuspiciousEnumUsageCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: SuspiciousEnumUsageCheck.cpp:171
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidyCheck.cpp:21
clang::tidy::bugprone::hasDisjointValueRange
static bool hasDisjointValueRange(const EnumDecl *Enum1, const EnumDecl *Enum2)
Definition: SuspiciousEnumUsageCheck.cpp:57