libreoffice/compilerplugins/clang/functionaddress.hxx
Stephan Bergmann 5d546de67b Adapt to Clang 12 trunk RecursiveASTVisitor change
<https://github.com/llvm/llvm-project/commit/
5689b38c6a4220cc5f6ba68a56486229b10071bf> "Removed a RecursiveASTVisitor feature
to visit operator kinds with different methods".

That change is incompatible in that before the change individual TraverseUnary*
and TraverseBin* functions were called, while now TraverseUnaryOperator and
TraverseBinaryOperator/TraverseCompoundAssignOperator are called for all the
different operators.  Fixed that with a few #if for the non-shared plugins, but
that doesn't work for the shared plugin.  So made the two affected plugins non-
shared for now and left a better fix as a TODO.

Change-Id: I5b87d329ae2c4c93bf605bb1ecc9641039f014a3
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/99000
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2020-07-19 23:05:57 +02:00

124 lines
3.6 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef INCLUDED_COMPILERPLUGINS_CLANG_FUNCTIONADDRESS_HXX
#define INCLUDED_COMPILERPLUGINS_CLANG_FUNCTIONADDRESS_HXX
#include <clang/AST/RecursiveASTVisitor.h>
#include <unordered_set>
#include "plugin.hxx"
/**
* Common code for checking if the address of a function was taken.
*/
namespace loplugin {
template<typename Base>
class FunctionAddress : public Base
{
public:
explicit FunctionAddress( const InstantiationData& data ) : Base(data) {}
bool TraverseCallExpr(CallExpr * expr) {
auto const saved = callee_;
callee_ = expr->getCallee();
auto const ret = Base::TraverseCallExpr(expr);
callee_ = saved;
return ret;
}
bool TraverseCXXOperatorCallExpr(CXXOperatorCallExpr * expr) {
auto const saved = callee_;
callee_ = expr->getCallee();
auto const ret = Base::TraverseCXXOperatorCallExpr(expr);
callee_ = saved;
return ret;
}
bool TraverseCXXMemberCallExpr(CXXMemberCallExpr * expr) {
auto const saved = callee_;
callee_ = expr->getCallee();
auto const ret = Base::TraverseCXXMemberCallExpr(expr);
callee_ = saved;
return ret;
}
bool TraverseCUDAKernelCallExpr(CUDAKernelCallExpr * expr) {
auto const saved = callee_;
callee_ = expr->getCallee();
auto const ret = Base::TraverseCUDAKernelCallExpr(expr);
callee_ = saved;
return ret;
}
bool TraverseUserDefinedLiteral(UserDefinedLiteral * expr) {
auto const saved = callee_;
callee_ = expr->getCallee();
auto const ret = Base::TraverseUserDefinedLiteral(expr);
callee_ = saved;
return ret;
}
bool VisitImplicitCastExpr(ImplicitCastExpr const * expr) {
if (expr == callee_) {
return true;
}
if (this->ignoreLocation(expr)) {
return true;
}
if (expr->getCastKind() != CK_FunctionToPointerDecay) {
return true;
}
auto const dre = dyn_cast<DeclRefExpr>(
expr->getSubExpr()->IgnoreParens());
if (dre == nullptr) {
return true;
}
auto const fd = dyn_cast<FunctionDecl>(dre->getDecl());
if (fd == nullptr) {
return true;
}
ignoredFunctions_.insert(fd->getCanonicalDecl());
return true;
}
bool VisitUnaryOperator(UnaryOperator * expr) {
if (expr->getOpcode() != UO_AddrOf) {
return Base::VisitUnaryOperator(expr);
}
if (this->ignoreLocation(expr)) {
return true;
}
auto const dre = dyn_cast<DeclRefExpr>(
expr->getSubExpr()->IgnoreParenImpCasts());
if (dre == nullptr) {
return true;
}
auto const fd = dyn_cast<FunctionDecl>(dre->getDecl());
if (fd == nullptr) {
return true;
}
ignoredFunctions_.insert(fd->getCanonicalDecl());
return true;
}
protected:
std::unordered_set<FunctionDecl const *> const & getFunctionsWithAddressTaken() { return ignoredFunctions_; }
private:
std::unordered_set<FunctionDecl const *> ignoredFunctions_;
Expr const * callee_ = nullptr;
};
}
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */