diff --git a/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.h b/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.h --- a/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.h +++ b/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.h @@ -22,9 +22,15 @@ /// 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/clang-tidy/readability/ConstReturnTypeCheck.cpp b/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp --- a/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp @@ -105,6 +105,10 @@ 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::check(const MatchFinder::MatchResult &Result) { const auto *Def = Result.Nodes.getNodeAs("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/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -166,6 +166,10 @@ ` to not warn about `const` value parameters of declarations inside macros. +- Change the default behavior of :doc:`readability-const-return-type + ` to not + warn about `const` value parameters of declarations inside macros. + - Change the behavior of :doc:`readability-const-return-type ` to not warn about `const` return types in overridden functions since the derived 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 --- 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 @@ 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 --- /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 --- 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 @@ // 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() {}