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

via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 23 21:14:32 PDT 2023


Author: Timm Baeder
Date: 2023-10-24T06:14:27+02:00
New Revision: bb7c515ba1a791e5ec7c83bc0dc4d6359736065a

URL: https://github.com/llvm/llvm-project/commit/bb7c515ba1a791e5ec7c83bc0dc4d6359736065a
DIFF: https://github.com/llvm/llvm-project/commit/bb7c515ba1a791e5ec7c83bc0dc4d6359736065a.diff

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

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;
      |   ^~~~~
```

Added: 
    

Modified: 
    clang/lib/Sema/SemaDeclCXX.cpp
    clang/test/Misc/constexpr-source-ranges.cpp

Removed: 
    


################################################################################
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}:


        


More information about the cfe-commits mailing list