<div dir="ltr"><div dir="ltr">On Thu, 11 Feb 2021 at 22:10, Pratyush Das via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Hi,</div><div><br></div><div>Is there a way to get a <code>char32_t</code> value from a variable of type <code>llvm::APSInt</code>? <br></div><div><br></div><div>I tried something like -
<pre><code>const char32_t Ch = Val.getZExtValue();</code></pre>
(where <code>Val</code> is of type <code>llvm::APSInt</code>)
but this implicit casting doesn't seem to work as it stores the same value as of type <code>char</code>. <br></div><div><br></div><div>
I tried playing around with <a title="https://llvm.org/doxygen/ConvertUTF_8h_source.html" href="https://llvm.org/doxygen/ConvertUTF_8h_source.html" rel="noreferrer noopener" target="_blank">https://llvm.org/doxygen/ConvertUTF_8h_source.html</a> but couldn't find a way to make this work.</div></div></blockquote><div><br></div><div>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.)<br></div><div><br></div><div><br></div></div></div>