This is an archive of the discontinued LLVM Phabricator instance.

[clang] Do not crash after suggesting typo correction to constexpr if condition
ClosedPublic

Authored by Fznamznon on Apr 13 2023, 2:56 AM.

Details

Summary

In some cases non-null non-constant yet valid expression may reach point where
ConditionResult is created. For example, typo correction mechanism can return
such expression, so double check before evaluating it.

Fixes https://github.com/llvm/llvm-project/issues/61885

Diff Detail

Event Timeline

Fznamznon created this revision.Apr 13 2023, 2:56 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 13 2023, 2:56 AM
Fznamznon requested review of this revision.Apr 13 2023, 2:56 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 13 2023, 2:56 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
aaron.ballman added inline comments.Apr 13 2023, 7:57 AM
clang/include/clang/Sema/Sema.h
12852

Oof, this adds quite a bit of expense to this constructor -- isIntegerConstantExpr() is implemented by evaluating the constant expression and throwing away the computed result... which we then recompute when setting KnownValue one line later. It'd be better to only do this computation once and use the computed value to initialize both members.

Fznamznon updated this revision to Diff 513276.Apr 13 2023, 9:15 AM

Rebase, evaluate the expression only only time

Fznamznon added inline comments.Apr 13 2023, 9:16 AM
clang/include/clang/Sema/Sema.h
12852

Oops, now it is evaluated only one time.
It seems clang's codebase have several places with the same mistake I've made here.

Use std::optional

Fznamznon updated this revision to Diff 513497.Apr 14 2023, 2:21 AM

Rebase, fix format

tbaeder accepted this revision.Apr 14 2023, 10:59 PM
This revision is now accepted and ready to land.Apr 14 2023, 10:59 PM

@aaron.ballman , are you ok with the change?

zixuan-wu added inline comments.
clang/test/SemaCXX/invalid-if-constexpr.cpp
8

Hi, why did you mean '__sync_swap' is not reported in riscv target? So it's failed in check-all.

Fznamznon added inline comments.May 22 2023, 3:37 AM
clang/test/SemaCXX/invalid-if-constexpr.cpp
8

I haven't seen any buildbot issues with the issue like this.
Adding -triple riscv64-unknown-unknown doesn't help me to see the issue, as well as adding -DLLVM_TARGETS_TO_BUILD="RISCV" to cmake line and running check-clang. Could you please help to reproduce the issue? I'm not quite familiar with RISCV targets.