[clang-tools-extra] a49fcca - [clang-tidy] Optionally ignore findings in macros in `readability-const-return-type`.

Yitzhak Mandelbaum via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 15 06:29:09 PST 2022


Author: Thomas Etter
Date: 2022-11-15T14:28:03Z
New Revision: a49fcca9e3ec9e310312416599405d26c189a81b

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

LOG: [clang-tidy] Optionally ignore findings in macros in `readability-const-return-type`.

Adds support for options-controlled configuration of the check to ignore results in macros.

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

Added: 
    clang-tools-extra/test/clang-tidy/checkers/readability/const-return-type-macros.cpp

Modified: 
    clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp
    clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.h
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/docs/clang-tidy/checks/readability/const-return-type.rst
    clang-tools-extra/test/clang-tidy/checkers/readability/const-return-type.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp b/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp
index 5a451cca800d..7d8bee4db96e 100644
--- a/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp
@@ -105,6 +105,10 @@ static CheckResult checkDef(const clang::FunctionDecl *Def,
   return Result;
 }
 
+void ConstReturnTypeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
+  Options.store(Opts, "IgnoreMacros", IgnoreMacros);
+}
+
 void ConstReturnTypeCheck::registerMatchers(MatchFinder *Finder) {
   // Find all function definitions for which the return types are `const`
   // qualified, ignoring decltype types.
@@ -123,6 +127,11 @@ void ConstReturnTypeCheck::registerMatchers(MatchFinder *Finder) {
 
 void ConstReturnTypeCheck::check(const MatchFinder::MatchResult &Result) {
   const auto *Def = Result.Nodes.getNodeAs<FunctionDecl>("func");
+  // Suppress the check if macros are involved.
+  if (IgnoreMacros &&
+      (Def->getBeginLoc().isMacroID() || Def->getEndLoc().isMacroID()))
+    return;
+
   CheckResult CR = checkDef(Def, Result);
   {
     // Clang only supports one in-flight diagnostic at a time. So, delimit the

diff  --git a/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.h b/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.h
index 6c89f7601c9b..f28e7c3ecd87 100644
--- a/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.h
+++ b/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.h
@@ -22,9 +22,15 @@ namespace readability {
 /// http://clang.llvm.org/extra/clang-tidy/checks/readability/const-return-type.html
 class ConstReturnTypeCheck : public ClangTidyCheck {
  public:
-  using ClangTidyCheck::ClangTidyCheck;
-  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
-  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+   ConstReturnTypeCheck(StringRef Name, ClangTidyContext *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;
+
+ private:
+   const bool IgnoreMacros;
 };
 
 } // namespace readability

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 7852c6f3d994..44c3743549ef 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -176,6 +176,10 @@ Changes in existing checks
   warn about `const` return types in overridden functions since the derived
   class cannot always choose to change the function signature.
 
+- Change the default behavior of :doc:`readability-const-return-type
+  <clang-tidy/checks/readability/const-return-type>` to not
+  warn about `const` value parameters of declarations inside macros.
+
 - Improved :doc:`misc-redundant-expression <clang-tidy/checks/misc/redundant-expression>`
   check.
 

diff  --git a/clang-tools-extra/docs/clang-tidy/checks/readability/const-return-type.rst b/clang-tools-extra/docs/clang-tidy/checks/readability/const-return-type.rst
index 154131b60351..ec81d46750d4 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/readability/const-return-type.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/readability/const-return-type.rst
@@ -24,3 +24,12 @@ pointers or references to const values. For example, these are fine:
    const int* foo();
    const int& foo();
    const Clazz* foo();
+
+
+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/const-return-type-macros.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/const-return-type-macros.cpp
new file mode 100644
index 000000000000..1e92759364ff
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/const-return-type-macros.cpp
@@ -0,0 +1,27 @@
+// RUN: %check_clang_tidy -std=c++14 %s readability-const-return-type %t -- \
+// RUN:   -config="{CheckOptions: [{key: readability-const-return-type.IgnoreMacros, value: false}]}"
+
+//  p# = positive test
+//  n# = negative test
+
+// Regression tests involving macros
+#define CONCAT(a, b) a##b
+CONCAT(cons, t) int p22(){}
+// CHECK-MESSAGES: [[@LINE-1]]:1: warning: return type 'const int' is 'const'-qu
+// We warn, but we can't give a fix
+
+#define CONSTINT const int
+CONSTINT p23() {}
+// CHECK-MESSAGES: [[@LINE-1]]:1: warning: return type 'const int' is 'const'-qu
+
+#define CONST const
+CONST int p24() {}
+// CHECK-MESSAGES: [[@LINE-1]]:1: warning: return type 'const int' is 'const'-qu
+
+#define CREATE_FUNCTION()                    \
+const int p_inside_macro() { \
+  return 1; \
+}
+CREATE_FUNCTION();
+// CHECK-MESSAGES: [[@LINE-1]]:1: warning: return type 'const int' is 'const'-qu
+// We warn, but we can't give a fix

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/readability/const-return-type.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/const-return-type.cpp
index dd3d7aa9fa22..144dc00fcfea 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/const-return-type.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/const-return-type.cpp
@@ -196,13 +196,22 @@ const /* comment */ /* another comment*/ int p16() { return 0; }
 // Test cases where the `const` token lexically is hidden behind some form of
 // indirection.
 
+// Regression tests involving macros, which are ignored by default because
+// IgnoreMacros defaults to true.
+#define CONCAT(a, b) a##b
+CONCAT(cons, t) int n22(){}
+
 #define CONSTINT const int
-CONSTINT p18() {}
-// CHECK-MESSAGES: [[@LINE-1]]:1: warning: return type 'const int' is 'const'-qu
+CONSTINT n23() {}
 
 #define CONST const
-CONST int p19() {}
-// CHECK-MESSAGES: [[@LINE-1]]:1: warning: return type 'const int' is 'const'-qu
+CONST int n24() {}
+
+#define CREATE_FUNCTION()                    \
+const int n_inside_macro() { \
+  return 1; \
+}
+CREATE_FUNCTION();
 
 using ty = const int;
 ty p21() {}


        


More information about the cfe-commits mailing list