]> granicus.if.org Git - clang/commitdiff
PR3062: statement expressions should be illegal at file scope. I don't
authorEli Friedman <eli.friedman@gmail.com>
Sat, 24 Jan 2009 23:09:00 +0000 (23:09 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Sat, 24 Jan 2009 23:09:00 +0000 (23:09 +0000)
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

include/clang/Basic/DiagnosticKinds.def
lib/Sema/SemaExpr.cpp
test/Sema/stmt_exprs.c

index 13f5403745111b865d08df90ef9df395f3596516..93b023e560999a6e649af406dbdf55fc5d714c09 100644 (file)
@@ -1301,6 +1301,8 @@ DIAG(ext_typecheck_comparison_of_distinct_pointers, WARNING,
      "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")
index d4babb4baa4f945ad43c1f63e616077322dc1ce2..1d55ed7c3aa4e450ba8c54daa88bc345497df7c1 100644 (file)
@@ -3940,6 +3940,11 @@ Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
   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.
index ee835a355dc732a761bf806937bf79d40d3715f8..0d6fe8585f515012981d16384544c92d7d917722 100644 (file)
@@ -20,3 +20,5 @@ int test5() { return ({L1: L2: L3: 5;}); }
 int test6() { return ({5;}); }
 void test7() { ({5;}); }                   // expected-warning {{expression result unused}}
 
+// PR3062
+int x[({10;})]; // expected-error {{illegal statement expression}}