[clang-tools-extra] 9a4e52e - [clang-tidy] Add an IgnoreMacros option to readability-avoid-const-params-in-decls

Yitzhak Mandelbaum via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 30 07:27:29 PDT 2022


Author: Yitzhak Mandelbaum
Date: 2022-09-30T14:27:02Z
New Revision: 9a4e52ebeb6dd8527bc1ee944a9466d68a95b6f1

URL: https://github.com/llvm/llvm-project/commit/9a4e52ebeb6dd8527bc1ee944a9466d68a95b6f1
DIFF: https://github.com/llvm/llvm-project/commit/9a4e52ebeb6dd8527bc1ee944a9466d68a95b6f1.diff

LOG: [clang-tidy] Add an IgnoreMacros option to readability-avoid-const-params-in-decls

readability-avoid-const-params-in-decls.IgnoreMacros is enabled by default to be consistent with most other checks.

Reviewed By: ymandel

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

Added: 
    clang-tools-extra/test/clang-tidy/checkers/readability/avoid-const-params-in-decls-macros.cpp

Modified: 
    clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp
    clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.h
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/docs/clang-tidy/checks/readability/avoid-const-params-in-decls.rst
    clang-tools-extra/test/clang-tidy/checkers/readability/avoid-const-params-in-decls.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp b/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp
index e655f013a254..b4ff4ad805d9 100644
--- a/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp
+++ b/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp
@@ -27,6 +27,10 @@ SourceRange getTypeRange(const ParmVarDecl &Param) {
 
 } // namespace
 
+void AvoidConstParamsInDecls::storeOptions(ClangTidyOptions::OptionMap &Opts) {
+  Options.store(Opts, "IgnoreMacros", IgnoreMacros);
+}
+
 void AvoidConstParamsInDecls::registerMatchers(MatchFinder *Finder) {
   const auto ConstParamDecl =
       parmVarDecl(hasType(qualType(isConstQualified()))).bind("param");
@@ -44,6 +48,12 @@ void AvoidConstParamsInDecls::check(const MatchFinder::MatchResult &Result) {
   if (!Param->getType().isLocalConstQualified())
     return;
 
+  if (IgnoreMacros &&
+      (Param->getBeginLoc().isMacroID() || Param->getEndLoc().isMacroID())) {
+    // Suppress the check if macros are involved.
+    return;
+  }
+
   auto Diag = diag(Param->getBeginLoc(),
                    "parameter %0 is const-qualified in the function "
                    "declaration; const-qualification of parameters only has an "

diff  --git a/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.h b/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.h
index d366949e4a16..a7637a9c3050 100644
--- a/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.h
+++ b/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.h
@@ -20,13 +20,18 @@ namespace readability {
 class AvoidConstParamsInDecls : public ClangTidyCheck {
 public:
   AvoidConstParamsInDecls(StringRef Name, ClangTidyContext *Context)
-      : ClangTidyCheck(Name, Context) {}
+      : ClangTidyCheck(Name, Context),
+        IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)) {}
 
+  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
   llvm::Optional<TraversalKind> getCheckTraversalKind() const override {
     return TK_IgnoreUnlessSpelledInSource;
   }
+
+private:
+  const bool IgnoreMacros;
 };
 
 } // namespace readability

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 89dd7e746e58..c47f986e4021 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -149,6 +149,10 @@ Changes in existing checks
   copy assignment operators with nonstandard return types. The check is restricted to
   c++11-or-later.
 
+- Change the default behavior of :doc:`readability-avoid-const-params-in-decls
+  <clang-tidy/checks/readability-avoid-const-params-in-decls>` to not
+  warn about `const` value parameters of declarations inside macros.
+
 Removed checks
 ^^^^^^^^^^^^^^
 

diff  --git a/clang-tools-extra/docs/clang-tidy/checks/readability/avoid-const-params-in-decls.rst b/clang-tools-extra/docs/clang-tidy/checks/readability/avoid-const-params-in-decls.rst
index 3aea5d0c0753..b1146e9da79b 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/readability/avoid-const-params-in-decls.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/readability/avoid-const-params-in-decls.rst
@@ -15,3 +15,11 @@ Examples:
 
   void f(const string);   // Bad: const is top level.
   void f(const string&);  // Good: const is not top level.
+
+Options
+-------
+
+.. option:: IgnoreMacros
+
+   If set to `true`, the check will not give warnings inside macros. Default
+   is `true`.

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/readability/avoid-const-params-in-decls-macros.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/avoid-const-params-in-decls-macros.cpp
new file mode 100644
index 000000000000..326f3d8b59d1
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/avoid-const-params-in-decls-macros.cpp
@@ -0,0 +1,22 @@
+// RUN: %check_clang_tidy %s readability-avoid-const-params-in-decls %t -- \
+// RUN:   -config="{CheckOptions: [{key: readability-avoid-const-params-in-decls.IgnoreMacros, value: false}]}"
+
+// Regression tests involving macros
+#define CONCAT(a, b) a##b
+void ConstNotVisible(CONCAT(cons, t) int i);
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: parameter 'i'
+// We warn, but we can't give a fix
+// CHECK-FIXES: void ConstNotVisible(CONCAT(cons, t) int i);
+
+#define CONST_INT_PARAM const int i
+void ConstInMacro(CONST_INT_PARAM);
+// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: parameter 'i'
+// We warn, but we can't give a fix
+// CHECK-FIXES: void ConstInMacro(CONST_INT_PARAM);
+
+#define DECLARE_FUNCTION_WITH_ARG(x) struct InsideMacro{ x }
+DECLARE_FUNCTION_WITH_ARG(
+    void member_function(const int i);
+);
+// CHECK-MESSAGES: :[[@LINE-2]]:26: warning: parameter 'i'
+// CHECK-FIXES: void member_function(int i);

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/readability/avoid-const-params-in-decls.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/avoid-const-params-in-decls.cpp
index 4ba1f2482d2d..d521fd238b7d 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/avoid-const-params-in-decls.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/avoid-const-params-in-decls.cpp
@@ -170,12 +170,17 @@ void NF(const int*);
 void NF(const int* const*);
 void NF(alias_const_type);
 
-// Regression test for when the 'const' token is not in the code.
+// Regression tests involving macros, which are ignored by default.
 #define CONCAT(a, b) a##b
 void ConstNotVisible(CONCAT(cons, t) int i);
-// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: parameter 'i'
-// We warn, but we can't give a fix
-// CHECK-FIXES: void ConstNotVisible(CONCAT(cons, t) int i);
+
+#define CONST_INT_PARAM const int i
+void ConstInMacro(CONST_INT_PARAM);
+
+#define DECLARE_FUNCTION_WITH_ARG(x) struct InsideMacro{ x }
+DECLARE_FUNCTION_WITH_ARG(
+    void member_function(const int i);
+);
 
 // Regression test. We should not warn (or crash) on lambda expressions
 auto lambda_with_name = [](const int n) {};


        


More information about the cfe-commits mailing list