1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
14 // Warn about unnamed namespaces nested (directly) within unnamed namespaces. (It can be hard to
15 // keep track whether a certain spot in a source file is already in an unnamed namespace, so it
16 // happens that additions to the source add redundant, nested unnamed namespaces.)
20 class NestedUnnamed
: public RecursiveASTVisitor
<NestedUnnamed
>, public loplugin::Plugin
23 explicit NestedUnnamed(loplugin::InstantiationData
const& data
)
30 if (compiler
.getLangOpts().CPlusPlus
)
32 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
36 bool VisitNamespaceDecl(NamespaceDecl
const* decl
)
38 if (ignoreLocation(decl
))
42 if (!decl
->isAnonymousNamespace())
46 NamespaceDecl
const* outer
;
47 for (auto p
= decl
->getLexicalParent();; p
= p
->getLexicalParent())
49 outer
= dyn_cast
<NamespaceDecl
>(p
);
54 if (isa
<TranslationUnitDecl
>(p
))
59 if (!outer
->isAnonymousNamespace())
63 report(DiagnosticsEngine::Warning
, "unnamed namespace directly nested in unnamed namespace",
65 << decl
->getSourceRange();
66 report(DiagnosticsEngine::Note
, "outer namespace declared here", outer
->getLocation())
67 << outer
->getSourceRange();
72 loplugin::Plugin::Registration
<NestedUnnamed
> X("nestedunnamed");
75 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */