Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / compilerplugins / clang / nestedunnamed.cxx
blobc26f5aac8efd2f255d0c531b69ce9529e67fa23c
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 */
10 #include <cassert>
12 #include "plugin.hxx"
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.)
18 namespace
20 class NestedUnnamed : public RecursiveASTVisitor<NestedUnnamed>, public loplugin::Plugin
22 public:
23 explicit NestedUnnamed(loplugin::InstantiationData const& data)
24 : Plugin(data)
28 void run() override
30 if (compiler.getLangOpts().CPlusPlus)
32 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
36 bool VisitNamespaceDecl(NamespaceDecl const* decl)
38 if (ignoreLocation(decl))
40 return true;
42 if (!decl->isAnonymousNamespace())
44 return true;
46 NamespaceDecl const* outer;
47 for (auto p = decl->getLexicalParent();; p = p->getLexicalParent())
49 outer = dyn_cast<NamespaceDecl>(p);
50 if (outer != nullptr)
52 break;
54 if (isa<TranslationUnitDecl>(p))
56 return true;
59 if (!outer->isAnonymousNamespace())
61 return true;
63 report(DiagnosticsEngine::Warning, "unnamed namespace directly nested in unnamed namespace",
64 decl->getLocation())
65 << decl->getSourceRange();
66 report(DiagnosticsEngine::Note, "outer namespace declared here", outer->getLocation())
67 << outer->getSourceRange();
68 return true;
72 loplugin::Plugin::Registration<NestedUnnamed> X("nestedunnamed");
75 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */