Skip to content

Commit 0bdcbb6

Browse files
committed
[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; | ^~~
1 parent 2744d9b commit 0bdcbb6

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3711,7 +3711,8 @@ findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj,
37113711
!isValidIndeterminateAccess(handler.AccessKind))) {
37123712
if (!Info.checkingPotentialConstantExpression())
37133713
Info.FFDiag(E, diag::note_constexpr_access_uninit)
3714-
<< handler.AccessKind << O->isIndeterminate();
3714+
<< handler.AccessKind << O->isIndeterminate()
3715+
<< E->getSourceRange();
37153716
return handler.failed();
37163717
}
37173718

@@ -4443,7 +4444,8 @@ struct CompoundAssignSubobjectHandler {
44434444
return foundVector(Subobj, SubobjType);
44444445
case APValue::Indeterminate:
44454446
Info.FFDiag(E, diag::note_constexpr_access_uninit)
4446-
<< /*read of=*/0 << /*uninitialized object=*/1;
4447+
<< /*read of=*/0 << /*uninitialized object=*/1
4448+
<< E->getLHS()->getSourceRange();
44474449
return false;
44484450
default:
44494451
// FIXME: can this happen?

clang/lib/AST/Interp/Interp.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,8 @@ bool CheckInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
261261
return true;
262262

263263
if (!S.checkingPotentialConstantExpression()) {
264-
const SourceInfo &Loc = S.Current->getSource(OpPC);
265-
S.FFDiag(Loc, diag::note_constexpr_access_uninit)
266-
<< AK << /*uninitialized=*/true;
264+
S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_access_uninit)
265+
<< AK << /*uninitialized=*/true << S.Current->getRange(OpPC);
267266
}
268267
return false;
269268
}

clang/lib/AST/Interp/InterpFrame.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,5 +230,8 @@ SourceLocation InterpFrame::getLocation(CodePtr PC) const {
230230
}
231231

232232
SourceRange InterpFrame::getRange(CodePtr PC) const {
233+
if (Func && Func->getDecl()->isImplicit() && Caller)
234+
return Caller->getRange(RetPC);
235+
233236
return S.getRange(Func, PC);
234237
}

clang/test/Misc/constexpr-source-ranges.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,11 @@ int x = -1 + __INT_MAX__ + 2 + 3;
4141
// CHECK: :{[[@LINE+1]]:9-[[@LINE+1]]:19}:
4242
int a = -(1 << 31) + 1;
4343
}
44+
45+
46+
constexpr int uninit() {
47+
int aaa;
48+
// CHECK: :{[[@LINE+1]]:10-[[@LINE+1]]:13}:
49+
return aaa;
50+
}
51+
static_assert(uninit() == 0, "");

0 commit comments

Comments
 (0)