From: Reid Kleckner Date: Mon, 20 Apr 2015 20:00:49 +0000 (+0000) Subject: Put statement expression decls in the enclosing code DeclContext X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f03ee81f953de3c5e17114f718cd6f68fb6db075;p=clang Put statement expression decls in the enclosing code DeclContext We already check that statement expressions are in a function or block, but we didn't do anything with that information. Now we use that DeclContext for the duration of the statement expression. Otherwise, we'd treat statement expression locals as static data members and go into the weeds. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@235335 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Parse/ParseExpr.cpp b/lib/Parse/ParseExpr.cpp index 315c9574e9..e889085b82 100644 --- a/lib/Parse/ParseExpr.cpp +++ b/lib/Parse/ParseExpr.cpp @@ -2106,6 +2106,17 @@ Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr, if (!getCurScope()->getFnParent() && !getCurScope()->getBlockParent()) { Result = ExprError(Diag(OpenLoc, diag::err_stmtexpr_file_scope)); } else { + // Find the nearest non-record decl context. Variables declared in a + // statement expression behave as if they were declared in the enclosing + // function, block, or other code construct. + DeclContext *CodeDC = Actions.CurContext; + while (CodeDC->isRecord() || isa(CodeDC)) { + CodeDC = CodeDC->getParent(); + assert(CodeDC && !CodeDC->isFileContext() && + "statement expr not in code context"); + } + Sema::ContextRAII SavedContext(Actions, CodeDC, /*NewThisContext=*/false); + Actions.ActOnStartStmtExpr(); StmtResult Stmt(ParseCompoundStatement(true)); diff --git a/test/Sema/statements.c b/test/Sema/statements.c index 9ab571521a..dbb4d56ee1 100644 --- a/test/Sema/statements.c +++ b/test/Sema/statements.c @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 %s -fsyntax-only -verify -triple x86_64-pc-linux-gnu +// RUN: %clang_cc1 %s -fsyntax-only -verify -triple x86_64-pc-linux-gnu -Wno-unevaluated-expression typedef unsigned __uint32_t; @@ -97,3 +97,16 @@ int test_pr8880() { return 1; } +// In PR22849, we considered __ptr to be a static data member of the anonymous +// union. Now we declare it in the parent DeclContext. +void test_pr22849() { + struct Bug { + typeof(({ unsigned long __ptr; (int *)(0); })) __val; + union Nested { + typeof(({ unsigned long __ptr; (int *)(0); })) __val; + } n; + }; + enum E { + SIZE = sizeof(({unsigned long __ptr; __ptr;})) + }; +}