diff options
| author | Alexander Kornienko <alexfh@google.com> | 2016-02-24 13:35:32 +0000 |
|---|---|---|
| committer | Alexander Kornienko <alexfh@google.com> | 2016-02-24 13:35:32 +0000 |
| commit | ea9fd99215088b136c224d40074f2faa735a073b (patch) | |
| tree | ce0a473299df3a7a7a170d5d61d61bb419f06831 /clang-tools-extra/clang-tidy/misc/ForwardDeclarationNamespaceCheck.h | |
| parent | 31bcca47d3c5e988826416a42a1bd92d511f5d38 (diff) | |
| download | bcm5719-llvm-ea9fd99215088b136c224d40074f2faa735a073b.tar.gz bcm5719-llvm-ea9fd99215088b136c224d40074f2faa735a073b.zip | |
[clang-tidy] Added a check for forward declaration in the potentially wrong namespace
Adds a new check "misc-forward-declaration-namespace".
In check, A forward declaration is considerred in a potentially wrong namespace
if there is any definition/declaration with the same name exists in a different
namespace.
Reviewers: akuegel, hokein, alexfh
Patch by Eric Liu!
Differential Revision: http://reviews.llvm.org/D17195
llvm-svn: 261737
Diffstat (limited to 'clang-tools-extra/clang-tidy/misc/ForwardDeclarationNamespaceCheck.h')
| -rw-r--r-- | clang-tools-extra/clang-tidy/misc/ForwardDeclarationNamespaceCheck.h | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/misc/ForwardDeclarationNamespaceCheck.h b/clang-tools-extra/clang-tidy/misc/ForwardDeclarationNamespaceCheck.h new file mode 100644 index 00000000000..cc701daad6a --- /dev/null +++ b/clang-tools-extra/clang-tidy/misc/ForwardDeclarationNamespaceCheck.h @@ -0,0 +1,59 @@ +//===--- ForwardDeclarationNamespaceCheck.h - clang-tidy --------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_FORWARDDECLARATIONNAMESPACECHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_FORWARDDECLARATIONNAMESPACECHECK_H + +#include <set> +#include <vector> +#include "llvm/ADT/SmallPtrSet.h" +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace misc { + +/// Checks if an unused forward declaration is in a wrong namespace. +/// +/// The check inspects all unused forward declarations and checks if there is +/// any declaration/definition with the same name, which could indicate +/// that the forward declaration is potentially in a wrong namespace. +/// +/// \code +/// namespace na { struct A; } +/// namespace nb { struct A {} }; +/// nb::A a; +/// // warning : no definition found for 'A', but a definition with the same +/// name 'A' found in another namespace 'nb::' +/// \endcode +/// +/// This check can only generate warnings, but it can't suggest fixes at this +/// point. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/misc-forward-declaration-namespace.html +class ForwardDeclarationNamespaceCheck : public ClangTidyCheck { +public: + ForwardDeclarationNamespaceCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + void onEndOfTranslationUnit() override; + +private: + llvm::StringMap<std::vector<const CXXRecordDecl *>> DeclNameToDefinitions; + llvm::StringMap<std::vector<const CXXRecordDecl *>> DeclNameToDeclarations; + llvm::SmallPtrSet<const Type *, 16> FriendTypes; +}; + +} // namespace misc +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_FORWARDDECLARATIONNAMESPACECHECK_H |

