update credits
[LibreOffice.git] / compilerplugins / clang / pointerbool.cxx
blob6530c76e7179d531a575ca2c424f75bbbc641a7b
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 <cassert>
11 #include <string>
12 #include <iostream>
13 #include <fstream>
14 #include <set>
16 #include <clang/AST/CXXInheritance.h>
17 #include "plugin.hxx"
18 #include "check.hxx"
19 #include "compat.hxx"
21 /**
22 Look for calls where the param is bool but the call-site-arg is pointer.
24 #ifndef LO_CLANG_SHARED_PLUGINS
26 namespace
28 class PointerBool : public loplugin::FilteringPlugin<PointerBool>
30 public:
31 explicit PointerBool(loplugin::InstantiationData const& data)
32 : FilteringPlugin(data)
36 virtual void run() override
38 if (preRun())
39 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
42 bool shouldVisitTemplateInstantiations() const { return true; }
44 bool PreTraverseFunctionDecl(FunctionDecl* decl);
45 bool PostTraverseFunctionDecl(FunctionDecl* decl, bool);
46 bool TraverseFunctionDecl(FunctionDecl* decl);
47 bool VisitCallExpr(CallExpr const*);
49 private:
50 compat::optional<APSInt> getCallValue(const Expr* arg);
51 std::vector<FunctionDecl*> functions_;
54 bool PointerBool::PreTraverseFunctionDecl(FunctionDecl* decl)
56 functions_.push_back(decl);
57 return true;
60 bool PointerBool::PostTraverseFunctionDecl(FunctionDecl*, bool)
62 assert(!functions_.empty());
63 functions_.pop_back();
64 return true;
67 bool PointerBool::TraverseFunctionDecl(FunctionDecl* decl)
69 bool ret = true;
70 if (PreTraverseFunctionDecl(decl))
72 ret = FilteringPlugin::TraverseFunctionDecl(decl);
73 PostTraverseFunctionDecl(decl, ret);
75 return ret;
78 bool PointerBool::VisitCallExpr(CallExpr const* callExpr)
80 if (ignoreLocation(callExpr))
81 return true;
82 // TODO this doesn't currently work, the args and the params don't seem to line up
83 if (isa<CXXOperatorCallExpr>(callExpr))
84 return true;
85 const FunctionDecl* functionDecl;
86 if (isa<CXXMemberCallExpr>(callExpr))
88 functionDecl = dyn_cast<CXXMemberCallExpr>(callExpr)->getMethodDecl();
90 else
92 functionDecl = callExpr->getDirectCallee();
94 if (!functionDecl)
95 return true;
97 unsigned len = std::min(callExpr->getNumArgs(), functionDecl->getNumParams());
98 for (unsigned i = 0; i < len; ++i)
100 auto param = functionDecl->getParamDecl(i);
101 auto paramTC = loplugin::TypeCheck(param->getType());
102 if (!paramTC.AnyBoolean())
103 continue;
104 auto arg = callExpr->getArg(i)->IgnoreImpCasts();
105 auto argTC = loplugin::TypeCheck(arg->getType());
106 if (argTC.AnyBoolean())
107 continue;
108 // sal_Bool is sometimes disguised
109 if (isa<SubstTemplateTypeParmType>(arg->getType()))
110 if (arg->getType()->getUnqualifiedDesugaredType()->isSpecificBuiltinType(
111 clang::BuiltinType::UChar))
112 continue;
113 if (arg->getType()->isDependentType())
114 continue;
115 if (arg->getType()->isIntegerType())
117 auto ret = getCallValue(arg);
118 if (compat::has_value(ret) && (compat::value(ret) == 1 || compat::value(ret) == 0))
119 continue;
120 // something like: priv->m_nLOKFeatures & LOK_FEATURE_DOCUMENT_PASSWORD
121 if (isa<BinaryOperator>(arg->IgnoreParenImpCasts()))
122 continue;
123 // something like: pbEmbolden ? FcTrue : FcFalse
124 if (isa<ConditionalOperator>(arg->IgnoreParenImpCasts()))
125 continue;
127 report(DiagnosticsEngine::Warning,
128 "possibly unwanted implicit conversion when calling bool param", arg->getExprLoc())
129 << arg->getSourceRange();
130 report(DiagnosticsEngine::Note, "method here", param->getLocation())
131 << param->getSourceRange();
132 if (!functions_.empty())
134 auto callerFD = functions_.back();
135 if (callerFD->isTemplateInstantiation())
136 report(DiagnosticsEngine::Note, "instantiated from here",
137 callerFD->getPointOfInstantiation());
140 return true;
143 compat::optional<APSInt> PointerBool::getCallValue(const Expr* arg)
145 arg = arg->IgnoreParenCasts();
146 if (auto defArg = dyn_cast<CXXDefaultArgExpr>(arg))
148 arg = defArg->getExpr()->IgnoreParenCasts();
150 // ignore this, it seems to trigger an infinite recursion
151 if (isa<UnaryExprOrTypeTraitExpr>(arg))
153 return compat::optional<APSInt>();
155 APSInt x1;
156 if (compat::EvaluateAsInt(arg, x1, compiler.getASTContext()))
158 return x1;
160 return compat::optional<APSInt>();
163 loplugin::Plugin::Registration<PointerBool> pointerbool("pointerbool");
165 } // namespace
167 #endif // LO_CLANG_SHARED_PLUGINS
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */