bump product version to 6.4.0.3
[LibreOffice.git] / compilerplugins / clang / pointerbool.cxx
blob66c6a04b3697fb374bde558efd23e67648fc1af5
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"
20 /**
21 Look for calls where the param is bool but the call-site-arg is pointer.
23 #ifndef LO_CLANG_SHARED_PLUGINS
25 namespace
27 class PointerBool : public loplugin::FilteringPlugin<PointerBool>
29 public:
30 explicit PointerBool(loplugin::InstantiationData const& data)
31 : FilteringPlugin(data)
35 virtual void run() override
37 if (preRun())
38 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
41 bool VisitCallExpr(CallExpr const*);
43 private:
44 llvm::Optional<APSInt> getCallValue(const Expr* arg);
47 bool PointerBool::VisitCallExpr(CallExpr const* callExpr)
49 if (ignoreLocation(callExpr))
50 return true;
51 // TODO this doesn't currently work, the args and the params don't seem to line up
52 if (isa<CXXOperatorCallExpr>(callExpr))
53 return true;
54 const FunctionDecl* functionDecl;
55 if (isa<CXXMemberCallExpr>(callExpr))
57 functionDecl = dyn_cast<CXXMemberCallExpr>(callExpr)->getMethodDecl();
59 else
61 functionDecl = callExpr->getDirectCallee();
63 if (!functionDecl)
64 return true;
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())
72 continue;
73 auto arg = callExpr->getArg(i)->IgnoreImpCasts();
74 auto argTC = loplugin::TypeCheck(arg->getType());
75 if (argTC.AnyBoolean())
76 continue;
77 // sal_Bool is sometimes disguised
78 if (isa<SubstTemplateTypeParmType>(arg->getType()))
79 if (arg->getType()->getUnqualifiedDesugaredType()->isSpecificBuiltinType(
80 clang::BuiltinType::UChar))
81 continue;
82 if (arg->getType()->isDependentType())
83 continue;
84 if (arg->getType()->isIntegerType())
86 auto ret = getCallValue(arg);
87 if (ret.hasValue() && (ret.getValue() == 1 || ret.getValue() == 0))
88 continue;
89 // something like: priv->m_nLOKFeatures & LOK_FEATURE_DOCUMENT_PASSWORD
90 if (isa<BinaryOperator>(arg->IgnoreParenImpCasts()))
91 continue;
92 // something like: pbEmbolden ? FcTrue : FcFalse
93 if (isa<ConditionalOperator>(arg->IgnoreParenImpCasts()))
94 continue;
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();
102 return true;
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>();
117 APSInt x1;
118 if (compat::EvaluateAsInt(arg, x1, compiler.getASTContext()))
120 return x1;
122 return llvm::Optional<APSInt>();
125 loplugin::Plugin::Registration<PointerBool> pointerbool("pointerbool");
127 } // namespace
129 #endif // LO_CLANG_SHARED_PLUGINS
131 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */