llvm-project/clang/Sema/SemaStmt.cpp
Steve Naroff 72cada0ad9 Bug #:
Submitted by:
Reviewed by:
Extended Expr's constant expression predicates to return a source location
if the predicate returns false. This enables us to position the cursor
exactly were the error occurred (simple pleasures:-).

constant.c:9:9: error: enumerator value for 'E2' is not an integer constant
  E2 = (aconst + 1), // illegal
        ^
constant.c:10:8: error: enumerator value for 'E3' is not an integer constant
  E3 = "abc",
       ^
constant.c:12:12: error: enumerator value for 'E5' is not an integer constant
  E5 = 0?7:printf("xx"), // illegal
           ^
constant.c:13:12: error: enumerator value for 'E6' is not an integer constant
  E6 = 1?7:printf("xx"), // legal
           ^
constant.c:16:14: error: enumerator value for 'E9' is not an integer constant
  E9 = E0 || a, // illegal
             ^
constant.c:21:6: error: array has incomplete element type 'void'
void ary[7];
     ^
constant.c:22:28: error: variable length array declared outside of any function
struct { int a; } ary2[1?7:printf("xx")],
                           ^
constant.c:23:34: error: variable length array declared outside of any function
                  aryIllegal[0?7:printf("yy")];
                                 ^
constant.c:25:10: error: variable length array declared outside of any function
int ary3[a]; // illegal
         ^
constant.c:26:17: error: size of array has non-integer type 'float'
typedef int vla[2.0]; // illegal
                ^
constant.c:30:22: error: size of array has non-integer type 'float'
int nonIntegerArray2[1+2.0];
                     ^

llvm-svn: 39454
2007-05-18 00:18:52 +00:00

133 lines
4.2 KiB
C++

//===--- SemaStmt.cpp - Semantic Analysis for Statements ------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by Chris Lattner and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements semantic analysis for statements.
//
//===----------------------------------------------------------------------===//
#include "Sema.h"
#include "clang/AST/Stmt.h"
#include "clang/AST/Expr.h"
#include "clang/Parse/Scope.h"
#include "clang/Basic/Diagnostic.h"
using namespace llvm;
using namespace clang;
Action::StmtResult
Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R,
StmtTy **Elts, unsigned NumElts) {
if (NumElts > 1)
return new CompoundStmt((Stmt**)Elts, NumElts);
else if (NumElts == 1)
return Elts[0]; // {stmt} -> stmt
else
return 0; // {} -> ;
}
Action::StmtResult
Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *LHSVal,
SourceLocation DotDotDotLoc, ExprTy *RHSVal,
SourceLocation ColonLoc, StmtTy *SubStmt) {
assert((LHSVal != 0) && "missing expression in case statement");
SourceLocation expLoc;
// C99 6.8.4.2p3: The expression shall be an integer constant.
if (!((Expr *)LHSVal)->isIntegerConstantExpr(expLoc))
return Diag(CaseLoc, diag::err_case_label_not_integer_constant_expr);
return new CaseStmt((Expr*)LHSVal, (Expr*)RHSVal, (Stmt*)SubStmt);
}
Action::StmtResult
Sema::ParseDefaultStmt(SourceLocation DefaultLoc,
SourceLocation ColonLoc, StmtTy *SubStmt) {
return new DefaultStmt((Stmt*)SubStmt);
}
Action::StmtResult
Sema::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
SourceLocation ColonLoc, StmtTy *SubStmt) {
return new LabelStmt(II, (Stmt*)SubStmt);
}
Action::StmtResult
Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
StmtTy *ThenVal, SourceLocation ElseLoc,
StmtTy *ElseVal) {
return new IfStmt((Expr*)CondVal, (Stmt*)ThenVal, (Stmt*)ElseVal);
}
Action::StmtResult
Sema::ParseSwitchStmt(SourceLocation SwitchLoc, ExprTy *Cond, StmtTy *Body) {
return new SwitchStmt((Expr*)Cond, (Stmt*)Body);
}
Action::StmtResult
Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body){
return new WhileStmt((Expr*)Cond, (Stmt*)Body);
}
Action::StmtResult
Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
SourceLocation WhileLoc, ExprTy *Cond) {
return new DoStmt((Stmt*)Body, (Expr*)Cond);
}
Action::StmtResult
Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
StmtTy *First, ExprTy *Second, ExprTy *Third,
SourceLocation RParenLoc, StmtTy *Body) {
return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
}
Action::StmtResult
Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
IdentifierInfo *LabelII) {
return new GotoStmt(LabelII);
}
Action::StmtResult
Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
ExprTy *DestExp) {
return new IndirectGotoStmt((Expr*)DestExp);
}
Action::StmtResult
Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
Scope *S = CurScope->getContinueParent();
if (!S) {
// C99 6.8.6.2p1: A break shall appear only in or as a loop body.
Diag(ContinueLoc, diag::err_continue_not_in_loop);
return true;
}
// FIXME: Remember that this continue goes with this loop.
return new ContinueStmt();
}
Action::StmtResult
Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
Scope *S = CurScope->getBreakParent();
if (!S) {
// C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
return true;
}
// FIXME: Remember that this break goes with this loop/switch.
return new BreakStmt();
}
Action::StmtResult
Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *RetValExp) {
return new ReturnStmt((Expr*)RetValExp);
}