From 6810f00d09679644ec5971b235a0ec01dc572a8b Mon Sep 17 00:00:00 2001 From: Stephan Bergmann Date: Thu, 25 Aug 2016 17:16:14 +0200 Subject: loplugin:staticaccess: Extend loplugin:staticcall to cover all access... to static members (data, in addition to function) via class member access syntax. Also covers the (somewhat obscure) access to enumerator members. Change-Id: Iec54b8df2fdb423c0caf21a0dd0f9fe8fdf33897 --- compilerplugins/clang/staticaccess.cxx | 76 ++++++++++++++++++++++++++++++++++ compilerplugins/clang/staticcall.cxx | 49 ---------------------- 2 files changed, 76 insertions(+), 49 deletions(-) create mode 100644 compilerplugins/clang/staticaccess.cxx delete mode 100644 compilerplugins/clang/staticcall.cxx (limited to 'compilerplugins') diff --git a/compilerplugins/clang/staticaccess.cxx b/compilerplugins/clang/staticaccess.cxx new file mode 100644 index 000000000000..7b825f39bafa --- /dev/null +++ b/compilerplugins/clang/staticaccess.cxx @@ -0,0 +1,76 @@ +/* -*- 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/. + */ + +#include + +#include "plugin.hxx" + +namespace { + +bool isStatic(ValueDecl const * decl, bool * memberEnumerator) { + assert(memberEnumerator != nullptr); + // clang::MemberExpr::getMemberDecl is documented to return either a + // FieldDecl or a CXXMethodDecl, but can apparently also return a VarDecl + // (as C++ static data members are modeled by VarDecl, not FieldDecl) or an + // EnumConstantDecl (struct { enum {E}; } s; s.E;): + auto fd = dyn_cast(decl); + if (fd != nullptr) { + *memberEnumerator = false; + return false; + } + auto vd = dyn_cast(decl); + if (vd != nullptr) { + *memberEnumerator = false; + return vd->isStaticDataMember(); + } + auto md = dyn_cast(decl); + if (md != nullptr) { + *memberEnumerator = false; + return md->isStatic(); + } + assert(dyn_cast(decl) != nullptr); + *memberEnumerator = true; + return true; +} + +class StaticAccess: + public RecursiveASTVisitor, public loplugin::Plugin +{ +public: + explicit StaticAccess(InstantiationData const & data): Plugin(data) {} + + void run() override + { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); } + + bool VisitMemberExpr(MemberExpr const * expr); +}; + +bool StaticAccess::VisitMemberExpr(MemberExpr const * expr) { + if (ignoreLocation(expr)) { + return true; + } + auto decl = expr->getMemberDecl(); + bool me; + if (!isStatic(decl, &me)) { + return true; + } + report( + DiagnosticsEngine::Warning, + ("accessing %select{static class member|member enumerator}0 through" + " class member access syntax, use a qualified name like '%1' instead"), + expr->getLocStart()) + << me << decl->getQualifiedNameAsString() << expr->getSourceRange(); + return true; +} + +loplugin::Plugin::Registration X("staticaccess"); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/staticcall.cxx b/compilerplugins/clang/staticcall.cxx deleted file mode 100644 index 8bbfcb6b38a8..000000000000 --- a/compilerplugins/clang/staticcall.cxx +++ /dev/null @@ -1,49 +0,0 @@ -/* -*- 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/. - */ - -#include "plugin.hxx" - -namespace { - -class StaticCall: - public RecursiveASTVisitor, public loplugin::Plugin -{ -public: - explicit StaticCall(InstantiationData const & data): Plugin(data) {} - - void run() override - { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); } - - bool VisitCallExpr(CallExpr const * expr); -}; - -bool StaticCall::VisitCallExpr(CallExpr const * expr) { - if (ignoreLocation(expr) - || !isa(expr->getCallee()->IgnoreImpCasts())) - { - return true; - } - CXXMethodDecl const * decl = dyn_cast_or_null( - expr->getDirectCallee()); - if (decl != nullptr && decl->isStatic()) { - report( - DiagnosticsEngine::Warning, - ("calling static member function through member call syntax, use" - " '%0' instead"), - expr->getLocStart()) - << decl->getQualifiedNameAsString() << expr->getSourceRange(); - } - return true; -} - -loplugin::Plugin::Registration X("staticcall"); - -} - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit