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
|
/* -*- 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/.
*/
#ifndef LO_CLANG_SHARED_PLUGINS
#include <cassert>
#include <string>
#include <iostream>
#include <fstream>
#include <set>
#include <clang/AST/CXXInheritance.h>
#include "check.hxx"
#include "plugin.hxx"
/**
* Look for a mistake I made (a lot) at one point where we assign a reference to a reference var, which
* does not do at all what I thought.
*/
namespace
{
class RefAssign : public loplugin::FilteringPlugin<RefAssign>
{
public:
explicit RefAssign(loplugin::InstantiationData const& data)
: FilteringPlugin(data)
{
}
virtual bool preRun() override
{
StringRef fn(handler.getMainFileName());
if (loplugin::isSamePathname(fn, SRCDIR "/comphelper/source/misc/syntaxhighlight.cxx"))
return false;
if (loplugin::isSamePathname(fn, SRCDIR "/svl/source/numbers/zformat.cxx"))
return false;
if (loplugin::isSamePathname(fn, SRCDIR "/tools/source/memtools/multisel.cxx"))
return false;
if (loplugin::isSamePathname(fn, SRCDIR "/tools/source/generic/point.cxx"))
return false;
if (loplugin::isSamePathname(fn, SRCDIR "/vcl/source/control/edit.cxx"))
return false;
if (loplugin::isSamePathname(fn, SRCDIR "/vcl/source/control/fmtfield.cxx"))
return false;
if (loplugin::isSamePathname(fn, SRCDIR "/vcl/source/control/field.cxx"))
return false;
if (loplugin::isSamePathname(fn, SRCDIR "/vcl/source/control/field2.cxx"))
return false;
if (loplugin::isSamePathname(fn, SRCDIR "/vcl/source/edit/textview.cxx"))
return false;
if (loplugin::isSamePathname(fn, SRCDIR "/vcl/source/edit/vclmedit.cxx"))
return false;
if (loplugin::isSamePathname(fn, SRCDIR "/editeng/source/editeng/editdoc.cxx"))
return false;
if (loplugin::isSamePathname(fn, SRCDIR "/editeng/source/editeng/impedit2.cxx"))
return false;
if (loplugin::isSamePathname(fn, SRCDIR "/svx/source/dialog/svxruler.cxx"))
return false;
if (loplugin::isSamePathname(fn, SRCDIR "/sw/source/uibase/ribbar/inputwin.cxx"))
return false;
if (loplugin::isSamePathname(fn, SRCDIR "/sw/source/core/text/txtftn.cxx"))
return false;
return true;
}
virtual void run() override
{
if (preRun())
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
}
bool VisitBinaryOperator(BinaryOperator const*);
// causes crashes in CallExpr::getReturnType
bool TraverseFunctionTemplateDecl(FunctionTemplateDecl*) { return true; }
bool TraverseClassTemplateDecl(ClassTemplateDecl*) { return true; }
private:
clang::QualType ExtractType(Expr const*);
};
bool RefAssign::VisitBinaryOperator(BinaryOperator const* binaryOp)
{
if (ignoreLocation(binaryOp))
return true;
if (binaryOp->getOpcode() != BO_Assign)
return true;
// ignore assigning to/from an element of a collection
if (isa<CXXOperatorCallExpr>(binaryOp->getLHS()->IgnoreParenImpCasts()))
return true;
if (isa<CXXOperatorCallExpr>(binaryOp->getRHS()->IgnoreParenImpCasts()))
return true;
// if we are assigning to a parameter we probably mean it
if (auto declRefExpr = dyn_cast<DeclRefExpr>(binaryOp->getLHS()->IgnoreParenImpCasts()))
if (declRefExpr->getDecl() && isa<ParmVarDecl>(declRefExpr->getDecl()))
return true;
if (auto callExpr = dyn_cast<CallExpr>(binaryOp->getRHS()->IgnoreParenImpCasts()))
if (auto functionDecl = dyn_cast_or_null<FunctionDecl>(callExpr->getCalleeDecl()))
if (functionDecl->getIdentifier()
&& (functionDecl->getName() == "min" || functionDecl->getName() == "max"))
return true;
auto lhsType = ExtractType(binaryOp->getLHS());
auto rhsType = ExtractType(binaryOp->getRHS());
if (!loplugin::TypeCheck(lhsType).LvalueReference())
return true;
if (!loplugin::TypeCheck(rhsType).LvalueReference())
return true;
binaryOp->dump();
report(DiagnosticsEngine::Warning,
"assigning a %0 to a var of type %1 probably does not do what you think",
binaryOp->getBeginLoc())
<< rhsType << lhsType << binaryOp->getSourceRange();
return true;
}
clang::QualType RefAssign::ExtractType(Expr const* expr)
{
expr = expr->IgnoreParenImpCasts();
if (auto declReflExpr = dyn_cast<DeclRefExpr>(expr))
{
if (auto varDecl = dyn_cast<VarDecl>(declReflExpr->getDecl()))
return varDecl->getType();
}
else if (auto callExpr = dyn_cast<CallExpr>(expr))
{
if (callExpr->isTypeDependent())
return {};
// callExpr->dump();
return callExpr->getCallReturnType(compiler.getASTContext());
}
return expr->getType();
}
loplugin::Plugin::Registration<RefAssign> refassign("refassign");
} // namespace
#endif // LO_CLANG_SHARED_PLUGINS
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|