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>
21 Look for calls where the param is bool but the call-site-arg is pointer.
23 #ifndef LO_CLANG_SHARED_PLUGINS
27 class PointerBool
: public loplugin::FilteringPlugin
<PointerBool
>
30 explicit PointerBool(loplugin::InstantiationData
const& data
)
31 : FilteringPlugin(data
)
35 virtual void run() override
38 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
41 bool VisitCallExpr(CallExpr
const*);
44 llvm::Optional
<APSInt
> getCallValue(const Expr
* arg
);
47 bool PointerBool::VisitCallExpr(CallExpr
const* callExpr
)
49 if (ignoreLocation(callExpr
))
51 // TODO this doesn't currently work, the args and the params don't seem to line up
52 if (isa
<CXXOperatorCallExpr
>(callExpr
))
54 const FunctionDecl
* functionDecl
;
55 if (isa
<CXXMemberCallExpr
>(callExpr
))
57 functionDecl
= dyn_cast
<CXXMemberCallExpr
>(callExpr
)->getMethodDecl();
61 functionDecl
= callExpr
->getDirectCallee();
66 unsigned len
= std::min(callExpr
->getNumArgs(), functionDecl
->getNumParams());
67 for (unsigned i
= 0; i
< len
; ++i
)
69 auto param
= functionDecl
->getParamDecl(i
);
70 auto paramTC
= loplugin::TypeCheck(param
->getType());
71 if (!paramTC
.AnyBoolean())
73 auto arg
= callExpr
->getArg(i
)->IgnoreImpCasts();
74 auto argTC
= loplugin::TypeCheck(arg
->getType());
75 if (argTC
.AnyBoolean())
77 // sal_Bool is sometimes disguised
78 if (isa
<SubstTemplateTypeParmType
>(arg
->getType()))
79 if (arg
->getType()->getUnqualifiedDesugaredType()->isSpecificBuiltinType(
80 clang::BuiltinType::UChar
))
82 if (arg
->getType()->isDependentType())
84 if (arg
->getType()->isIntegerType())
86 auto ret
= getCallValue(arg
);
87 if (ret
.hasValue() && (ret
.getValue() == 1 || ret
.getValue() == 0))
89 // something like: priv->m_nLOKFeatures & LOK_FEATURE_DOCUMENT_PASSWORD
90 if (isa
<BinaryOperator
>(arg
->IgnoreParenImpCasts()))
92 // something like: pbEmbolden ? FcTrue : FcFalse
93 if (isa
<ConditionalOperator
>(arg
->IgnoreParenImpCasts()))
96 report(DiagnosticsEngine::Warning
,
97 "possibly unwanted implicit conversion when calling bool param", arg
->getExprLoc())
98 << arg
->getSourceRange();
99 report(DiagnosticsEngine::Note
, "method here", param
->getLocation())
100 << param
->getSourceRange();
105 llvm::Optional
<APSInt
> PointerBool::getCallValue(const Expr
* arg
)
107 arg
= arg
->IgnoreParenCasts();
108 if (auto defArg
= dyn_cast
<CXXDefaultArgExpr
>(arg
))
110 arg
= defArg
->getExpr()->IgnoreParenCasts();
112 // ignore this, it seems to trigger an infinite recursion
113 if (isa
<UnaryExprOrTypeTraitExpr
>(arg
))
115 return llvm::Optional
<APSInt
>();
118 if (compat::EvaluateAsInt(arg
, x1
, compiler
.getASTContext()))
122 return llvm::Optional
<APSInt
>();
125 loplugin::Plugin::Registration
<PointerBool
> pointerbool("pointerbool");
129 #endif // LO_CLANG_SHARED_PLUGINS
131 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */