clang-tools 10.0.0

ForwardDeclarationNamespaceCheck.h
Go to the documentation of this file.
1 //===--- ForwardDeclarationNamespaceCheck.h - clang-tidy --------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_FORWARDDECLARATIONNAMESPACECHECK_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_FORWARDDECLARATIONNAMESPACECHECK_H
11
12 #include "../ClangTidyCheck.h"
13 #include "llvm/ADT/SmallPtrSet.h"
14 #include <set>
15 #include <vector>
16
17 namespace clang {
18 namespace tidy {
19 namespace bugprone {
20
21 /// Checks if an unused forward declaration is in a wrong namespace.
22 ///
23 /// The check inspects all unused forward declarations and checks if there is
24 /// any declaration/definition with the same name, which could indicate
25 /// that the forward declaration is potentially in a wrong namespace.
26 ///
27 /// \code
28 /// namespace na { struct A; }
29 /// namespace nb { struct A {} };
30 /// nb::A a;
31 /// // warning : no definition found for 'A', but a definition with the same
32 /// name 'A' found in another namespace 'nb::'
33 /// \endcode
34 ///
35 /// This check can only generate warnings, but it can't suggest fixes at this
36 /// point.
37 ///
38 /// For the user-facing documentation see:
39 /// http://clang.llvm.org/extra/clang-tidy/checks/bugprone-forward-declaration-namespace.html
40 class ForwardDeclarationNamespaceCheck : public ClangTidyCheck {
41 public:
42 ForwardDeclarationNamespaceCheck(StringRef Name, ClangTidyContext *Context)
43 : ClangTidyCheck(Name, Context) {}
44 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
45 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
46 void onEndOfTranslationUnit() override;
47
48 private:
49 llvm::StringMap<std::vector<const CXXRecordDecl *>> DeclNameToDefinitions;
50 llvm::StringMap<std::vector<const CXXRecordDecl *>> DeclNameToDeclarations;
51 llvm::SmallPtrSet<const Type *, 16> FriendTypes;
52 };
53
54 } // namespace bugprone
55 } // namespace tidy
56 } // namespace clang
57
58 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_FORWARDDECLARATIONNAMESPACECHECK_H
clang::tidy::bugprone::ForwardDeclarationNamespaceCheck
Checks if an unused forward declaration is in a wrong namespace.
Definition: ForwardDeclarationNamespaceCheck.h:40
clang::tidy::bugprone::ForwardDeclarationNamespaceCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: ForwardDeclarationNamespaceCheck.cpp:48
clang::tidy::bugprone::ForwardDeclarationNamespaceCheck::onEndOfTranslationUnit
void onEndOfTranslationUnit() override
Definition: ForwardDeclarationNamespaceCheck.cpp:116
clang::tidy::bugprone::ForwardDeclarationNamespaceCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: ForwardDeclarationNamespaceCheck.cpp:23
clang::tidy::bugprone::ForwardDeclarationNamespaceCheck::ForwardDeclarationNamespaceCheck
ForwardDeclarationNamespaceCheck(StringRef Name, ClangTidyContext *Context)
Definition: ForwardDeclarationNamespaceCheck.h:42
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:47
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77