[clang-tools-extra] [clang-tidy] ignore consteval function in `ExceptionAnalyzer` (PR #116643)
Congcong Cai via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 20 18:58:04 PST 2024
https://github.com/HerrCai0907 updated https://github.com/llvm/llvm-project/pull/116643
>From 529e5af068588e18e061aa399a28cd146816fc80 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Mon, 18 Nov 2024 23:50:22 +0800
Subject: [PATCH 1/2] [clang-tidy] ignore consteval function in
`ExceptionAnalyzer`
`ExceptionAnalyzer` can ignore `consteval` function even if it will throw exception. `consteval` function must produce compile-time constant. But throw statement cannot appear in constant evaluation.
Fixed: #104457.
---
.../clang-tidy/utils/ExceptionAnalyzer.cpp | 5 +++++
clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++
.../bugprone/exception-escape-consteval.cpp | 14 ++++++++++++++
3 files changed, 23 insertions(+)
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-consteval.cpp
diff --git a/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp b/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
index 9bfb7e2677533a..740c7852439219 100644
--- a/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
+++ b/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
@@ -320,6 +320,11 @@ bool isQualificationConvertiblePointer(QualType From, QualType To,
} // namespace
static bool canThrow(const FunctionDecl *Func) {
+ // consteval specifies every call to the function must produce a compile-time
+ // constant. compile-time constant cannot be evaluate a throw expression.
+ if (Func->isConsteval())
+ return false;
+
const auto *FunProto = Func->getType()->getAs<FunctionProtoType>();
if (!FunProto)
return true;
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index f967dfabd1c940..70f62f2dc8c393 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -162,6 +162,10 @@ Changes in existing checks
<clang-tidy/checks/bugprone/dangling-handle>` check to treat `std::span` as a
handle class.
+- Improved :doc:`bugprone-exception-escape
+ <clang-tidy/checks/bugprone/exception-escape>` by fixing false positives
+ when consteval function with throw statements.
+
- Improved :doc:`bugprone-forwarding-reference-overload
<clang-tidy/checks/bugprone/forwarding-reference-overload>` check by fixing
a crash when determining if an ``enable_if[_t]`` was found.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-consteval.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-consteval.cpp
new file mode 100644
index 00000000000000..6e4298bba8bf1f
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-consteval.cpp
@@ -0,0 +1,14 @@
+// RUN: %check_clang_tidy -std=c++20 %s bugprone-exception-escape %t -- \
+// RUN: -- -fexceptions -Wno-everything
+
+namespace GH104457 {
+
+consteval int consteval_fn(int a) {
+ if (a == 0)
+ throw 1;
+ return a;
+}
+
+int test() noexcept { return consteval_fn(1); }
+
+} // namespace GH104457
>From 272506dedc5364785d93cb74a1acc7962c727bd7 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Thu, 21 Nov 2024 10:57:52 +0800
Subject: [PATCH 2/2] fix review
---
clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp | 5 +++--
clang-tools-extra/docs/ReleaseNotes.rst | 2 +-
2 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp b/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
index 740c7852439219..8f3bc9534f931f 100644
--- a/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
+++ b/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
@@ -320,8 +320,9 @@ bool isQualificationConvertiblePointer(QualType From, QualType To,
} // namespace
static bool canThrow(const FunctionDecl *Func) {
- // consteval specifies every call to the function must produce a compile-time
- // constant. compile-time constant cannot be evaluate a throw expression.
+ // consteval specifies that every call to the function must produce a
+ // compile-time constant, which cannot evaluate a throw expression without
+ // producing a compilation error.
if (Func->isConsteval())
return false;
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 70f62f2dc8c393..dcfe68e020fc93 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -164,7 +164,7 @@ Changes in existing checks
- Improved :doc:`bugprone-exception-escape
<clang-tidy/checks/bugprone/exception-escape>` by fixing false positives
- when consteval function with throw statements.
+ when a consteval function with throw statements.
- Improved :doc:`bugprone-forwarding-reference-overload
<clang-tidy/checks/bugprone/forwarding-reference-overload>` check by fixing
More information about the cfe-commits
mailing list