Skip to content

[clang-tidy] ignore consteval function in ExceptionAnalyzer #116643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

HerrCai0907
Copy link
Contributor

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.

`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: llvm#104457.
@llvmbot
Copy link
Member

llvmbot commented Nov 18, 2024

@llvm/pr-subscribers-clang-tidy

@llvm/pr-subscribers-clang-tools-extra

Author: Congcong Cai (HerrCai0907)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/116643.diff

3 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp (+5)
  • (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4)
  • (added) clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-consteval.cpp (+14)
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

Copy link
Contributor

@5chmidti 5chmidti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM minus comment nits

@@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when encountering a

Comment on lines 323 to 324
// consteval specifies every call to the function must produce a compile-time
// constant. compile-time constant cannot be evaluate a throw expression.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// 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.

@HerrCai0907 HerrCai0907 merged commit fd2e048 into llvm:main Nov 21, 2024
9 checks passed
@HerrCai0907 HerrCai0907 deleted the 104457-bugprone-exception-escape-should-ignore-consteval branch November 21, 2024 21:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bugprone-exception-escape should ignore consteval
3 participants