clang-tools 6.0.0

ForwardDeclarationNamespaceCheck.h
Go to the documentation of this file.
1 //===--- ForwardDeclarationNamespaceCheck.h - clang-tidy --------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_FORWARDDECLARATIONNAMESPACECHECK_H
11 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_FORWARDDECLARATIONNAMESPACECHECK_H
12
13 #include "../ClangTidy.h"
14 #include "llvm/ADT/SmallPtrSet.h"
15 #include <set>
16 #include <vector>
17
18 namespace clang {
19 namespace tidy {
20 namespace bugprone {
21
22 /// Checks if an unused forward declaration is in a wrong namespace.
23 ///
24 /// The check inspects all unused forward declarations and checks if there is
25 /// any declaration/definition with the same name, which could indicate
26 /// that the forward declaration is potentially in a wrong namespace.
27 ///
28 /// \code
29 /// namespace na { struct A; }
30 /// namespace nb { struct A {} };
31 /// nb::A a;
32 /// // warning : no definition found for 'A', but a definition with the same
33 /// name 'A' found in another namespace 'nb::'
34 /// \endcode
35 ///
36 /// This check can only generate warnings, but it can't suggest fixes at this
37 /// point.
38 ///
39 /// For the user-facing documentation see:
40 /// http://clang.llvm.org/extra/clang-tidy/checks/bugprone-forward-declaration-namespace.html
41 class ForwardDeclarationNamespaceCheck : public ClangTidyCheck {
42 public:
43 ForwardDeclarationNamespaceCheck(StringRef Name, ClangTidyContext *Context)
44 : ClangTidyCheck(Name, Context) {}
45 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
46 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
47 void onEndOfTranslationUnit() override;
48
49 private:
50 llvm::StringMap<std::vector<const CXXRecordDecl *>> DeclNameToDefinitions;
51 llvm::StringMap<std::vector<const CXXRecordDecl *>> DeclNameToDeclarations;
52 llvm::SmallPtrSet<const Type *, 16> FriendTypes;
53 };
54
55 } // namespace bugprone
56 } // namespace tidy
57 } // namespace clang
58
59 #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:41
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:49
Name
StringHandle Name
Definition: PreprocessorTracker.cpp:525
clang::tidy::bugprone::ForwardDeclarationNamespaceCheck::onEndOfTranslationUnit
void onEndOfTranslationUnit() override
Definition: ForwardDeclarationNamespaceCheck.cpp:117
clang::tidy::bugprone::ForwardDeclarationNamespaceCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: ForwardDeclarationNamespaceCheck.cpp:24
clang::tidy::bugprone::ForwardDeclarationNamespaceCheck::ForwardDeclarationNamespaceCheck
ForwardDeclarationNamespaceCheck(StringRef Name, ClangTidyContext *Context)
Definition: ForwardDeclarationNamespaceCheck.h:43
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidy.h:127
clang
Definition: AndroidTidyModule.cpp:28
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:104