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 <nech...@gmail.com> 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<CXXBoolLiteralExpr>(NoexceptExpr)) + if (!isa<CXXBoolLiteralExpr>(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 <clang-tidy/checks/misc/redundant-expression>` check by providing additional examples and fixing some macro related false positives. +- Improved :doc:`performance-noexcept-move-constructor + <clang-tidy/checks/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 0000000000000..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 <typename T> + 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: noexcept specifier on the move assignment operator evaluates to 'false' [performance-noexcept-move-constructor] +}; + +struct C_2 { + static constexpr bool kEval = std::is_nothrow_move_constructible<ThrowOnAnything>::value; + static_assert(!kEval); // kEval == false; + + C_2(C_2&&) noexcept(kEval) = 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_2 &operator=(C_2 &&) noexcept(kEval); + // 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: noexcept specifier on the move assignment operator evaluates to 'false' [performance-noexcept-move-constructor] + + ThrowOnAnything field; +}; + +struct C_3 { + static constexpr bool kEval = std::is_nothrow_move_constructible<ThrowOnAnything>::value; + static_assert(!kEval); // kEval == false; + + C_3(C_3&&) noexcept(kEval) = 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_3() noexcept(kEval) = default; + // CHECK-MESSAGES-CONFIG: :[[@LINE-1]]:21: warning: noexcept specifier on the destructor evaluates to 'false' + + ThrowOnAnything field; +}; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits