Alexander Kornienko | ea9fd99 | 2016-02-24 13:35:32 +0000 | [diff] [blame] | 1 | //===--- ForwardDeclarationNamespaceCheck.h - clang-tidy --------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Alexander Kornienko | ea9fd99 | 2016-02-24 13:35:32 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Alexander Kornienko | d4ac4af | 2017-11-24 14:16:29 +0000 | [diff] [blame] | 9 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_FORWARDDECLARATIONNAMESPACECHECK_H |
| 10 | #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_FORWARDDECLARATIONNAMESPACECHECK_H |
Alexander Kornienko | ea9fd99 | 2016-02-24 13:35:32 +0000 | [diff] [blame] | 11 | |
Alexander Kornienko | 478fc5c | 2019-03-25 12:38:26 +0000 | [diff] [blame] | 12 | #include "../ClangTidyCheck.h" |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/SmallPtrSet.h" |
Alexander Kornienko | ea9fd99 | 2016-02-24 13:35:32 +0000 | [diff] [blame] | 14 | #include <set> |
| 15 | #include <vector> |
Alexander Kornienko | ea9fd99 | 2016-02-24 13:35:32 +0000 | [diff] [blame] | 16 | |
| 17 | namespace clang { |
| 18 | namespace tidy { |
Alexander Kornienko | d4ac4af | 2017-11-24 14:16:29 +0000 | [diff] [blame] | 19 | namespace bugprone { |
Alexander Kornienko | ea9fd99 | 2016-02-24 13:35:32 +0000 | [diff] [blame] | 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: |
Alexander Kornienko | d4ac4af | 2017-11-24 14:16:29 +0000 | [diff] [blame] | 39 | /// http://clang.llvm.org/extra/clang-tidy/checks/bugprone-forward-declaration-namespace.html |
Alexander Kornienko | ea9fd99 | 2016-02-24 13:35:32 +0000 | [diff] [blame] | 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 | |
Alexander Kornienko | d4ac4af | 2017-11-24 14:16:29 +0000 | [diff] [blame] | 54 | } // namespace bugprone |
Alexander Kornienko | ea9fd99 | 2016-02-24 13:35:32 +0000 | [diff] [blame] | 55 | } // namespace tidy |
| 56 | } // namespace clang |
| 57 | |
Alexander Kornienko | d4ac4af | 2017-11-24 14:16:29 +0000 | [diff] [blame] | 58 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_FORWARDDECLARATIONNAMESPACECHECK_H |