[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 13:41:18 PDT 2015


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();
    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



More information about the cfe-dev mailing list