[cfe-commits] r62943 - in /cfe/trunk: include/clang/Basic/DiagnosticKinds.def lib/Sema/SemaExpr.cpp test/Sema/stmt_exprs.c

Eli Friedman eli.friedman at gmail.com
Sat Jan 24 15:09:01 PST 2009


Author: efriedma
Date: Sat Jan 24 17:09:00 2009
New Revision: 62943

URL: http://llvm.org/viewvc/llvm-project?rev=62943&view=rev
Log:
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.


Modified:
    cfe/trunk/include/clang/Basic/DiagnosticKinds.def
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/Sema/stmt_exprs.c

Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=62943&r1=62942&r2=62943&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Sat Jan 24 17:09:00 2009
@@ -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")

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=62943&r1=62942&r2=62943&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Sat Jan 24 17:09:00 2009
@@ -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.

Modified: cfe/trunk/test/Sema/stmt_exprs.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/stmt_exprs.c?rev=62943&r1=62942&r2=62943&view=diff

==============================================================================
--- cfe/trunk/test/Sema/stmt_exprs.c (original)
+++ cfe/trunk/test/Sema/stmt_exprs.c Sat Jan 24 17:09:00 2009
@@ -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}}





More information about the cfe-commits mailing list