[cfe-commits] r41477 - in /cfe/trunk: Parse/ParseStmt.cpp Sema/SemaStmt.cpp include/clang/Basic/DiagnosticKinds.def test/Sema/c89.c

Chris Lattner sabre at nondot.org
Sun Aug 26 21:29:42 PDT 2007


Author: lattner
Date: Sun Aug 26 23:29:41 2007
New Revision: 41477

URL: http://llvm.org/viewvc/llvm-project?rev=41477&view=rev
Log:
extwarn about decls intermixed with code in c89 mode.

Added:
    cfe/trunk/test/Sema/c89.c
Modified:
    cfe/trunk/Parse/ParseStmt.cpp
    cfe/trunk/Sema/SemaStmt.cpp
    cfe/trunk/include/clang/Basic/DiagnosticKinds.def

Modified: cfe/trunk/Parse/ParseStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Parse/ParseStmt.cpp?rev=41477&r1=41476&r2=41477&view=diff

==============================================================================
--- cfe/trunk/Parse/ParseStmt.cpp (original)
+++ cfe/trunk/Parse/ParseStmt.cpp Sun Aug 26 23:29:41 2007
@@ -84,7 +84,6 @@
 
   default:
     if (!OnlyStatement && isDeclarationSpecifier()) {
-      // TODO: warn/disable if declaration is in the middle of a block and !C99.
       return Actions.ParseDeclStmt(ParseDeclaration(Declarator::BlockContext));
     } else if (Tok.getKind() == tok::r_brace) {
       Diag(Tok, diag::err_expected_statement);
@@ -305,7 +304,6 @@
   if (SubStmt.isInvalid)
     SubStmt = Actions.ParseNullStmt(ColonLoc);
   
-  // TODO: look up enclosing switch stmt.
   return Actions.ParseCaseStmt(CaseLoc, LHS.Val, DotDotDotLoc, RHSVal, ColonLoc,
                                SubStmt.Val);
 }
@@ -337,7 +335,6 @@
   if (SubStmt.isInvalid)
     return true;
   
-  // TODO: look up enclosing switch stmt.
   return Actions.ParseDefaultStmt(DefaultLoc, ColonLoc, SubStmt.Val, CurScope);
 }
 

Modified: cfe/trunk/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaStmt.cpp?rev=41477&r1=41476&r2=41477&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/Sema/SemaStmt.cpp Sun Aug 26 23:29:41 2007
@@ -47,12 +47,28 @@
 
 Action::StmtResult 
 Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R,
-                        StmtTy **Elts, unsigned NumElts) {
-  // FIXME: ISO C90 forbids mixed declarations and code
-  // Note that __extension__ can be around a decl.
-  
+                        StmtTy **elts, unsigned NumElts) {
+  Stmt **Elts = reinterpret_cast<Stmt**>(elts);
+  // If we're in C89 mode, check that we don't have any decls after stmts.  If
+  // so, emit an extension diagnostic.
+  if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) {
+    // Note that __extension__ can be around a decl.
+    unsigned i = 0;
+    // Skip over all declarations.
+    for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i)
+      /*empty*/;
+
+    // We found the end of the list or a statement.  Scan for another declstmt.
+    for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i)
+      /*empty*/;
+    
+    if (i != NumElts) {
+      Decl *D = cast<DeclStmt>(Elts[i])->getDecl();
+      Diag(D->getLocation(), diag::ext_mixed_decls_code);
+    }
+  }
   
-  return new CompoundStmt((Stmt**)Elts, NumElts);
+  return new CompoundStmt(Elts, NumElts);
 }
 
 Action::StmtResult

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

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Sun Aug 26 23:29:41 2007
@@ -279,6 +279,9 @@
      "ISO C does not support '++'/'--' on complex integer types")
 DIAG(ext_integer_complement_complex, EXTENSION,
      "ISO C does not support '~' for complex conjugation")
+     
+DIAG(ext_mixed_decls_code, EXTENSION,
+     "ISO C90 forbids mixing declarations and code")
 
 DIAG(ext_empty_struct_union_enum, EXTENSION,
      "use of empty %0 extension")

Added: cfe/trunk/test/Sema/c89.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/c89.c?rev=41477&view=auto

==============================================================================
--- cfe/trunk/test/Sema/c89.c (added)
+++ cfe/trunk/test/Sema/c89.c Sun Aug 26 23:29:41 2007
@@ -0,0 +1,19 @@
+/* RUN: clang %s -std=c89 -pedantic -parse-ast-check
+ */
+void foo() {
+  {
+    int i;
+    i = i + 1;
+    int j;          /* expected-warning {{mixing declarations and code}} */
+  }
+  {
+    __extension__ int i;
+    i = i + 1;
+    int j;          /* expected-warning {{mixing declarations and code}} */
+  }
+  {
+    int i;
+    i = i + 1;
+    __extension__ int j; /* expected-warning {{mixing declarations and code}} */
+  }
+}





More information about the cfe-commits mailing list