1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
16 #include <clang/AST/CXXInheritance.h>
22 Look for calls where the param is bool but the call-site-arg is pointer.
24 #ifndef LO_CLANG_SHARED_PLUGINS
28 class PointerBool
: public loplugin::FilteringPlugin
<PointerBool
>
31 explicit PointerBool(loplugin::InstantiationData
const& data
)
32 : FilteringPlugin(data
)
36 virtual void run() override
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*);
50 compat::optional
<APSInt
> getCallValue(const Expr
* arg
);
51 std::vector
<FunctionDecl
*> functions_
;
54 bool PointerBool::PreTraverseFunctionDecl(FunctionDecl
* decl
)
56 functions_
.push_back(decl
);
60 bool PointerBool::PostTraverseFunctionDecl(FunctionDecl
*, bool)
62 assert(!functions_
.empty());
63 functions_
.pop_back();
67 bool PointerBool::TraverseFunctionDecl(FunctionDecl
* decl
)
70 if (PreTraverseFunctionDecl(decl
))
72 ret
= FilteringPlugin::TraverseFunctionDecl(decl
);
73 PostTraverseFunctionDecl(decl
, ret
);
78 bool PointerBool::VisitCallExpr(CallExpr
const* callExpr
)
80 if (ignoreLocation(callExpr
))
82 // TODO this doesn't currently work, the args and the params don't seem to line up
83 if (isa
<CXXOperatorCallExpr
>(callExpr
))
85 const FunctionDecl
* functionDecl
;
86 if (isa
<CXXMemberCallExpr
>(callExpr
))
88 functionDecl
= dyn_cast
<CXXMemberCallExpr
>(callExpr
)->getMethodDecl();
92 functionDecl
= callExpr
->getDirectCallee();
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())
104 auto arg
= callExpr
->getArg(i
)->IgnoreImpCasts();
105 auto argTC
= loplugin::TypeCheck(arg
->getType());
106 if (argTC
.AnyBoolean())
108 // sal_Bool is sometimes disguised
109 if (isa
<SubstTemplateTypeParmType
>(arg
->getType()))
110 if (arg
->getType()->getUnqualifiedDesugaredType()->isSpecificBuiltinType(
111 clang::BuiltinType::UChar
))
113 if (arg
->getType()->isDependentType())
115 if (arg
->getType()->isIntegerType())
117 auto ret
= getCallValue(arg
);
118 if (compat::has_value(ret
) && (compat::value(ret
) == 1 || compat::value(ret
) == 0))
120 // something like: priv->m_nLOKFeatures & LOK_FEATURE_DOCUMENT_PASSWORD
121 if (isa
<BinaryOperator
>(arg
->IgnoreParenImpCasts()))
123 // something like: pbEmbolden ? FcTrue : FcFalse
124 if (isa
<ConditionalOperator
>(arg
->IgnoreParenImpCasts()))
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());
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
>();
156 if (compat::EvaluateAsInt(arg
, x1
, compiler
.getASTContext()))
160 return compat::optional
<APSInt
>();
163 loplugin::Plugin::Registration
<PointerBool
> pointerbool("pointerbool");
167 #endif // LO_CLANG_SHARED_PLUGINS
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */