Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / cow_wrapper.cxx
blobc98f70cbeb872c5509076d6a4769cff6a3cd2fcc
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 * Based on LLVM/Clang.
7 * This file is distributed under the University of Illinois Open Source
8 * License. See LICENSE.TXT for details.
11 #ifndef LO_CLANG_SHARED_PLUGINS
13 #include <cassert>
14 #include <string>
15 #include <iostream>
16 #include <fstream>
17 #include <set>
18 #include <unordered_set>
19 #include "plugin.hxx"
20 #include "check.hxx"
23 Look for places where we are using cow_wrapper, but we are calling a const method on the impl object
24 with a non-const pointer, which means we will unnecessarily trigger a copy.
27 namespace
29 class Cow_Wrapper : public loplugin::FilteringPlugin<Cow_Wrapper>
31 public:
32 explicit Cow_Wrapper(loplugin::InstantiationData const& data)
33 : FilteringPlugin(data)
37 virtual bool preRun() override { return true; }
39 virtual void run() override
41 if (preRun())
42 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
45 bool VisitCXXMemberCallExpr(const CXXMemberCallExpr*);
48 bool Cow_Wrapper::VisitCXXMemberCallExpr(const CXXMemberCallExpr* memberCallExpr)
50 if (ignoreLocation(memberCallExpr))
51 return true;
52 auto methodDecl = memberCallExpr->getMethodDecl();
53 if (!methodDecl || !methodDecl->isConst())
54 return true;
56 auto expr = memberCallExpr->getImplicitObjectArgument()->IgnoreImplicit()->IgnoreParens();
57 auto operatorCallExpr = dyn_cast<CXXOperatorCallExpr>(expr);
59 if (operatorCallExpr && operatorCallExpr->getOperator() == OO_Arrow)
61 auto arrowMethodDecl = dyn_cast_or_null<CXXMethodDecl>(operatorCallExpr->getDirectCallee());
62 if (!arrowMethodDecl)
63 return true;
64 if (arrowMethodDecl->isConst())
65 return true;
66 auto dc = loplugin::DeclCheck(arrowMethodDecl->getParent())
67 .Class("cow_wrapper")
68 .Namespace("o3tl")
69 .GlobalNamespace();
70 if (!dc)
71 return true;
73 else if (operatorCallExpr)
75 auto methodDecl2 = dyn_cast_or_null<CXXMethodDecl>(operatorCallExpr->getDirectCallee());
76 if (!methodDecl2)
77 return true;
78 auto dc = loplugin::DeclCheck(methodDecl2->getParent())
79 .Class("cow_wrapper")
80 .Namespace("o3tl")
81 .GlobalNamespace();
82 if (!dc)
83 return true;
85 else if (auto callExpr = dyn_cast<CallExpr>(expr))
87 if (!isa<ImplicitCastExpr>(callExpr->getCallee())) // std::as_const shows up as this
88 return true;
89 if (callExpr->getNumArgs() < 1)
90 return true;
91 auto arg0 = dyn_cast<CXXOperatorCallExpr>(callExpr->getArg(0));
92 if (!arg0)
93 return true;
94 auto starMethodDecl = dyn_cast_or_null<CXXMethodDecl>(arg0->getDirectCallee());
95 if (!starMethodDecl)
96 return true;
97 auto dc = loplugin::DeclCheck(starMethodDecl->getParent())
98 .Class("cow_wrapper")
99 .Namespace("o3tl")
100 .GlobalNamespace();
101 if (!dc)
102 return true;
104 else
105 return true;
107 report(DiagnosticsEngine::Warning,
108 "calling const method on o3tl::cow_wrapper impl class via non-const pointer, rather use "
109 "std::as_const to prevent triggering an unnecessary copy",
110 memberCallExpr->getBeginLoc())
111 << memberCallExpr->getSourceRange();
112 return true;
115 loplugin::Plugin::Registration<Cow_Wrapper> cow_wrapper("cow_wrapper", true);
117 } // namespace
119 #endif // LO_CLANG_SHARED_PLUGINS
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */