Debian Codegraph @

src/checks/manuallevel/unexpected-flag-enumerator-value.cpp

raw
1 /*
2 SPDX-FileCopyrightText: 2021 Klarälvdalens Datakonsult AB a KDAB Group company [email protected]
3 SPDX-FileContributor: Waqar Ahmed <[email protected]>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8 #include "unexpected-flag-enumerator-value.h"
9 #include "HierarchyUtils.h"
10
11 #include <clang/AST/AST.h>
12
13 #include <algorithm>
14
15 using namespace clang;
16
17 static ConstantExpr *getConstantExpr(EnumConstantDecl *enCD)
18 {
19 auto *cexpr = dyn_cast_or_null<ConstantExpr>(enCD->getInitExpr());
20 if (cexpr) {
21 return cexpr;
22 }
23 return clazy::getFirstChildOfType<ConstantExpr>(enCD->getInitExpr());
24 }
25
26 static bool isBinaryOperatorExpression(ConstantExpr *cexpr)
27 {
28 return clazy::getFirstChildOfType<BinaryOperator>(cexpr);
29 }
30
31 static bool isReferenceToEnumerator(ConstantExpr *cexpr)
32 {
33 return dyn_cast_or_null<DeclRefExpr>(cexpr->getSubExpr());
34 }
35
36 static bool isIntentionallyNotPowerOf2(EnumConstantDecl *en)
37 {
38 constexpr unsigned MinOnesToQualifyAsMask = 3;
39
40 const auto val = en->getInitVal();
41 if (val.isMask() && val.countTrailingOnes() >= MinOnesToQualifyAsMask) {
42 return true;
43 }
44
45 if (val.isShiftedMask() && val.popcount() >= MinOnesToQualifyAsMask) {
46 return true;
47 }
48
49 if (en->getName().contains_insensitive("mask")) {
50 return true;
51 }
52
53 auto *cexpr = getConstantExpr(en);
54 if (!cexpr) {
55 return false;
56 }
57
58 if (isBinaryOperatorExpression(cexpr)) {
59 return true;
60 }
61
62 if (isReferenceToEnumerator(cexpr)) {
63 return true;
64 }
65
66 return false;
67 }
68
69 static SmallVector<EnumConstantDecl *, 16> getEnumerators(EnumDecl *enDecl)
70 {
71 SmallVector<EnumConstantDecl *, 16> ret;
72 for (auto *enumerator : enDecl->enumerators()) {
73 ret.push_back(enumerator);
74 }
75 return ret;
76 }
77
78 static uint64_t getIntegerValue(EnumConstantDecl *e)
79 {
80 return e->getInitVal().getLimitedValue();
81 }
82
83 static bool hasConsecutiveValues(const SmallVector<EnumConstantDecl *, 16> &enumerators)
84 {
85 auto val = getIntegerValue(enumerators.front());
86 const size_t until = std::min<size_t>(4, enumerators.size());
87 for (size_t i = 1; i < until; ++i) {
88 val++;
89 if (getIntegerValue(enumerators[i]) != val) {
90 return false;
91 }
92 }
93 return true;
94 }
95
96 static bool hasInitExprs(const SmallVector<EnumConstantDecl *, 16> &enumerators)
97 {
98 size_t enumeratorsWithInitExpr = 0;
99 for (auto *enumerator : enumerators) {
100 if (enumerator->getInitExpr()) {
101 enumeratorsWithInitExpr++;
102 }
103 }
104
105 return enumeratorsWithInitExpr == enumerators.size();
106 }
107
108 static bool isFlagEnum(const SmallVector<EnumConstantDecl *, 16> &enumerators)
109 {
110 if (enumerators.size() < 4) {
111 return false;
112 }
113
114 // For an enum to be considered "flag like", all enumerators
115 // must have an explicit init value / expr
116 if (!hasInitExprs(enumerators)) {
117 return false;
118 }
119
120 if (hasConsecutiveValues(enumerators)) {
121 return false;
122 }
123
124 llvm::SmallVector<bool, 16> enumValues;
125 for (auto *enumerator : enumerators) {
126 enumValues.push_back(enumerator->getInitVal().isPowerOf2());
127 }
128
129 const size_t count = std::count(enumValues.begin(), enumValues.end(), false);
130
131 // If half of our values were power-of-2, this is probably a flag enum
132 return count <= (enumerators.size() / 2);
133 }
134
135 void UnexpectedFlagEnumeratorValue::VisitDecl(clang::Decl *decl)
136 {
137 auto *enDecl = dyn_cast_or_null<EnumDecl>(decl);
138 if (!enDecl || !enDecl->hasNameForLinkage()) {
139 return;
140 }
141
142 const SmallVector<EnumConstantDecl *, 16> enumerators = getEnumerators(enDecl);
143
144 if (!isFlagEnum(enumerators)) {
145 return;
146 }
147
148 for (EnumConstantDecl *enumerator : enumerators) {
149 const auto &initVal = enumerator->getInitVal();
150 if (!initVal.isPowerOf2() && !initVal.isZero() && !initVal.isNegative()) {
151 if (isIntentionallyNotPowerOf2(enumerator)) {
152 continue;
153 }
154 const auto value = enumerator->getInitVal().getLimitedValue();
155 Expr *initExpr = enumerator->getInitExpr();
156 emitWarning(initExpr ? initExpr->getBeginLoc() : enumerator->getBeginLoc(), "Unexpected non power-of-2 enumerator value: " + std::to_string(value));
157 }
158 }
159 }