Author: djasper Date: Tue Apr 19 08:48:39 2016 New Revision: 266735 URL: http://llvm.org/viewvc/llvm-project?rev=266735&view=rev Log: Initial version of misc-unused-using-decl check.
Added: clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.h clang-tools-extra/trunk/docs/clang-tidy/checks/misc-unused-using-decls.rst clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp Modified: clang-tools-extra/trunk/clang-tidy/misc/CMakeLists.txt clang-tools-extra/trunk/clang-tidy/misc/MiscTidyModule.cpp clang-tools-extra/trunk/clang-tidy/misc/UnusedAliasDeclsCheck.h clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst Modified: clang-tools-extra/trunk/clang-tidy/misc/CMakeLists.txt URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/CMakeLists.txt?rev=266735&r1=266734&r2=266735&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/misc/CMakeLists.txt (original) +++ clang-tools-extra/trunk/clang-tidy/misc/CMakeLists.txt Tue Apr 19 08:48:39 2016 @@ -32,10 +32,11 @@ add_clang_library(clangTidyMiscModule SwappedArgumentsCheck.cpp ThrowByValueCatchByReferenceCheck.cpp UndelegatedConstructor.cpp + UniqueptrResetReleaseCheck.cpp UnusedAliasDeclsCheck.cpp UnusedParametersCheck.cpp UnusedRAIICheck.cpp - UniqueptrResetReleaseCheck.cpp + UnusedUsingDeclsCheck.cpp VirtualNearMissCheck.cpp LINK_LIBS Modified: clang-tools-extra/trunk/clang-tidy/misc/MiscTidyModule.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/MiscTidyModule.cpp?rev=266735&r1=266734&r2=266735&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/misc/MiscTidyModule.cpp (original) +++ clang-tools-extra/trunk/clang-tidy/misc/MiscTidyModule.cpp Tue Apr 19 08:48:39 2016 @@ -44,6 +44,7 @@ #include "UnusedAliasDeclsCheck.h" #include "UnusedParametersCheck.h" #include "UnusedRAIICheck.h" +#include "UnusedUsingDeclsCheck.h" #include "VirtualNearMissCheck.h" namespace clang { @@ -118,6 +119,8 @@ public: CheckFactories.registerCheck<UnusedParametersCheck>( "misc-unused-parameters"); CheckFactories.registerCheck<UnusedRAIICheck>("misc-unused-raii"); + CheckFactories.registerCheck<UnusedUsingDeclsCheck>( + "misc-unused-using-decls"); CheckFactories.registerCheck<VirtualNearMissCheck>( "misc-virtual-near-miss"); } Modified: clang-tools-extra/trunk/clang-tidy/misc/UnusedAliasDeclsCheck.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/UnusedAliasDeclsCheck.h?rev=266735&r1=266734&r2=266735&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/misc/UnusedAliasDeclsCheck.h (original) +++ clang-tools-extra/trunk/clang-tidy/misc/UnusedAliasDeclsCheck.h Tue Apr 19 08:48:39 2016 @@ -11,6 +11,7 @@ #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_ALIAS_DECLS_H #include "../ClangTidy.h" +#include "llvm/ADT/DenseMap.h" namespace clang { namespace tidy { Added: clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp?rev=266735&view=auto ============================================================================== --- clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp (added) +++ clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp Tue Apr 19 08:48:39 2016 @@ -0,0 +1,73 @@ +//===--- UnusedUsingDeclsCheck.cpp - clang-tidy----------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "UnusedUsingDeclsCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace misc { + +void UnusedUsingDeclsCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher(usingDecl(isExpansionInMainFile()).bind("using"), this); + Finder->addMatcher(recordType(hasDeclaration(namedDecl().bind("used"))), + this); +} + +void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) { + if (const auto *Using = Result.Nodes.getNodeAs<UsingDecl>("using")) { + // FIXME: Implement the correct behavior for using declarations with more + // than one shadow. + if (Using->shadow_size() != 1) + return; + const auto* TargetDecl = Using->shadow_begin()->getTargetDecl(); + + // FIXME: Handle other target types. + if (!isa<RecordDecl>(TargetDecl)) + return; + + FoundDecls[TargetDecl] = Using; + FoundRanges[TargetDecl] = CharSourceRange::getCharRange( + Using->getLocStart(), + Lexer::findLocationAfterToken( + Using->getLocEnd(), tok::semi, *Result.SourceManager, + Result.Context->getLangOpts(), + /*SkipTrailingWhitespaceAndNewLine=*/true)); + return; + } + + // Mark using declarations as used by setting FoundDecls' value to zero. As + // the AST is walked in order, usages are only marked after a the + // corresponding using declaration has been found. + // FIXME: This currently doesn't look at whether the type reference is + // actually found with the help of the using declaration. + if (const auto *Used = Result.Nodes.getNodeAs<NamedDecl>("used")) { + if (FoundDecls.find(Used) != FoundDecls.end()) + FoundDecls[Used] = nullptr; + } +} + +void UnusedUsingDeclsCheck::onEndOfTranslationUnit() { + for (const auto &FoundDecl : FoundDecls) { + if (FoundDecl.second == nullptr) + continue; + diag(FoundDecl.second->getLocation(), "using decl %0 is unused") + << FoundDecl.second + << FixItHint::CreateRemoval(FoundRanges[FoundDecl.first]); + } + FoundDecls.clear(); +} + +} // namespace misc +} // namespace tidy +} // namespace clang Added: clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.h?rev=266735&view=auto ============================================================================== --- clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.h (added) +++ clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.h Tue Apr 19 08:48:39 2016 @@ -0,0 +1,41 @@ +//===--- UnusedUsingDeclsCheck.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_UNUSED_USING_DECLS_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_USING_DECLS_H + +#include "../ClangTidy.h" +#include "llvm/ADT/DenseMap.h" + +namespace clang { +namespace tidy { +namespace misc { + +/// Finds unused using declarations. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/misc-unused-using-decls.html +class UnusedUsingDeclsCheck : public ClangTidyCheck { +public: + UnusedUsingDeclsCheck(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::DenseMap<const NamedDecl*, const UsingDecl*> FoundDecls; + llvm::DenseMap<const NamedDecl*, CharSourceRange> FoundRanges; +}; + +} // namespace misc +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_USING_DECLS_H Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst?rev=266735&r1=266734&r2=266735&view=diff ============================================================================== --- clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst (original) +++ clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst Tue Apr 19 08:48:39 2016 @@ -80,6 +80,7 @@ Clang-Tidy Checks misc-unused-alias-decls misc-unused-parameters misc-unused-raii + misc-unused-using-decls misc-virtual-near-miss modernize-deprecated-headers modernize-loop-convert Added: clang-tools-extra/trunk/docs/clang-tidy/checks/misc-unused-using-decls.rst URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/misc-unused-using-decls.rst?rev=266735&view=auto ============================================================================== --- clang-tools-extra/trunk/docs/clang-tidy/checks/misc-unused-using-decls.rst (added) +++ clang-tools-extra/trunk/docs/clang-tidy/checks/misc-unused-using-decls.rst Tue Apr 19 08:48:39 2016 @@ -0,0 +1,14 @@ +.. title:: clang-tidy - misc-unused-using-decls + +misc-unused-using-decls +======================= + +Finds unused ``using`` declarations. + +Example: + +.. code:: c++ + + namespace n { class C; } + using n::C; // Never actually used. + Added: clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp?rev=266735&view=auto ============================================================================== --- clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp (added) +++ clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp Tue Apr 19 08:48:39 2016 @@ -0,0 +1,27 @@ +// RUN: %check_clang_tidy %s misc-unused-using-decls %t + +// ----- Definitions ----- +template <typename T> class vector {}; +namespace n { +class A; +class B; +class C; +class D { public: static int i; }; +} + +// ----- Using declarations ----- +// eol-comments aren't removed (yet) +using n::A; // A +// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: using decl 'A' is unused +// CHECK-FIXES: {{^}}// A +using n::B; +using n::C; +using n::D; + +// ----- Usages ----- +void f(B b); +void g() { + vector<C> data; + D::i = 1; +} + _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits