Files
libreoffice/compilerplugins/clang/reservedid.cxx

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

329 lines
13 KiB
C++
Raw Normal View History

/* -*- 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/.
*/
make (some) clang plugins share the same RecursiveASTVisitor Each plugin currently uses its own recursive AST run, which adds up. This patch adds another shared plugin which internally contains all (suitable) plugins and dispatches to them from the same one recursive run. This patch converts ~25 plugins and for starmath's accessibility.cxx reduces clang build time from 5.43s to 5.14s (and it's 4.39s without any plugins). As there are almost 50 more plugins to go, this can theoretically result in 4.56s final time, although probably not all plugins can be that easily converted, if at all. This mostly requires very little change in many plugins (see e.g. BadStatics), some even work without any functionality change (e.g. CharRightShift). Traverse* calls require some changes but are often not that difficult. WalkUp* probably can't be supported, although some plugins can(?) possibly be adjusted to not rely on them. And of course some plugins can be left as they are, using their own recursive run. See description at the top of generator.cxx for description of how to convert a plugin. The sharedvisitor.cxx source is generated based on scanning relevant plugin sources using a clang-based scanner/generator. The generated source is intentionally included instead of getting always generated, as the generating currently takes some time, so it should get updated in git whenever a change in a plugin triggers a source change in it. Change-Id: Ia0d2e3a5a464659503dbb4ed6c20b6cc89b4de01 Reviewed-on: https://gerrit.libreoffice.org/68026 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk> Reviewed-by: Luboš Luňák <l.lunak@collabora.com>
2019-03-07 14:31:17 +01:00
#ifndef LO_CLANG_SHARED_PLUGINS
#include <algorithm>
#include <cassert>
#include <limits>
#include <set>
#include <string>
#include "clang/AST/Attr.h"
#include "config_clang.h"
#include "plugin.hxx"
namespace {
bool isJniFunction(NamedDecl const * decl) {
auto const fdecl = dyn_cast<FunctionDecl>(decl);
if (fdecl == nullptr
|| !(decl->getDeclContext()->getDeclKind() == Decl::LinkageSpec
&& decl->getDeclContext()->getParent()->isTranslationUnit())
|| !fdecl->isExternC())
{
return false;
}
auto const id = decl->getIdentifier();
return id != nullptr && id->getName().startswith("Java_");
}
class ReservedId:
public loplugin::FilteringPlugin<ReservedId>
{
public:
explicit ReservedId(loplugin::InstantiationData const & data): FilteringPlugin(data)
{}
void run() override;
make (some) clang plugins share the same RecursiveASTVisitor Each plugin currently uses its own recursive AST run, which adds up. This patch adds another shared plugin which internally contains all (suitable) plugins and dispatches to them from the same one recursive run. This patch converts ~25 plugins and for starmath's accessibility.cxx reduces clang build time from 5.43s to 5.14s (and it's 4.39s without any plugins). As there are almost 50 more plugins to go, this can theoretically result in 4.56s final time, although probably not all plugins can be that easily converted, if at all. This mostly requires very little change in many plugins (see e.g. BadStatics), some even work without any functionality change (e.g. CharRightShift). Traverse* calls require some changes but are often not that difficult. WalkUp* probably can't be supported, although some plugins can(?) possibly be adjusted to not rely on them. And of course some plugins can be left as they are, using their own recursive run. See description at the top of generator.cxx for description of how to convert a plugin. The sharedvisitor.cxx source is generated based on scanning relevant plugin sources using a clang-based scanner/generator. The generated source is intentionally included instead of getting always generated, as the generating currently takes some time, so it should get updated in git whenever a change in a plugin triggers a source change in it. Change-Id: Ia0d2e3a5a464659503dbb4ed6c20b6cc89b4de01 Reviewed-on: https://gerrit.libreoffice.org/68026 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk> Reviewed-by: Luboš Luňák <l.lunak@collabora.com>
2019-03-07 14:31:17 +01:00
void postRun() override;
bool VisitNamedDecl(NamedDecl const * decl);
private:
enum class Kind {
Ok, DoubleUnderscore, UnderscoreUppercase, UnderscoreLowercase };
Kind determineKind(llvm::StringRef const & id);
bool isInLokIncludeFile(SourceLocation spellingLocation) const;
bool isApi(NamedDecl const * decl);
};
void ReservedId::run() {
//TODO: Rules for C?
make (some) clang plugins share the same RecursiveASTVisitor Each plugin currently uses its own recursive AST run, which adds up. This patch adds another shared plugin which internally contains all (suitable) plugins and dispatches to them from the same one recursive run. This patch converts ~25 plugins and for starmath's accessibility.cxx reduces clang build time from 5.43s to 5.14s (and it's 4.39s without any plugins). As there are almost 50 more plugins to go, this can theoretically result in 4.56s final time, although probably not all plugins can be that easily converted, if at all. This mostly requires very little change in many plugins (see e.g. BadStatics), some even work without any functionality change (e.g. CharRightShift). Traverse* calls require some changes but are often not that difficult. WalkUp* probably can't be supported, although some plugins can(?) possibly be adjusted to not rely on them. And of course some plugins can be left as they are, using their own recursive run. See description at the top of generator.cxx for description of how to convert a plugin. The sharedvisitor.cxx source is generated based on scanning relevant plugin sources using a clang-based scanner/generator. The generated source is intentionally included instead of getting always generated, as the generating currently takes some time, so it should get updated in git whenever a change in a plugin triggers a source change in it. Change-Id: Ia0d2e3a5a464659503dbb4ed6c20b6cc89b4de01 Reviewed-on: https://gerrit.libreoffice.org/68026 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk> Reviewed-by: Luboš Luňák <l.lunak@collabora.com>
2019-03-07 14:31:17 +01:00
if (TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()))
postRun();
}
void ReservedId::postRun() {
if( compiler.hasPreprocessor())
{
auto & prep = compiler.getPreprocessor();
for (auto const & m: prep.macros(false)) {
auto id = m.first->getName();
if (determineKind(id) != Kind::Ok
&& id != "_ATL_APARTMENT_THREADED"
// extensions/source/activex/StdAfx2.h
&& id != "_ATL_STATIC_REGISTRY"
// extensions/source/activex/StdAfx2.h
&& id != "_CRT_RAND_S" // sal/osl/w32/random.cxx
&& id != "_GLIBCXX_CDTOR_CALLABI"
&& id != "_HAS_AUTO_PTR_ETC" // unotools/source/i18n/resmgr.cxx
&& id != "_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR" // unotools/source/i18n/resmgr.cxx
&& id != "_MAX_PATH" // Windows
&& id != "_POSIX_SOURCE"
&& id != "_USE_MATH_DEFINES" // include/sal/config.h, Windows
&& id != "_WIN32_DCOM" // embedserv/source/embed/esdll.cxx
&& id != "_WTL_NO_CSTRING"
// fpicker/source/win32/filepicker/platform_vista.h (TODO:
// needed?)
&& id != "__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES"
&& id != "__Column_FWD_DEFINED__"
// connectivity/source/inc/ado/Awrapadox.hxx, MS SDK
// adoctint.h
&& id != "__Group_FWD_DEFINED__"
// connectivity/source/inc/ado/Awrapadox.hxx, MS SDK
// adoctint.h
&& id != "__Index_FWD_DEFINED__"
// connectivity/source/inc/ado/Awrapadox.hxx, MS SDK
// adoctint.h
&& id != "__Key_FWD_DEFINED__"
// connectivity/source/inc/ado/Awrapadox.hxx, MS SDK
// adoctint.h
&& id != "__ORCUS_STATIC_LIB"
&& id != "__Table_FWD_DEFINED__"
// connectivity/source/inc/ado/Awrapadox.hxx, MS SDK
// adoctint.h
&& id != "__USE_GNU"
&& id != "__User_FWD_DEFINED__")
// connectivity/source/inc/ado/Awrapadox.hxx, MS SDK
// adoctint.h
{
auto d = prep.getLocalMacroDirectiveHistory(m.first);
for (;;) {
if (d->getKind() == MacroDirective::MD_Define) {
auto loc = d->getLocation();
Adapt to LLVM 15 trunk libc++ dropping std::unary_/binary_function ...for C++17 and beyond with <https://github.com/llvm/llvm-project/commit/681cde7dd8b5613dbafc9ca54e0288477f946be3> "[libc++] Complete the implementation of N4190". (Unless explicitly opted-in with _LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION. This is similar to the MSVC standard library needing _HAS_AUTO_PTR_ETC=1 to enable those zombie functions for quite some time now. Only libstdc++ still supports them unconditionally.) Most uses of those zombie functions across LibreOffice itself and the bundled external/* are indirectly within Boost include files. And many (but not all) of those Boost include files only use those zombie functions conditionally, based on BOOST_NO_CXX98_FUNCTION_BASE. For the (Dinkumware-derived) MSVC standard library, workdir/UnpackedTarball/boost/boost/config/stdlib/dinkumware.hpp already defined BOOST_NO_CXX98_FUNCTION_BASE. So add a patch to define that also in workdir/UnpackedTarball/boost/boost/config/stdlib/libcpp.hpp (for all of C++11 and beyond, even if those functions were still available as deprecated in C++11 and C++14, but which shouldn't make a difference with our C++17 baseline anyway; only make sure that things still work if those Boost include files ever get used by code built with gb_CXX03FLAGS). (Patching our bundled external/boost of course doesn't help when building with such a new libc++ and --with-system-boost against an unpatched Boost, but lets consider that "not my problem". Also, one could always use a sledgehammer like passing CPPFLAGS=-D_LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION into gbuild in such a case.) Then there are two places that include boost/multi_array.hpp, which indirectly includes workdir/UnpackedTarball/boost/boost/functional.hpp, which still uses those zombie functions for non-MSVC builds (at least in the bundled Boost 1.79.0). Lets do a targeted _LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION in those cases. (Alternatively, we could patch workdir/UnpackedTarball/boost/boost/functional.hpp. Also, I decided to make loplugin:reservedid support the new suppression mechanism, rather than extending its existing ignorelist even further.) And then there is external/clucene using those zombie functions even outside of a Boost include file, so extend the existing hack there that was already needed for MSVC. (And adapt the accompanying comment: For one, we are unconditionally "in C++17 mode" by now. And for another, the exact places where external/clucene uses those functions have apparently changed over time.) Change-Id: Id0eec3bedcfddae86b16d33c02c7b5d3b3f8a16f Reviewed-on: https://gerrit.libreoffice.org/c/core/+/136579 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2022-06-28 16:57:14 +02:00
if (loc.isValid() && !ignoreLocation(loc) && !suppressWarningAt(loc)) {
auto file = getFilenameOfLocation(loc);
if (!loplugin::isSameUnoIncludePathname(
file,
"cppuhelper/implbase_ex_post.hxx")
&& !loplugin::isSameUnoIncludePathname(
file,
"cppuhelper/implbase_ex_pre.hxx"))
{
report(
DiagnosticsEngine::Warning,
"reserved macro identifier", loc);
}
}
}
d = d->getPrevious();
if (d == nullptr) {
break;
}
}
}
}
}
}
bool ReservedId::VisitNamedDecl(NamedDecl const * decl) {
auto spelLoc = compiler.getSourceManager().getSpellingLoc(
decl->getLocation());
if (ignoreLocation(spelLoc)) {
return true;
}
auto filename = getFilenameOfLocation(spelLoc);
if (loplugin::hasPathnamePrefix(filename, SRCDIR "/bridges/source/cpp_uno/")
&& (filename.endswith("abi.hxx") || filename.endswith("share.hxx")))
{
return true;
}
auto const id = decl->getIdentifier();
if (id == nullptr) {
return true;
}
auto const s = id->getName();
switch (determineKind(s)) {
case Kind::Ok:
break;
case Kind::DoubleUnderscore:
/*TODO*/if(s=="BIFF__5"||s=="XML__COLON"||s=="XML__EMPTY"||s=="XML__UNKNOWN_")break;
if (!(isApi(decl) || isJniFunction(decl))
&& s != "__CERT_DecodeDERCertificate"
// xmlsecurity/source/xmlsec/nss/nssrenam.h
&& s != "__CERT_NewTempCertificate"
// xmlsecurity/source/xmlsec/nss/nssrenam.h
&& s != "__CTFont"
// vcl/source/window/cairo_cairo.cxx -> include/vcl/sysdata.hxx
&& s != "__CxxDetectRethrow"
// bridges/source/cpp_uno/msvc_win32_x86-64/mscx.hxx
&& s != "__ImageBase"
// bridges/source/cpp_uno/msvc_win32_x86-64/cpp2uno.cxx, MS linker magic
&& s != "__PK11_GetKeyData"
// xmlsecurity/source/xmlsec/nss/nssrenam.h
&& s != "__current_exception" // bridges/inc/except.hxx, Windows
&& s != "__data_start") // sal/osl/unx/system.cxx
{
report(
DiagnosticsEngine::Warning,
"identifier %select{beginning with|containing}0 a double"
" underscore is reserved",
decl->getLocation())
<< compiler.getLangOpts().CPlusPlus << decl->getSourceRange();
}
break;
case Kind::UnderscoreUppercase:
if (!isApi(decl)
&& s != "_ADOColumn"
// connectivity/source/inc/ado/Awrapadox.hxx, MS SDK adoctint.h
&& s != "_ADOGroup"
// connectivity/source/inc/ado/Awrapadox.hxx, MS SDK adoctint.h
&& s != "_ADOIndex"
// connectivity/source/inc/ado/Awrapadox.hxx, MS SDK adoctint.h
&& s != "_ADOKey"
// connectivity/source/inc/ado/Awrapadox.hxx, MS SDK adoctint.h
&& s != "_ADOTable"
// connectivity/source/inc/ado/Awrapadox.hxx, MS SDK adoctint.h
&& s != "_ADOUser"
// connectivity/source/inc/ado/Awrapadox.hxx, MS SDK adoctint.h
&& s != "_CustomCellRenderer" // vcl/unx/gtk3/customcellrenderer.hxx
&& s != "_CustomCellRendererClass" // vcl/unx/gtk3/customcellrenderer.cxx
&& s != "_FcPattern" // vcl/inc/unx/fc_fontoptions.hxx
&& s != "_GdkDisplay"
// vcl/unx/gtk/xid_fullscreen_on_all_monitors.c
&& s != "_GdkEvent" // vcl/unx/gtk/xid_fullscreen_on_all_monitors.c
&& s != "_GdkScreen" // vcl/unx/gtk/xid_fullscreen_on_all_monitors.c
&& s != "_GdkWindow"
// vcl/unx/gtk/xid_fullscreen_on_all_monitors.c
&& s != "_GstVideoOverlay"
// avmedia/source/gstreamer/gstplayer.hxx
&& s != "_GtkMediaStream"
// avmedia/source/gtk/gtkplayer.hxx
&& s != "_GtkWidget"
// avmedia/source/gtk/gtkplayer.hxx
&& s != "_Module" // extensions/source/activex/StdAfx2.h, CComModule
&& s != "_NotifyingLayout" // vcl/unx/gtk4/notifyinglayout.cxx
&& s != "_SurfacePaintable" // vcl/unx/gtk3/gtkinst.cxx
&& s != "_SurfacePaintableClass" // vcl/unx/gtk3/gtkinst.cxx
&& s != "_SurfaceCellRenderer" // vcl/unx/gtk4/surfacecellrenderer.cxx
&& s != "_SurfaceCellRendererClass" // vcl/unx/gtk4/surfacecellrenderer.cxx
&& s != "_TransferableContent" // vcl/unx/gtk4/transferableprovider.cxx
&& s != "_TransferableContentClass") // vcl/unx/gtk4/transferableprovider.cxx
{
report(
DiagnosticsEngine::Warning,
"identifier beginning with an underscore followed by an"
" uppercase letter is reserved",
decl->getLocation())
<< decl->getSourceRange();
}
break;
case Kind::UnderscoreLowercase:
if (decl->getDeclContext()->isTranslationUnit()
&& !isa<ParmVarDecl>(decl) && !isApi(decl)
&& s != "_cairo" && s != "_cairo_surface"
// tools/source/ref/errinf.cxx -> include/vcl/window.hxx ->
// include/vcl/outdev.hxx -> include/vcl/cairo.hxx
&& s != "_cairo_font_options"
// vcl/source/window/accessibility.cxx -> vcl/inc/salinst.hxx
&& s != "_cairo_user_data_key"
// vcl/headless/svpbmp.cxx -> vcl/inc/headless/svpgdi.hxx
&& s != "_end" // sal/osl/unx/system.cxx
&& s != "_rtl_Locale"
// i18nlangtag/source/isolang/mslangid.cxx ->
// include/i18nlangtag/languagetag.hxx
&& s != "_uno_ExtEnvironment"
// cppu/source/threadpool/threadident.cxx ->
// threadpool/current.hxx
&& s != "_xmlTextWriter") // include/svl/poolitem.hxx
{
report(
DiagnosticsEngine::Warning,
"identifier beginning with an underscore followed by a"
" lowercase letter is reserved in the global namespace",
decl->getLocation())
<< decl->getSourceRange();
}
break;
}
return true;
}
ReservedId::Kind ReservedId::determineKind(llvm::StringRef const & id) {
if (compiler.getLangOpts().CPlusPlus
&& id.find("__") != llvm::StringRef::npos)
{
return Kind::DoubleUnderscore;
}
if (id.size() >= 2 && id[0] == '_') {
auto c = id[1];
if (c == '_') {
return Kind::DoubleUnderscore;
}
if (c >= 'A' && c <= 'Z') {
return Kind::UnderscoreUppercase;
}
if (c >= 'a' && c <= 'z') {
return Kind::UnderscoreLowercase;
}
}
return Kind::Ok;
}
bool ReservedId::isInLokIncludeFile(SourceLocation spellingLocation) const {
return loplugin::hasPathnamePrefix(
getFilenameOfLocation(spellingLocation),
SRCDIR "/include/LibreOfficeKit/");
}
bool ReservedId::isApi(NamedDecl const * decl) {
auto const fdecl = dyn_cast<FunctionDecl>(decl);
if (fdecl != nullptr) {
decl = fdecl->getCanonicalDecl();
} else {
auto const tdecl = dyn_cast<TagDecl>(decl);
if (tdecl != nullptr) {
decl = tdecl->getCanonicalDecl();
}
}
auto const loc = compiler.getSourceManager().getSpellingLoc(
decl->getLocation());
if (!(isInUnoIncludeFile(loc) || isInLokIncludeFile(loc))
|| isa<ParmVarDecl>(decl))
{
return false;
}
auto const ctx = decl->getDeclContext();
if (ctx->isTranslationUnit()
|| (ctx->getDeclKind() == Decl::LinkageSpec
&& ctx->getParent()->isTranslationUnit()))
{
return true;
}
if (ctx->isNamespace()) {
auto const id = dyn_cast<NamespaceDecl>(ctx)->getIdentifier();
return !(id == nullptr || id->getName() == "detail");
}
return false;
}
make (some) clang plugins share the same RecursiveASTVisitor Each plugin currently uses its own recursive AST run, which adds up. This patch adds another shared plugin which internally contains all (suitable) plugins and dispatches to them from the same one recursive run. This patch converts ~25 plugins and for starmath's accessibility.cxx reduces clang build time from 5.43s to 5.14s (and it's 4.39s without any plugins). As there are almost 50 more plugins to go, this can theoretically result in 4.56s final time, although probably not all plugins can be that easily converted, if at all. This mostly requires very little change in many plugins (see e.g. BadStatics), some even work without any functionality change (e.g. CharRightShift). Traverse* calls require some changes but are often not that difficult. WalkUp* probably can't be supported, although some plugins can(?) possibly be adjusted to not rely on them. And of course some plugins can be left as they are, using their own recursive run. See description at the top of generator.cxx for description of how to convert a plugin. The sharedvisitor.cxx source is generated based on scanning relevant plugin sources using a clang-based scanner/generator. The generated source is intentionally included instead of getting always generated, as the generating currently takes some time, so it should get updated in git whenever a change in a plugin triggers a source change in it. Change-Id: Ia0d2e3a5a464659503dbb4ed6c20b6cc89b4de01 Reviewed-on: https://gerrit.libreoffice.org/68026 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk> Reviewed-by: Luboš Luňák <l.lunak@collabora.com>
2019-03-07 14:31:17 +01:00
loplugin::Plugin::Registration<ReservedId> reservedid("reservedid");
}
make (some) clang plugins share the same RecursiveASTVisitor Each plugin currently uses its own recursive AST run, which adds up. This patch adds another shared plugin which internally contains all (suitable) plugins and dispatches to them from the same one recursive run. This patch converts ~25 plugins and for starmath's accessibility.cxx reduces clang build time from 5.43s to 5.14s (and it's 4.39s without any plugins). As there are almost 50 more plugins to go, this can theoretically result in 4.56s final time, although probably not all plugins can be that easily converted, if at all. This mostly requires very little change in many plugins (see e.g. BadStatics), some even work without any functionality change (e.g. CharRightShift). Traverse* calls require some changes but are often not that difficult. WalkUp* probably can't be supported, although some plugins can(?) possibly be adjusted to not rely on them. And of course some plugins can be left as they are, using their own recursive run. See description at the top of generator.cxx for description of how to convert a plugin. The sharedvisitor.cxx source is generated based on scanning relevant plugin sources using a clang-based scanner/generator. The generated source is intentionally included instead of getting always generated, as the generating currently takes some time, so it should get updated in git whenever a change in a plugin triggers a source change in it. Change-Id: Ia0d2e3a5a464659503dbb4ed6c20b6cc89b4de01 Reviewed-on: https://gerrit.libreoffice.org/68026 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk> Reviewed-by: Luboš Luňák <l.lunak@collabora.com>
2019-03-07 14:31:17 +01:00
#endif // LO_CLANG_SHARED_PLUGINS
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */