[clang-tools-extra] r242654 - Initial version of clang-tidy check to find and fix unused parameters.

Daniel Jasper djasper at google.com
Sun Jul 19 18:06:45 PDT 2015


Author: djasper
Date: Sun Jul 19 20:06:44 2015
New Revision: 242654

URL: http://llvm.org/viewvc/llvm-project?rev=242654&view=rev
Log:
Initial version of clang-tidy check to find and fix unused parameters.

Also see: llvm.org/PR24180.

Added:
    clang-tools-extra/trunk/clang-tidy/misc/UnusedParametersCheck.cpp
    clang-tools-extra/trunk/clang-tidy/misc/UnusedParametersCheck.h
    clang-tools-extra/trunk/test/clang-tidy/misc-unused-parameters.cpp
Modified:
    clang-tools-extra/trunk/clang-tidy/misc/CMakeLists.txt
    clang-tools-extra/trunk/clang-tidy/misc/MiscTidyModule.cpp

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=242654&r1=242653&r2=242654&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/CMakeLists.txt Sun Jul 19 20:06:44 2015
@@ -14,6 +14,7 @@ add_clang_library(clangTidyMiscModule
   StaticAssertCheck.cpp
   SwappedArgumentsCheck.cpp
   UndelegatedConstructor.cpp
+  UnusedParametersCheck.cpp
   UnusedRAIICheck.cpp
   UniqueptrResetReleaseCheck.cpp
   UseOverrideCheck.cpp

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=242654&r1=242653&r2=242654&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/MiscTidyModule.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/MiscTidyModule.cpp Sun Jul 19 20:06:44 2015
@@ -23,6 +23,7 @@
 #include "SwappedArgumentsCheck.h"
 #include "UndelegatedConstructor.h"
 #include "UniqueptrResetReleaseCheck.h"
+#include "UnusedParametersCheck.h"
 #include "UnusedRAIICheck.h"
 #include "UseOverrideCheck.h"
 
@@ -58,6 +59,8 @@ public:
         "misc-undelegated-constructor");
     CheckFactories.registerCheck<UniqueptrResetReleaseCheck>(
         "misc-uniqueptr-reset-release");
+    CheckFactories.registerCheck<UnusedParametersCheck>(
+        "misc-unused-parameters");
     CheckFactories.registerCheck<UnusedRAIICheck>("misc-unused-raii");
     CheckFactories.registerCheck<UseOverrideCheck>("misc-use-override");
   }

Added: clang-tools-extra/trunk/clang-tidy/misc/UnusedParametersCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/UnusedParametersCheck.cpp?rev=242654&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/UnusedParametersCheck.cpp (added)
+++ clang-tools-extra/trunk/clang-tidy/misc/UnusedParametersCheck.cpp Sun Jul 19 20:06:44 2015
@@ -0,0 +1,43 @@
+//===--- UnusedParametersCheck.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 "UnusedParametersCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+
+void UnusedParametersCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(
+      parmVarDecl(hasAncestor(functionDecl().bind("function"))).bind("x"),
+      this);
+}
+
+void UnusedParametersCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("function");
+  if (!Function->doesThisDeclarationHaveABody())
+    return;
+  const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("x");
+  if (Param->isUsed())
+    return;
+
+  auto MyDiag = diag(Param->getLocation(), "parameter '%0' is unused")
+                << Param->getName();
+
+  SourceRange RemovalRange(Param->getLocation(), Param->getLocEnd());
+  MyDiag << FixItHint::CreateReplacement(
+      RemovalRange, (Twine(" /*") + Param->getName() + "*/").str());
+}
+
+} // namespace tidy
+} // namespace clang
+

Added: clang-tools-extra/trunk/clang-tidy/misc/UnusedParametersCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/UnusedParametersCheck.h?rev=242654&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/UnusedParametersCheck.h (added)
+++ clang-tools-extra/trunk/clang-tidy/misc/UnusedParametersCheck.h Sun Jul 19 20:06:44 2015
@@ -0,0 +1,32 @@
+//===--- UnusedParametersCheck.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_PARAMETERS_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_PARAMETERS_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+
+/// \brief Finds unused parameters and fixes them, so that -Wunused-parameter
+/// can be turned on.
+class UnusedParametersCheck : public ClangTidyCheck {
+public:
+  UnusedParametersCheck(StringRef Name, ClangTidyContext *Context)
+      : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_PARAMETERS_H
+

Added: clang-tools-extra/trunk/test/clang-tidy/misc-unused-parameters.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-unused-parameters.cpp?rev=242654&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/misc-unused-parameters.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-unused-parameters.cpp Sun Jul 19 20:06:44 2015
@@ -0,0 +1,21 @@
+// RUN: $(dirname %s)/check_clang_tidy.sh %s misc-unused-parameters %t
+// REQUIRES: shell
+
+// Basic removal
+// =============
+void a(int i) {}
+// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: parameter 'i' is unused [misc-unused-parameters]
+// CHECK-FIXES: {{^}}void a(int  /*i*/) {}{{$}}
+
+void b(int i = 1) {}
+// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: parameter 'i' is unused [misc-unused-parameters]
+// CHECK-FIXES: {{^}}void b(int  /*i*/) {}{{$}}
+
+void c(int *i) {}
+// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: parameter 'i' is unused [misc-unused-parameters]
+// CHECK-FIXES: {{^}}void c(int * /*i*/) {}{{$}}
+
+// Unchanged cases
+// ===============
+void g(int i);             // Don't remove stuff in declarations
+void h(int i) { (void)i; } // Don't remove used parameters





More information about the cfe-commits mailing list