/
MultipleUnaryOperatorRule.cpp
80 lines (68 loc) · 1.92 KB
/
MultipleUnaryOperatorRule.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "oclint/AbstractASTVisitorRule.h"
#include "oclint/RuleSet.h"
using namespace std;
using namespace clang;
using namespace oclint;
class MultipleUnaryOperatorRule : public AbstractASTVisitorRule<MultipleUnaryOperatorRule>
{
private:
bool isTargetUnaryOperator(UnaryOperator *unaryOperator)
{
return unaryOperator->getOpcode() == UO_LNot ||
unaryOperator->getOpcode() == UO_Not ||
unaryOperator->getOpcode() == UO_Plus ||
unaryOperator->getOpcode() == UO_Minus;
}
public:
virtual const string name() const override
{
return "multiple unary operator";
}
virtual int priority() const override
{
return 2;
}
virtual const string category() const override
{
return "basic";
}
#ifdef DOCGEN
virtual const std::string since() const override
{
return "0.6";
}
virtual const std::string description() const override
{
return "Multiple unary operator can always be confusing and should be simplified.";
}
virtual const std::string example() const override
{
return R"rst(
.. code-block:: cpp
void example()
{
int b = -(+(!(~1)));
}
)rst";
}
#endif
bool VisitUnaryOperator(UnaryOperator *unaryOperator)
{
Expr *subExpr = unaryOperator->getSubExpr();
while (subExpr && isa<ParenExpr>(subExpr))
{
ParenExpr *parenExpr = dyn_cast<ParenExpr>(subExpr);
subExpr = parenExpr->getSubExpr();
}
if (subExpr && isa<UnaryOperator>(subExpr))
{
UnaryOperator *subUnaryOperator = dyn_cast<UnaryOperator>(subExpr);
if (isTargetUnaryOperator(unaryOperator) && isTargetUnaryOperator(subUnaryOperator))
{
addViolation(unaryOperator, this);
}
}
return true;
}
};
static RuleSet rules(new MultipleUnaryOperatorRule());