Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / compilerplugins / clang / expressionalwayszero.cxx
blob6633f138cfb3bf0eef36dfe0869d4611e0206799
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <string>
11 #include <set>
12 #include <iostream>
13 #include <fstream>
15 #include "plugin.hxx"
16 #include "compat.hxx"
17 #include "check.hxx"
19 /**
20 Look for & and operator& expressions where the result is always zero.
21 Generally a mistake when people meant to use | or operator|
24 namespace {
26 static bool startswith(const std::string& rStr, const char* pSubStr) {
27 return rStr.compare(0, strlen(pSubStr), pSubStr) == 0;
30 class ExpressionAlwaysZero:
31 public RecursiveASTVisitor<ExpressionAlwaysZero>, public loplugin::Plugin
33 public:
34 explicit ExpressionAlwaysZero(loplugin::InstantiationData const & data): Plugin(data) {}
36 virtual void run() override
38 std::string fn( compiler.getSourceManager().getFileEntryForID(
39 compiler.getSourceManager().getMainFileID())->getName() );
40 loplugin::normalizeDotDotInFilePath(fn);
41 // encoding of constant value for binary file format
42 if (startswith(fn, SRCDIR "/package/source/zipapi/ZipFile.cxx"))
43 return;
44 // some auto-generated static data
45 if (startswith(fn, SRCDIR "/sal/textenc/tables.cxx"))
46 return;
47 // nested conditional defines that are not worth cleaning up
48 if (startswith(fn, SRCDIR "/opencl/source/openclwrapper.cxx"))
49 return;
50 // some kind of matrix calculation, the compiler will optimise it out anyway
51 if (startswith(fn, SRCDIR "/vcl/source/gdi/bitmap4.cxx"))
52 return;
53 // code follows a pattern
54 if (startswith(fn, SRCDIR "/svx/source/svdraw/svdhdl.cxx"))
55 return;
56 // looks like some kind of TODO marker
57 if (startswith(fn, SRCDIR "/chart2/source/view/main/PropertyMapper.cxx")
58 || startswith(fn, SRCDIR "/sc/source/core/data/formulacell.cxx"))
59 return;
60 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
63 bool VisitBinaryOperator(BinaryOperator const *);
64 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr const *);
65 bool TraverseStaticAssertDecl(StaticAssertDecl *);
66 private:
67 // note, abusing std::unique_ptr as a std::optional lookalike
68 std::unique_ptr<APSInt> getExprValue(const Expr* arg);
71 bool ExpressionAlwaysZero::VisitBinaryOperator( BinaryOperator const * binaryOperator )
73 if (ignoreLocation(binaryOperator))
74 return true;
75 if (binaryOperator->getLocStart().isMacroID())
76 return true;
78 auto op = binaryOperator->getOpcode();
79 if (!(op == BO_And || op == BO_AndAssign || op == BO_LAnd))
80 return true;
82 auto lhsValue = getExprValue(binaryOperator->getLHS());
83 auto rhsValue = getExprValue(binaryOperator->getRHS());
84 if (lhsValue && lhsValue->getExtValue() == 0)
85 ; // ok
86 else if (rhsValue && rhsValue->getExtValue() == 0)
87 ; // ok
88 else if (lhsValue && rhsValue && (lhsValue->getExtValue() & rhsValue->getExtValue()) == 0)
89 ; // ok
90 else
91 return true;
92 report(
93 DiagnosticsEngine::Warning, "expression always evaluates to zero, lhs=%0 rhs=%1",
94 binaryOperator->getLocStart())
95 << (lhsValue ? lhsValue->toString(10) : "unknown")
96 << (rhsValue ? rhsValue->toString(10) : "unknown")
97 << binaryOperator->getSourceRange();
98 return true;
101 bool ExpressionAlwaysZero::VisitCXXOperatorCallExpr( CXXOperatorCallExpr const * cxxOperatorCallExpr )
103 if (ignoreLocation(cxxOperatorCallExpr))
104 return true;
105 if (cxxOperatorCallExpr->getLocStart().isMacroID())
106 return true;
108 auto op = cxxOperatorCallExpr->getOperator();
109 if ( !(op == OO_Amp || op == OO_AmpEqual || op == OO_AmpAmp))
110 return true;
112 if (cxxOperatorCallExpr->getNumArgs() != 2)
113 return true;
114 auto lhsValue = getExprValue(cxxOperatorCallExpr->getArg(0));
115 auto rhsValue = getExprValue(cxxOperatorCallExpr->getArg(1));
116 if (lhsValue && lhsValue->getExtValue() == 0)
117 ; // ok
118 else if (rhsValue && rhsValue->getExtValue() == 0)
119 ; // ok
120 else if (lhsValue && rhsValue && (lhsValue->getExtValue() & rhsValue->getExtValue()) == 0)
121 ; // ok
122 else
123 return true;
124 report(
125 DiagnosticsEngine::Warning, "expression always evaluates to zero, lhs=%0 rhs=%1",
126 cxxOperatorCallExpr->getLocStart())
127 << (lhsValue ? lhsValue->toString(10) : "unknown")
128 << (rhsValue ? rhsValue->toString(10) : "unknown")
129 << cxxOperatorCallExpr->getSourceRange();
130 return true;
133 std::unique_ptr<APSInt> ExpressionAlwaysZero::getExprValue(Expr const * expr)
135 expr = expr->IgnoreParenCasts();
136 // ignore this, it seems to trigger an infinite recursion
137 if (isa<UnaryExprOrTypeTraitExpr>(expr)) {
138 return std::unique_ptr<APSInt>();
140 APSInt x1;
141 if (expr->EvaluateAsInt(x1, compiler.getASTContext()))
142 return std::unique_ptr<APSInt>(new APSInt(x1));
143 return std::unique_ptr<APSInt>();
146 // these will often evaluate to zero harmlessly
147 bool ExpressionAlwaysZero::TraverseStaticAssertDecl( StaticAssertDecl * )
149 return true;
152 // on clang-3.8, this plugin can generate OOM
153 #if CLANG_VERSION >= 30900
154 loplugin::Plugin::Registration< ExpressionAlwaysZero > X("expressionalwayszero");
155 #else
156 loplugin::Plugin::Registration< ExpressionAlwaysZero > X("expressionalwayszero", false);
157 #endif
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */