Skip to content

Commit

Permalink
[clang-tidy] Optionally ignore findings in macros in `readability-con…
Browse files Browse the repository at this point in the history
…st-return-type`.

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

Differential Revision: https://reviews.llvm.org/D137972
  • Loading branch information
thomasetter authored and ymand committed Nov 15, 2022
1 parent f3afd16 commit a49fcca
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 7 deletions.
Expand Up @@ -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.
Expand All @@ -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
Expand Down
12 changes: 9 additions & 3 deletions clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.h
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Expand Up @@ -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.

Expand Down
Expand Up @@ -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`.
@@ -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
Expand Up @@ -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() {}
Expand Down

0 comments on commit a49fcca

Please sign in to comment.