From 9e6b37a9f1d499e7ca0950edacd0b6569e491d7f Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Fri, 30 Oct 2009 04:01:58 +0000 Subject: [PATCH] warn about returning the address of a label. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@85576 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 6 ++++-- lib/Sema/SemaChecking.cpp | 7 ++++++- test/Sema/statements.c | 6 ++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 1ce4d91a17..00723a3af9 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -2140,6 +2140,10 @@ 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_addr_label : Warning< + "returning address of label, which is local">; +def err_ret_local_block : Error< + "returning block that lives on the local stack">; // For non-floating point, expressions of the form x == x or x != x @@ -2163,8 +2167,6 @@ def err_return_in_block_expression : Error< def err_block_returns_array : Error< "block declared as returning an array">; -def err_ret_local_block : Error< - "returning block that lives on the local stack">; // CFString checking def err_cfstring_literal_not_string_constant : Error< diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index b7ccedec70..38b6ebeefa 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -1272,10 +1272,15 @@ Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType, // Skip over implicit cast expressions when checking for block expressions. RetValExp = RetValExp->IgnoreParenCasts(); - if (BlockExpr *C = dyn_cast_or_null(RetValExp)) + if (BlockExpr *C = dyn_cast(RetValExp)) if (C->hasBlockDeclRefExprs()) Diag(C->getLocStart(), diag::err_ret_local_block) << C->getSourceRange(); + + if (AddrLabelExpr *ALE = dyn_cast(RetValExp)) + Diag(ALE->getLocStart(), diag::warn_ret_addr_label) + << ALE->getSourceRange(); + } else if (lhsType->isReferenceType()) { // Perform checking for stack values returned by reference. // Check for a reference to the stack diff --git a/test/Sema/statements.c b/test/Sema/statements.c index 9a71a40370..8eac052a25 100644 --- a/test/Sema/statements.c +++ b/test/Sema/statements.c @@ -27,3 +27,9 @@ int test8[({10;})]; // expected-error {{statement expression not allowed at file void test9(const void *P) { __builtin_prefetch(P); } + + +void *test10() { +bar: + return &&bar; // expected-warning {{returning address of label, which is local}} +} -- 2.40.0