PR3062: statement expressions should be illegal at file scope.  I don't 
think this has any significant effects at the moment, but it could 
matter if we start constant-folding statement expressions like gcc does.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@62943 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/Basic/DiagnosticKinds.def b/include/clang/Basic/DiagnosticKinds.def
index 13f5403..93b023e 100644
--- a/include/clang/Basic/DiagnosticKinds.def
+++ b/include/clang/Basic/DiagnosticKinds.def
@@ -1301,6 +1301,8 @@
      "comparison of distinct pointer types (%0 and %1)")
 DIAG(err_typecheck_assign_const, ERROR,
      "read-only variable is not assignable")
+DIAG(err_stmtexpr_file_scope, ERROR,
+     "statement expression not allowed at file scope")
 
 DIAG(err_invalid_this_use, ERROR,
      "invalid use of 'this' outside of a nonstatic member function")
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index d4babb4..1d55ed7 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -3940,6 +3940,11 @@
   assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
   CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
 
+  bool isFileScope = getCurFunctionOrMethodDecl() == 0;
+  if (isFileScope) {
+    return Diag(LPLoc, diag::err_stmtexpr_file_scope);
+  }
+
   // FIXME: there are a variety of strange constraints to enforce here, for
   // example, it is not possible to goto into a stmt expression apparently.
   // More semantic analysis is needed.
diff --git a/test/Sema/stmt_exprs.c b/test/Sema/stmt_exprs.c
index ee835a3..0d6fe85 100644
--- a/test/Sema/stmt_exprs.c
+++ b/test/Sema/stmt_exprs.c
@@ -20,3 +20,5 @@
 int test6() { return ({5;}); }
 void test7() { ({5;}); }                   // expected-warning {{expression result unused}}
 
+// PR3062
+int x[({10;})]; // expected-error {{illegal statement expression}}