LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / compilerplugins / clang / pointerbool.cxx
blob6886e1fac63f6cf67c2c93dfcd5508e300d96d63
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 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*);
48 private:
49 llvm::Optional<APSInt> getCallValue(const Expr* arg);
50 std::vector<FunctionDecl*> functions_;
53 bool PointerBool::PreTraverseFunctionDecl(FunctionDecl* decl)
55 functions_.push_back(decl);
56 return true;
59 bool PointerBool::PostTraverseFunctionDecl(FunctionDecl*, bool)
61 assert(!functions_.empty());
62 functions_.pop_back();
63 return true;
66 bool PointerBool::TraverseFunctionDecl(FunctionDecl* decl)
68 bool ret = true;
69 if (PreTraverseFunctionDecl(decl))
71 ret = FilteringPlugin::TraverseFunctionDecl(decl);
72 PostTraverseFunctionDecl(decl, ret);
74 return ret;
77 bool PointerBool::VisitCallExpr(CallExpr const* callExpr)
79 if (ignoreLocation(callExpr))
80 return true;
81 // TODO this doesn't currently work, the args and the params don't seem to line up
82 if (isa<CXXOperatorCallExpr>(callExpr))
83 return true;
84 const FunctionDecl* functionDecl;
85 if (isa<CXXMemberCallExpr>(callExpr))
87 functionDecl = dyn_cast<CXXMemberCallExpr>(callExpr)->getMethodDecl();
89 else
91 functionDecl = callExpr->getDirectCallee();
93 if (!functionDecl)
94 return true;
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())
102 continue;
103 auto arg = callExpr->getArg(i)->IgnoreImpCasts();
104 auto argTC = loplugin::TypeCheck(arg->getType());
105 if (argTC.AnyBoolean())
106 continue;
107 // sal_Bool is sometimes disguised
108 if (isa<SubstTemplateTypeParmType>(arg->getType()))
109 if (arg->getType()->getUnqualifiedDesugaredType()->isSpecificBuiltinType(
110 clang::BuiltinType::UChar))
111 continue;
112 if (arg->getType()->isDependentType())
113 continue;
114 if (arg->getType()->isIntegerType())
116 auto ret = getCallValue(arg);
117 if (ret.hasValue() && (ret.getValue() == 1 || ret.getValue() == 0))
118 continue;
119 // something like: priv->m_nLOKFeatures & LOK_FEATURE_DOCUMENT_PASSWORD
120 if (isa<BinaryOperator>(arg->IgnoreParenImpCasts()))
121 continue;
122 // something like: pbEmbolden ? FcTrue : FcFalse
123 if (isa<ConditionalOperator>(arg->IgnoreParenImpCasts()))
124 continue;
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());
139 return true;
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>();
154 APSInt x1;
155 if (compat::EvaluateAsInt(arg, x1, compiler.getASTContext()))
157 return x1;
159 return llvm::Optional<APSInt>();
162 loplugin::Plugin::Registration<PointerBool> pointerbool("pointerbool");
164 } // namespace
166 #endif // LO_CLANG_SHARED_PLUGINS
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */