[cfe-dev] clang ast: how do I find out whether a catch is done by const reference

Tobias Langner randomcppprogrammer at gmail.com
Thu Jul 16 14:05:25 PDT 2015


> On 16 Jul 2015, at 22:50, Aaron Ballman <aaron at aaronballman.com> wrote:
> 
> On Thu, Jul 16, 2015 at 4:41 PM, Tobias Langner
> <randomcppprogrammer at gmail.com <mailto:randomcppprogrammer at gmail.com>> wrote:
>> Hi,
>> 
>> 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:
>> 
>>  const auto *catchStmt = Result.Nodes.getNodeAs<CXXCatchStmt>("catch");
>>  if (catchStmt != nullptr) {
>>    auto caughtType = catchStmt->getCaughtType();
> 
> This returns the QualType you are looking for.
> 
> caughType.isConstant() && caughtType->isLValueReferenceType();
> 
> should tell you if it's a const-qualified lvalue reference type.
> 
> ~Aaron
> 
This gives me the same result as using QualType::isConstQualified() (see below). Both my examples are detected as not being caught by const reference:

void catchByReference() {
  try {
    testThrowFunc();
  } catch (logic_error &e) {
  }
}

void catchByConstReference() {
  try {
    testThrowFunc();
  } catch (const logic_error &e) {
  }
}

giving this result:

/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: warning: catch by const reference to avoid unnecessary copies [misc-throw-by-value-catch-by-reference]
  } catch (logic_error &e) {
           ^
/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: warning: catch by const reference to avoid unnecessary copies [misc-throw-by-value-catch-by-reference]
  } catch (const logic_error &e) {

so for both caughtTypes of these catch statements, neither isConstant() nor isConstQualified() returns true.

Regards
Tobias

>>    auto *type = caughtType.getTypePtr();
>>    auto *varDecl = catchStmt->getExceptionDecl();
>>    if (type->isPointerType()) {
>>      diag(varDecl->getLocStart(),
>>           "catch by const reference instead of pointer");
>>    } else if (!type->isReferenceType()) {
>>      diag(varDecl->getLocStart(), "catch by const reference");
>>    } else {
>>      if (caughtType.isConstQualified() == false) {
>>        diag(varDecl->getLocStart(),
>>             "catch by const reference to avoid unnecessary copies");
>>      }
>>    }
>>  }
>> 
>> 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?
>> 
>> Regards
>> Tobias
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at cs.uiuc.edu <mailto:cfe-dev at cs.uiuc.edu>
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev <http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150716/98d62835/attachment.html>


More information about the cfe-dev mailing list