[clang] [clang][Diagnostics] Provide source range to constexpr function diags (PR #69721)

via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 20 06:18:57 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>

Before:
```console
./array.cpp:290:16: error: constexpr function never produces a constant expression [-Winvalid-constexpr]
  290 | constexpr void neverValid() {
      |                ^
./array.cpp:291:3: note: subexpression not valid in a constant expression
  291 |   throw;
      |   ^~~~~
```

After:
```console
./array.cpp:290:16: error: constexpr function never produces a constant expression [-Winvalid-constexpr]
  290 | constexpr void neverValid() {
      |                ^~~~~~~~~~
./array.cpp:291:3: note: subexpression not valid in a constant expression
  291 |   throw;
      |   ^~~~~
```

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


2 Files Affected:

- (modified) clang/lib/Sema/SemaDeclCXX.cpp (+2-1) 
- (modified) clang/test/Misc/constexpr-source-ranges.cpp (+4) 


``````````diff
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 0193e476b3a781b..1d24609d99718ab 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -2457,7 +2457,8 @@ static bool CheckConstexprFunctionBody(Sema &SemaRef, const FunctionDecl *Dcl,
       !Expr::isPotentialConstantExpr(Dcl, Diags)) {
     SemaRef.Diag(Dcl->getLocation(),
                  diag::ext_constexpr_function_never_constant_expr)
-        << isa<CXXConstructorDecl>(Dcl) << Dcl->isConsteval();
+        << isa<CXXConstructorDecl>(Dcl) << Dcl->isConsteval()
+        << Dcl->getNameInfo().getSourceRange();
     for (size_t I = 0, N = Diags.size(); I != N; ++I)
       SemaRef.Diag(Diags[I].first, Diags[I].second);
     // Don't return false here: we allow this for compatibility in
diff --git a/clang/test/Misc/constexpr-source-ranges.cpp b/clang/test/Misc/constexpr-source-ranges.cpp
index 7f5c522ae305b54..fde05b5c75aa46a 100644
--- a/clang/test/Misc/constexpr-source-ranges.cpp
+++ b/clang/test/Misc/constexpr-source-ranges.cpp
@@ -49,3 +49,7 @@ constexpr int uninit() {
   return aaa;
 }
 static_assert(uninit() == 0, "");
+
+
+constexpr void neverValid() { throw; }
+// CHECK: :{[[@LINE-1]]:16-[[@LINE-1]]:26}:

``````````

</details>


https://github.com/llvm/llvm-project/pull/69721


More information about the cfe-commits mailing list