summaryrefslogtreecommitdiffstats
path: root/clang
diff options
context:
space:
mode:
Diffstat (limited to 'clang')
-rw-r--r--clang/include/clang/Basic/DiagnosticSemaKinds.td6
-rw-r--r--clang/lib/Sema/SemaChecking.cpp30
-rw-r--r--clang/test/Analysis/stack-addr-ps.cpp9
-rw-r--r--clang/test/SemaCXX/rval-references.cpp2
4 files changed, 4 insertions, 43 deletions
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 9404c6861d4..fd049e37d90 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -3230,12 +3230,6 @@ def warn_ret_stack_addr : Warning<
"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<
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 807a5507508..ed45de53f26 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -1787,35 +1787,11 @@ Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType,
<< 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)) {
- 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;
- }
-
+ if (DeclRefExpr *DR = EvalVal(RetValExp))
Diag(DR->getLocStart(), diag::warn_ret_stack_ref)
- << VD->getDeclName() << RetValExp->getSourceRange();
- }
+ << DR->getDecl()->getDeclName() << RetValExp->getSourceRange();
}
}
@@ -1977,7 +1953,7 @@ do {
DeclRefExpr *DR = cast<DeclRefExpr>(E);
if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl()))
- if (V->hasLocalStorage()) return DR;
+ if (V->hasLocalStorage() && !V->getType()->isReferenceType()) return DR;
return NULL;
}
diff --git a/clang/test/Analysis/stack-addr-ps.cpp b/clang/test/Analysis/stack-addr-ps.cpp
index 8ecb6a67eae..593ba1df94d 100644
--- a/clang/test/Analysis/stack-addr-ps.cpp
+++ b/clang/test/Analysis/stack-addr-ps.cpp
@@ -6,12 +6,3 @@ const int& g() {
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}}
-}
diff --git a/clang/test/SemaCXX/rval-references.cpp b/clang/test/SemaCXX/rval-references.cpp
index 2068642c9d0..30622ccbfed 100644
--- a/clang/test/SemaCXX/rval-references.cpp
+++ b/clang/test/SemaCXX/rval-references.cpp
@@ -6,7 +6,7 @@ typedef int& ilr;
typedef ilr&& ilr_c2; // Collapses to int&
irr ret_irr() {
- return 0; // expected-warning {{returning reference to local temporary}}
+ return 0;
}
struct not_int {};
OpenPOWER on IntegriCloud