<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><span style="font-family:arial,sans-serif;font-size:14px">Kirill and Artem,</span></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><span style="font-family:arial,sans-serif;font-size:14px"><br></span></div><div class="gmail_default"><span style="font-family:arial,sans-serif;font-size:14px"> My original approach is </span><span style="font-size:14px">dyn_cast the type pointer to TypedefType recursively since I need to take</span></div><div class="gmail_default"><span style="font-size:14px">nested typedef into consideration.</span></div><div class="gmail_default"><span style="font-size:14px"><br></span></div><div class="gmail_default"><span style="font-size:14px"> typedef foo FOO;</span></div><div class="gmail_default"><span style="font-size:14px"> typedef FOO MyFoo;</span></div><div class="gmail_default"><span style="font-size:14px"><br></span></div><div class="gmail_default"><span style="font-size:14px"> </span><span style="font-size:14px">MyFoo b = (</span><span style="font-size:14px">MyFoo</span><span style="font-size:14px">)(a);</span><span style="font-size:14px"> </span></div><div class="gmail_default"><span style="font-size:14px"><br></span></div><div class="gmail_default"><span style="font-size:14px">The recursive call is kinda of like,</span></div><div class="gmail_default"><span style="font-size:14px"><br></span></div><div class="gmail_default"><span style="font-size:14px"> QualType getUnderlyingType(QualType QT) {</span></div><div class="gmail_default"><span style="font-size:14px"><br></span></div><div class="gmail_default"><span style="font-size:14px"> if (const </span><span style="font-size:14px">TypedefType *</span><span style="font-size:14px">TT = </span><span style="font-size:14px">dyn_cast<TypedefType>(QT)</span><span style="font-size:14px">) {</span></div><div class="gmail_default"><span style="font-size:14px"> TypedefNameDef TND = TT->getDecl();</span></div><div class="gmail_default"><span style="font-size:14px"> return </span><span style="font-size:14px">getUnderlyingType(</span><span style="font-size:14px">TND->getUnderlyingType());</span></div><div class="gmail_default"><span style="font-size:14px"> }</span></div><div class="gmail_default"><span style="font-size:14px"><br></span></div><div class="gmail_default"><span style="font-size:14px"> return QT;</span></div><div class="gmail_default"><span style="font-size:14px"> }</span></div><div class="gmail_default"><span style="font-size:14px"><br></span></div><div class="gmail_default"><span style="font-size:14px">Both of you suggested </span><span style="font-size:14px">*canonical* type, I tried and it works.</span></div><div class="gmail_default"><span style="font-size:14px"><br></span></div><div class="gmail_default"><span style="font-size:14px"> QualType SrcCanonType = SrcExpr()->getType().</span><wbr style="font-size:14px"><span style="font-size:14px">getCanonicalType(),</span><br></div><div class="gmail_default"><br style="font-size:14px"><span style="font-size:14px"> const BuiltinType *Src = SrcCanonType->getAs<</span><wbr style="font-size:14px"><span style="font-size:14px">BuiltinType>()</span><span style="font-size:14px"><br></span></div><div class="gmail_default"><span style="font-size:14px"><br></span></div><div class="gmail_default"><span style="font-size:14px">I guess getCanonicalType() way is preferred?</span></div><div class="gmail_default"><span style="font-size:14px"><br></span></div><div class="gmail_default"><span style="font-size:14px">Regards,</span></div><div class="gmail_default"><span style="font-size:14px">chenwj</span></div><div class="gmail_default"><span style="font-size:14px"><br></span></div></div><div class="gmail_extra"><br><div class="gmail_quote">2017-04-13 5:51 GMT+08:00 Романенков Кирилл via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
You can use getCanonicalType() to look through the typedefs, for example:<br>
<br>
QualType SrcCanonType = SrcExpr()->getType().<wbr>getCanonicalType(),<br>
DestCanonType = DestType.getCanonicalType();<br>
<br>
if (const BuiltinType *Src = SrcCanonType->getAs<<wbr>BuiltinType>())<br>
if (const BuiltinType *Dest = DestCanonType->getAs<<wbr>BuiltinType>()) {<br>
if (Src.getKind() == Dest.getKind()) {<br>
// do something<br>
} else {<br>
// do something else<br>
}<br>
}<br>
<br>
Kirill<br>
<br>
> Date: Wed, 12 Apr 2017 23:11:29 +0800<br>
> From: 陳韋任 via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>><br>
> To: <a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a><br>
> Subject: [cfe-dev] Get underlying type of QualType if it's TypedefType<br>
> Message-ID:<br>
> <<wbr>CAFSLk9eRMBhHqLXR2WGWH7YLRgYe_<wbr>eXNb0s8yXS6A=<a href="mailto:Y5MeTrZg@mail.gmail.com">Y5MeTrZg@mail.<wbr>gmail.com</a>><br>
> Content-Type: text/plain; charset="utf-8"<br>
><br>
> Hi All,<br>
><br>
> I have SrcType and DestType used in explicit cast, and I'd like to check<br>
> if the type conversion is valid in CastOperation::CheckCStyleCast [1]. The<br>
> code snippet below (just get the idea, the code is not runable),<br>
><br>
> Builtin *Src = dyn_cast<BuiltinType>(SrcExpr.<wbr>get()->getType());<br>
> Builtin *Dest = dyn_cast<BuiltinType>(<wbr>DestType);<br>
><br>
> if (Src.getKind() == Dest.getKind()) {<br>
> // do something<br>
> } else {<br>
> // do something else<br>
> }<br>
><br>
> The problem is the SrcType and DestType might be typedef. For example,<br>
><br>
> typedef foo FOO;<br>
><br>
> FOO b = (FOO)(a);<br>
><br>
> Src and Dest might be nullptr due to the fail dyn_cast, and I will get<br>
> segfault while calling getKind() upon them.<br>
> I am not familiar with Clang, so is there a better way that I can get the<br>
> underlying type if SrcType and DestType<br>
> are typedef?<br>
><br>
> Thanks.<br>
><br>
> [1] <a href="https://clang.llvm.org/doxygen/SemaCast_8cpp_source.html" rel="noreferrer" target="_blank">https://clang.llvm.org/<wbr>doxygen/SemaCast_8cpp_source.<wbr>html</a><br>
><br>
> Regards,<br>
> chenwj<br>
______________________________<wbr>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><br>--<br>Wei-Ren Chen (陳韋任)<br>Homepage: <a href="https://people.cs.nctu.edu.tw/~chenwj" target="_blank">https://people.cs.nctu.edu.tw/~chenwj</a></div>
</div>