Alexander Kornienko | ea9fd99 | 2016-02-24 13:35:32 +0000 | [diff] [blame^] | 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_MISC_FORWARDDECLARATIONNAMESPACECHECK_H |
| 11 | #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_FORWARDDECLARATIONNAMESPACECHECK_H |
| 12 | |
| 13 | #include <set> |
| 14 | #include <vector> |
| 15 | #include "llvm/ADT/SmallPtrSet.h" |
| 16 | #include "../ClangTidy.h" |
| 17 | |
| 18 | namespace clang { |
| 19 | namespace tidy { |
| 20 | namespace misc { |
| 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/misc-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 misc |
| 56 | } // namespace tidy |
| 57 | } // namespace clang |
| 58 | |
| 59 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_FORWARDDECLARATIONNAMESPACECHECK_H |