[cfe-commits] r120331 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaChecking.cpp test/Analysis/stack-addr-ps.cpp test/SemaCXX/rval-references.cpp

Argyrios Kyrtzidis akyrtzi at gmail.com
Mon Nov 29 14:32:08 PST 2010


Author: akirtzidis
Date: Mon Nov 29 16:32:08 2010
New Revision: 120331

URL: http://llvm.org/viewvc/llvm-project?rev=120331&view=rev
Log:
Emit warnings if we are returning a reference to a local temporary.
The issue was brought to our attention by Matthieu Monrocq.

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/test/Analysis/stack-addr-ps.cpp
    cfe/trunk/test/SemaCXX/rval-references.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=120331&r1=120330&r2=120331&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Mon Nov 29 16:32:08 2010
@@ -3230,6 +3230,12 @@
   "address of stack memory associated with local variable %0 returned">;
 def warn_ret_stack_ref : Warning<
   "reference to stack memory associated with local variable %0 returned">;
+def warn_ret_local_temp_ref : Warning<
+  "returning reference to local temporary">;
+def warn_ret_local_temp_var_ref : Warning<
+  "reference to temporary associated with local variable %0 returned">;
+def note_local_temp_var_ref : Note<
+  "binding variable %0 to temporary here">;
 def warn_ret_addr_label : Warning<
   "returning address of label, which is local">;
 def err_ret_local_block : Error<

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=120331&r1=120330&r2=120331&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Mon Nov 29 16:32:08 2010
@@ -1787,11 +1787,35 @@
         << ALE->getSourceRange();
 
   } else if (lhsType->isReferenceType()) {
+    // Perform checking for local temporaries returned by reference.
+    if (RetValExp->Classify(Context).isPRValue()) {
+      Diag(RetValExp->getLocStart(), diag::warn_ret_local_temp_ref)
+        << RetValExp->getSourceRange();
+      return;
+    }
+
     // Perform checking for stack values returned by reference.
     // Check for a reference to the stack
-    if (DeclRefExpr *DR = EvalVal(RetValExp))
+    if (DeclRefExpr *DR = EvalVal(RetValExp)) {
+      const VarDecl *VD = cast<VarDecl>(DR->getDecl());
+      // Check for returning reference variable that binds to temporary.
+      if (VD->getType()->isReferenceType()) {
+        if (const Expr *init = VD->getInit())
+          if (init->Classify(Context).isPRValue()) {
+            Diag(DR->getLocStart(), diag::warn_ret_local_temp_var_ref)
+              << VD->getDeclName() << RetValExp->getSourceRange();
+            Diag(VD->getLocation(), diag::note_local_temp_var_ref)
+              << VD->getDeclName() << VD->getInit()->getSourceRange();
+          }
+
+        // When returning a reference variable that doesn't bind to temporary,
+        // no warning.
+        return;
+      }
+
       Diag(DR->getLocStart(), diag::warn_ret_stack_ref)
-        << DR->getDecl()->getDeclName() << RetValExp->getSourceRange();
+        << VD->getDeclName() << RetValExp->getSourceRange();
+    }
   }
 }
 
@@ -1953,7 +1977,7 @@
     DeclRefExpr *DR = cast<DeclRefExpr>(E);
 
     if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl()))
-      if (V->hasLocalStorage() && !V->getType()->isReferenceType()) return DR;
+      if (V->hasLocalStorage()) return DR;
 
     return NULL;
   }

Modified: cfe/trunk/test/Analysis/stack-addr-ps.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/stack-addr-ps.cpp?rev=120331&r1=120330&r2=120331&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/stack-addr-ps.cpp (original)
+++ cfe/trunk/test/Analysis/stack-addr-ps.cpp Mon Nov 29 16:32:08 2010
@@ -6,3 +6,12 @@
   int s;
   return s; // expected-warning{{reference to stack memory associated with local variable 's' returned}}
 }
+
+int get_value();
+
+const int &get_reference1() { return get_value(); } // expected-warning {{returning reference to local temporary}}
+
+const int &get_reference2() {
+  int const& w2 = get_value(); // expected-note {{binding variable 'w2' to temporary here}}
+  return w2; // expected-warning {{reference to temporary associated with local variable 'w2' returned}}
+}

Modified: cfe/trunk/test/SemaCXX/rval-references.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/rval-references.cpp?rev=120331&r1=120330&r2=120331&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/rval-references.cpp (original)
+++ cfe/trunk/test/SemaCXX/rval-references.cpp Mon Nov 29 16:32:08 2010
@@ -6,7 +6,7 @@
 typedef ilr&& ilr_c2; // Collapses to int&
 
 irr ret_irr() {
-  return 0;
+  return 0; // expected-warning {{returning reference to local temporary}}
 }
 
 struct not_int {};





More information about the cfe-commits mailing list