[clang] [clang][Diagnostics] Add source range to uninitialized diagnostics (PR #65896)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 10 05:32:17 PDT 2023


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/65896:

Before:

array.cpp:319:10: note: read of uninitialized object is not allowed in a constant expression
  319 |    return aaa;
      |           ^

After:

array.cpp:319:10: note: read of uninitialized object is not allowed in a constant expression
  319 |    return aaa;
      |           ^~~

>From 00132d7f98dd3c4b994b6afd2a94773e2a46a496 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Sun, 10 Sep 2023 14:28:02 +0200
Subject: [PATCH] [clang][Diagnostics] Add source range to uninitialized
 diagnostics

Before:

array.cpp:319:10: note: read of uninitialized object is not allowed in a constant expression
  319 |    return aaa;
      |           ^

After:

array.cpp:319:10: note: read of uninitialized object is not allowed in a constant expression
  319 |    return aaa;
      |           ^~~
---
 clang/lib/AST/ExprConstant.cpp              | 5 +++--
 clang/lib/AST/Interp/Interp.cpp             | 6 +++---
 clang/test/Misc/constexpr-source-ranges.cpp | 8 ++++++++
 3 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index d8632f53bb1eef4..54a8d582939e118 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -3711,7 +3711,8 @@ findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj,
          !isValidIndeterminateAccess(handler.AccessKind))) {
       if (!Info.checkingPotentialConstantExpression())
         Info.FFDiag(E, diag::note_constexpr_access_uninit)
-            << handler.AccessKind << O->isIndeterminate();
+            << handler.AccessKind << O->isIndeterminate()
+            << E->getSourceRange();
       return handler.failed();
     }
 
@@ -4443,7 +4444,7 @@ struct CompoundAssignSubobjectHandler {
       return foundVector(Subobj, SubobjType);
     case APValue::Indeterminate:
       Info.FFDiag(E, diag::note_constexpr_access_uninit)
-          << /*read of=*/0 << /*uninitialized object=*/1;
+          << /*read of=*/0 << /*uninitialized object=*/1 << E->getSourceRange();
       return false;
     default:
       // FIXME: can this happen?
diff --git a/clang/lib/AST/Interp/Interp.cpp b/clang/lib/AST/Interp/Interp.cpp
index 719a96daaebdd73..c94b48c8fc614e6 100644
--- a/clang/lib/AST/Interp/Interp.cpp
+++ b/clang/lib/AST/Interp/Interp.cpp
@@ -261,9 +261,9 @@ bool CheckInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
     return true;
 
   if (!S.checkingPotentialConstantExpression()) {
-    const SourceInfo &Loc = S.Current->getSource(OpPC);
-    S.FFDiag(Loc, diag::note_constexpr_access_uninit)
-        << AK << /*uninitialized=*/true;
+    const Expr *E = S.Current->getExpr(OpPC);
+    S.FFDiag(E, diag::note_constexpr_access_uninit)
+        << AK << /*uninitialized=*/true << E->getSourceRange();
   }
   return false;
 }
diff --git a/clang/test/Misc/constexpr-source-ranges.cpp b/clang/test/Misc/constexpr-source-ranges.cpp
index f21373eff3a95ca..7f5c522ae305b54 100644
--- a/clang/test/Misc/constexpr-source-ranges.cpp
+++ b/clang/test/Misc/constexpr-source-ranges.cpp
@@ -41,3 +41,11 @@ int x = -1 + __INT_MAX__ + 2 + 3;
 // CHECK:      :{[[@LINE+1]]:9-[[@LINE+1]]:19}:
 int a = -(1 << 31) + 1;
 }
+
+
+constexpr int uninit() {
+  int aaa;
+  // CHECK: :{[[@LINE+1]]:10-[[@LINE+1]]:13}:
+  return aaa;
+}
+static_assert(uninit() == 0, "");



More information about the cfe-commits mailing list