1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #ifndef LO_CLANG_SHARED_PLUGINS
18 #include "clang/AST/Attr.h"
24 bool isJniFunction(NamedDecl
const * decl
) {
25 auto const fdecl
= dyn_cast
<FunctionDecl
>(decl
);
27 || !(decl
->getDeclContext()->getDeclKind() == Decl::LinkageSpec
28 && decl
->getDeclContext()->getParent()->isTranslationUnit())
29 || !fdecl
->isExternC())
33 auto const id
= decl
->getIdentifier();
34 return id
!= nullptr && id
->getName().startswith("Java_");
38 public loplugin::FilteringPlugin
<ReservedId
>
41 explicit ReservedId(loplugin::InstantiationData
const & data
): FilteringPlugin(data
)
45 void postRun() override
;
47 bool VisitNamedDecl(NamedDecl
const * decl
);
51 Ok
, DoubleUnderscore
, UnderscoreUppercase
, UnderscoreLowercase
};
53 Kind
determineKind(llvm::StringRef
const & id
);
55 bool isInLokIncludeFile(SourceLocation spellingLocation
) const;
57 bool isApi(NamedDecl
const * decl
);
60 void ReservedId::run() {
62 if (TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl()))
66 void ReservedId::postRun() {
67 if( compiler
.hasPreprocessor())
69 auto & prep
= compiler
.getPreprocessor();
70 for (auto const & m
: prep
.macros(false)) {
71 auto id
= m
.first
->getName();
72 if (determineKind(id
) != Kind::Ok
73 && id
!= "_ATL_APARTMENT_THREADED"
74 // extensions/source/activex/StdAfx2.h
75 && id
!= "_ATL_STATIC_REGISTRY"
76 // extensions/source/activex/StdAfx2.h
77 && id
!= "_GLIBCXX_CDTOR_CALLABI"
78 && id
!= "_HAS_AUTO_PTR_ETC" // unotools/source/i18n/resmgr.cxx
79 && id
!= "_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR" // unotools/source/i18n/resmgr.cxx
80 && id
!= "_MAX_PATH" // Windows
81 && id
!= "_POSIX_SOURCE"
82 && id
!= "_USE_MATH_DEFINES" // include/sal/config.h, Windows
83 && id
!= "_WIN32_DCOM" // embedserv/source/embed/esdll.cxx
84 && id
!= "_WTL_NO_CSTRING"
85 // fpicker/source/win32/filepicker/platform_vista.h (TODO:
87 && id
!= "__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES"
88 && id
!= "__Column_FWD_DEFINED__"
89 // connectivity/source/inc/ado/Awrapadox.hxx, MS SDK
91 && id
!= "__Group_FWD_DEFINED__"
92 // connectivity/source/inc/ado/Awrapadox.hxx, MS SDK
94 && id
!= "__Index_FWD_DEFINED__"
95 // connectivity/source/inc/ado/Awrapadox.hxx, MS SDK
97 && id
!= "__Key_FWD_DEFINED__"
98 // connectivity/source/inc/ado/Awrapadox.hxx, MS SDK
100 && id
!= "__ORCUS_STATIC_LIB"
101 && id
!= "__Table_FWD_DEFINED__"
102 // connectivity/source/inc/ado/Awrapadox.hxx, MS SDK
105 && id
!= "__User_FWD_DEFINED__")
106 // connectivity/source/inc/ado/Awrapadox.hxx, MS SDK
109 auto d
= prep
.getLocalMacroDirectiveHistory(m
.first
);
111 if (d
->getKind() == MacroDirective::MD_Define
) {
112 auto loc
= d
->getLocation();
113 if (loc
.isValid() && !ignoreLocation(loc
)) {
114 auto file
= getFileNameOfSpellingLoc(loc
);
115 if (!loplugin::isSamePathname(
118 "/include/cppuhelper/implbase_ex_post.hxx")
119 && !loplugin::isSamePathname(
122 "/include/cppuhelper/implbase_ex_pre.hxx"))
125 DiagnosticsEngine::Warning
,
126 "reserved macro identifier", loc
);
130 d
= d
->getPrevious();
140 bool ReservedId::VisitNamedDecl(NamedDecl
const * decl
) {
141 auto spelLoc
= compiler
.getSourceManager().getSpellingLoc(
142 decl
->getLocation());
143 if (ignoreLocation(spelLoc
)) {
146 auto filename
= getFileNameOfSpellingLoc(spelLoc
);
147 if (loplugin::hasPathnamePrefix(filename
, SRCDIR
"/bridges/source/cpp_uno/")
148 && filename
.endswith("share.hxx"))
152 auto const id
= decl
->getIdentifier();
156 auto const s
= id
->getName();
157 switch (determineKind(s
)) {
160 case Kind::DoubleUnderscore
:
161 /*TODO*/if(s
=="BIFF__5"||s
=="XML__COLON"||s
=="XML__EMPTY"||s
=="XML__UNKNOWN_")break;
162 if (!(isApi(decl
) || isJniFunction(decl
))
163 && s
!= "__CERT_DecodeDERCertificate"
164 // xmlsecurity/source/xmlsec/nss/nssrenam.h
165 && s
!= "__CERT_NewTempCertificate"
166 // xmlsecurity/source/xmlsec/nss/nssrenam.h
168 // vcl/source/window/cairo_cairo.cxx -> include/vcl/sysdata.hxx
169 && s
!= "__CxxDetectRethrow"
170 // bridges/source/cpp_uno/msvc_win32_x86-64/mscx.hxx
171 && s
!= "__GLXcontextRec" // vcl/unx/glxtest.cxx
172 && s
!= "__GLXFBConfigRec" // vcl/unx/glxtest.cxx
173 && s
!= "__PK11_GetKeyData"
174 // xmlsecurity/source/xmlsec/nss/nssrenam.h
175 && s
!= "__current_exception" // bridges/inc/except.hxx, Windows
176 && s
!= "__data_start") // sal/osl/unx/system.cxx
179 DiagnosticsEngine::Warning
,
180 "identifier %select{beginning with|containing}0 a double"
181 " underscore is reserved",
183 << compiler
.getLangOpts().CPlusPlus
<< decl
->getSourceRange();
186 case Kind::UnderscoreUppercase
:
189 // connectivity/source/inc/ado/Awrapadox.hxx, MS SDK adoctint.h
191 // connectivity/source/inc/ado/Awrapadox.hxx, MS SDK adoctint.h
193 // connectivity/source/inc/ado/Awrapadox.hxx, MS SDK adoctint.h
195 // connectivity/source/inc/ado/Awrapadox.hxx, MS SDK adoctint.h
197 // connectivity/source/inc/ado/Awrapadox.hxx, MS SDK adoctint.h
199 // connectivity/source/inc/ado/Awrapadox.hxx, MS SDK adoctint.h
200 && s
!= "_DllMainCRTStartup"
201 // odk/source/unowinreg/win/unowinreg.cxx (TODO: needed?)
202 && s
!= "_FcPattern" // vcl/inc/unx/fc_fontoptions.hxx
203 && s
!= "_GdkDisplay"
204 // vcl/unx/gtk/xid_fullscreen_on_all_monitors.c
205 && s
!= "_GdkEvent" // vcl/unx/gtk/xid_fullscreen_on_all_monitors.c
206 && s
!= "_GdkScreen" // vcl/unx/gtk/xid_fullscreen_on_all_monitors.c
208 // vcl/unx/gtk/xid_fullscreen_on_all_monitors.c
209 && s
!= "_GstVideoOverlay"
210 // avmedia/source/gstreamer/gstplayer.hxx
211 && s
!= "_Module" // extensions/source/activex/StdAfx2.h, CComModule
212 && s
!= "_XRegion" // vcl/unx/generic/gdi/x11cairotextrender.cxx
213 && s
!= "_XTrap") // vcl/unx/generic/gdi/xrender_peer.hxx
216 DiagnosticsEngine::Warning
,
217 "identifier beginning with an underscore followed by an"
218 " uppercase letter is reserved",
220 << decl
->getSourceRange();
223 case Kind::UnderscoreLowercase
:
224 if (decl
->getDeclContext()->isTranslationUnit()
225 && !isa
<ParmVarDecl
>(decl
) && !isApi(decl
)
226 && s
!= "_cairo" && s
!= "_cairo_surface"
227 // tools/source/ref/errinf.cxx -> include/vcl/window.hxx ->
228 // include/vcl/outdev.hxx -> include/vcl/cairo.hxx
229 && s
!= "_cairo_font_options"
230 // vcl/source/window/accessibility.cxx -> vcl/inc/salinst.hxx
231 && s
!= "_cairo_user_data_key"
232 // vcl/headless/svpbmp.cxx -> vcl/inc/headless/svpgdi.hxx
233 && s
!= "_end" // sal/osl/unx/system.cxx
234 && s
!= "_rtl_Locale"
235 // i18nlangtag/source/isolang/mslangid.cxx ->
236 // include/i18nlangtag/languagetag.hxx
237 && s
!= "_uno_ExtEnvironment"
238 // cppu/source/threadpool/threadident.cxx ->
239 // threadpool/current.hxx
240 && s
!= "_xmlTextWriter") // include/svl/poolitem.hxx
243 DiagnosticsEngine::Warning
,
244 "identifier beginning with an underscore followed by a"
245 " lowercase letter is reserved in the global namespace",
247 << decl
->getSourceRange();
254 ReservedId::Kind
ReservedId::determineKind(llvm::StringRef
const & id
) {
255 if (compiler
.getLangOpts().CPlusPlus
256 && id
.find("__") != llvm::StringRef::npos
)
258 return Kind::DoubleUnderscore
;
260 if (id
.size() >= 2 && id
[0] == '_') {
263 return Kind::DoubleUnderscore
;
265 if (c
>= 'A' && c
<= 'Z') {
266 return Kind::UnderscoreUppercase
;
268 if (c
>= 'a' && c
<= 'z') {
269 return Kind::UnderscoreLowercase
;
275 bool ReservedId::isInLokIncludeFile(SourceLocation spellingLocation
) const {
276 return loplugin::hasPathnamePrefix(
277 getFileNameOfSpellingLoc(spellingLocation
),
278 SRCDIR
"/include/LibreOfficeKit/");
281 bool ReservedId::isApi(NamedDecl
const * decl
) {
282 auto const fdecl
= dyn_cast
<FunctionDecl
>(decl
);
283 if (fdecl
!= nullptr) {
284 decl
= fdecl
->getCanonicalDecl();
286 auto const tdecl
= dyn_cast
<TagDecl
>(decl
);
287 if (tdecl
!= nullptr) {
288 decl
= tdecl
->getCanonicalDecl();
291 auto const loc
= compiler
.getSourceManager().getSpellingLoc(
292 decl
->getLocation());
293 if (!(isInUnoIncludeFile(loc
) || isInLokIncludeFile(loc
))
294 || isa
<ParmVarDecl
>(decl
))
298 auto const ctx
= decl
->getDeclContext();
299 if (ctx
->isTranslationUnit()
300 || (ctx
->getDeclKind() == Decl::LinkageSpec
301 && ctx
->getParent()->isTranslationUnit()))
305 if (ctx
->isNamespace()) {
306 auto const id
= dyn_cast
<NamespaceDecl
>(ctx
)->getIdentifier();
307 return !(id
== nullptr || id
->getName() == "detail");
312 loplugin::Plugin::Registration
<ReservedId
> reservedid("reservedid");
316 #endif // LO_CLANG_SHARED_PLUGINS
318 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */