From: Alexander Kornienko Date: Thu, 23 Mar 2017 15:17:44 +0000 (+0000) Subject: [clang-tidy] Fix diag message for catch-by-value X-Git-Tag: llvmorg-5.0.0-rc1~9340 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=5816a8bbb197a28715c77908a0dd089f0f237a93;p=platform%2Fupstream%2Fllvm.git [clang-tidy] Fix diag message for catch-by-value Summary: ``` catch (std::exception ex) { } ``` Was flagged with "catch handler catches a pointer value". Reviewers: alexfh, aaron.ballman Reviewed By: aaron.ballman Subscribers: cfe-commits, JDevlieghere Patch by Florian Gross! Differential Revision: https://reviews.llvm.org/D30592 llvm-svn: 298608 --- diff --git a/clang-tools-extra/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp b/clang-tools-extra/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp index 00befd2..90398cc 100644 --- a/clang-tools-extra/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp @@ -131,9 +131,6 @@ void ThrowByValueCatchByReferenceCheck::diagnoseThrowLocations( void ThrowByValueCatchByReferenceCheck::diagnoseCatchLocations( const CXXCatchStmt *catchStmt, ASTContext &context) { - const char *diagMsgCatchReference = "catch handler catches a pointer value; " - "should throw a non-pointer value and " - "catch by reference instead"; if (!catchStmt) return; auto caughtType = catchStmt->getCaughtType(); @@ -141,12 +138,17 @@ void ThrowByValueCatchByReferenceCheck::diagnoseCatchLocations( return; auto *varDecl = catchStmt->getExceptionDecl(); if (const auto *PT = caughtType.getCanonicalType()->getAs()) { + const char *diagMsgCatchReference = "catch handler catches a pointer value; " + "should throw a non-pointer value and " + "catch by reference instead"; // We do not diagnose when catching pointer to strings since we also allow // throwing string literals. if (!PT->getPointeeType()->isAnyCharacterType()) diag(varDecl->getLocStart(), diagMsgCatchReference); } else if (!caughtType->isReferenceType()) { - // If it's not a pointer and not a reference then it must be thrown "by + const char *diagMsgCatchReference = "catch handler catches by value; " + "should catch by reference instead"; + // If it's not a pointer and not a reference then it must be caught "by // value". In this case we should emit a diagnosis message unless the type // is trivial. if (!caughtType.isTrivialType(context)) diff --git a/clang-tools-extra/test/clang-tidy/misc-throw-by-value-catch-by-reference.cpp b/clang-tools-extra/test/clang-tidy/misc-throw-by-value-catch-by-reference.cpp index 4bf4f33..120b07e 100644 --- a/clang-tools-extra/test/clang-tidy/misc-throw-by-value-catch-by-reference.cpp +++ b/clang-tools-extra/test/clang-tidy/misc-throw-by-value-catch-by-reference.cpp @@ -62,7 +62,7 @@ void catchByValue() { try { testThrowFunc(); } catch (logic_error e) { - // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: catch handler catches a pointer value; should throw a non-pointer value and catch by reference instead [misc-throw-by-value-catch-by-reference] + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference] } }