Skip to content

Commit 66202d8

Browse files
sousajo-ccAaronBallman
authored andcommitted
Make 'static assertion failed' diagnostics point to the static assertion expression
"static assertion failed" pointed to the static_assert token and then underlined the static assertion expression: <source>:3:1: error: static assertion failed static_assert(false); ^ ~~~~~ 1 error generated. See Godbolt: https://godbolt.org/z/r38booz59 Now it points to and highlights the assertion expression. Fixes llvm#61951 Differential Revision: https://reviews.llvm.org/D147745
1 parent dfafb7f commit 66202d8

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ Improvements to Clang's diagnostics
218218
- Clang now avoids unnecessary diagnostic warnings for obvious expressions in
219219
the case of binary operators with logical OR operations.
220220
(`#57906 <https://github.com/llvm/llvm-project/issues/57906>`_)
221+
- Clang's "static assertion failed" diagnostic now points to the static assertion
222+
expression instead of pointing to the ``static_assert`` token.
223+
(`#61951 <https://github.com/llvm/llvm-project/issues/61951>`_)
221224

222225
Bug Fixes in This Version
223226
-------------------------

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16820,19 +16820,20 @@ Decl *Sema::BuildStaticAssertDeclaration(SourceLocation StaticAssertLoc,
1682016820
if (InnerCond && isa<ConceptSpecializationExpr>(InnerCond)) {
1682116821
// Drill down into concept specialization expressions to see why they
1682216822
// weren't satisfied.
16823-
Diag(StaticAssertLoc, diag::err_static_assert_failed)
16824-
<< !AssertMessage << Msg.str() << AssertExpr->getSourceRange();
16823+
Diag(AssertExpr->getBeginLoc(), diag::err_static_assert_failed)
16824+
<< !AssertMessage << Msg.str() << AssertExpr->getSourceRange();
1682516825
ConstraintSatisfaction Satisfaction;
1682616826
if (!CheckConstraintSatisfaction(InnerCond, Satisfaction))
1682716827
DiagnoseUnsatisfiedConstraint(Satisfaction);
1682816828
} else if (InnerCond && !isa<CXXBoolLiteralExpr>(InnerCond)
1682916829
&& !isa<IntegerLiteral>(InnerCond)) {
16830-
Diag(StaticAssertLoc, diag::err_static_assert_requirement_failed)
16831-
<< InnerCondDescription << !AssertMessage
16832-
<< Msg.str() << InnerCond->getSourceRange();
16830+
Diag(InnerCond->getBeginLoc(),
16831+
diag::err_static_assert_requirement_failed)
16832+
<< InnerCondDescription << !AssertMessage << Msg.str()
16833+
<< InnerCond->getSourceRange();
1683316834
DiagnoseStaticAssertDetails(InnerCond);
1683416835
} else {
16835-
Diag(StaticAssertLoc, diag::err_static_assert_failed)
16836+
Diag(AssertExpr->getBeginLoc(), diag::err_static_assert_failed)
1683616837
<< !AssertMessage << Msg.str() << AssertExpr->getSourceRange();
1683716838
PrintContextStack();
1683816839
}

clang/test/SemaCXX/static-assert.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,5 +287,28 @@ namespace Diagnostics {
287287
static_assert(CHECK_4(a) && A_IS_B, ""); // expected-error {{failed}} \
288288
// expected-note {{evaluates to '4 == 5'}}
289289

290+
static_assert(
291+
false, // expected-error {{static assertion failed}}
292+
""
293+
);
294+
295+
static_assert(
296+
true && false, // expected-error {{static assertion failed due to requirement 'true && false'}}
297+
""
298+
);
299+
300+
static_assert(
301+
// with a comment here
302+
true && false, // expected-error {{static assertion failed due to requirement 'true && false'}}
303+
""
304+
);
305+
306+
static_assert(
307+
// with a comment here
308+
(true && // expected-error {{static assertion failed due to requirement '(true && false) || false'}}
309+
false)
310+
|| false,
311+
""
312+
);
290313

291314
}

0 commit comments

Comments
 (0)