[cfe-dev] char32_t value from APSInt
Richard Smith via cfe-dev
cfe-dev at lists.llvm.org
Fri Feb 12 00:04:05 PST 2021
On Thu, 11 Feb 2021 at 22:10, Pratyush Das via cfe-dev <
cfe-dev at lists.llvm.org> wrote:
> Hi,
>
> Is there a way to get a char32_t value from a variable of type
> llvm::APSInt?
>
> I tried something like -
>
> const char32_t Ch = Val.getZExtValue();
>
> (where Val is of type llvm::APSInt) but this implicit casting doesn't
> seem to work as it stores the same value as of type char.
>
> I tried playing around with
> https://llvm.org/doxygen/ConvertUTF_8h_source.html but couldn't find a
> way to make this work.
>
Well, an llvm::APSInt represents an arbitrary-width signed or unsigned
integer, and a char32_t (generally) represents a UCS-4 code unit / Unicode
code point. How do you want to interpret the APSInt, such that it can be
converted to a Unicode code point? If the APSInt represents the Unicode
code point value, then Val.getLimitedValue() is probably reasonable, but
you'll also need to check for Val.isNegative() to avoid the possibility of
a narrow negative APSInt getting confused for a small positive integer.
(Generally you should also beware of getZExtValue() because it has a
precondition that the value fits in 64 bits; it's also questionable to call
getZExtValue on an APSInt because it assumes the APSInt represents an
unsigned integer.)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20210212/6a320232/attachment.html>
More information about the cfe-dev
mailing list