[clang-tools-extra] r321913 - clang-tidy: add IgnoreMacros option to readability-inconsistent-declaration-parameter-name

Miklos Vajna via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 5 15:22:10 PST 2018


Author: vmiklos
Date: Fri Jan  5 15:22:10 2018
New Revision: 321913

URL: http://llvm.org/viewvc/llvm-project?rev=321913&view=rev
Log:
clang-tidy: add IgnoreMacros option to readability-inconsistent-declaration-parameter-name

And also enable it by default to be consistent with e.g. modernize-use-using.

This helps e.g. when running this check on client code where the macro is
provided by the system, so there is no easy way to modify it.

Reviewers: alexfh, piotrdz, hokein, ilya-biryukov

Reviewed By: alexfh

Differential Revision: https://reviews.llvm.org/D41716

Added:
    clang-tools-extra/trunk/test/clang-tidy/readability-inconsistent-declaration-parameter-name-macros.cpp
Modified:
    clang-tools-extra/trunk/clang-tidy/readability/InconsistentDeclarationParameterNameCheck.cpp
    clang-tools-extra/trunk/clang-tidy/readability/InconsistentDeclarationParameterNameCheck.h
    clang-tools-extra/trunk/docs/clang-tidy/checks/readability-inconsistent-declaration-parameter-name.rst
    clang-tools-extra/trunk/test/clang-tidy/readability-inconsistent-declaration-parameter-name.cpp

Modified: clang-tools-extra/trunk/clang-tidy/readability/InconsistentDeclarationParameterNameCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/InconsistentDeclarationParameterNameCheck.cpp?rev=321913&r1=321912&r2=321913&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/InconsistentDeclarationParameterNameCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/InconsistentDeclarationParameterNameCheck.cpp Fri Jan  5 15:22:10 2018
@@ -281,6 +281,11 @@ void formatDiagnostics(
 
 } // anonymous namespace
 
