From e557bd4eb73ecee802c724c7f7f16ad8b3793bed Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Tue, 16 Jul 2019 14:39:20 +0200 Subject: rename override plugin in preparation for using it in the shared plugin infrastructure, which needs the name of the Registration variable to match the name of the plugin, which doesn't work when the plugin is named the same as a keyword Change-Id: I40a3dc1902f2fa1efe76b5e472cd03f4b480ff34 Reviewed-on: https://gerrit.libreoffice.org/75741 Tested-by: Jenkins Reviewed-by: Noel Grandin --- compilerplugins/clang/override.cxx | 168 ------------------------------ compilerplugins/clang/overridevirtual.cxx | 168 ++++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+), 168 deletions(-) delete mode 100644 compilerplugins/clang/override.cxx create mode 100644 compilerplugins/clang/overridevirtual.cxx (limited to 'compilerplugins') diff --git a/compilerplugins/clang/override.cxx b/compilerplugins/clang/override.cxx deleted file mode 100644 index bd63792c3e28..000000000000 --- a/compilerplugins/clang/override.cxx +++ /dev/null @@ -1,168 +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 -#include -#include -#include -#include - -#include "clang/AST/Attr.h" - -#include "plugin.hxx" - -namespace { - -class Override: - public loplugin::FilteringRewritePlugin -{ -public: - explicit Override(loplugin::InstantiationData const & data): - FilteringRewritePlugin(data) {} - - virtual void run() override; - - bool VisitCXXMethodDecl(CXXMethodDecl const * decl); - -private: - std::set insertions_; -}; - -void Override::run() { - if (compiler.getLangOpts().CPlusPlus - && compiler.getPreprocessor().getIdentifierInfo( - "LIBO_INTERNAL_ONLY")->hasMacroDefinition()) - { - TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); - } -} -bool Override::VisitCXXMethodDecl(CXXMethodDecl const * decl) { - // As a heuristic, ignore declarations where the name is spelled out in an - // ignored location; that e.g. handles uses of the Q_OBJECT macro from - // external QtCore/qobjectdefs.h: - if (ignoreLocation(decl) || !decl->isFirstDecl() - || decl->begin_overridden_methods() == decl->end_overridden_methods() - || decl->hasAttr() - || ignoreLocation( - compiler.getSourceManager().getSpellingLoc( - decl->getNameInfo().getLoc()))) - { - return true; - } - std::string over( - isInUnoIncludeFile(decl->getSourceRange().getBegin()) - ? "SAL_OVERRIDE" : "override"); - if (rewriter != nullptr) { - // In void MACRO(...); getSourceRange().getEnd() would (erroneously?) - // point at "MACRO" rather than ")", so make the loop always terminate - // at the first ";" or "{" instead of getSourceRange().getEnd(): - unsigned parens = 0; - bool seenSpace = false; - //TODO: Whether to add a space after an inserted "SAL_OVERRIDE" should - // depend on the following token at the spelling location where - // "SAL_OVERRIDE" is inserted, not on the following token in the fully- - // macro-expanded view: - bool addSpace = bool(); - SourceLocation loc; - for (SourceLocation l(decl->getSourceRange().getBegin());;) { - SourceLocation sl(compiler.getSourceManager().getSpellingLoc(l)); - unsigned n = Lexer::MeasureTokenLength( - sl, compiler.getSourceManager(), compiler.getLangOpts()); - StringRef s(compiler.getSourceManager().getCharacterData(sl), n); - //TODO: Looks like a Clang bug that in some cases like - // (filter/source/svg/svgexport.cxx) - // - // | #define TEXT_FIELD_GET_CLASS_NAME_METHOD( class_name ) \ | - // | virtual OUString getClassName() const \ | - // | { \ | - // | static const char className[] = #class_name; \ | - // | return OUString( className ); \ | - // | } | - // | | - // | TEXT_FIELD_GET_CLASS_NAME_METHOD( TextField ) | - // - // where "\" is followed directly by a real token without - // intervening whitespace, tokens "\virtual" and "\{" are - // reported: - if (s.startswith("\\\n")) { - s = s.drop_front(2); - } - if (parens == 0) { - if (s == "=" || s == "{") { - if (!seenSpace) { - addSpace = true; - } - break; - } - if (s == ";") { - break; - } - } - if (s == "(") { - assert(parens < std::numeric_limits::max()); - ++parens; - } else if (s == ")") { - assert(parens != 0); - --parens; - } - if (s.empty()) { - if (!seenSpace) { - addSpace = false; - } - seenSpace = true; - } else if (s.startswith("/*") || s.startswith("//") || s == "\\") { - if (!seenSpace) { - addSpace = true; - } - seenSpace = true; - } else { - seenSpace = false; - addSpace = false; - loc = sl; - } - if (l.isMacroID() - && compiler.getSourceManager().isAtEndOfImmediateMacroExpansion( - l, &l)) - { - n = Lexer::MeasureTokenLength( - compiler.getSourceManager().getSpellingLoc(l), - compiler.getSourceManager(), compiler.getLangOpts()); - } - l = l.getLocWithOffset(std::max(n, 1)); - } - assert(loc.isValid()); - if (!insertions_.insert(loc).second - || insertTextAfterToken( - loc, - std::string(" ") + over + std::string(addSpace ? " " : ""))) - { - return true; - } - } - report( - DiagnosticsEngine::Warning, - ("overriding virtual function declaration not marked '%0'"), - decl->getLocation()) - << over << decl->getSourceRange(); - for (auto i = decl->begin_overridden_methods(); - i != decl->end_overridden_methods(); ++i) - { - report( - DiagnosticsEngine::Note, "overridden declaration is here", - (*i)->getLocation()) - << (*i)->getSourceRange(); - } - return true; -} - -loplugin::Plugin::Registration X("override", true); - -} - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/overridevirtual.cxx b/compilerplugins/clang/overridevirtual.cxx new file mode 100644 index 000000000000..9b31035d2156 --- /dev/null +++ b/compilerplugins/clang/overridevirtual.cxx @@ -0,0 +1,168 @@ +/* -*- 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 +#include +#include +#include + +#include "clang/AST/Attr.h" + +#include "plugin.hxx" + +namespace { + +class OverrideVirtual: + public loplugin::FilteringRewritePlugin +{ +public: + explicit OverrideVirtual(loplugin::InstantiationData const & data): + FilteringRewritePlugin(data) {} + + virtual void run() override; + + bool VisitCXXMethodDecl(CXXMethodDecl const * decl); + +private: + std::set insertions_; +}; + +void OverrideVirtual::run() { + if (compiler.getLangOpts().CPlusPlus + && compiler.getPreprocessor().getIdentifierInfo( + "LIBO_INTERNAL_ONLY")->hasMacroDefinition()) + { + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } +} +bool OverrideVirtual::VisitCXXMethodDecl(CXXMethodDecl const * decl) { + // As a heuristic, ignore declarations where the name is spelled out in an + // ignored location; that e.g. handles uses of the Q_OBJECT macro from + // external QtCore/qobjectdefs.h: + if (ignoreLocation(decl) || !decl->isFirstDecl() + || decl->begin_overridden_methods() == decl->end_overridden_methods() + || decl->hasAttr() + || ignoreLocation( + compiler.getSourceManager().getSpellingLoc( + decl->getNameInfo().getLoc()))) + { + return true; + } + std::string over( + isInUnoIncludeFile(decl->getSourceRange().getBegin()) + ? "SAL_OVERRIDE" : "override"); + if (rewriter != nullptr) { + // In void MACRO(...); getSourceRange().getEnd() would (erroneously?) + // point at "MACRO" rather than ")", so make the loop always terminate + // at the first ";" or "{" instead of getSourceRange().getEnd(): + unsigned parens = 0; + bool seenSpace = false; + //TODO: Whether to add a space after an inserted "SAL_OVERRIDE" should + // depend on the following token at the spelling location where + // "SAL_OVERRIDE" is inserted, not on the following token in the fully- + // macro-expanded view: + bool addSpace = bool(); + SourceLocation loc; + for (SourceLocation l(decl->getSourceRange().getBegin());;) { + SourceLocation sl(compiler.getSourceManager().getSpellingLoc(l)); + unsigned n = Lexer::MeasureTokenLength( + sl, compiler.getSourceManager(), compiler.getLangOpts()); + StringRef s(compiler.getSourceManager().getCharacterData(sl), n); + //TODO: Looks like a Clang bug that in some cases like + // (filter/source/svg/svgexport.cxx) + // + // | #define TEXT_FIELD_GET_CLASS_NAME_METHOD( class_name ) \ | + // | virtual OUString getClassName() const \ | + // | { \ | + // | static const char className[] = #class_name; \ | + // | return OUString( className ); \ | + // | } | + // | | + // | TEXT_FIELD_GET_CLASS_NAME_METHOD( TextField ) | + // + // where "\" is followed directly by a real token without + // intervening whitespace, tokens "\virtual" and "\{" are + // reported: + if (s.startswith("\\\n")) { + s = s.drop_front(2); + } + if (parens == 0) { + if (s == "=" || s == "{") { + if (!seenSpace) { + addSpace = true; + } + break; + } + if (s == ";") { + break; + } + } + if (s == "(") { + assert(parens < std::numeric_limits::max()); + ++parens; + } else if (s == ")") { + assert(parens != 0); + --parens; + } + if (s.empty()) { + if (!seenSpace) { + addSpace = false; + } + seenSpace = true; + } else if (s.startswith("/*") || s.startswith("//") || s == "\\") { + if (!seenSpace) { + addSpace = true; + } + seenSpace = true; + } else { + seenSpace = false; + addSpace = false; + loc = sl; + } + if (l.isMacroID() + && compiler.getSourceManager().isAtEndOfImmediateMacroExpansion( + l, &l)) + { + n = Lexer::MeasureTokenLength( + compiler.getSourceManager().getSpellingLoc(l), + compiler.getSourceManager(), compiler.getLangOpts()); + } + l = l.getLocWithOffset(std::max(n, 1)); + } + assert(loc.isValid()); + if (!insertions_.insert(loc).second + || insertTextAfterToken( + loc, + std::string(" ") + over + std::string(addSpace ? " " : ""))) + { + return true; + } + } + report( + DiagnosticsEngine::Warning, + ("overriding virtual function declaration not marked '%0'"), + decl->getLocation()) + << over << decl->getSourceRange(); + for (auto i = decl->begin_overridden_methods(); + i != decl->end_overridden_methods(); ++i) + { + report( + DiagnosticsEngine::Note, "overridden declaration is here", + (*i)->getLocation()) + << (*i)->getSourceRange(); + } + return true; +} + +loplugin::Plugin::Registration X("overridevirtual", true); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit