bump product version to 6.3.0.0.beta1
[LibreOffice.git] / compilerplugins / clang / store / returnunique.cxx
blob913c043a471261ee2fee89ec15f0ae10855f3846
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 // Find places where a std::unique_ptr is release()'ed and returned as a raw
11 // pointer. Some occurrences of that might better be rewritten to return the
12 // unique_ptr is returned directly. (But other occurrences might be fine the
13 // way they are, hence place this plugin into store/).
15 #include <memory>
16 #include "plugin.hxx"
18 namespace {
20 class ReturnUnique:
21 public loplugin::FilteringPlugin<ReturnUnique>
23 public:
24 explicit ReturnUnique(InstantiationData const & data): FilteringPlugin(data) {}
26 void run() override {
27 if (compiler.getLangOpts().CPlusPlus) {
28 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
32 bool VisitReturnStmt(ReturnStmt const * stmt);
35 bool ReturnUnique::VisitReturnStmt(ReturnStmt const * stmt) {
36 if (ignoreLocation(stmt)) {
37 return true;
39 auto const e1 = stmt->getRetValue();
40 if (e1 == nullptr) {
41 return true;
43 auto const e2 = dyn_cast<CXXMemberCallExpr>(e1->IgnoreParenImpCasts());
44 if (e2 == nullptr) {
45 return true;
47 auto const d1 = e2->getMethodDecl();
48 if (d1 == nullptr) { // call via ptr to member
49 return true;
51 auto const d2 = d1->getParent();
52 assert(d2 != nullptr);
53 assert(d2->getParent() != nullptr);
54 auto const d3 = dyn_cast<NamespaceDecl>(d2->getParent());
55 if (d3 == nullptr
56 /* || dyn_cast<TranslationUnitDecl>(d3->getParent()) == nullptr */)
58 return true;
60 auto const id3 = d3->getIdentifier();
61 if (id3 == nullptr /* || id3->getName() != "std" */) {
62 return true;
64 auto const id2 = d2->getIdentifier();
65 if (id2 == nullptr || id2->getName() != "unique_ptr") {
66 return true;
68 auto const id1 = d1->getIdentifier();
69 if (id1 == nullptr || id1->getName() != "release") {
70 return true;
72 report(
73 DiagnosticsEngine::Warning, "return std::unique_ptr::release",
74 e2->getLocStart())
75 << stmt->getSourceRange();
76 return true;
79 loplugin::Plugin::Registration<ReturnUnique> X("returnunique");
83 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */