<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 16 Jul 2015, at 22:50, Aaron Ballman <<a href="mailto:aaron@aaronballman.com" class="">aaron@aaronballman.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">On Thu, Jul 16, 2015 at 4:41 PM, Tobias Langner</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class=""><</span><a href="mailto:randomcppprogrammer@gmail.com" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">randomcppprogrammer@gmail.com</a><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">> wrote:</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Hi,<br class=""><br class="">I want to write a check that tests whether a catch is done by const reference or not. I currently have the following code but it can’t distinguish between const reference and reference:<br class=""><br class=""> const auto *catchStmt = Result.Nodes.getNodeAs<CXXCatchStmt>("catch");<br class=""> if (catchStmt != nullptr) {<br class="">   auto caughtType = catchStmt->getCaughtType();<br class=""></blockquote><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">This returns the QualType you are looking for.</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">caughType.isConstant() && caughtType->isLValueReferenceType();</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">should tell you if it's a const-qualified lvalue reference type.</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">~Aaron</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""></div></blockquote><div>This gives me the same result as using QualType::isConstQualified() (see below). Both my examples are detected as not being caught by const reference:</div><div><br class=""></div><div><div>void catchByReference() {</div><div>  try {</div><div>    testThrowFunc();</div><div>  } catch (logic_error &e) {</div><div>  }</div><div>}</div><div><br class=""></div><div>void catchByConstReference() {</div><div>  try {</div><div>    testThrowFunc();</div><div>  } catch (const logic_error &e) {</div><div>  }</div><div>}</div><div><br class=""></div><div>giving this result:</div><div><br class=""></div><div><div style="margin: 0px; font-size: 10px; font-family: Monaco; color: rgb(255, 255, 255); background-color: rgb(0, 0, 0);" class="">/Users/tobias/Documents/Development/git/llvm/build/ninja-debug/../../llvm/tools/clang/tools/extra/test/clang-tidy/misc-throw-by-value-catch-by-reference.cpp:44:12: <span style="font-variant-ligatures: no-common-ligatures; color: #d53bd3" class="">warning: </span>catch by const reference to avoid unnecessary copies [misc-throw-by-value-catch-by-reference]</div><div style="margin: 0px; font-size: 10px; font-family: Monaco; color: rgb(245, 245, 245); background-color: rgb(0, 0, 0);" class="">  } catch (logic_error &e) {</div><div style="margin: 0px; font-size: 10px; font-family: Monaco; color: rgb(52, 189, 38); background-color: rgb(0, 0, 0);" class="">           ^</div><div style="margin: 0px; font-size: 10px; font-family: Monaco; color: rgb(255, 255, 255); background-color: rgb(0, 0, 0);" class="">/Users/tobias/Documents/Development/git/llvm/build/ninja-debug/../../llvm/tools/clang/tools/extra/test/clang-tidy/misc-throw-by-value-catch-by-reference.cpp:51:12: <span style="font-variant-ligatures: no-common-ligatures; color: #d53bd3" class="">warning: </span>catch by const reference to avoid unnecessary copies [misc-throw-by-value-catch-by-reference]</div><div style="margin: 0px; font-size: 10px; font-family: Monaco; color: rgb(245, 245, 245); background-color: rgb(0, 0, 0);" class="">  } catch (const logic_error &e) {</div></div><div class=""><br class=""></div><div class="">so for both caughtTypes of these catch statements, neither isConstant() nor isConstQualified() returns true.</div><div class=""><br class=""></div><div class="">Regards</div><div class="">Tobias</div></div><br class=""><blockquote type="cite" class=""><div class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">   auto *type = caughtType.getTypePtr();<br class="">   auto *varDecl = catchStmt->getExceptionDecl();<br class="">   if (type->isPointerType()) {<br class="">     diag(varDecl->getLocStart(),<br class="">          "catch by const reference instead of pointer");<br class="">   } else if (!type->isReferenceType()) {<br class="">     diag(varDecl->getLocStart(), "catch by const reference");<br class="">   } else {<br class="">     if (caughtType.isConstQualified() == false) {<br class="">       diag(varDecl->getLocStart(),<br class="">            "catch by const reference to avoid unnecessary copies");<br class="">     }<br class="">   }<br class=""> }<br class=""><br class="">I tried looking at the varDecl but from there I can’t even find out how to get the QualType. Can somebody give me a hint?<br class=""><br class="">Regards<br class="">Tobias<br class="">_______________________________________________<br class="">cfe-dev mailing list<br class=""><a href="mailto:cfe-dev@cs.uiuc.edu" class="">cfe-dev@cs.uiuc.edu</a><br class=""><a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" class="">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a></blockquote></div></blockquote></div><br class=""></body></html>