[clang-tools-extra] Add AllowFalseEvaluated flag to clang-tidy noexcept-move-constructor check (PR #126897)
Nechda wrote: Ask for review @PiotrZSL https://github.com/llvm/llvm-project/pull/126897 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] Add AllowFalseEvaluated flag to clang-tidy noexcept-move-constructor check (PR #126897)
https://github.com/Nechda updated https://github.com/llvm/llvm-project/pull/126897 >From 8baebe758b4f07933294b88bf2390fbb14ddeed0 Mon Sep 17 00:00:00 2001 From: Dmitry Nechitaev Date: Wed, 12 Feb 2025 14:30:47 +0300 Subject: [PATCH 1/2] Add AllowFalseEvaluated to clang-tidy noexcept-move-constructor check --- .../performance/NoexceptFunctionBaseCheck.cpp | 2 +- .../performance/NoexceptFunctionBaseCheck.h | 8 ++- ...move-constructor-allow-false-evaluated.cpp | 63 +++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp index 911cd1b533367..8371b6aafd8ba 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp @@ -30,7 +30,7 @@ void NoexceptFunctionBaseCheck::check(const MatchFinder::MatchResult &Result) { const Expr *NoexceptExpr = ProtoType->getNoexceptExpr(); if (NoexceptExpr) { NoexceptExpr = NoexceptExpr->IgnoreImplicit(); -if (!isa(NoexceptExpr)) +if (!isa(NoexceptExpr) && !AllowFalseEvaluated) reportNoexceptEvaluatedToFalse(FuncDecl, NoexceptExpr); return; } diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h index 4775219d7e439..cd1da1fbeca55 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h @@ -22,7 +22,8 @@ namespace clang::tidy::performance { class NoexceptFunctionBaseCheck : public ClangTidyCheck { public: NoexceptFunctionBaseCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + : ClangTidyCheck(Name, Context) + , AllowFalseEvaluated(Options.getLocalOrGlobal("AllowFalseEvaluated", false)) {} bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { return LangOpts.CPlusPlus11 && LangOpts.CXXExceptions; @@ -33,6 +34,10 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { return TK_IgnoreUnlessSpelledInSource; } + void storeOptions(ClangTidyOptions::OptionMap &Opts) override { +Options.store(Opts, "AllowFalseEvaluated", AllowFalseEvaluated); + } + protected: virtual DiagnosticBuilder reportMissingNoexcept(const FunctionDecl *FuncDecl) = 0; @@ -42,6 +47,7 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { static constexpr StringRef BindFuncDeclName = "FuncDecl"; private: + bool AllowFalseEvaluated; utils::ExceptionSpecAnalyzer SpecAnalyzer; }; diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp new file mode 100644 index 0..3552eaa7c50ea --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp @@ -0,0 +1,63 @@ +// RUN: %check_clang_tidy %s performance-noexcept-move-constructor %t -- -- -fexceptions + +// RUN: %check_clang_tidy -check-suffix=CONFIG %s performance-noexcept-move-constructor,performance-noexcept-destructor %t -- \ +// RUN: -config="{CheckOptions: {performance-noexcept-move-constructor.AllowFalseEvaluated: true}}" \ +// RUN: -- -fexceptions + +namespace std +{ + template + struct is_nothrow_move_constructible + { +static constexpr bool value = __is_nothrow_constructible(T, __add_rvalue_reference(T)); + }; +} // namespace std + +struct ThrowOnAnything { + ThrowOnAnything() noexcept(false); + ThrowOnAnything(ThrowOnAnything&&) noexcept(false); + // CHECK-MESSAGES-NOT: :[[@LINE-1]]:3: warning: move constructors should be marked noexcept + // CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:3: warning: move constructors should be marked noexcept + ThrowOnAnything& operator=(ThrowOnAnything &&) noexcept(false); + ~ThrowOnAnything() noexcept(false); +}; + +struct C_1 { +static constexpr bool kFalse = false; +C_1(C_1&&) noexcept(kFalse) = default; +// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor] +// CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:25: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor] + +C_1 &operator=(C_1 &&) noexcept(kFalse); +// CHECK-MESSAGES: :[[@LINE-1]]:37: warning: noexcept specifier on the move assignment operator evaluates to 'false' [performance-noexcept-move-constructor] +// CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:37: warning
[clang-tools-extra] Add AllowFalseEvaluated flag to clang-tidy noexcept-move-constructor check (PR #126897)
https://github.com/Nechda updated https://github.com/llvm/llvm-project/pull/126897 >From 8baebe758b4f07933294b88bf2390fbb14ddeed0 Mon Sep 17 00:00:00 2001 From: Dmitry Nechitaev Date: Wed, 12 Feb 2025 14:30:47 +0300 Subject: [PATCH 1/3] Add AllowFalseEvaluated to clang-tidy noexcept-move-constructor check --- .../performance/NoexceptFunctionBaseCheck.cpp | 2 +- .../performance/NoexceptFunctionBaseCheck.h | 8 ++- ...move-constructor-allow-false-evaluated.cpp | 63 +++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp index 911cd1b533367..8371b6aafd8ba 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp @@ -30,7 +30,7 @@ void NoexceptFunctionBaseCheck::check(const MatchFinder::MatchResult &Result) { const Expr *NoexceptExpr = ProtoType->getNoexceptExpr(); if (NoexceptExpr) { NoexceptExpr = NoexceptExpr->IgnoreImplicit(); -if (!isa(NoexceptExpr)) +if (!isa(NoexceptExpr) && !AllowFalseEvaluated) reportNoexceptEvaluatedToFalse(FuncDecl, NoexceptExpr); return; } diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h index 4775219d7e439..cd1da1fbeca55 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h @@ -22,7 +22,8 @@ namespace clang::tidy::performance { class NoexceptFunctionBaseCheck : public ClangTidyCheck { public: NoexceptFunctionBaseCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + : ClangTidyCheck(Name, Context) + , AllowFalseEvaluated(Options.getLocalOrGlobal("AllowFalseEvaluated", false)) {} bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { return LangOpts.CPlusPlus11 && LangOpts.CXXExceptions; @@ -33,6 +34,10 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { return TK_IgnoreUnlessSpelledInSource; } + void storeOptions(ClangTidyOptions::OptionMap &Opts) override { +Options.store(Opts, "AllowFalseEvaluated", AllowFalseEvaluated); + } + protected: virtual DiagnosticBuilder reportMissingNoexcept(const FunctionDecl *FuncDecl) = 0; @@ -42,6 +47,7 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { static constexpr StringRef BindFuncDeclName = "FuncDecl"; private: + bool AllowFalseEvaluated; utils::ExceptionSpecAnalyzer SpecAnalyzer; }; diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp new file mode 100644 index 0..3552eaa7c50ea --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp @@ -0,0 +1,63 @@ +// RUN: %check_clang_tidy %s performance-noexcept-move-constructor %t -- -- -fexceptions + +// RUN: %check_clang_tidy -check-suffix=CONFIG %s performance-noexcept-move-constructor,performance-noexcept-destructor %t -- \ +// RUN: -config="{CheckOptions: {performance-noexcept-move-constructor.AllowFalseEvaluated: true}}" \ +// RUN: -- -fexceptions + +namespace std +{ + template + struct is_nothrow_move_constructible + { +static constexpr bool value = __is_nothrow_constructible(T, __add_rvalue_reference(T)); + }; +} // namespace std + +struct ThrowOnAnything { + ThrowOnAnything() noexcept(false); + ThrowOnAnything(ThrowOnAnything&&) noexcept(false); + // CHECK-MESSAGES-NOT: :[[@LINE-1]]:3: warning: move constructors should be marked noexcept + // CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:3: warning: move constructors should be marked noexcept + ThrowOnAnything& operator=(ThrowOnAnything &&) noexcept(false); + ~ThrowOnAnything() noexcept(false); +}; + +struct C_1 { +static constexpr bool kFalse = false; +C_1(C_1&&) noexcept(kFalse) = default; +// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor] +// CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:25: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor] + +C_1 &operator=(C_1 &&) noexcept(kFalse); +// CHECK-MESSAGES: :[[@LINE-1]]:37: warning: noexcept specifier on the move assignment operator evaluates to 'false' [performance-noexcept-move-constructor] +// CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:37: warning
[clang-tools-extra] Add AllowFalseEvaluated flag to clang-tidy noexcept-move-constructor check (PR #126897)
https://github.com/Nechda updated https://github.com/llvm/llvm-project/pull/126897 >From 8baebe758b4f07933294b88bf2390fbb14ddeed0 Mon Sep 17 00:00:00 2001 From: Dmitry Nechitaev Date: Wed, 12 Feb 2025 14:30:47 +0300 Subject: [PATCH 1/4] Add AllowFalseEvaluated to clang-tidy noexcept-move-constructor check --- .../performance/NoexceptFunctionBaseCheck.cpp | 2 +- .../performance/NoexceptFunctionBaseCheck.h | 8 ++- ...move-constructor-allow-false-evaluated.cpp | 63 +++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp index 911cd1b533367..8371b6aafd8ba 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp @@ -30,7 +30,7 @@ void NoexceptFunctionBaseCheck::check(const MatchFinder::MatchResult &Result) { const Expr *NoexceptExpr = ProtoType->getNoexceptExpr(); if (NoexceptExpr) { NoexceptExpr = NoexceptExpr->IgnoreImplicit(); -if (!isa(NoexceptExpr)) +if (!isa(NoexceptExpr) && !AllowFalseEvaluated) reportNoexceptEvaluatedToFalse(FuncDecl, NoexceptExpr); return; } diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h index 4775219d7e439..cd1da1fbeca55 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h @@ -22,7 +22,8 @@ namespace clang::tidy::performance { class NoexceptFunctionBaseCheck : public ClangTidyCheck { public: NoexceptFunctionBaseCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + : ClangTidyCheck(Name, Context) + , AllowFalseEvaluated(Options.getLocalOrGlobal("AllowFalseEvaluated", false)) {} bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { return LangOpts.CPlusPlus11 && LangOpts.CXXExceptions; @@ -33,6 +34,10 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { return TK_IgnoreUnlessSpelledInSource; } + void storeOptions(ClangTidyOptions::OptionMap &Opts) override { +Options.store(Opts, "AllowFalseEvaluated", AllowFalseEvaluated); + } + protected: virtual DiagnosticBuilder reportMissingNoexcept(const FunctionDecl *FuncDecl) = 0; @@ -42,6 +47,7 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { static constexpr StringRef BindFuncDeclName = "FuncDecl"; private: + bool AllowFalseEvaluated; utils::ExceptionSpecAnalyzer SpecAnalyzer; }; diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp new file mode 100644 index 0..3552eaa7c50ea --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp @@ -0,0 +1,63 @@ +// RUN: %check_clang_tidy %s performance-noexcept-move-constructor %t -- -- -fexceptions + +// RUN: %check_clang_tidy -check-suffix=CONFIG %s performance-noexcept-move-constructor,performance-noexcept-destructor %t -- \ +// RUN: -config="{CheckOptions: {performance-noexcept-move-constructor.AllowFalseEvaluated: true}}" \ +// RUN: -- -fexceptions + +namespace std +{ + template + struct is_nothrow_move_constructible + { +static constexpr bool value = __is_nothrow_constructible(T, __add_rvalue_reference(T)); + }; +} // namespace std + +struct ThrowOnAnything { + ThrowOnAnything() noexcept(false); + ThrowOnAnything(ThrowOnAnything&&) noexcept(false); + // CHECK-MESSAGES-NOT: :[[@LINE-1]]:3: warning: move constructors should be marked noexcept + // CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:3: warning: move constructors should be marked noexcept + ThrowOnAnything& operator=(ThrowOnAnything &&) noexcept(false); + ~ThrowOnAnything() noexcept(false); +}; + +struct C_1 { +static constexpr bool kFalse = false; +C_1(C_1&&) noexcept(kFalse) = default; +// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor] +// CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:25: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor] + +C_1 &operator=(C_1 &&) noexcept(kFalse); +// CHECK-MESSAGES: :[[@LINE-1]]:37: warning: noexcept specifier on the move assignment operator evaluates to 'false' [performance-noexcept-move-constructor] +// CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:37: warning
[clang-tools-extra] Add AllowFalseEvaluated flag to clang-tidy noexcept-move-constructor check (PR #126897)
https://github.com/Nechda updated https://github.com/llvm/llvm-project/pull/126897 >From 8baebe758b4f07933294b88bf2390fbb14ddeed0 Mon Sep 17 00:00:00 2001 From: Dmitry Nechitaev Date: Wed, 12 Feb 2025 14:30:47 +0300 Subject: [PATCH 1/5] Add AllowFalseEvaluated to clang-tidy noexcept-move-constructor check --- .../performance/NoexceptFunctionBaseCheck.cpp | 2 +- .../performance/NoexceptFunctionBaseCheck.h | 8 ++- ...move-constructor-allow-false-evaluated.cpp | 63 +++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp index 911cd1b533367..8371b6aafd8ba 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp @@ -30,7 +30,7 @@ void NoexceptFunctionBaseCheck::check(const MatchFinder::MatchResult &Result) { const Expr *NoexceptExpr = ProtoType->getNoexceptExpr(); if (NoexceptExpr) { NoexceptExpr = NoexceptExpr->IgnoreImplicit(); -if (!isa(NoexceptExpr)) +if (!isa(NoexceptExpr) && !AllowFalseEvaluated) reportNoexceptEvaluatedToFalse(FuncDecl, NoexceptExpr); return; } diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h index 4775219d7e439..cd1da1fbeca55 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h @@ -22,7 +22,8 @@ namespace clang::tidy::performance { class NoexceptFunctionBaseCheck : public ClangTidyCheck { public: NoexceptFunctionBaseCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + : ClangTidyCheck(Name, Context) + , AllowFalseEvaluated(Options.getLocalOrGlobal("AllowFalseEvaluated", false)) {} bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { return LangOpts.CPlusPlus11 && LangOpts.CXXExceptions; @@ -33,6 +34,10 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { return TK_IgnoreUnlessSpelledInSource; } + void storeOptions(ClangTidyOptions::OptionMap &Opts) override { +Options.store(Opts, "AllowFalseEvaluated", AllowFalseEvaluated); + } + protected: virtual DiagnosticBuilder reportMissingNoexcept(const FunctionDecl *FuncDecl) = 0; @@ -42,6 +47,7 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { static constexpr StringRef BindFuncDeclName = "FuncDecl"; private: + bool AllowFalseEvaluated; utils::ExceptionSpecAnalyzer SpecAnalyzer; }; diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp new file mode 100644 index 0..3552eaa7c50ea --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp @@ -0,0 +1,63 @@ +// RUN: %check_clang_tidy %s performance-noexcept-move-constructor %t -- -- -fexceptions + +// RUN: %check_clang_tidy -check-suffix=CONFIG %s performance-noexcept-move-constructor,performance-noexcept-destructor %t -- \ +// RUN: -config="{CheckOptions: {performance-noexcept-move-constructor.AllowFalseEvaluated: true}}" \ +// RUN: -- -fexceptions + +namespace std +{ + template + struct is_nothrow_move_constructible + { +static constexpr bool value = __is_nothrow_constructible(T, __add_rvalue_reference(T)); + }; +} // namespace std + +struct ThrowOnAnything { + ThrowOnAnything() noexcept(false); + ThrowOnAnything(ThrowOnAnything&&) noexcept(false); + // CHECK-MESSAGES-NOT: :[[@LINE-1]]:3: warning: move constructors should be marked noexcept + // CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:3: warning: move constructors should be marked noexcept + ThrowOnAnything& operator=(ThrowOnAnything &&) noexcept(false); + ~ThrowOnAnything() noexcept(false); +}; + +struct C_1 { +static constexpr bool kFalse = false; +C_1(C_1&&) noexcept(kFalse) = default; +// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor] +// CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:25: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor] + +C_1 &operator=(C_1 &&) noexcept(kFalse); +// CHECK-MESSAGES: :[[@LINE-1]]:37: warning: noexcept specifier on the move assignment operator evaluates to 'false' [performance-noexcept-move-constructor] +// CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:37: warning
[clang-tools-extra] Add AllowFalseEvaluated flag to clang-tidy noexcept-move-constructor check (PR #126897)
https://github.com/Nechda updated https://github.com/llvm/llvm-project/pull/126897 >From 6b9b6e44b7b122721a4371dbe20618a90753f666 Mon Sep 17 00:00:00 2001 From: Dmitry Nechitaev Date: Mon, 17 Feb 2025 13:30:46 +0300 Subject: [PATCH] Add AllowFalseEvaluated to clang-tidy noexcept-move-constructor check --- .../performance/NoexceptFunctionBaseCheck.cpp | 2 +- .../performance/NoexceptFunctionBaseCheck.h | 8 ++- clang-tools-extra/docs/ReleaseNotes.rst | 6 ++ .../performance/noexcept-move-constructor.rst | 9 +++ ...move-constructor-allow-false-evaluated.cpp | 63 +++ 5 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp index 911cd1b533367..8371b6aafd8ba 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp @@ -30,7 +30,7 @@ void NoexceptFunctionBaseCheck::check(const MatchFinder::MatchResult &Result) { const Expr *NoexceptExpr = ProtoType->getNoexceptExpr(); if (NoexceptExpr) { NoexceptExpr = NoexceptExpr->IgnoreImplicit(); -if (!isa(NoexceptExpr)) +if (!isa(NoexceptExpr) && !AllowFalseEvaluated) reportNoexceptEvaluatedToFalse(FuncDecl, NoexceptExpr); return; } diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h index 4775219d7e439..cd1da1fbeca55 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h @@ -22,7 +22,8 @@ namespace clang::tidy::performance { class NoexceptFunctionBaseCheck : public ClangTidyCheck { public: NoexceptFunctionBaseCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + : ClangTidyCheck(Name, Context) + , AllowFalseEvaluated(Options.getLocalOrGlobal("AllowFalseEvaluated", false)) {} bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { return LangOpts.CPlusPlus11 && LangOpts.CXXExceptions; @@ -33,6 +34,10 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { return TK_IgnoreUnlessSpelledInSource; } + void storeOptions(ClangTidyOptions::OptionMap &Opts) override { +Options.store(Opts, "AllowFalseEvaluated", AllowFalseEvaluated); + } + protected: virtual DiagnosticBuilder reportMissingNoexcept(const FunctionDecl *FuncDecl) = 0; @@ -42,6 +47,7 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { static constexpr StringRef BindFuncDeclName = "FuncDecl"; private: + bool AllowFalseEvaluated; utils::ExceptionSpecAnalyzer SpecAnalyzer; }; diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 6b8fe22242417..457b0dbeb2383 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -110,6 +110,12 @@ Changes in existing checks ` check by providing additional examples and fixing some macro related false positives. +- Improved :doc:`performance-noexcept-move-constructor + ` check by adding + a new (off-by-default) option `AllowFalseEvaluated`, which allows marking + move constructors with ``noexcept(expr)`` even if ``expr`` + evaluates to ``false``. + Removed checks ^^ diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance/noexcept-move-constructor.rst b/clang-tools-extra/docs/clang-tidy/checks/performance/noexcept-move-constructor.rst index 05f1d85f1af5a..288343723b19a 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/performance/noexcept-move-constructor.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/performance/noexcept-move-constructor.rst @@ -11,3 +11,12 @@ evaluates to ``false`` (but is not a ``false`` literal itself). Move constructors of all the types used with STL containers, for example, need to be declared ``noexcept``. Otherwise STL will choose copy constructors instead. The same is valid for move assignment operations. + +Options +--- + +.. option:: AllowFalseEvaluated + +When `true`, the check will not generate any warning +if the ``expr`` in ``noexcept(expr)`` evaluates to ``false``. +Default is `false`. diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp new file mode 100644 index 0..3552eaa7c50ea --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp @@ -0,0 +1,63 @@ +// R
[clang-tools-extra] Add AllowFalseEvaluated flag to clang-tidy noexcept-move-constructor check (PR #126897)
https://github.com/Nechda updated https://github.com/llvm/llvm-project/pull/126897 >From 4d5b0beebec8f3b2f5c6f90d7d26bde53118a26a Mon Sep 17 00:00:00 2001 From: Dmitry Nechitaev Date: Mon, 17 Feb 2025 13:30:46 +0300 Subject: [PATCH] Add AllowFalseEvaluated to clang-tidy noexcept-move-constructor check --- .../performance/NoexceptFunctionBaseCheck.cpp | 2 +- .../performance/NoexceptFunctionBaseCheck.h | 8 ++- clang-tools-extra/docs/ReleaseNotes.rst | 6 ++ .../performance/noexcept-move-constructor.rst | 9 +++ ...move-constructor-allow-false-evaluated.cpp | 63 +++ 5 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp index 911cd1b533367..8371b6aafd8ba 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp @@ -30,7 +30,7 @@ void NoexceptFunctionBaseCheck::check(const MatchFinder::MatchResult &Result) { const Expr *NoexceptExpr = ProtoType->getNoexceptExpr(); if (NoexceptExpr) { NoexceptExpr = NoexceptExpr->IgnoreImplicit(); -if (!isa(NoexceptExpr)) +if (!isa(NoexceptExpr) && !AllowFalseEvaluated) reportNoexceptEvaluatedToFalse(FuncDecl, NoexceptExpr); return; } diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h index 4775219d7e439..cd1da1fbeca55 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h @@ -22,7 +22,8 @@ namespace clang::tidy::performance { class NoexceptFunctionBaseCheck : public ClangTidyCheck { public: NoexceptFunctionBaseCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + : ClangTidyCheck(Name, Context) + , AllowFalseEvaluated(Options.getLocalOrGlobal("AllowFalseEvaluated", false)) {} bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { return LangOpts.CPlusPlus11 && LangOpts.CXXExceptions; @@ -33,6 +34,10 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { return TK_IgnoreUnlessSpelledInSource; } + void storeOptions(ClangTidyOptions::OptionMap &Opts) override { +Options.store(Opts, "AllowFalseEvaluated", AllowFalseEvaluated); + } + protected: virtual DiagnosticBuilder reportMissingNoexcept(const FunctionDecl *FuncDecl) = 0; @@ -42,6 +47,7 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { static constexpr StringRef BindFuncDeclName = "FuncDecl"; private: + bool AllowFalseEvaluated; utils::ExceptionSpecAnalyzer SpecAnalyzer; }; diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 6b8fe22242417..457b0dbeb2383 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -110,6 +110,12 @@ Changes in existing checks ` check by providing additional examples and fixing some macro related false positives. +- Improved :doc:`performance-noexcept-move-constructor + ` check by adding + a new (off-by-default) option `AllowFalseEvaluated`, which allows marking + move constructors with ``noexcept(expr)`` even if ``expr`` + evaluates to ``false``. + Removed checks ^^ diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance/noexcept-move-constructor.rst b/clang-tools-extra/docs/clang-tidy/checks/performance/noexcept-move-constructor.rst index 05f1d85f1af5a..288343723b19a 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/performance/noexcept-move-constructor.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/performance/noexcept-move-constructor.rst @@ -11,3 +11,12 @@ evaluates to ``false`` (but is not a ``false`` literal itself). Move constructors of all the types used with STL containers, for example, need to be declared ``noexcept``. Otherwise STL will choose copy constructors instead. The same is valid for move assignment operations. + +Options +--- + +.. option:: AllowFalseEvaluated + +When `true`, the check will not generate any warning +if the ``expr`` in ``noexcept(expr)`` evaluates to ``false``. +Default is `false`. diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp new file mode 100644 index 0..3552eaa7c50ea --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp @@ -0,0 +1,63 @@ +// R
[clang-tools-extra] Add AllowFalseEvaluated flag to clang-tidy noexcept-move-constructor check (PR #126897)
https://github.com/Nechda updated https://github.com/llvm/llvm-project/pull/126897 >From 2d09fb25fcb82a1e05403d520f9e541486b0eb82 Mon Sep 17 00:00:00 2001 From: Dmitry Nechitaev Date: Mon, 17 Feb 2025 12:43:29 +0300 Subject: [PATCH 1/2] Add AllowFalseEvaluated to clang-tidy noexcept-move-constructor check --- .../performance/NoexceptFunctionBaseCheck.cpp | 2 +- .../performance/NoexceptFunctionBaseCheck.h | 8 ++- ...move-constructor-allow-false-evaluated.cpp | 63 +++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp index 911cd1b533367..8371b6aafd8ba 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp @@ -30,7 +30,7 @@ void NoexceptFunctionBaseCheck::check(const MatchFinder::MatchResult &Result) { const Expr *NoexceptExpr = ProtoType->getNoexceptExpr(); if (NoexceptExpr) { NoexceptExpr = NoexceptExpr->IgnoreImplicit(); -if (!isa(NoexceptExpr)) +if (!isa(NoexceptExpr) && !AllowFalseEvaluated) reportNoexceptEvaluatedToFalse(FuncDecl, NoexceptExpr); return; } diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h index 4775219d7e439..cd1da1fbeca55 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h @@ -22,7 +22,8 @@ namespace clang::tidy::performance { class NoexceptFunctionBaseCheck : public ClangTidyCheck { public: NoexceptFunctionBaseCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + : ClangTidyCheck(Name, Context) + , AllowFalseEvaluated(Options.getLocalOrGlobal("AllowFalseEvaluated", false)) {} bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { return LangOpts.CPlusPlus11 && LangOpts.CXXExceptions; @@ -33,6 +34,10 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { return TK_IgnoreUnlessSpelledInSource; } + void storeOptions(ClangTidyOptions::OptionMap &Opts) override { +Options.store(Opts, "AllowFalseEvaluated", AllowFalseEvaluated); + } + protected: virtual DiagnosticBuilder reportMissingNoexcept(const FunctionDecl *FuncDecl) = 0; @@ -42,6 +47,7 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { static constexpr StringRef BindFuncDeclName = "FuncDecl"; private: + bool AllowFalseEvaluated; utils::ExceptionSpecAnalyzer SpecAnalyzer; }; diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp new file mode 100644 index 0..3552eaa7c50ea --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp @@ -0,0 +1,63 @@ +// RUN: %check_clang_tidy %s performance-noexcept-move-constructor %t -- -- -fexceptions + +// RUN: %check_clang_tidy -check-suffix=CONFIG %s performance-noexcept-move-constructor,performance-noexcept-destructor %t -- \ +// RUN: -config="{CheckOptions: {performance-noexcept-move-constructor.AllowFalseEvaluated: true}}" \ +// RUN: -- -fexceptions + +namespace std +{ + template + struct is_nothrow_move_constructible + { +static constexpr bool value = __is_nothrow_constructible(T, __add_rvalue_reference(T)); + }; +} // namespace std + +struct ThrowOnAnything { + ThrowOnAnything() noexcept(false); + ThrowOnAnything(ThrowOnAnything&&) noexcept(false); + // CHECK-MESSAGES-NOT: :[[@LINE-1]]:3: warning: move constructors should be marked noexcept + // CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:3: warning: move constructors should be marked noexcept + ThrowOnAnything& operator=(ThrowOnAnything &&) noexcept(false); + ~ThrowOnAnything() noexcept(false); +}; + +struct C_1 { +static constexpr bool kFalse = false; +C_1(C_1&&) noexcept(kFalse) = default; +// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor] +// CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:25: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor] + +C_1 &operator=(C_1 &&) noexcept(kFalse); +// CHECK-MESSAGES: :[[@LINE-1]]:37: warning: noexcept specifier on the move assignment operator evaluates to 'false' [performance-noexcept-move-constructor] +// CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:37: warning
[clang-tools-extra] Add AllowFalseEvaluated flag to clang-tidy noexcept-move-constructor check (PR #126897)
https://github.com/Nechda updated https://github.com/llvm/llvm-project/pull/126897 >From 4cb769546f81634e6734859d3232084aa8aa33e8 Mon Sep 17 00:00:00 2001 From: Dmitry Nechitaev Date: Mon, 17 Feb 2025 12:55:04 +0300 Subject: [PATCH 1/2] Add AllowFalseEvaluated to clang-tidy noexcept-move-constructor check --- .../performance/NoexceptFunctionBaseCheck.cpp | 2 +- .../performance/NoexceptFunctionBaseCheck.h | 8 ++- ...move-constructor-allow-false-evaluated.cpp | 63 +++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp index 911cd1b533367..8371b6aafd8ba 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp @@ -30,7 +30,7 @@ void NoexceptFunctionBaseCheck::check(const MatchFinder::MatchResult &Result) { const Expr *NoexceptExpr = ProtoType->getNoexceptExpr(); if (NoexceptExpr) { NoexceptExpr = NoexceptExpr->IgnoreImplicit(); -if (!isa(NoexceptExpr)) +if (!isa(NoexceptExpr) && !AllowFalseEvaluated) reportNoexceptEvaluatedToFalse(FuncDecl, NoexceptExpr); return; } diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h index 4775219d7e439..cd1da1fbeca55 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h @@ -22,7 +22,8 @@ namespace clang::tidy::performance { class NoexceptFunctionBaseCheck : public ClangTidyCheck { public: NoexceptFunctionBaseCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + : ClangTidyCheck(Name, Context) + , AllowFalseEvaluated(Options.getLocalOrGlobal("AllowFalseEvaluated", false)) {} bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { return LangOpts.CPlusPlus11 && LangOpts.CXXExceptions; @@ -33,6 +34,10 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { return TK_IgnoreUnlessSpelledInSource; } + void storeOptions(ClangTidyOptions::OptionMap &Opts) override { +Options.store(Opts, "AllowFalseEvaluated", AllowFalseEvaluated); + } + protected: virtual DiagnosticBuilder reportMissingNoexcept(const FunctionDecl *FuncDecl) = 0; @@ -42,6 +47,7 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { static constexpr StringRef BindFuncDeclName = "FuncDecl"; private: + bool AllowFalseEvaluated; utils::ExceptionSpecAnalyzer SpecAnalyzer; }; diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp new file mode 100644 index 0..3552eaa7c50ea --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp @@ -0,0 +1,63 @@ +// RUN: %check_clang_tidy %s performance-noexcept-move-constructor %t -- -- -fexceptions + +// RUN: %check_clang_tidy -check-suffix=CONFIG %s performance-noexcept-move-constructor,performance-noexcept-destructor %t -- \ +// RUN: -config="{CheckOptions: {performance-noexcept-move-constructor.AllowFalseEvaluated: true}}" \ +// RUN: -- -fexceptions + +namespace std +{ + template + struct is_nothrow_move_constructible + { +static constexpr bool value = __is_nothrow_constructible(T, __add_rvalue_reference(T)); + }; +} // namespace std + +struct ThrowOnAnything { + ThrowOnAnything() noexcept(false); + ThrowOnAnything(ThrowOnAnything&&) noexcept(false); + // CHECK-MESSAGES-NOT: :[[@LINE-1]]:3: warning: move constructors should be marked noexcept + // CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:3: warning: move constructors should be marked noexcept + ThrowOnAnything& operator=(ThrowOnAnything &&) noexcept(false); + ~ThrowOnAnything() noexcept(false); +}; + +struct C_1 { +static constexpr bool kFalse = false; +C_1(C_1&&) noexcept(kFalse) = default; +// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor] +// CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:25: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor] + +C_1 &operator=(C_1 &&) noexcept(kFalse); +// CHECK-MESSAGES: :[[@LINE-1]]:37: warning: noexcept specifier on the move assignment operator evaluates to 'false' [performance-noexcept-move-constructor] +// CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:37: warning
[clang-tools-extra] Add AllowFalseEvaluated flag to clang-tidy noexcept-move-constructor check (PR #126897)
https://github.com/Nechda updated https://github.com/llvm/llvm-project/pull/126897 >From 4cb769546f81634e6734859d3232084aa8aa33e8 Mon Sep 17 00:00:00 2001 From: Dmitry Nechitaev Date: Mon, 17 Feb 2025 12:55:04 +0300 Subject: [PATCH 1/2] Add AllowFalseEvaluated to clang-tidy noexcept-move-constructor check --- .../performance/NoexceptFunctionBaseCheck.cpp | 2 +- .../performance/NoexceptFunctionBaseCheck.h | 8 ++- ...move-constructor-allow-false-evaluated.cpp | 63 +++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp index 911cd1b533367..8371b6aafd8ba 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp @@ -30,7 +30,7 @@ void NoexceptFunctionBaseCheck::check(const MatchFinder::MatchResult &Result) { const Expr *NoexceptExpr = ProtoType->getNoexceptExpr(); if (NoexceptExpr) { NoexceptExpr = NoexceptExpr->IgnoreImplicit(); -if (!isa(NoexceptExpr)) +if (!isa(NoexceptExpr) && !AllowFalseEvaluated) reportNoexceptEvaluatedToFalse(FuncDecl, NoexceptExpr); return; } diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h index 4775219d7e439..cd1da1fbeca55 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h @@ -22,7 +22,8 @@ namespace clang::tidy::performance { class NoexceptFunctionBaseCheck : public ClangTidyCheck { public: NoexceptFunctionBaseCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + : ClangTidyCheck(Name, Context) + , AllowFalseEvaluated(Options.getLocalOrGlobal("AllowFalseEvaluated", false)) {} bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { return LangOpts.CPlusPlus11 && LangOpts.CXXExceptions; @@ -33,6 +34,10 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { return TK_IgnoreUnlessSpelledInSource; } + void storeOptions(ClangTidyOptions::OptionMap &Opts) override { +Options.store(Opts, "AllowFalseEvaluated", AllowFalseEvaluated); + } + protected: virtual DiagnosticBuilder reportMissingNoexcept(const FunctionDecl *FuncDecl) = 0; @@ -42,6 +47,7 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { static constexpr StringRef BindFuncDeclName = "FuncDecl"; private: + bool AllowFalseEvaluated; utils::ExceptionSpecAnalyzer SpecAnalyzer; }; diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp new file mode 100644 index 0..3552eaa7c50ea --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp @@ -0,0 +1,63 @@ +// RUN: %check_clang_tidy %s performance-noexcept-move-constructor %t -- -- -fexceptions + +// RUN: %check_clang_tidy -check-suffix=CONFIG %s performance-noexcept-move-constructor,performance-noexcept-destructor %t -- \ +// RUN: -config="{CheckOptions: {performance-noexcept-move-constructor.AllowFalseEvaluated: true}}" \ +// RUN: -- -fexceptions + +namespace std +{ + template + struct is_nothrow_move_constructible + { +static constexpr bool value = __is_nothrow_constructible(T, __add_rvalue_reference(T)); + }; +} // namespace std + +struct ThrowOnAnything { + ThrowOnAnything() noexcept(false); + ThrowOnAnything(ThrowOnAnything&&) noexcept(false); + // CHECK-MESSAGES-NOT: :[[@LINE-1]]:3: warning: move constructors should be marked noexcept + // CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:3: warning: move constructors should be marked noexcept + ThrowOnAnything& operator=(ThrowOnAnything &&) noexcept(false); + ~ThrowOnAnything() noexcept(false); +}; + +struct C_1 { +static constexpr bool kFalse = false; +C_1(C_1&&) noexcept(kFalse) = default; +// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor] +// CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:25: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor] + +C_1 &operator=(C_1 &&) noexcept(kFalse); +// CHECK-MESSAGES: :[[@LINE-1]]:37: warning: noexcept specifier on the move assignment operator evaluates to 'false' [performance-noexcept-move-constructor] +// CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:37: warning
[clang-tools-extra] Add AllowFalseEvaluated flag to clang-tidy noexcept-move-constructor check (PR #126897)
https://github.com/Nechda updated https://github.com/llvm/llvm-project/pull/126897 >From 7b9028f66ec305f734ff29ffcfd623b0bb993f52 Mon Sep 17 00:00:00 2001 From: Dmitry Nechitaev Date: Mon, 17 Feb 2025 13:16:09 +0300 Subject: [PATCH] Add AllowFalseEvaluated to clang-tidy noexcept-move-constructor check --- .../performance/NoexceptFunctionBaseCheck.cpp | 2 +- .../performance/NoexceptFunctionBaseCheck.h | 8 ++- clang-tools-extra/docs/ReleaseNotes.rst | 6 ++ .../performance/noexcept-move-constructor.rst | 9 +++ ...move-constructor-allow-false-evaluated.cpp | 63 +++ 5 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp index 911cd1b533367..8371b6aafd8ba 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp @@ -30,7 +30,7 @@ void NoexceptFunctionBaseCheck::check(const MatchFinder::MatchResult &Result) { const Expr *NoexceptExpr = ProtoType->getNoexceptExpr(); if (NoexceptExpr) { NoexceptExpr = NoexceptExpr->IgnoreImplicit(); -if (!isa(NoexceptExpr)) +if (!isa(NoexceptExpr) && !AllowFalseEvaluated) reportNoexceptEvaluatedToFalse(FuncDecl, NoexceptExpr); return; } diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h index 4775219d7e439..cd1da1fbeca55 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h @@ -22,7 +22,8 @@ namespace clang::tidy::performance { class NoexceptFunctionBaseCheck : public ClangTidyCheck { public: NoexceptFunctionBaseCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + : ClangTidyCheck(Name, Context) + , AllowFalseEvaluated(Options.getLocalOrGlobal("AllowFalseEvaluated", false)) {} bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { return LangOpts.CPlusPlus11 && LangOpts.CXXExceptions; @@ -33,6 +34,10 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { return TK_IgnoreUnlessSpelledInSource; } + void storeOptions(ClangTidyOptions::OptionMap &Opts) override { +Options.store(Opts, "AllowFalseEvaluated", AllowFalseEvaluated); + } + protected: virtual DiagnosticBuilder reportMissingNoexcept(const FunctionDecl *FuncDecl) = 0; @@ -42,6 +47,7 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { static constexpr StringRef BindFuncDeclName = "FuncDecl"; private: + bool AllowFalseEvaluated; utils::ExceptionSpecAnalyzer SpecAnalyzer; }; diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 6b8fe22242417..457b0dbeb2383 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -110,6 +110,12 @@ Changes in existing checks ` check by providing additional examples and fixing some macro related false positives. +- Improved :doc:`performance-noexcept-move-constructor + ` check by adding + a new (off-by-default) option `AllowFalseEvaluated`, which allows marking + move constructors with ``noexcept(expr)`` even if ``expr`` + evaluates to ``false``. + Removed checks ^^ diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance/noexcept-move-constructor.rst b/clang-tools-extra/docs/clang-tidy/checks/performance/noexcept-move-constructor.rst index 05f1d85f1af5a..288343723b19a 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/performance/noexcept-move-constructor.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/performance/noexcept-move-constructor.rst @@ -11,3 +11,12 @@ evaluates to ``false`` (but is not a ``false`` literal itself). Move constructors of all the types used with STL containers, for example, need to be declared ``noexcept``. Otherwise STL will choose copy constructors instead. The same is valid for move assignment operations. + +Options +--- + +.. option:: AllowFalseEvaluated + +When `true`, the check will not generate any warning +if the ``expr`` in ``noexcept(expr)`` evaluates to ``false``. +Default is `false`. diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp new file mode 100644 index 0..3552eaa7c50ea --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp @@ -0,0 +1,63 @@ +// R
[clang-tools-extra] Add AllowFalseEvaluated flag to clang-tidy noexcept-move-constructor check (PR #126897)
Nechda wrote: I apologize for the spam in my emails. I am having some issues with the GitHub UI. The changes to the branch are only related to merging commits into one. https://github.com/llvm/llvm-project/pull/126897 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] Add AllowFalseEvaluated flag to clang-tidy noexcept-move-constructor check (PR #126897)
https://github.com/Nechda updated https://github.com/llvm/llvm-project/pull/126897 >From 2d09fb25fcb82a1e05403d520f9e541486b0eb82 Mon Sep 17 00:00:00 2001 From: Dmitry Nechitaev Date: Mon, 17 Feb 2025 12:43:29 +0300 Subject: [PATCH 1/2] Add AllowFalseEvaluated to clang-tidy noexcept-move-constructor check --- .../performance/NoexceptFunctionBaseCheck.cpp | 2 +- .../performance/NoexceptFunctionBaseCheck.h | 8 ++- ...move-constructor-allow-false-evaluated.cpp | 63 +++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp index 911cd1b533367..8371b6aafd8ba 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp @@ -30,7 +30,7 @@ void NoexceptFunctionBaseCheck::check(const MatchFinder::MatchResult &Result) { const Expr *NoexceptExpr = ProtoType->getNoexceptExpr(); if (NoexceptExpr) { NoexceptExpr = NoexceptExpr->IgnoreImplicit(); -if (!isa(NoexceptExpr)) +if (!isa(NoexceptExpr) && !AllowFalseEvaluated) reportNoexceptEvaluatedToFalse(FuncDecl, NoexceptExpr); return; } diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h index 4775219d7e439..cd1da1fbeca55 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h @@ -22,7 +22,8 @@ namespace clang::tidy::performance { class NoexceptFunctionBaseCheck : public ClangTidyCheck { public: NoexceptFunctionBaseCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + : ClangTidyCheck(Name, Context) + , AllowFalseEvaluated(Options.getLocalOrGlobal("AllowFalseEvaluated", false)) {} bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { return LangOpts.CPlusPlus11 && LangOpts.CXXExceptions; @@ -33,6 +34,10 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { return TK_IgnoreUnlessSpelledInSource; } + void storeOptions(ClangTidyOptions::OptionMap &Opts) override { +Options.store(Opts, "AllowFalseEvaluated", AllowFalseEvaluated); + } + protected: virtual DiagnosticBuilder reportMissingNoexcept(const FunctionDecl *FuncDecl) = 0; @@ -42,6 +47,7 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { static constexpr StringRef BindFuncDeclName = "FuncDecl"; private: + bool AllowFalseEvaluated; utils::ExceptionSpecAnalyzer SpecAnalyzer; }; diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp new file mode 100644 index 0..3552eaa7c50ea --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp @@ -0,0 +1,63 @@ +// RUN: %check_clang_tidy %s performance-noexcept-move-constructor %t -- -- -fexceptions + +// RUN: %check_clang_tidy -check-suffix=CONFIG %s performance-noexcept-move-constructor,performance-noexcept-destructor %t -- \ +// RUN: -config="{CheckOptions: {performance-noexcept-move-constructor.AllowFalseEvaluated: true}}" \ +// RUN: -- -fexceptions + +namespace std +{ + template + struct is_nothrow_move_constructible + { +static constexpr bool value = __is_nothrow_constructible(T, __add_rvalue_reference(T)); + }; +} // namespace std + +struct ThrowOnAnything { + ThrowOnAnything() noexcept(false); + ThrowOnAnything(ThrowOnAnything&&) noexcept(false); + // CHECK-MESSAGES-NOT: :[[@LINE-1]]:3: warning: move constructors should be marked noexcept + // CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:3: warning: move constructors should be marked noexcept + ThrowOnAnything& operator=(ThrowOnAnything &&) noexcept(false); + ~ThrowOnAnything() noexcept(false); +}; + +struct C_1 { +static constexpr bool kFalse = false; +C_1(C_1&&) noexcept(kFalse) = default; +// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor] +// CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:25: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor] + +C_1 &operator=(C_1 &&) noexcept(kFalse); +// CHECK-MESSAGES: :[[@LINE-1]]:37: warning: noexcept specifier on the move assignment operator evaluates to 'false' [performance-noexcept-move-constructor] +// CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:37: warning
[clang-tools-extra] Add AllowFalseEvaluated flag to clang-tidy noexcept-move-constructor check (PR #126897)
https://github.com/Nechda updated https://github.com/llvm/llvm-project/pull/126897 >From c521c70a89e619de0c460ce416fe7920c972a01f Mon Sep 17 00:00:00 2001 From: Dmitry Nechitaev Date: Mon, 17 Feb 2025 12:43:29 +0300 Subject: [PATCH 1/2] Add AllowFalseEvaluated to clang-tidy noexcept-move-constructor check --- .../performance/NoexceptFunctionBaseCheck.cpp | 2 +- .../performance/NoexceptFunctionBaseCheck.h | 8 ++- ...move-constructor-allow-false-evaluated.cpp | 63 +++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp index 911cd1b533367..8371b6aafd8ba 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp @@ -30,7 +30,7 @@ void NoexceptFunctionBaseCheck::check(const MatchFinder::MatchResult &Result) { const Expr *NoexceptExpr = ProtoType->getNoexceptExpr(); if (NoexceptExpr) { NoexceptExpr = NoexceptExpr->IgnoreImplicit(); -if (!isa(NoexceptExpr)) +if (!isa(NoexceptExpr) && !AllowFalseEvaluated) reportNoexceptEvaluatedToFalse(FuncDecl, NoexceptExpr); return; } diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h index 4775219d7e439..cd1da1fbeca55 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h @@ -22,7 +22,8 @@ namespace clang::tidy::performance { class NoexceptFunctionBaseCheck : public ClangTidyCheck { public: NoexceptFunctionBaseCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + : ClangTidyCheck(Name, Context) + , AllowFalseEvaluated(Options.getLocalOrGlobal("AllowFalseEvaluated", false)) {} bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { return LangOpts.CPlusPlus11 && LangOpts.CXXExceptions; @@ -33,6 +34,10 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { return TK_IgnoreUnlessSpelledInSource; } + void storeOptions(ClangTidyOptions::OptionMap &Opts) override { +Options.store(Opts, "AllowFalseEvaluated", AllowFalseEvaluated); + } + protected: virtual DiagnosticBuilder reportMissingNoexcept(const FunctionDecl *FuncDecl) = 0; @@ -42,6 +47,7 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { static constexpr StringRef BindFuncDeclName = "FuncDecl"; private: + bool AllowFalseEvaluated; utils::ExceptionSpecAnalyzer SpecAnalyzer; }; diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp new file mode 100644 index 0..3552eaa7c50ea --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp @@ -0,0 +1,63 @@ +// RUN: %check_clang_tidy %s performance-noexcept-move-constructor %t -- -- -fexceptions + +// RUN: %check_clang_tidy -check-suffix=CONFIG %s performance-noexcept-move-constructor,performance-noexcept-destructor %t -- \ +// RUN: -config="{CheckOptions: {performance-noexcept-move-constructor.AllowFalseEvaluated: true}}" \ +// RUN: -- -fexceptions + +namespace std +{ + template + struct is_nothrow_move_constructible + { +static constexpr bool value = __is_nothrow_constructible(T, __add_rvalue_reference(T)); + }; +} // namespace std + +struct ThrowOnAnything { + ThrowOnAnything() noexcept(false); + ThrowOnAnything(ThrowOnAnything&&) noexcept(false); + // CHECK-MESSAGES-NOT: :[[@LINE-1]]:3: warning: move constructors should be marked noexcept + // CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:3: warning: move constructors should be marked noexcept + ThrowOnAnything& operator=(ThrowOnAnything &&) noexcept(false); + ~ThrowOnAnything() noexcept(false); +}; + +struct C_1 { +static constexpr bool kFalse = false; +C_1(C_1&&) noexcept(kFalse) = default; +// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor] +// CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:25: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor] + +C_1 &operator=(C_1 &&) noexcept(kFalse); +// CHECK-MESSAGES: :[[@LINE-1]]:37: warning: noexcept specifier on the move assignment operator evaluates to 'false' [performance-noexcept-move-constructor] +// CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:37: warning
[clang-tools-extra] Add AllowFalseEvaluated flag to clang-tidy noexcept-move-constructor check (PR #126897)
https://github.com/Nechda updated https://github.com/llvm/llvm-project/pull/126897 >From 11328f0ceeefade85fc55b7e423f8589ea07d949 Mon Sep 17 00:00:00 2001 From: Dmitry Nechitaev Date: Mon, 17 Feb 2025 12:55:04 +0300 Subject: [PATCH 1/2] Add AllowFalseEvaluated to clang-tidy noexcept-move-constructor check --- .../performance/NoexceptFunctionBaseCheck.cpp | 2 +- .../performance/NoexceptFunctionBaseCheck.h | 8 ++- ...move-constructor-allow-false-evaluated.cpp | 63 +++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp index 911cd1b533367..8371b6aafd8ba 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp @@ -30,7 +30,7 @@ void NoexceptFunctionBaseCheck::check(const MatchFinder::MatchResult &Result) { const Expr *NoexceptExpr = ProtoType->getNoexceptExpr(); if (NoexceptExpr) { NoexceptExpr = NoexceptExpr->IgnoreImplicit(); -if (!isa(NoexceptExpr)) +if (!isa(NoexceptExpr) && !AllowFalseEvaluated) reportNoexceptEvaluatedToFalse(FuncDecl, NoexceptExpr); return; } diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h index 4775219d7e439..cd1da1fbeca55 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h @@ -22,7 +22,8 @@ namespace clang::tidy::performance { class NoexceptFunctionBaseCheck : public ClangTidyCheck { public: NoexceptFunctionBaseCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + : ClangTidyCheck(Name, Context) + , AllowFalseEvaluated(Options.getLocalOrGlobal("AllowFalseEvaluated", false)) {} bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { return LangOpts.CPlusPlus11 && LangOpts.CXXExceptions; @@ -33,6 +34,10 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { return TK_IgnoreUnlessSpelledInSource; } + void storeOptions(ClangTidyOptions::OptionMap &Opts) override { +Options.store(Opts, "AllowFalseEvaluated", AllowFalseEvaluated); + } + protected: virtual DiagnosticBuilder reportMissingNoexcept(const FunctionDecl *FuncDecl) = 0; @@ -42,6 +47,7 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { static constexpr StringRef BindFuncDeclName = "FuncDecl"; private: + bool AllowFalseEvaluated; utils::ExceptionSpecAnalyzer SpecAnalyzer; }; diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp new file mode 100644 index 0..3552eaa7c50ea --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp @@ -0,0 +1,63 @@ +// RUN: %check_clang_tidy %s performance-noexcept-move-constructor %t -- -- -fexceptions + +// RUN: %check_clang_tidy -check-suffix=CONFIG %s performance-noexcept-move-constructor,performance-noexcept-destructor %t -- \ +// RUN: -config="{CheckOptions: {performance-noexcept-move-constructor.AllowFalseEvaluated: true}}" \ +// RUN: -- -fexceptions + +namespace std +{ + template + struct is_nothrow_move_constructible + { +static constexpr bool value = __is_nothrow_constructible(T, __add_rvalue_reference(T)); + }; +} // namespace std + +struct ThrowOnAnything { + ThrowOnAnything() noexcept(false); + ThrowOnAnything(ThrowOnAnything&&) noexcept(false); + // CHECK-MESSAGES-NOT: :[[@LINE-1]]:3: warning: move constructors should be marked noexcept + // CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:3: warning: move constructors should be marked noexcept + ThrowOnAnything& operator=(ThrowOnAnything &&) noexcept(false); + ~ThrowOnAnything() noexcept(false); +}; + +struct C_1 { +static constexpr bool kFalse = false; +C_1(C_1&&) noexcept(kFalse) = default; +// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor] +// CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:25: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor] + +C_1 &operator=(C_1 &&) noexcept(kFalse); +// CHECK-MESSAGES: :[[@LINE-1]]:37: warning: noexcept specifier on the move assignment operator evaluates to 'false' [performance-noexcept-move-constructor] +// CHECK-MESSAGES-CONFIG-NOT: :[[@LINE-2]]:37: warning
[clang-tools-extra] [clang-tidy] Add AllowFalseEvaluated flag to clang-tidy noexcept-move-constructor check (PR #126897)
@@ -110,6 +110,12 @@ Changes in existing checks ` check by providing additional examples and fixing some macro related false positives. +- Improved :doc:`performance-noexcept-move-constructor + ` check by adding + a new (off-by-default) option `AllowFalseEvaluated`, which allows marking Nechda wrote: Hm, these changes are related to the release notes, not the option description. The check's documentation has already satisfied your change request. [Changes](https://github.com/llvm/llvm-project/pull/126897/files#diff-a1a8cda32e35cd375c8258cc579ee9b6603fe9acebf8c34370140f752568bc08R18-R22) https://github.com/llvm/llvm-project/pull/126897 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add AllowFalseEvaluated flag to clang-tidy noexcept-move-constructor check (PR #126897)
https://github.com/Nechda updated https://github.com/llvm/llvm-project/pull/126897 >From 6b61bff3f6ad48baf7414f0e88fd98e098a78e9e Mon Sep 17 00:00:00 2001 From: Dmitry Nechitaev Date: Mon, 17 Feb 2025 13:30:46 +0300 Subject: [PATCH 1/3] Add AllowFalseEvaluated to clang-tidy noexcept-move-constructor check --- .../performance/NoexceptFunctionBaseCheck.cpp | 2 +- .../performance/NoexceptFunctionBaseCheck.h | 8 ++- clang-tools-extra/docs/ReleaseNotes.rst | 6 ++ .../performance/noexcept-move-constructor.rst | 9 +++ ...move-constructor-allow-false-evaluated.cpp | 63 +++ 5 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp index 911cd1b533367..8371b6aafd8ba 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp @@ -30,7 +30,7 @@ void NoexceptFunctionBaseCheck::check(const MatchFinder::MatchResult &Result) { const Expr *NoexceptExpr = ProtoType->getNoexceptExpr(); if (NoexceptExpr) { NoexceptExpr = NoexceptExpr->IgnoreImplicit(); -if (!isa(NoexceptExpr)) +if (!isa(NoexceptExpr) && !AllowFalseEvaluated) reportNoexceptEvaluatedToFalse(FuncDecl, NoexceptExpr); return; } diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h index 4775219d7e439..cd1da1fbeca55 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h @@ -22,7 +22,8 @@ namespace clang::tidy::performance { class NoexceptFunctionBaseCheck : public ClangTidyCheck { public: NoexceptFunctionBaseCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + : ClangTidyCheck(Name, Context) + , AllowFalseEvaluated(Options.getLocalOrGlobal("AllowFalseEvaluated", false)) {} bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { return LangOpts.CPlusPlus11 && LangOpts.CXXExceptions; @@ -33,6 +34,10 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { return TK_IgnoreUnlessSpelledInSource; } + void storeOptions(ClangTidyOptions::OptionMap &Opts) override { +Options.store(Opts, "AllowFalseEvaluated", AllowFalseEvaluated); + } + protected: virtual DiagnosticBuilder reportMissingNoexcept(const FunctionDecl *FuncDecl) = 0; @@ -42,6 +47,7 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { static constexpr StringRef BindFuncDeclName = "FuncDecl"; private: + bool AllowFalseEvaluated; utils::ExceptionSpecAnalyzer SpecAnalyzer; }; diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 41ff1c1016f25..b1275891e3441 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -115,6 +115,12 @@ Changes in existing checks ` check by providing additional examples and fixing some macro related false positives. +- Improved :doc:`performance-noexcept-move-constructor + ` check by adding + a new (off-by-default) option `AllowFalseEvaluated`, which allows marking + move constructors with ``noexcept(expr)`` even if ``expr`` + evaluates to ``false``. + Removed checks ^^ diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance/noexcept-move-constructor.rst b/clang-tools-extra/docs/clang-tidy/checks/performance/noexcept-move-constructor.rst index 05f1d85f1af5a..288343723b19a 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/performance/noexcept-move-constructor.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/performance/noexcept-move-constructor.rst @@ -11,3 +11,12 @@ evaluates to ``false`` (but is not a ``false`` literal itself). Move constructors of all the types used with STL containers, for example, need to be declared ``noexcept``. Otherwise STL will choose copy constructors instead. The same is valid for move assignment operations. + +Options +--- + +.. option:: AllowFalseEvaluated + +When `true`, the check will not generate any warning +if the ``expr`` in ``noexcept(expr)`` evaluates to ``false``. +Default is `false`. diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp new file mode 100644 index 0..3552eaa7c50ea --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp @@ -0,0 +1,63 @@ +
[clang-tools-extra] [clang-tidy] Add AllowFalseEvaluated flag to clang-tidy noexcept-move-constructor check (PR #126897)
https://github.com/Nechda updated https://github.com/llvm/llvm-project/pull/126897 >From 6b61bff3f6ad48baf7414f0e88fd98e098a78e9e Mon Sep 17 00:00:00 2001 From: Dmitry Nechitaev Date: Mon, 17 Feb 2025 13:30:46 +0300 Subject: [PATCH 1/2] Add AllowFalseEvaluated to clang-tidy noexcept-move-constructor check --- .../performance/NoexceptFunctionBaseCheck.cpp | 2 +- .../performance/NoexceptFunctionBaseCheck.h | 8 ++- clang-tools-extra/docs/ReleaseNotes.rst | 6 ++ .../performance/noexcept-move-constructor.rst | 9 +++ ...move-constructor-allow-false-evaluated.cpp | 63 +++ 5 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp index 911cd1b533367..8371b6aafd8ba 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp @@ -30,7 +30,7 @@ void NoexceptFunctionBaseCheck::check(const MatchFinder::MatchResult &Result) { const Expr *NoexceptExpr = ProtoType->getNoexceptExpr(); if (NoexceptExpr) { NoexceptExpr = NoexceptExpr->IgnoreImplicit(); -if (!isa(NoexceptExpr)) +if (!isa(NoexceptExpr) && !AllowFalseEvaluated) reportNoexceptEvaluatedToFalse(FuncDecl, NoexceptExpr); return; } diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h index 4775219d7e439..cd1da1fbeca55 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h +++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.h @@ -22,7 +22,8 @@ namespace clang::tidy::performance { class NoexceptFunctionBaseCheck : public ClangTidyCheck { public: NoexceptFunctionBaseCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + : ClangTidyCheck(Name, Context) + , AllowFalseEvaluated(Options.getLocalOrGlobal("AllowFalseEvaluated", false)) {} bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { return LangOpts.CPlusPlus11 && LangOpts.CXXExceptions; @@ -33,6 +34,10 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { return TK_IgnoreUnlessSpelledInSource; } + void storeOptions(ClangTidyOptions::OptionMap &Opts) override { +Options.store(Opts, "AllowFalseEvaluated", AllowFalseEvaluated); + } + protected: virtual DiagnosticBuilder reportMissingNoexcept(const FunctionDecl *FuncDecl) = 0; @@ -42,6 +47,7 @@ class NoexceptFunctionBaseCheck : public ClangTidyCheck { static constexpr StringRef BindFuncDeclName = "FuncDecl"; private: + bool AllowFalseEvaluated; utils::ExceptionSpecAnalyzer SpecAnalyzer; }; diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 41ff1c1016f25..b1275891e3441 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -115,6 +115,12 @@ Changes in existing checks ` check by providing additional examples and fixing some macro related false positives. +- Improved :doc:`performance-noexcept-move-constructor + ` check by adding + a new (off-by-default) option `AllowFalseEvaluated`, which allows marking + move constructors with ``noexcept(expr)`` even if ``expr`` + evaluates to ``false``. + Removed checks ^^ diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance/noexcept-move-constructor.rst b/clang-tools-extra/docs/clang-tidy/checks/performance/noexcept-move-constructor.rst index 05f1d85f1af5a..288343723b19a 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/performance/noexcept-move-constructor.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/performance/noexcept-move-constructor.rst @@ -11,3 +11,12 @@ evaluates to ``false`` (but is not a ``false`` literal itself). Move constructors of all the types used with STL containers, for example, need to be declared ``noexcept``. Otherwise STL will choose copy constructors instead. The same is valid for move assignment operations. + +Options +--- + +.. option:: AllowFalseEvaluated + +When `true`, the check will not generate any warning +if the ``expr`` in ``noexcept(expr)`` evaluates to ``false``. +Default is `false`. diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp new file mode 100644 index 0..3552eaa7c50ea --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-allow-false-evaluated.cpp @@ -0,0 +1,63 @@ +
[clang-tools-extra] [clang-tidy] Add AllowFalseEvaluated flag to clang-tidy noexcept-move-constructor check (PR #126897)
Nechda wrote: If you are happy with this change, please approve it and merge it. I do not have the necessary permissions to perform the merge operation. https://github.com/llvm/llvm-project/pull/126897 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add AllowFalseEvaluated flag to clang-tidy noexcept-move-constructor check (PR #126897)
https://github.com/Nechda edited https://github.com/llvm/llvm-project/pull/126897 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add AllowFalseEvaluated flag to clang-tidy noexcept-move-constructor check (PR #126897)
Nechda wrote: Ping https://github.com/llvm/llvm-project/pull/126897 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits