summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--clang-tools-extra/clang-tidy/misc/CMakeLists.txt1
-rw-r--r--clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp3
-rw-r--r--clang-tools-extra/clang-tidy/misc/UnusedAliasDeclsCheck.cpp63
-rw-r--r--clang-tools-extra/clang-tidy/misc/UnusedAliasDeclsCheck.h35
-rw-r--r--clang-tools-extra/test/clang-tidy/misc-unused-alias-decls.cpp13
5 files changed, 115 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/misc/CMakeLists.txt b/clang-tools-extra/clang-tidy/misc/CMakeLists.txt
index 02f666cb0fe..ed15e46c741 100644
--- a/clang-tools-extra/clang-tidy/misc/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/misc/CMakeLists.txt
@@ -14,6 +14,7 @@ add_clang_library(clangTidyMiscModule
StaticAssertCheck.cpp
SwappedArgumentsCheck.cpp
UndelegatedConstructor.cpp
+ UnusedAliasDeclsCheck.cpp
UnusedParametersCheck.cpp
UnusedRAIICheck.cpp
UniqueptrResetReleaseCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp b/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
index 8502e5a92c3..8c401eecd4c 100644
--- a/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
@@ -23,6 +23,7 @@
#include "SwappedArgumentsCheck.h"
#include "UndelegatedConstructor.h"
#include "UniqueptrResetReleaseCheck.h"
+#include "UnusedAliasDeclsCheck.h"
#include "UnusedParametersCheck.h"
#include "UnusedRAIICheck.h"
#include "UseOverrideCheck.h"
@@ -59,6 +60,8 @@ public:
"misc-undelegated-constructor");
CheckFactories.registerCheck<UniqueptrResetReleaseCheck>(
"misc-uniqueptr-reset-release");
+ CheckFactories.registerCheck<UnusedAliasDeclsCheck>(
+ "misc-unused-alias-decls");
CheckFactories.registerCheck<UnusedParametersCheck>(
"misc-unused-parameters");
CheckFactories.registerCheck<UnusedRAIICheck>("misc-unused-raii");
diff --git a/clang-tools-extra/clang-tidy/misc/UnusedAliasDeclsCheck.cpp b/clang-tools-extra/clang-tidy/misc/UnusedAliasDeclsCheck.cpp
new file mode 100644
index 00000000000..a56c1bfa8bd
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/misc/UnusedAliasDeclsCheck.cpp
@@ -0,0 +1,63 @@
+//===--- UnusedAliasDeclsCheck.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 "UnusedAliasDeclsCheck.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 {
+
+// FIXME: Move this to ASTMatchers.h.
+const ast_matchers::internal::VariadicDynCastAllOfMatcher<
+ Decl, NamespaceAliasDecl> namespaceAliasDecl;
+
+void UnusedAliasDeclsCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(namespaceAliasDecl().bind("alias"), this);
+ Finder->addMatcher(nestedNameSpecifier().bind("nns"), this);
+}
+
+void UnusedAliasDeclsCheck::check(const MatchFinder::MatchResult &Result) {
+ if (const auto *AliasDecl = Result.Nodes.getNodeAs<Decl>("alias")) {
+ // We cannot do anything about headers (yet), as the alias declarations used
+ // in one header could be used by some other translation unit.
+ if (!Result.SourceManager->isInMainFile(AliasDecl->getLocation()))
+ return;
+
+ FoundDecls[AliasDecl] = CharSourceRange::getCharRange(
+ AliasDecl->getLocStart(),
+ Lexer::findLocationAfterToken(
+ AliasDecl->getLocEnd(), tok::semi, *Result.SourceManager,
+ Result.Context->getLangOpts(),
+ /*SkipTrailingWhitespaceAndNewLine=*/true));
+ return;
+ }
+
+ if (const auto *NestedName =
+ Result.Nodes.getNodeAs<NestedNameSpecifier>("nns")) {
+ if (const auto *AliasDecl = NestedName->getAsNamespaceAlias()) {
+ FoundDecls[AliasDecl] = CharSourceRange();
+ }
+ }
+}
+
+void UnusedAliasDeclsCheck::onEndOfTranslationUnit() {
+ for (const auto &FoundDecl : FoundDecls) {
+ if (!FoundDecl.second.isValid())
+ continue;
+ diag(FoundDecl.first->getLocation(), "this namespace alias decl is unused")
+ << FixItHint::CreateRemoval(FoundDecl.second);
+ }
+}
+
+} // namespace tidy
+} // namespace clang
diff --git a/clang-tools-extra/clang-tidy/misc/UnusedAliasDeclsCheck.h b/clang-tools-extra/clang-tidy/misc/UnusedAliasDeclsCheck.h
new file mode 100644
index 00000000000..c025c1ebc19
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/misc/UnusedAliasDeclsCheck.h
@@ -0,0 +1,35 @@
+//===--- UnusedAliasDeclsCheck.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_ALIAS_DECLS_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_ALIAS_DECLS_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+
+/// \brief Finds unused namespace alias declarations.
+class UnusedAliasDeclsCheck : public ClangTidyCheck {
+public:
+ UnusedAliasDeclsCheck(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();
+
+private:
+ llvm::DenseMap<const Decl *, CharSourceRange> FoundDecls;
+};
+
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_ALIAS_DECLS_H
+
diff --git a/clang-tools-extra/test/clang-tidy/misc-unused-alias-decls.cpp b/clang-tools-extra/test/clang-tidy/misc-unused-alias-decls.cpp
new file mode 100644
index 00000000000..01d6d9a0406
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/misc-unused-alias-decls.cpp
@@ -0,0 +1,13 @@
+// RUN: $(dirname %s)/check_clang_tidy.sh %s misc-unused-alias-decls %t
+// REQUIRES: shell
+
+namespace my_namespace {
+class C {};
+}
+
+namespace unused_alias = ::my_namespace; // eol-comments aren't removed (yet)
+// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: this namespace alias decl is unused
+// CHECK-FIXES: {{^}}// eol-comments aren't removed (yet)
+
+namespace used_alias = ::my_namespace;
+void f() { used_alias::C c; }
OpenPOWER on IntegriCloud