<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Thanks. :-)</div></div><div class="gmail_extra"><br><div class="gmail_quote">2017-04-15 1:40 GMT+08:00 Романенков Кирилл <span dir="ltr"><<a href="mailto:kromanenkov2@yandex.ru" target="_blank">kromanenkov2@yandex.ru</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>Yes, I think <span style="font-size:14px">getCanonicalType() is prefered for among others performance considerations.</span></div><div> </div><div>Kirill.</div><div> </div><div>13.04.2017, 15:27, "陳韋任" <<a href="mailto:chenwj.cs97g@g2.nctu.edu.tw" target="_blank">chenwj.cs97g@g2.nctu.edu.tw</a>>:</div><blockquote type="cite"><div><div style="font-family:arial,helvetica,sans-serif"><span style="font-family:arial,sans-serif;font-size:14px">Kirill and Artem,</span></div><div style="font-family:arial,helvetica,sans-serif"> </div><div><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><span style="font-size:14px">nested typedef into consideration.</span></div><div> </div><div><span style="font-size:14px">    typedef foo FOO;</span></div><div><span style="font-size:14px">    typedef FOO MyFoo;</span></div><div> </div><div><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> </div><div><span style="font-size:14px">The recursive call is kinda of like,</span></div><div> </div><div><span style="font-size:14px">    QualType getUnderlyingType(QualType QT) {</span></div><div> </div><div><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><span style="font-size:14px">          TypedefNameDef TND = TT->getDecl();</span></div><div><span style="font-size:14px">          return </span><span style="font-size:14px">getUnderlyingType(</span><span style="font-size:14px">TND-><wbr>getUnderlyingType());</span></div><div><span style="font-size:14px">      }</span></div><div> </div><div><span style="font-size:14px">      return QT;</span></div><div><span style="font-size:14px">    }</span></div><div> </div><div><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> </div><div><span style="font-size:14px">    QualType SrcCanonType = SrcExpr()->getType().</span><span style="font-size:14px">getCanoni<wbr>calType(),</span></div><div><br style="font-size:14px"><span style="font-size:14px">    const BuiltinType *Src = SrcCanonType->getAs<</span><span style="font-size:14px">BuiltinTyp<wbr>e>()</span></div><div> </div><div><span style="font-size:14px">I guess getCanonicalType() way is preferred?</span></div><div> </div><div><span style="font-size:14px">Regards,</span></div><div><span style="font-size:14px">chenwj</span></div><div> </div></div><div> <div><span>2017-04-13 5</span>:51 GMT+08:00 Романенков Кирилл via cfe-dev <span><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span>:<blockquote style="margin:0 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" target="_blank">cfe-dev@lists.llvm.org</a>><br>> To: <a href="mailto:cfe-dev@lists.llvm.org" target="_blank">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" target="_blank">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" 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" target="_blank">cfe-dev@lists.llvm.org</a><br><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a></blockquote></div> <div> </div><span class="HOEnZb"><font color="#888888">--<div><br>--<br>Wei-Ren Chen (陳韋任)<br>Homepage: <a href="https://people.cs.nctu.edu.tw/~chenwj" target="_blank">https://people.cs.nctu.edu.tw/<wbr>~chenwj</a></div></font></span></div></blockquote></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>