+void InconsistentDeclarationParameterNameCheck::storeOptions(
+    ClangTidyOptions::OptionMap &Opts) {
+  Options.store(Opts, "IgnoreMacros", IgnoreMacros);
+}
+
 void InconsistentDeclarationParameterNameCheck::registerMatchers(
     MatchFinder *Finder) {
   Finder->addMatcher(functionDecl(unless(isImplicit()), hasOtherDeclarations())
@@ -308,6 +313,12 @@ void InconsistentDeclarationParameterNam
     markRedeclarationsAsVisited(OriginalDeclaration);
     return;
   }
+
+  SourceLocation StartLoc = OriginalDeclaration->getLocStart();
+  if (StartLoc.isMacroID() && IgnoreMacros) {
+    markRedeclarationsAsVisited(OriginalDeclaration);
+    return;
+  }
 
   if (OriginalDeclaration->getTemplatedKind() ==
       FunctionDecl::TK_FunctionTemplateSpecialization) {

Modified: clang-tools-extra/trunk/clang-tidy/readability/InconsistentDeclarationParameterNameCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/InconsistentDeclarationParameterNameCheck.h?rev=321913&r1=321912&r2=321913&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/InconsistentDeclarationParameterNameCheck.h (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/InconsistentDeclarationParameterNameCheck.h Fri Jan  5 15:22:10 2018
@@ -27,8 +27,10 @@ class InconsistentDeclarationParameterNa
 public:
   InconsistentDeclarationParameterNameCheck(StringRef Name,
                                             ClangTidyContext *Context)
-      : ClangTidyCheck(Name, Context) {}
+      : ClangTidyCheck(Name, Context),
+        IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", 1) != 0) {}
 
+  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
 
@@ -36,6 +38,7 @@ private:
   void markRedeclarationsAsVisited(const FunctionDecl *FunctionDeclaration);
 
   llvm::DenseSet<const FunctionDecl *> VisitedDeclarations;
+  const bool IgnoreMacros;
 };
 
 } // namespace readability

Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/readability-inconsistent-declaration-parameter-name.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/readability-inconsistent-declaration-parameter-name.rst?rev=321913&r1=321912&r2=321913&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/readability-inconsistent-declaration-parameter-name.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/readability-inconsistent-declaration-parameter-name.rst Fri Jan  5 15:22:10 2018
@@ -42,3 +42,8 @@ references parameter names in its body.
 In the case of multiple redeclarations or function template specializations,
 a warning is issued for every redeclaration or specialization inconsistent with
 the definition or the first declaration seen in a translation unit.
+
+.. option:: IgnoreMacros
+
+   If this option is set to non-zero (default is `1`), the check will not warn
+   about names declared inside macros.

Added: clang-tools-extra/trunk/test/clang-tidy/readability-inconsistent-declaration-parameter-name-macros.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/readability-inconsistent-declaration-parameter-name-macros.cpp?rev=321913&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/readability-inconsistent-declaration-parameter-name-macros.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/readability-inconsistent-declaration-parameter-name-macros.cpp Fri Jan  5 15:22:10 2018
@@ -0,0 +1,25 @@
+// RUN: %check_clang_tidy %s readability-inconsistent-declaration-parameter-name %t -- \
+// RUN:   -config="{CheckOptions: [{key: readability-inconsistent-declaration-parameter-name.IgnoreMacros, value: 0}]}" \
+// RUN:   -- -std=c++11
+
+#define MACRO() \
+  void f(int x);
+
+struct S {
+  MACRO();
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: function 'S::f' has a definition with different parameter names
+};
+
+void S::f(int y) {
+}
+
+//////////////////////////////////////////////////////
+
+#define DECLARE_FUNCTION_WITH_PARAM_NAME(function_name, param_name) \
+  void function_name(int param_name)
+
+// CHECK-MESSAGES: :[[@LINE+1]]:34: warning: function 'macroFunction' has 1 other declaration with different parameter names [readability-inconsistent-declaration-parameter-name]
+DECLARE_FUNCTION_WITH_PARAM_NAME(macroFunction, a);
+// CHECK-MESSAGES: :[[@LINE+2]]:34: note: the 1st inconsistent declaration seen here
+// CHECK-MESSAGES: :[[@LINE+1]]:34: note: differing parameters are named here: ('b'), in the other declaration: ('a')
+DECLARE_FUNCTION_WITH_PARAM_NAME(macroFunction, b);

Modified: clang-tools-extra/trunk/test/clang-tidy/readability-inconsistent-declaration-parameter-name.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/readability-inconsistent-declaration-parameter-name.cpp?rev=321913&r1=321912&r2=321913&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/readability-inconsistent-declaration-parameter-name.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/readability-inconsistent-declaration-parameter-name.cpp Fri Jan  5 15:22:10 2018
@@ -178,11 +178,14 @@ void Class::memberFunctionTemplateWithSp
 
 //////////////////////////////////////////////////////
 
-#define DECLARE_FUNCTION_WITH_PARAM_NAME(function_name, param_name) \
-  void function_name(int param_name)
+// This resulted in a warning by default.
+#define MACRO() \
+  void f(int x);
 
-// CHECK-MESSAGES: :[[@LINE+1]]:34: warning: function 'macroFunction' has 1 other declaration with different parameter names [readability-inconsistent-declaration-parameter-name]
-DECLARE_FUNCTION_WITH_PARAM_NAME(macroFunction, a);
-// CHECK-MESSAGES: :[[@LINE+2]]:34: note: the 1st inconsistent declaration seen here
-// CHECK-MESSAGES: :[[@LINE+1]]:34: note: differing parameters are named here: ('b'), in the other declaration: ('a')
-DECLARE_FUNCTION_WITH_PARAM_NAME(macroFunction, b);
+struct S {
+  MACRO();
+};
+
+void S::f(int y)
+{
+}




More information about the cfe-commits mailing list