This is an archive of the discontinued LLVM Phabricator instance.

Emit sizeof/alignof values as notes when a static_assert fails
AbandonedPublic

Authored by arichardson on Aug 17 2021, 8:31 AM.

Details

Summary

Trying to debug a static_assert(sizeof(foo) == ...) failure can be rather
awkward since there is no easy way to print the value of the sizeof() at
compile-time without another compiler diagnostic involving an array size.
This patch emits additional notes when a static_assert fails for any
UnaryExprOrTypeTraitExpr expression inside the static_assert.

Diff Detail

Event Timeline

arichardson requested review of this revision.Aug 17 2021, 8:31 AM
arichardson created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptAug 17 2021, 8:31 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
aaron.ballman added inline comments.
clang/lib/Sema/SemaTemplate.cpp
3655
clang/test/SemaCXX/static-assert.cpp
233

Can you also add a test where the unary expression isn't the reason for the failure? e.g.,

static_assert(is_same_v<float, decltype(sizeof(int))>);

Perhaps we'd want to silence the notes if the expression context for the call to sizeof is itself unevaluated?

arichardson marked 2 inline comments as done.

rebase and address feedback

Herald added a project: Restricted Project. · View Herald TranscriptAug 24 2022, 9:52 AM

This is less useful now that 09117b21890c652994f7ada0229d309b35b44259 / D130894 has landed, but it might still be worth including (e.g. for expressions that include multiple sizeof() values?

From looking at the output in the test cases, the additional diagnostics seem unnecessary to me in almost all cases...?

From looking at the output in the test cases, the additional diagnostics seem unnecessary to me in almost all cases...?

+1; my observation is that the extra note repeats information because the new note says what the LHS and the RHS of the expression evaluates to. I could see this new note being more useful in a case like: static_assert(sizeof(foo) + sizeof(bar) == sizeof(baz) / 12); where there are multiple sizeof expressions involved and we could note what each constituent part evaluates to, but I'm not certain it's worth the extra complexity to go to those lengths (I'd want to see real world code where this would really help).

arichardson abandoned this revision.Sep 22 2022, 6:43 AM

Would require significant work to still be useful now that we print the value of expressions (e.g. only print for more complex expressions).