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
14 This is a compile check.
16 Warns about functions with static keyword in an unnamed namespace.
23 : public loplugin::FilteringPlugin
<RedundantStatic
>
26 explicit RedundantStatic( const InstantiationData
& data
);
28 bool preRun() override
{ return compiler
.getLangOpts().CPlusPlus
; }
30 virtual void run() override
;
31 bool VisitFunctionDecl( const FunctionDecl
* func
);
33 bool VisitVarDecl(VarDecl
const * decl
) {
34 if (ignoreLocation(decl
)) {
37 if (!decl
->isFileVarDecl() || decl
->isStaticDataMember()) {
40 if (decl
->getStorageClass() != SC_Static
) {
43 if (decl
->isInAnonymousNamespace()) {
44 auto loc
= decl
->getBeginLoc();
45 while (compiler
.getSourceManager().isMacroArgExpansion(loc
)) {
46 loc
= compiler
.getSourceManager().getImmediateMacroCallerLoc(loc
);
48 if (compiler
.getSourceManager().isMacroBodyExpansion(loc
)) {
49 auto const name
= Lexer::getImmediateMacroName(
50 loc
, compiler
.getSourceManager(), compiler
.getLangOpts());
51 if (name
== "CPPUNIT_TEST_SUITE_REGISTRATION"
52 || name
== "CPPUNIT_TEST_SUITE_NAMED_REGISTRATION")
54 // Those macros contain a `static`, but are often used in an unnamed
55 // namespace, so filter them out:
60 DiagnosticsEngine::Warning
, "redundant 'static' keyword in unnamed namespace",
62 << decl
->getSourceRange();
65 if (decl
->isInline()) {
68 if (!loplugin::TypeCheck(decl
->getType()).ConstNonVolatile()) {
72 DiagnosticsEngine::Warning
,
73 "non-inline variable of non-volatile const-qualified type is redundantly marked as"
76 << decl
->getSourceRange();
81 RedundantStatic::RedundantStatic( const InstantiationData
& data
)
82 : FilteringPlugin( data
)
86 void RedundantStatic::run()
89 TraverseDecl( compiler
.getASTContext().getTranslationUnitDecl());
94 bool RedundantStatic::VisitFunctionDecl( const FunctionDecl
* func
)
97 if( ignoreLocation( func
) )
99 if( func
-> isInAnonymousNamespace () )
101 if ( !isa
<CXXMethodDecl
>(func
) && !func
->isInExternCContext() )
103 if(func
-> getStorageClass() == SC_Static
)
105 report( DiagnosticsEngine::Warning
,
106 "redundant 'static' keyword in unnamed namespace",
107 func
->getBeginLoc());
115 // Register the plugin action with the LO plugin handling.
116 static Plugin::Registration
< RedundantStatic
> redundantstatic("redundantstatic");
120 #endif // LO_CLANG_SHARED_PLUGINS
122 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */