bump product version to 6.4.0.3
[LibreOffice.git] / compilerplugins / clang / nestedunnamed.cxx
blobadc4f81a81307e7ea65cc60ce6250f96c8fbd7cf
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
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/.
8 */
9 #ifndef LO_CLANG_SHARED_PLUGINS
11 #include <cassert>
13 #include "plugin.hxx"
15 // Warn about unnamed namespaces nested (directly) within unnamed namespaces. (It can be hard to
16 // keep track whether a certain spot in a source file is already in an unnamed namespace, so it
17 // happens that additions to the source add redundant, nested unnamed namespaces.)
19 namespace
21 class NestedUnnamed : public loplugin::FilteringPlugin<NestedUnnamed>
23 public:
24 explicit NestedUnnamed(loplugin::InstantiationData const& data)
25 : FilteringPlugin(data)
29 bool preRun() override { return compiler.getLangOpts().CPlusPlus; }
31 void run() override
33 if (preRun())
35 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
39 bool VisitNamespaceDecl(NamespaceDecl const* decl)
41 if (ignoreLocation(decl))
43 return true;
45 if (!decl->isAnonymousNamespace())
47 return true;
49 NamespaceDecl const* outer;
50 for (auto p = decl->getLexicalParent();; p = p->getLexicalParent())
52 outer = dyn_cast<NamespaceDecl>(p);
53 if (outer != nullptr)
55 break;
57 if (isa<TranslationUnitDecl>(p))
59 return true;
62 if (!outer->isAnonymousNamespace())
64 return true;
66 report(DiagnosticsEngine::Warning, "unnamed namespace directly nested in unnamed namespace",
67 decl->getLocation())
68 << decl->getSourceRange();
69 report(DiagnosticsEngine::Note, "outer namespace declared here", outer->getLocation())
70 << outer->getSourceRange();
71 return true;
75 loplugin::Plugin::Registration<NestedUnnamed> nestedunnamed("nestedunnamed");
77 } // namespace
79 #endif // LO_CLANG_SHARED_PLUGINS
81 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */