[clang] [C23] Select the correct promoted type for a bit-field (PR #89254)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 19 05:41:33 PDT 2024


AaronBallman wrote:

> > @tbaederr -- this triggers an assertion with the new constexpr interpreter in `clang/test/AST/Interp/intap.cpp` but I'm not certain I understand why. Can you help me figure out how to fix that?
> > https://buildkite.com/llvm-project/github-pull-requests/builds/56785#018ef1ee-1e9c-48be-8944-0bce2c6f441d/6-1940
> 
> In `IntegralAP::truncate()` (which we call when getting the `3` and setting it as the value of the bitfield), we actually truncate the bitwith of the underlying `IntAP`, not just the _value_.
> 
> This fixes things for me:
> 
> ```diff
> diff --git a/clang/lib/AST/Interp/IntegralAP.h b/clang/lib/AST/Interp/IntegralAP.h
> index bab9774288bf..fb7ee1451571 100644
> --- a/clang/lib/AST/Interp/IntegralAP.h
> +++ b/clang/lib/AST/Interp/IntegralAP.h
> @@ -154,7 +154,10 @@ public:
>    }
> 
>    IntegralAP truncate(unsigned BitWidth) const {
> -    return IntegralAP(V.trunc(BitWidth));
> +    if constexpr (Signed)
> +      return IntegralAP(V.trunc(BitWidth).sextOrTrunc(this->bitWidth()));
> +    else
> +      return IntegralAP(V.trunc(BitWidth).zextOrTrunc(this->bitWidth()));
>    }
> 
>    IntegralAP<false> toUnsigned() const {
> ```
> 
> I'd push this (it doesn't break any existing tests), but I'm not 100% sure if this is the right way to do that. Also, I should follow up with a `truncate` -> `truncateValue` rename.

Thank you for the help, that does pass all the tests, so I've added a commit for it.

https://github.com/llvm/llvm-project/pull/89254


More information about the cfe-commits mailing list