Author: jonastoth
Date: Wed Oct  4 09:49:20 2017
New Revision: 314913

URL: http://llvm.org/viewvc/llvm-project?rev=314913&view=rev
Log:
[clang-tidy] Emit note for variable declaration that are later deleted

This patch introduces a note for variable declaration that are later deleted.
Adds FIXME notes for possible automatic type-rewriting positions as well.

Reviewed by aaron.ballman
Differential: https://reviews.llvm.org/D38411

Modified:
    clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/OwningMemoryCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-owning-memory.cpp

Modified: 
clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/OwningMemoryCheck.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/OwningMemoryCheck.cpp?rev=314913&r1=314912&r2=314913&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/OwningMemoryCheck.cpp 
(original)
+++ clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/OwningMemoryCheck.cpp 
Wed Oct  4 09:49:20 2017
@@ -156,6 +156,13 @@ bool OwningMemoryCheck::handleDeletion(c
          "not marked 'gsl::owner<>'; consider using a "
          "smart pointer instead")
         << DeletedVariable->getSourceRange();
+
+    // FIXME: The declaration of the variable that was deleted can be
+    // rewritten.
+    const ValueDecl *Decl = DeletedVariable->getDecl();
+    diag(Decl->getLocStart(), "variable declared here", DiagnosticIDs::Note)
+        << Decl->getSourceRange();
+
     return true;
   }
   return false;
@@ -244,7 +251,9 @@ bool OwningMemoryCheck::handleAssignment
          "initializing non-owner %0 with a newly created 'gsl::owner<>'")
         << BadOwnerInitialization->getType()
         << BadOwnerInitialization->getSourceRange();
-    // FIXME: FixitHint to rewrite the type if possible.
+
+    // FIXME: FixitHint to rewrite the type of the initialized variable
+    // as 'gsl::owner<OriginalType>'
 
     // If the type of the variable was deduced, the wrapping owner typedef is
     // eliminated, therefore the check emits a special note for that case.
@@ -277,14 +286,15 @@ bool OwningMemoryCheck::handleReturnValu
 
   // Function return values, that should be owners but aren't.
   if (BadReturnType) {
-    // The returned value is of type owner, but not the declared return type.
+    // The returned value is a resource or variable that was not annotated with
+    // owner<> and the function return type is not owner<>.
     diag(BadReturnType->getLocStart(),
          "returning a newly created resource of "
          "type %0 or 'gsl::owner<>' from a "
          "function whose return type is not 'gsl::owner<>'")
         << Function->getReturnType() << BadReturnType->getSourceRange();
-    // The returned value is a resource that was not annotated with owner<> and
-    // the function return type is not owner<>.
+
+    // FIXME: Rewrite the return type as 'gsl::owner<OriginalType>'
     return true;
   }
   return false;

Modified: 
clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-owning-memory.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-owning-memory.cpp?rev=314913&r1=314912&r2=314913&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-owning-memory.cpp 
(original)
+++ clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-owning-memory.cpp 
Wed Oct  4 09:49:20 2017
@@ -142,11 +142,13 @@ void test_deletion() {
   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: initializing non-owner 'int *' 
with a newly created 'gsl::owner<>'
   delete unowned_int1; // BAD, since no owner
   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: deleting a pointer through a type 
that is not marked 'gsl::owner<>'; consider using a smart pointer instead
+  // CHECK-MESSAGES: [[@LINE-4]]:3: note: variable declared here
 
   int *unowned_int2 = new int[42]; // BAD, since new creates and owner
   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: initializing non-owner 'int *' 
with a newly created 'gsl::owner<>'
   delete[] unowned_int2; // BAD since no owner
   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: deleting a pointer through a type 
that is not marked 'gsl::owner<>'; consider using a smart pointer instead
+  // CHECK-MESSAGES: [[@LINE-4]]:3: note: variable declared here
 
   delete new int(42);   // Technically ok, but stupid
   delete[] new int[42]; // Technically ok, but stupid


_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to