bump product version to 6.4.0.3
[LibreOffice.git] / compilerplugins / clang / functionaddress.hxx
blob93241ea5b4b0f9115ad55c32fe0976dd55d7a2c9
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 #ifndef INCLUDED_COMPILERPLUGINS_CLANG_FUNCTIONADDRESS_HXX
11 #define INCLUDED_COMPILERPLUGINS_CLANG_FUNCTIONADDRESS_HXX
13 #include <clang/AST/RecursiveASTVisitor.h>
14 #include <unordered_set>
15 #include "plugin.hxx"
17 /**
18 * Common code for checking if the address of a function was taken.
20 namespace loplugin {
22 template<typename Base>
23 class FunctionAddress : public Base
25 public:
26 explicit FunctionAddress( const InstantiationData& data ) : Base(data) {}
28 bool TraverseCallExpr(CallExpr * expr) {
29 auto const saved = callee_;
30 callee_ = expr->getCallee();
31 auto const ret = Base::TraverseCallExpr(expr);
32 callee_ = saved;
33 return ret;
36 bool TraverseCXXOperatorCallExpr(CXXOperatorCallExpr * expr) {
37 auto const saved = callee_;
38 callee_ = expr->getCallee();
39 auto const ret = Base::TraverseCXXOperatorCallExpr(expr);
40 callee_ = saved;
41 return ret;
44 bool TraverseCXXMemberCallExpr(CXXMemberCallExpr * expr) {
45 auto const saved = callee_;
46 callee_ = expr->getCallee();
47 auto const ret = Base::TraverseCXXMemberCallExpr(expr);
48 callee_ = saved;
49 return ret;
52 bool TraverseCUDAKernelCallExpr(CUDAKernelCallExpr * expr) {
53 auto const saved = callee_;
54 callee_ = expr->getCallee();
55 auto const ret = Base::TraverseCUDAKernelCallExpr(expr);
56 callee_ = saved;
57 return ret;
60 bool TraverseUserDefinedLiteral(UserDefinedLiteral * expr) {
61 auto const saved = callee_;
62 callee_ = expr->getCallee();
63 auto const ret = Base::TraverseUserDefinedLiteral(expr);
64 callee_ = saved;
65 return ret;
68 bool VisitImplicitCastExpr(ImplicitCastExpr const * expr) {
69 if (expr == callee_) {
70 return true;
72 if (this->ignoreLocation(expr)) {
73 return true;
75 if (expr->getCastKind() != CK_FunctionToPointerDecay) {
76 return true;
78 auto const dre = dyn_cast<DeclRefExpr>(
79 expr->getSubExpr()->IgnoreParens());
80 if (dre == nullptr) {
81 return true;
83 auto const fd = dyn_cast<FunctionDecl>(dre->getDecl());
84 if (fd == nullptr) {
85 return true;
87 ignoredFunctions_.insert(fd->getCanonicalDecl());
88 return true;
91 bool VisitUnaryAddrOf(UnaryOperator const * expr) {
92 if (this->ignoreLocation(expr)) {
93 return true;
95 auto const dre = dyn_cast<DeclRefExpr>(
96 expr->getSubExpr()->IgnoreParenImpCasts());
97 if (dre == nullptr) {
98 return true;
100 auto const fd = dyn_cast<FunctionDecl>(dre->getDecl());
101 if (fd == nullptr) {
102 return true;
104 ignoredFunctions_.insert(fd->getCanonicalDecl());
105 return true;
108 protected:
109 std::unordered_set<FunctionDecl const *> const & getFunctionsWithAddressTaken() { return ignoredFunctions_; }
111 private:
112 std::unordered_set<FunctionDecl const *> ignoredFunctions_;
113 Expr const * callee_ = nullptr;
118 #endif
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */