1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
8 #ifndef LO_CLANG_SHARED_PLUGINS
15 This is a compile check.
17 Warns about functions with static keyword in an unnamed namespace.
24 : public loplugin::FilteringPlugin
<RedundantStatic
>
27 explicit RedundantStatic( const InstantiationData
& data
);
29 bool preRun() override
{ return compiler
.getLangOpts().CPlusPlus
; }
31 virtual void run() override
;
32 bool VisitFunctionDecl( const FunctionDecl
* func
);
34 bool VisitVarDecl(VarDecl
const * decl
) {
35 if (ignoreLocation(decl
)) {
38 if (!decl
->isFileVarDecl() || decl
->isStaticDataMember()) {
41 if (decl
->getStorageClass() != SC_Static
) {
44 if (decl
->isInAnonymousNamespace()) {
45 auto loc
= compat::getBeginLoc(decl
);
46 while (compiler
.getSourceManager().isMacroArgExpansion(loc
)) {
47 loc
= compiler
.getSourceManager().getImmediateMacroCallerLoc(loc
);
49 if (compiler
.getSourceManager().isMacroBodyExpansion(loc
)) {
50 auto const name
= Lexer::getImmediateMacroName(
51 loc
, compiler
.getSourceManager(), compiler
.getLangOpts());
52 if (name
== "CPPUNIT_TEST_SUITE_REGISTRATION"
53 || name
== "CPPUNIT_TEST_SUITE_NAMED_REGISTRATION")
55 // Those macros contain a `static`, but are often used in an unnamed
56 // namespace, so filter them out:
61 DiagnosticsEngine::Warning
, "redundant 'static' keyword in unnamed namespace",
63 << decl
->getSourceRange();
66 if (decl
->isInline()) {
69 if (!loplugin::TypeCheck(decl
->getType()).ConstNonVolatile()) {
73 DiagnosticsEngine::Warning
,
74 "non-inline variable of non-volatile const-qualified type is redundantly marked as"
77 << decl
->getSourceRange();
82 RedundantStatic::RedundantStatic( const InstantiationData
& data
)
83 : FilteringPlugin( data
)
87 void RedundantStatic::run()
90 TraverseDecl( compiler
.getASTContext().getTranslationUnitDecl());
95 bool RedundantStatic::VisitFunctionDecl( const FunctionDecl
* func
)
98 if( ignoreLocation( func
) )
100 if( func
-> isInAnonymousNamespace () )
102 if ( !isa
<CXXMethodDecl
>(func
) && !func
->isInExternCContext() )
104 if(func
-> getStorageClass() == SC_Static
)
106 report( DiagnosticsEngine::Warning
,
107 "redundant 'static' keyword in unnamed namespace",
108 compat::getBeginLoc(func
));
116 // Register the plugin action with the LO plugin handling.
117 static Plugin::Registration
< RedundantStatic
> redundantstatic("redundantstatic");
121 #endif // LO_CLANG_SHARED_PLUGINS
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */