blob: cd07a94583577efddd2e4a1740b2dd95637dcbd5 [file] [log] [blame]
Alexander Kornienkoea9fd992016-02-24 13:35:32 +00001//===--- ForwardDeclarationNamespaceCheck.h - clang-tidy --------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Kornienkoea9fd992016-02-24 13:35:32 +00006//
7//===----------------------------------------------------------------------===//
8
Alexander Kornienkod4ac4af2017-11-24 14:16:29 +00009#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_FORWARDDECLARATIONNAMESPACECHECK_H
10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_FORWARDDECLARATIONNAMESPACECHECK_H
Alexander Kornienkoea9fd992016-02-24 13:35:32 +000011
Alexander Kornienko478fc5c2019-03-25 12:38:26 +000012#include "../ClangTidyCheck.h"
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000013#include "llvm/ADT/SmallPtrSet.h"
Alexander Kornienkoea9fd992016-02-24 13:35:32 +000014#include <set>
15#include <vector>
Alexander Kornienkoea9fd992016-02-24 13:35:32 +000016
17namespace clang {
18namespace tidy {
Alexander Kornienkod4ac4af2017-11-24 14:16:29 +000019namespace bugprone {
Alexander Kornienkoea9fd992016-02-24 13:35:32 +000020
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 Kornienkod4ac4af2017-11-24 14:16:29 +000039/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone-forward-declaration-namespace.html
Alexander Kornienkoea9fd992016-02-24 13:35:32 +000040class ForwardDeclarationNamespaceCheck : public ClangTidyCheck {
41public:
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
48private:
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 Kornienkod4ac4af2017-11-24 14:16:29 +000054} // namespace bugprone
Alexander Kornienkoea9fd992016-02-24 13:35:32 +000055} // namespace tidy
56} // namespace clang
57
Alexander Kornienkod4ac4af2017-11-24 14:16:29 +000058#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_FORWARDDECLARATIONNAMESPACECHECK_H