lok: getSlideShowInfo: interactions: check that properties are available
[LibreOffice.git] / compilerplugins / clang / redundantstatic.cxx
blob40f6ab45f132654011d4769aef50da429dfd59aa
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 "plugin.hxx"
14 This is a compile check.
16 Warns about functions with static keyword in an unnamed namespace.
19 namespace loplugin
22 class RedundantStatic
23 : public loplugin::FilteringPlugin<RedundantStatic>
25 public:
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)) {
35 return true;
37 if (!decl->isFileVarDecl() || decl->isStaticDataMember()) {
38 return true;
40 if (decl->getStorageClass() != SC_Static) {
41 return true;
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:
56 return true;
59 report(
60 DiagnosticsEngine::Warning, "redundant 'static' keyword in unnamed namespace",
61 decl->getLocation())
62 << decl->getSourceRange();
63 return true;
65 if (decl->isInline()) {
66 return true;
68 if (!loplugin::TypeCheck(decl->getType()).ConstNonVolatile()) {
69 return true;
71 report(
72 DiagnosticsEngine::Warning,
73 "non-inline variable of non-volatile const-qualified type is redundantly marked as"
74 " 'static'",
75 decl->getLocation())
76 << decl->getSourceRange();
77 return true;
81 RedundantStatic::RedundantStatic( const InstantiationData& data )
82 : FilteringPlugin( data )
86 void RedundantStatic::run()
88 if (preRun()) {
89 TraverseDecl( compiler.getASTContext().getTranslationUnitDecl());
94 bool RedundantStatic::VisitFunctionDecl( const FunctionDecl* func )
97 if( ignoreLocation( func ) )
98 return true;
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());
112 return true;
115 // Register the plugin action with the LO plugin handling.
116 static Plugin::Registration< RedundantStatic > redundantstatic("redundantstatic");
118 } // namespace
120 #endif // LO_CLANG_SHARED_PLUGINS
122 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */