bump product version to 7.2.5.1
[LibreOffice.git] / compilerplugins / clang / redundantstatic.cxx
blob95a3c85c9d17c0567742bce90e8e6b3533ac2cdd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * Based on LLVM/Clang.
7 */
8 #ifndef LO_CLANG_SHARED_PLUGINS
10 #include "check.hxx"
11 #include "compat.hxx"
12 #include "plugin.hxx"
15 This is a compile check.
17 Warns about functions with static keyword in an unnamed namespace.
20 namespace loplugin
23 class RedundantStatic
24 : public loplugin::FilteringPlugin<RedundantStatic>
26 public:
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)) {
36 return true;
38 if (!decl->isFileVarDecl() || decl->isStaticDataMember()) {
39 return true;
41 if (decl->getStorageClass() != SC_Static) {
42 return true;
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:
57 return true;
60 report(
61 DiagnosticsEngine::Warning, "redundant 'static' keyword in unnamed namespace",
62 decl->getLocation())
63 << decl->getSourceRange();
64 return true;
66 if (decl->isInline()) {
67 return true;
69 if (!loplugin::TypeCheck(decl->getType()).ConstNonVolatile()) {
70 return true;
72 report(
73 DiagnosticsEngine::Warning,
74 "non-inline variable of non-volatile const-qualified type is redundantly marked as"
75 " 'static'",
76 decl->getLocation())
77 << decl->getSourceRange();
78 return true;
82 RedundantStatic::RedundantStatic( const InstantiationData& data )
83 : FilteringPlugin( data )
87 void RedundantStatic::run()
89 if (preRun()) {
90 TraverseDecl( compiler.getASTContext().getTranslationUnitDecl());
95 bool RedundantStatic::VisitFunctionDecl( const FunctionDecl* func )
98 if( ignoreLocation( func ) )
99 return true;
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));
113 return true;
116 // Register the plugin action with the LO plugin handling.
117 static Plugin::Registration< RedundantStatic > redundantstatic("redundantstatic");
119 } // namespace
121 #endif // LO_CLANG_SHARED_PLUGINS
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */