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::tidy::bugprone { 18 19 /// Checks if an unused forward declaration is in a wrong namespace. 20 /// 21 /// The check inspects all unused forward declarations and checks if there is 22 /// any declaration/definition with the same name, which could indicate 23 /// that the forward declaration is potentially in a wrong namespace. 24 /// 25 /// \code 26 /// namespace na { struct A; } 27 /// namespace nb { struct A {} }; 28 /// nb::A a; 29 /// // warning : no definition found for 'A', but a definition with the same 30 /// name 'A' found in another namespace 'nb::' 31 /// \endcode 32 /// 33 /// This check can only generate warnings, but it can't suggest fixes at this 34 /// point. 35 /// 36 /// For the user-facing documentation see: 37 /// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/forward-declaration-namespace.html 38 class ForwardDeclarationNamespaceCheck : public ClangTidyCheck { 39 public: ForwardDeclarationNamespaceCheck(StringRef Name,ClangTidyContext * Context)40 ForwardDeclarationNamespaceCheck(StringRef Name, ClangTidyContext *Context) 41 : ClangTidyCheck(Name, Context) {} 42 void registerMatchers(ast_matchers::MatchFinder *Finder) override; 43 void check(const ast_matchers::MatchFinder::MatchResult &Result) override; 44 void onEndOfTranslationUnit() override; 45 46 private: 47 llvm::StringMap<std::vector<const CXXRecordDecl *>> DeclNameToDefinitions; 48 llvm::StringMap<std::vector<const CXXRecordDecl *>> DeclNameToDeclarations; 49 llvm::SmallPtrSet<const Type *, 16> FriendTypes; 50 }; 51 52 } // namespace clang::tidy::bugprone 53 54 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_FORWARDDECLARATIONNAMESPACECHECK_H 55