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 shouldVisitTemplateInstantiations() const { return true; }
43 bool PreTraverseFunctionDecl(FunctionDecl
* decl
);
44 bool PostTraverseFunctionDecl(FunctionDecl
* decl
, bool);
45 bool TraverseFunctionDecl(FunctionDecl
* decl
);
46 bool VisitCallExpr(CallExpr
const*);
49 llvm::Optional
<APSInt
> getCallValue(const Expr
* arg
);
50 std::vector
<FunctionDecl
*> functions_
;
53 bool PointerBool::PreTraverseFunctionDecl(FunctionDecl
* decl
)
55 functions_
.push_back(decl
);
59 bool PointerBool::PostTraverseFunctionDecl(FunctionDecl
*, bool)
61 assert(!functions_
.empty());
62 functions_
.pop_back();
66 bool PointerBool::TraverseFunctionDecl(FunctionDecl
* decl
)
69 if (PreTraverseFunctionDecl(decl
))
71 ret
= FilteringPlugin::TraverseFunctionDecl(decl
);
72 PostTraverseFunctionDecl(decl
, ret
);
77 bool PointerBool::VisitCallExpr(CallExpr
const* callExpr
)
79 if (ignoreLocation(callExpr
))
81 // TODO this doesn't currently work, the args and the params don't seem to line up
82 if (isa
<CXXOperatorCallExpr
>(callExpr
))
84 const FunctionDecl
* functionDecl
;
85 if (isa
<CXXMemberCallExpr
>(callExpr
))
87 functionDecl
= dyn_cast
<CXXMemberCallExpr
>(callExpr
)->getMethodDecl();
91 functionDecl
= callExpr
->getDirectCallee();
96 unsigned len
= std::min(callExpr
->getNumArgs(), functionDecl
->getNumParams());
97 for (unsigned i
= 0; i
< len
; ++i
)
99 auto param
= functionDecl
->getParamDecl(i
);
100 auto paramTC
= loplugin::TypeCheck(param
->getType());
101 if (!paramTC
.AnyBoolean())
103 auto arg
= callExpr
->getArg(i
)->IgnoreImpCasts();
104 auto argTC
= loplugin::TypeCheck(arg
->getType());
105 if (argTC
.AnyBoolean())
107 // sal_Bool is sometimes disguised
108 if (isa
<SubstTemplateTypeParmType
>(arg
->getType()))
109 if (arg
->getType()->getUnqualifiedDesugaredType()->isSpecificBuiltinType(
110 clang::BuiltinType::UChar
))
112 if (arg
->getType()->isDependentType())
114 if (arg
->getType()->isIntegerType())
116 auto ret
= getCallValue(arg
);
117 if (ret
.hasValue() && (ret
.getValue() == 1 || ret
.getValue() == 0))
119 // something like: priv->m_nLOKFeatures & LOK_FEATURE_DOCUMENT_PASSWORD
120 if (isa
<BinaryOperator
>(arg
->IgnoreParenImpCasts()))
122 // something like: pbEmbolden ? FcTrue : FcFalse
123 if (isa
<ConditionalOperator
>(arg
->IgnoreParenImpCasts()))
126 report(DiagnosticsEngine::Warning
,
127 "possibly unwanted implicit conversion when calling bool param", arg
->getExprLoc())
128 << arg
->getSourceRange();
129 report(DiagnosticsEngine::Note
, "method here", param
->getLocation())
130 << param
->getSourceRange();
131 if (!functions_
.empty())
133 auto callerFD
= functions_
.back();
134 if (callerFD
->isTemplateInstantiation())
135 report(DiagnosticsEngine::Note
, "instantiated from here",
136 callerFD
->getPointOfInstantiation());
142 llvm::Optional
<APSInt
> PointerBool::getCallValue(const Expr
* arg
)
144 arg
= arg
->IgnoreParenCasts();
145 if (auto defArg
= dyn_cast
<CXXDefaultArgExpr
>(arg
))
147 arg
= defArg
->getExpr()->IgnoreParenCasts();
149 // ignore this, it seems to trigger an infinite recursion
150 if (isa
<UnaryExprOrTypeTraitExpr
>(arg
))
152 return llvm::Optional
<APSInt
>();
155 if (compat::EvaluateAsInt(arg
, x1
, compiler
.getASTContext()))
159 return llvm::Optional
<APSInt
>();
162 loplugin::Plugin::Registration
<PointerBool
> pointerbool("pointerbool");
166 #endif // LO_CLANG_SHARED_PLUGINS
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */