This is an archive of the discontinued LLVM Phabricator instance.

[clang][AST] Improve diagnostic for `nullptr` constexpr function pointer call
ClosedPublic

Authored by hazohelet on Mar 10 2023, 7:18 AM.

Details

Summary

This patch improves diagnostic for clang constexpr evaluator by adding a check for nullptr in function pointer call evaluations.
This fixes https://github.com/llvm/llvm-project/issues/59872

ex.

constexpr int foo(int (*bla)(void)) {
  return bla();
}

static_assert(foo(nullptr) == 1);

BEFORE this patch, clang generates the following diagnostic for the code above

<source>:5:15: error: static assertion expression is not an integral constant expression
static_assert(foo(nullptr) == 1);
              ^~~~~~~~~~~~~~~~~
<source>:2:10: note: subexpression not valid in a constant expression
  return bla();
         ^
<source>:5:15: note: in call to 'foo(nullptr)'
static_assert(foo(nullptr) == 1);
              ^
1 error generated.

AFTER this patch, subexpression not valid in a constant expression note is replaced with 'bla' evaluates to a null function pointer.

Diff Detail

Event Timeline

hazohelet created this revision.Mar 10 2023, 7:18 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 10 2023, 7:18 AM
hazohelet requested review of this revision.Mar 10 2023, 7:18 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 10 2023, 7:18 AM
tbaeder added inline comments.Mar 10 2023, 7:47 AM
clang/lib/AST/ExprConstant.cpp
7673

Is the const_cast really necessary?

hazohelet added inline comments.Mar 10 2023, 8:27 AM
clang/lib/AST/ExprConstant.cpp
7673

Is the const_cast really necessary?

Without const_cast, it did not compile.
I searched the existing codebase to find this line https://github.com/llvm/llvm-project/blob/151d3b607e1e3256ed901e02b48b92e79a77021d/clang/lib/Sema/SemaConcept.cpp#L300 and I did the same here.

aaron.ballman added inline comments.Mar 10 2023, 10:09 AM
clang/include/clang/Basic/DiagnosticASTKinds.td
131

Small rewording.

shafik added a subscriber: shafik.Mar 10 2023, 2:59 PM
shafik added inline comments.
clang/lib/AST/ExprConstant.cpp
7673

Yeah, we do seem to const_cast on const Expr* all over and in some places it is obviously harmless but others it is far from clear, at least on a cursory examination.

cjdb accepted this revision.Mar 10 2023, 5:22 PM

LGTM pending other reviewers' commentary. Thanks for working on this!

This revision is now accepted and ready to land.Mar 10 2023, 5:22 PM
hazohelet updated this revision to Diff 504324.Mar 10 2023, 7:03 PM
hazohelet edited the summary of this revision. (Show Details)

Address comment from @aaron.ballman

  • Rewording the diagnostic
aaron.ballman accepted this revision.Mar 13 2023, 7:29 AM

LGTM, though please add a release note for the fix. If you need someone to land on your behalf, let us know what name and email address you'd like used for patch attribution.

clang/lib/AST/ExprConstant.cpp
7673

Clang has historically had very poor const-correctness, and trying to retrofit it is always an exercise in avoiding viral changes. So new interfaces tend to be more likely to be const-correct, while older ones (like the diagnostics engine) tend to not be.

The const_cast<> looks necessary to me, but if someone wanted to explore fixing up the interface so we can remove the const_casts from multiple places (as a follow-up patch, not as part of this work), that could be fruitful.

hazohelet updated this revision to Diff 504678.Mar 13 2023, 8:41 AM

Added a release note

LGTM, though please add a release note for the fix. If you need someone to land on your behalf, let us know what name and email address you'd like used for patch attribution.

Yeah, I don't have commit access, so I need someone to commit for me.
Please use "Takuya Shimizu <shimizu2486@gmail.com>" for the patch attribution. Thank you!

This revision was landed with ongoing or failed builds.Mar 13 2023, 9:55 AM
This revision was automatically updated to reflect the committed changes.