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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <cassert>
#include <string>
#include <iostream>
#include <fstream>
#include <set>
#include "compat.hxx"
#include "plugin.hxx"
/**
Look for comparisons where the constant is on the left, it should be on the right.
*/
namespace {
class ComparisonWithConstant :
public RecursiveASTVisitor<ComparisonWithConstant>, public loplugin::RewritePlugin
{
public:
explicit ComparisonWithConstant(InstantiationData const & data): RewritePlugin(data) {}
virtual void run() override
{
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
}
bool VisitBinaryOperator(const BinaryOperator *);
private:
bool rewrite(const BinaryOperator *);
std::string getExprAsString(SourceRange range);
SourceRange ignoreMacroExpansions(SourceRange range);
};
bool ComparisonWithConstant::VisitBinaryOperator(const BinaryOperator* binaryOp)
{
if (ignoreLocation(binaryOp)) {
return true;
}
if (!(binaryOp->getOpcode() == BO_EQ || binaryOp->getOpcode() == BO_NE)) {
return true;
}
// ignore logging macros
if (compiler.getSourceManager().isMacroBodyExpansion(binaryOp->getSourceRange().getBegin())
|| compiler.getSourceManager().isMacroArgExpansion(binaryOp->getSourceRange().getBegin())) {
return true;
}
// protect against clang assert
if (binaryOp->getLHS()->isValueDependent() || binaryOp->getRHS()->isValueDependent()) {
return true;
}
APValue result;
if (!binaryOp->getLHS()->isIntegerConstantExpr(compiler.getASTContext())) {
return true;
}
if (binaryOp->getRHS()->isIntegerConstantExpr(compiler.getASTContext())) {
return true;
}
if (!rewrite(binaryOp))
{
report(
DiagnosticsEngine::Warning, "Rather put constant on right when comparing",
binaryOp->getSourceRange().getBegin())
<< binaryOp->getSourceRange();
}
return true;
}
bool ComparisonWithConstant::rewrite(const BinaryOperator * binaryOp) {
if (rewriter == nullptr) {
return false;
}
auto lhsRange = ignoreMacroExpansions(binaryOp->getLHS()->getSourceRange());
if (!lhsRange.isValid()) {
return false;
}
auto rhsRange = ignoreMacroExpansions(binaryOp->getRHS()->getSourceRange());
if (!rhsRange.isValid()) {
return false;
}
const std::string lhsString = getExprAsString(lhsRange);
const std::string rhsString = getExprAsString(rhsRange);
/* we can't safely move around stuff containing comments, we mess up the resulting code */
if ( lhsString.find("/*") != std::string::npos || lhsString.find("//") != std::string::npos ) {
return false;
}
if ( rhsString.find("/*") != std::string::npos || rhsString.find("//") != std::string::npos ) {
return false;
}
// switch LHS and RHS
RewriteOptions opts;
if (!replaceText(lhsRange, rhsString)) {
return false;
}
if (!replaceText(rhsRange, lhsString)) {
return false;
}
return true;
}
// get the expression contents
std::string ComparisonWithConstant::getExprAsString(SourceRange range)
{
SourceManager& SM = compiler.getSourceManager();
SourceLocation startLoc = range.getBegin();
SourceLocation endLoc = range.getEnd();
const char *p1 = SM.getCharacterData( startLoc );
const char *p2 = SM.getCharacterData( endLoc );
unsigned n = Lexer::MeasureTokenLength( endLoc, SM, compiler.getLangOpts());
return std::string( p1, p2 - p1 + n);
}
SourceRange ComparisonWithConstant::ignoreMacroExpansions(SourceRange range) {
if (range.getBegin().isMacroID()) {
SourceLocation loc;
if (Lexer::isAtStartOfMacroExpansion(
range.getBegin(), compiler.getSourceManager(),
compiler.getLangOpts(), &loc))
{
range.setBegin(loc);
}
}
if (range.getEnd().isMacroID()) {
SourceLocation loc;
if (Lexer::isAtEndOfMacroExpansion(
range.getEnd(), compiler.getSourceManager(),
compiler.getLangOpts(), &loc))
{
range.setEnd(loc);
}
}
return range.getBegin().isMacroID() || range.getEnd().isMacroID()
? SourceRange() : range;
}
loplugin::Plugin::Registration< ComparisonWithConstant > X("comparisonwithconstant", false);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|