Version 24.2.2.2, tag libreoffice-24.2.2.2
[LibreOffice.git] / compilerplugins / clang / cow_wrapper.cxx
blob073a4fa9fb897f8495e773ab721d6b397a1e8e3f
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 bool shouldVisitTemplateInstantiations() const { return true; }
39 virtual bool preRun() override { return true; }
41 virtual void run() override
43 if (preRun())
44 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
47 bool VisitCXXMemberCallExpr(const CXXMemberCallExpr*);
50 bool Cow_Wrapper::VisitCXXMemberCallExpr(const CXXMemberCallExpr* memberCallExpr)
52 if (ignoreLocation(memberCallExpr))
53 return true;
54 auto methodDecl = memberCallExpr->getMethodDecl();
55 if (!methodDecl || !methodDecl->isConst())
56 return true;
58 auto expr = memberCallExpr->getImplicitObjectArgument()->IgnoreImplicit()->IgnoreParens();
59 auto operatorCallExpr = dyn_cast<CXXOperatorCallExpr>(expr);
61 if (operatorCallExpr && operatorCallExpr->getOperator() == OO_Arrow)
63 auto arrowMethodDecl = dyn_cast_or_null<CXXMethodDecl>(operatorCallExpr->getDirectCallee());
64 if (!arrowMethodDecl)
65 return true;
66 if (arrowMethodDecl->isConst())
67 return true;
68 auto dc = loplugin::DeclCheck(arrowMethodDecl->getParent())
69 .Class("cow_wrapper")
70 .Namespace("o3tl")
71 .GlobalNamespace();
72 if (!dc)
73 return true;
75 else if (operatorCallExpr)
77 auto methodDecl2 = dyn_cast_or_null<CXXMethodDecl>(operatorCallExpr->getDirectCallee());
78 if (!methodDecl2)
79 return true;
80 auto dc = loplugin::DeclCheck(methodDecl2->getParent())
81 .Class("cow_wrapper")
82 .Namespace("o3tl")
83 .GlobalNamespace();
84 if (!dc)
85 return true;
87 else if (auto callExpr = dyn_cast<CallExpr>(expr))
89 if (!isa<ImplicitCastExpr>(callExpr->getCallee())) // std::as_const shows up as this
90 return true;
91 if (callExpr->getNumArgs() < 1)
92 return true;
93 auto arg0 = dyn_cast<CXXOperatorCallExpr>(callExpr->getArg(0));
94 if (!arg0)
95 return true;
96 auto starMethodDecl = dyn_cast_or_null<CXXMethodDecl>(arg0->getDirectCallee());
97 if (!starMethodDecl)
98 return true;
99 auto dc = loplugin::DeclCheck(starMethodDecl->getParent())
100 .Class("cow_wrapper")
101 .Namespace("o3tl")
102 .GlobalNamespace();
103 if (!dc)
104 return true;
106 else
107 return true;
109 report(DiagnosticsEngine::Warning,
110 "calling const method on o3tl::cow_wrapper impl class via non-const pointer, rather use "
111 "std::as_const to prevent triggering an unnecessary copy",
112 memberCallExpr->getBeginLoc())
113 << memberCallExpr->getSourceRange();
114 return true;
117 loplugin::Plugin::Registration<Cow_Wrapper> cow_wrapper("cow_wrapper", true);
119 } // namespace
121 #endif // LO_CLANG_SHARED_PLUGINS
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */