• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
ForwardDeclarationNamespaceCheck(StringRef Name,ClangTidyContext * Context)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
59