bump product version to 6.4.0.3
[LibreOffice.git] / compilerplugins / clang / getstr.cxx
blobaba53bd6e2e5747b46354f03b0771a3a1a036979
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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 #ifndef LO_CLANG_SHARED_PLUGINS
12 #include <cassert>
13 #include <stack>
15 #include "check.hxx"
16 #include "compat.hxx"
17 #include "plugin.hxx"
19 // Find matches of
21 // ... << s.getStr()
23 // (for the rtl string classes) that can be written as just
25 // ... << s
27 namespace
29 class GetStr final : public loplugin::FilteringPlugin<GetStr>
31 public:
32 explicit GetStr(loplugin::InstantiationData const& data)
33 : FilteringPlugin(data)
37 bool PreTraverseFunctionDecl(FunctionDecl* decl)
39 functions_.push(decl);
40 return true;
43 bool PostTraverseFunctionDecl(FunctionDecl*, bool)
45 assert(!functions_.empty());
46 functions_.pop();
47 return true;
50 bool TraverseFunctionDecl(FunctionDecl* decl)
52 bool ret = true;
53 if (PreTraverseFunctionDecl(decl))
55 ret = FilteringPlugin::TraverseFunctionDecl(decl);
56 PostTraverseFunctionDecl(decl, ret);
58 return ret;
61 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr* expr)
63 if (ignoreLocation(expr))
65 return true;
67 if (expr->getOperator() != OO_LessLess)
69 return true;
71 assert(expr->getNumArgs() == 2);
72 if (!loplugin::TypeCheck(expr->getArg(0)->getType())
73 .ClassOrStruct("basic_ostream")
74 .StdNamespace()) //TODO: check template args
76 return true;
78 auto const arg1 = expr->getArg(1);
79 auto const e = dyn_cast<CXXMemberCallExpr>(arg1->IgnoreParenImpCasts());
80 if (e == nullptr)
82 return true;
84 bool castToVoid = false;
85 if (auto const ic = dyn_cast<ImplicitCastExpr>(arg1))
87 if (loplugin::TypeCheck(arg1->getType()).Pointer().Void())
89 castToVoid = true;
92 auto const t = compat::getObjectType(e);
93 auto const tc = loplugin::TypeCheck(t);
94 if (!(tc.Class("OString").Namespace("rtl").GlobalNamespace()
95 || tc.Class("OUString").Namespace("rtl").GlobalNamespace()
96 || (castToVoid
97 && (tc.Class("OStringBuffer").Namespace("rtl").GlobalNamespace()
98 || tc.Class("OUStringBuffer").Namespace("rtl").GlobalNamespace()))))
100 return true;
102 if (!loplugin::DeclCheck(e->getMethodDecl()).Function("getStr"))
104 return true;
106 if (castToVoid)
108 report(DiagnosticsEngine::Warning,
109 ("suspicious use of 'getStr' on an object of type %0; the result is implicitly"
110 " cast to a void pointer in a call of 'operator <<'"),
111 e->getExprLoc())
112 << t.getLocalUnqualifiedType() << expr->getSourceRange();
113 return true;
115 if (!functions_.empty())
117 // Filter out occurrences of `s << t.getStr()` in the implementation of
118 // `operator <<(std::basic_ostream<...> & s, T const & t)`:
119 auto const fd = functions_.top();
120 if (fd->getOverloadedOperator() == OO_LessLess)
122 assert(fd->getNumParams() == 2);
123 if (loplugin::TypeCheck(fd->getParamDecl(0)->getType())
124 .LvalueReference()
125 .NonConstVolatile()
126 .TemplateSpecializationClass()
127 .ClassOrStruct("basic_ostream")
128 .StdNamespace()) //TODO: check template args
130 if (auto const t2
131 = fd->getParamDecl(1)->getType()->getAs<LValueReferenceType>())
133 auto const t3 = t2->getPointeeType();
134 if (t3.isConstQualified() && !t3.isVolatileQualified()
135 && (t3.getCanonicalType().getTypePtr()
136 == t.getCanonicalType().getTypePtr()))
138 return true;
144 report(DiagnosticsEngine::Warning,
145 ("directly use object of type %0 in a call of 'operator <<', instead of calling"
146 " 'getStr' first"),
147 e->getExprLoc())
148 << t.getLocalUnqualifiedType() << expr->getSourceRange();
149 return true;
152 bool preRun() override { return compiler.getLangOpts().CPlusPlus; }
154 private:
155 std::stack<FunctionDecl*> functions_;
157 void run() override
159 if (preRun())
161 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
166 loplugin::Plugin::Registration<GetStr> getstr("getstr");
169 #endif
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */