[clang-tools-extra] r298608 - [clang-tidy] Fix diag message for catch-by-value

Alexander Kornienko via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 23 08:17:44 PDT 2017


Author: alexfh
Date: Thu Mar 23 10:17:44 2017
New Revision: 298608

URL: http://llvm.org/viewvc/llvm-project?rev=298608&view=rev
Log:
[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

Modified:
    clang-tools-extra/trunk/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/misc-throw-by-value-catch-by-reference.cpp

Modified: clang-tools-extra/trunk/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp?rev=298608&r1=298607&r2=298608&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp Thu Mar 23 10:17:44 2017
@@ -131,9 +131,6 @@ void ThrowByValueCatchByReferenceCheck::
 
 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::
     return;
   auto *varDecl = catchStmt->getExceptionDecl();
   if (const auto *PT = caughtType.getCanonicalType()->getAs<PointerType>()) {
+    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))

Modified: clang-tools-extra/trunk/test/clang-tidy/misc-throw-by-value-catch-by-reference.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-throw-by-value-catch-by-reference.cpp?rev=298608&r1=298607&r2=298608&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/misc-throw-by-value-catch-by-reference.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-throw-by-value-catch-by-reference.cpp Thu Mar 23 10:17:44 2017
@@ -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]
   }
 }
 




More information about the cfe-commits mailing list