https://github.com/khuldraeseth updated https://github.com/llvm/llvm-project/pull/129406
>From 68737895ee3c642df8cf01f97f5bfa953da0ea5e Mon Sep 17 00:00:00 2001 From: khuldraeseth <28711596+khuldraes...@users.noreply.github.com> Date: Wed, 26 Feb 2025 11:28:37 -0600 Subject: [PATCH 1/2] add WarnOnNontrailingVoid option to modernize-use-trailing-return-type check --- .../modernize/UseTrailingReturnTypeCheck.cpp | 19 ++++++++--- .../modernize/UseTrailingReturnTypeCheck.h | 1 + clang-tools-extra/docs/ReleaseNotes.rst | 10 ++++++ .../modernize/use-trailing-return-type.rst | 7 ++++ .../modernize/use-trailing-return-type.cpp | 32 ++++++++++++++++--- 5 files changed, 59 insertions(+), 10 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.cpp index ced4825f79a99..fceb5f9ed55cc 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.cpp @@ -8,8 +8,11 @@ #include "UseTrailingReturnTypeCheck.h" #include "clang/AST/ASTContext.h" +#include "clang/AST/ASTTypeTraits.h" +#include "clang/AST/DeclCXX.h" #include "clang/AST/RecursiveASTVisitor.h" #include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" #include "clang/Lex/Preprocessor.h" #include "clang/Tooling/FixIt.h" #include "llvm/ADT/StringExtras.h" @@ -457,6 +460,7 @@ static void keepSpecifiers(std::string &ReturnType, std::string &Auto, UseTrailingReturnTypeCheck::UseTrailingReturnTypeCheck( StringRef Name, ClangTidyContext *Context) : ClangTidyCheck(Name, Context), + WarnOnNonTrailingVoid(Options.get("WarnOnNonTrailingVoid", false)), TransformFunctions(Options.get("TransformFunctions", true)), TransformLambdas(Options.get("TransformLambdas", TransformLambda::All)) { @@ -469,18 +473,23 @@ UseTrailingReturnTypeCheck::UseTrailingReturnTypeCheck( void UseTrailingReturnTypeCheck::storeOptions( ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "WarnOnNonTrailingVoid", WarnOnNonTrailingVoid); Options.store(Opts, "TransformFunctions", TransformFunctions); Options.store(Opts, "TransformLambdas", TransformLambdas); } void UseTrailingReturnTypeCheck::registerMatchers(MatchFinder *Finder) { + const auto HasNoWrittenReturnType = + anyOf(cxxConversionDecl(), cxxConstructorDecl(), cxxDestructorDecl()); + auto F = - functionDecl( - unless(anyOf( - hasTrailingReturn(), returns(voidType()), cxxConversionDecl(), + traverse( + TK_IgnoreUnlessSpelledInSource, + functionDecl(unless(anyOf( + hasTrailingReturn(), HasNoWrittenReturnType, + WarnOnNonTrailingVoid ? unless(anything()) : returns(voidType()), cxxMethodDecl( - anyOf(isImplicit(), - hasParent(cxxRecordDecl(hasParent(lambdaExpr())))))))) + hasParent(cxxRecordDecl(hasParent(lambdaExpr())))))))) .bind("Func"); if (TransformFunctions) { diff --git a/clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.h b/clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.h index 91369919c5d36..6590328236d49 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.h +++ b/clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.h @@ -40,6 +40,7 @@ class UseTrailingReturnTypeCheck : public ClangTidyCheck { private: Preprocessor *PP = nullptr; + const bool WarnOnNonTrailingVoid; const bool TransformFunctions; const TransformLambda TransformLambdas; diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 2dc5c73073cf8..6523d220e2813 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -156,6 +156,11 @@ Changes in existing checks - Improved :doc:`misc-header-include-cycle <clang-tidy/checks/misc/header-include-cycle>` check performance. +- Improved :doc:`modernize-use-trailing-return-type + <clang-tidy/checks/modernize/use-trailing-return-type>` check by adding the + option `WarnOnNonTrailingVoid` that applies the check to ``void``-returning + functions that by default are excluded from this check. + - Improved :doc:`modernize-use-designated-initializers <clang-tidy/checks/modernize/use-designated-initializers>` check to suggest using designated initializers for aliased aggregate types. @@ -190,6 +195,11 @@ Changes in existing checks <clang-tidy/checks/readability/qualified-auto>` check by adding the option `IgnoreAliasing`, that allows not looking at underlying types of type aliases. +- Improved :doc:`performance-unnecessary-value-param + <clang-tidy/checks/performance/unnecessary-value-param>` check performance by + tolerating fix-it breaking compilation when functions is used as pointers + to avoid matching usage of functions within the current compilation unit. + Removed checks ^^^^^^^^^^^^^^ diff --git a/clang-tools-extra/docs/clang-tidy/checks/modernize/use-trailing-return-type.rst b/clang-tools-extra/docs/clang-tidy/checks/modernize/use-trailing-return-type.rst index 63b8885014e60..7ee67ca34c042 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/modernize/use-trailing-return-type.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/modernize/use-trailing-return-type.rst @@ -27,6 +27,13 @@ transforms to: virtual auto f3() const && -> float = delete; auto lambda = []() -> void {}; +Options +------- + +.. option:: WarnOnNonTrailingVoid + + If the option is set to `true`, the check will apply even to function + signatures with return type ``void``. Default is `false`. Limitations ----------- diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type.cpp index c16c033f0b016..6b645dc153e23 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type.cpp @@ -1,4 +1,12 @@ -// RUN: %check_clang_tidy -std=c++14-or-later %s modernize-use-trailing-return-type %t -- -- -fdeclspec -fexceptions -DCOMMAND_LINE_INT=int +// RUN: %check_clang_tidy \ +// RUN: -std=c++14-or-later %s modernize-use-trailing-return-type %t -- \ +// RUN: -- -fdeclspec -fexceptions -DCOMMAND_LINE_INT=int +// RUN: %check_clang_tidy -check-suffixes=,WARN-ON-NONTRAILING-VOID \ +// RUN: -std=c++14-or-later %s modernize-use-trailing-return-type %t -- \ +// RUN: -config="{CheckOptions: { \ +// RUN: modernize-use-trailing-return-type.WarnOnNonTrailingVoid: true, \ +// RUN: }}" \ +// RUN: -- -fdeclspec -fexceptions -DCOMMAND_LINE_INT=int namespace std { template <typename T> @@ -566,6 +574,24 @@ ostream& operator<<(ostream& ostream, int i); // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use a trailing return type for this function [modernize-use-trailing-return-type] // CHECK-FIXES: {{^}}ostream& operator<<(ostream& ostream, int i);{{$}} +// +// WarnOnNonTrailingVoid option +// + +void leadingVoid(); +// CHECK-MESSAGES-WARN-ON-NONTRAILING-VOID: :[[@LINE-1]]:6: warning: use a trailing return type for this function [modernize-use-trailing-return-type] +// CHECK-FIXES-WARN-ON-NONTRAILING-VOID: {{^}}auto leadingVoid() -> void;{{$}} +void leadingVoid(int arg); +// CHECK-MESSAGES-WARN-ON-NONTRAILING-VOID: :[[@LINE-1]]:6: warning: use a trailing return type for this function [modernize-use-trailing-return-type] +// CHECK-FIXES-WARN-ON-NONTRAILING-VOID: {{^}}auto leadingVoid(int arg) -> void;{{$}} +void leadingVoid(int arg) { return; } +// CHECK-MESSAGES-WARN-ON-NONTRAILING-VOID: :[[@LINE-1]]:6: warning: use a trailing return type for this function [modernize-use-trailing-return-type] +// CHECK-FIXES-WARN-ON-NONTRAILING-VOID: {{^}}auto leadingVoid(int arg) -> void { return; }{{$}} + +auto trailingVoid() -> void; +auto trailingVoid(int arg) -> void; +auto trailingVoid(int arg) -> void { return; } + // // Samples which do not trigger the check // @@ -579,10 +605,6 @@ template <typename T> auto f(T t) -> int; auto ff(); -void c(); -void c(int arg); -void c(int arg) { return; } - struct D2 : B { D2(); virtual ~D2(); >From 695d5bf06418bd0749569f0b4d00e7ce34ecf241 Mon Sep 17 00:00:00 2001 From: khuldraeseth <28711596+khuldraes...@users.noreply.github.com> Date: Thu, 7 Aug 2025 09:09:12 -0500 Subject: [PATCH 2/2] Update clang-tools-extra/docs/ReleaseNotes.rst Co-authored-by: Congcong Cai <congcongcai0...@163.com> --- clang-tools-extra/docs/ReleaseNotes.rst | 5 ----- 1 file changed, 5 deletions(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 6523d220e2813..31e2cf0ddba25 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -195,11 +195,6 @@ Changes in existing checks <clang-tidy/checks/readability/qualified-auto>` check by adding the option `IgnoreAliasing`, that allows not looking at underlying types of type aliases. -- Improved :doc:`performance-unnecessary-value-param - <clang-tidy/checks/performance/unnecessary-value-param>` check performance by - tolerating fix-it breaking compilation when functions is used as pointers - to avoid matching usage of functions within the current compilation unit. - Removed checks ^^^^^^^^^^^^^^ _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits