Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / getstr.cxx
blob671699e7355115d57a0339b24b94a0ada7119a5d
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 "plugin.hxx"
18 // Find matches of
20 // ... << s.getStr()
22 // (for the rtl string classes) that can be written as just
24 // ... << s
26 namespace
28 class GetStr final : public loplugin::FilteringPlugin<GetStr>
30 public:
31 explicit GetStr(loplugin::InstantiationData const& data)
32 : FilteringPlugin(data)
36 bool PreTraverseFunctionDecl(FunctionDecl* decl)
38 functions_.push(decl);
39 return true;
42 bool PostTraverseFunctionDecl(FunctionDecl*, bool)
44 assert(!functions_.empty());
45 functions_.pop();
46 return true;
49 bool TraverseFunctionDecl(FunctionDecl* decl)
51 bool ret = true;
52 if (PreTraverseFunctionDecl(decl))
54 ret = FilteringPlugin::TraverseFunctionDecl(decl);
55 PostTraverseFunctionDecl(decl, ret);
57 return ret;
60 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr* expr)
62 if (ignoreLocation(expr))
64 return true;
66 if (expr->getOperator() != OO_LessLess)
68 return true;
70 assert(expr->getNumArgs() == 2);
71 if (!loplugin::TypeCheck(expr->getArg(0)->getType())
72 .ClassOrStruct("basic_ostream")
73 .StdNamespace()) //TODO: check template args
75 return true;
77 auto const arg1 = expr->getArg(1);
78 auto const e = dyn_cast<CXXMemberCallExpr>(arg1->IgnoreParenImpCasts());
79 if (e == nullptr)
81 return true;
83 bool castToVoid = false;
84 if (auto const ic = dyn_cast<ImplicitCastExpr>(arg1))
86 if (loplugin::TypeCheck(arg1->getType()).Pointer().Void())
88 castToVoid = true;
91 auto const t = e->getObjectType();
92 auto const tc = loplugin::TypeCheck(t);
93 if (!(tc.Class("OString").Namespace("rtl").GlobalNamespace()
94 || tc.Class("OUString").Namespace("rtl").GlobalNamespace()
95 || (castToVoid
96 && (tc.Class("OStringBuffer").Namespace("rtl").GlobalNamespace()
97 || tc.Class("OUStringBuffer").Namespace("rtl").GlobalNamespace()))))
99 return true;
101 if (!loplugin::DeclCheck(e->getMethodDecl()).Function("getStr"))
103 return true;
105 if (castToVoid)
107 report(DiagnosticsEngine::Warning,
108 ("suspicious use of 'getStr' on an object of type %0; the result is implicitly"
109 " cast to a void pointer in a call of 'operator <<'"),
110 e->getExprLoc())
111 << t.getLocalUnqualifiedType() << expr->getSourceRange();
112 return true;
114 if (!functions_.empty())
116 // Filter out occurrences of `s << t.getStr()` in the implementation of
117 // `operator <<(std::basic_ostream<...> & s, T const & t)`:
118 auto const fd = functions_.top();
119 if (fd->getOverloadedOperator() == OO_LessLess)
121 assert(fd->getNumParams() == 2);
122 if (loplugin::TypeCheck(fd->getParamDecl(0)->getType())
123 .LvalueReference()
124 .NonConstVolatile()
125 .TemplateSpecializationClass()
126 .ClassOrStruct("basic_ostream")
127 .StdNamespace()) //TODO: check template args
129 if (auto const t2
130 = fd->getParamDecl(1)->getType()->getAs<LValueReferenceType>())
132 auto const t3 = t2->getPointeeType();
133 if (t3.isConstQualified() && !t3.isVolatileQualified()
134 && (t3.getCanonicalType().getTypePtr()
135 == t.getCanonicalType().getTypePtr()))
137 return true;
143 report(DiagnosticsEngine::Warning,
144 ("directly use object of type %0 in a call of 'operator <<', instead of calling"
145 " 'getStr' first"),
146 e->getExprLoc())
147 << t.getLocalUnqualifiedType() << expr->getSourceRange();
148 return true;
151 bool preRun() override { return compiler.getLangOpts().CPlusPlus; }
153 private:
154 std::stack<FunctionDecl*> functions_;
156 void run() override
158 if (preRun())
160 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
165 loplugin::Plugin::Registration<GetStr> getstr("getstr");
168 #endif
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */