Skip to content

Commit

Permalink
switch: auto-break
Browse files Browse the repository at this point in the history
  • Loading branch information
bvdberg committed Nov 19, 2019
1 parent 51cd325 commit 974c1a4
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 12 deletions.
16 changes: 16 additions & 0 deletions c2c/AST/Stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ void Stmt::print(StringBuilder& buffer, unsigned indent) const {
return cast<BreakStmt>(this)->print(buffer, indent);
case STMT_CONTINUE:
return cast<ContinueStmt>(this)->print(buffer, indent);
case STMT_FALLTHROUGH:
return cast<FallthroughStmt>(this)->print(buffer, indent);
case STMT_LABEL:
return cast<LabelStmt>(this)->print(buffer, indent);
case STMT_GOTO:
Expand Down Expand Up @@ -101,6 +103,8 @@ SourceLocation Stmt::getLocation() const {
return cast<BreakStmt>(this)->getLocation();
case STMT_CONTINUE:
return cast<ContinueStmt>(this)->getLocation();
case STMT_FALLTHROUGH:
return cast<FallthroughStmt>(this)->getLocation();
case STMT_LABEL:
return cast<LabelStmt>(this)->getLocation();
case STMT_GOTO:
Expand Down Expand Up @@ -322,6 +326,18 @@ void ContinueStmt::print(StringBuilder& buffer, unsigned indent) const {
}


FallthroughStmt::FallthroughStmt(SourceLocation Loc_)
: Stmt(STMT_FALLTHROUGH)
, Loc(Loc_)
{}

void FallthroughStmt::print(StringBuilder& buffer, unsigned indent) const {
buffer.indent(indent);
buffer.setColor(COL_STMT);
buffer << "FallthroughStmt\n";
}


LabelStmt::LabelStmt(const char* name_, SourceLocation Loc_, Stmt* subStmt_)
: Stmt(STMT_LABEL)
, Loc(Loc_)
Expand Down
15 changes: 15 additions & 0 deletions c2c/AST/Stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ enum StmtKind {
STMT_DEFAULT,
STMT_BREAK,
STMT_CONTINUE,
STMT_FALLTHROUGH,
STMT_LABEL,
STMT_GOTO,
STMT_COMPOUND,
Expand Down Expand Up @@ -469,6 +470,20 @@ class ContinueStmt : public Stmt {
};


class FallthroughStmt : public Stmt {
public:
FallthroughStmt(SourceLocation Loc_);
static bool classof(const Stmt* S) {
return S->getKind() == STMT_FALLTHROUGH;
}

void print(StringBuilder& buffer, unsigned indent) const;
SourceLocation getLocation() const { return Loc; }
private:
SourceLocation Loc;
};


class LabelStmt : public Stmt {
public:
LabelStmt(const char* name_, SourceLocation Loc_, Stmt* subStmt_);
Expand Down
2 changes: 1 addition & 1 deletion c2c/Parser/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "Parser/Ownership.h"
#include "Parser/ParserTypes.h"

//#define PARSER_DEBUG
#define PARSER_DEBUG

#ifdef PARSER_DEBUG
#include <iostream>
Expand Down
1 change: 0 additions & 1 deletion test/Stmt/switch_case_not_enum.c2
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public func void foo() {
case 1: // @error{case value not in enumerated type '(enum)Foo' (aka 'i8')}
case bar: // @error{case value not in enumerated type '(enum)Foo' (aka 'i8')}
case Bar.C: // @error{case value not in enumerated type '(enum)Foo' (aka 'i8')}
break;
}
}

2 changes: 0 additions & 2 deletions test/Stmt/switch_duplicate_case.c2
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @skip
// @warnings{no-unused}
module test;

Expand All @@ -11,7 +10,6 @@ public func void foo() {
switch (bar) {
case 10:
case 10: // @error{duplicate case value '10'}
break;
}
}

1 change: 0 additions & 1 deletion test/Stmt/switch_duplicate_enum_case.c2
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public func void foo() {
switch (f) {
case Foo.A:
case Foo.A: // @error{duplicate case value 'A'}
break;
}
}

7 changes: 0 additions & 7 deletions test/Stmt/switch_enum_not_handled.c2
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ Foo f = Foo.A;
func void test1() {
switch (f) { // @error{4 enumeration values not handled in switch: B, C, D...}
case Foo.A:
break;
}
}

func void test2() {
switch (f) { // @error{enumeration values B, D, and E not handled in switch}
case Foo.A:
case Foo.C:
break;
}
}

Expand All @@ -30,7 +28,6 @@ func void test3() {
case Foo.A:
case Foo.C:
case Foo.D:
break;
}
}

Expand All @@ -40,7 +37,6 @@ func void test4() {
case Foo.B:
case Foo.C:
case Foo.D:
break;
}
}

Expand All @@ -51,14 +47,11 @@ func void test5() {
case Foo.C:
case Foo.D:
case Foo.E:
break;
default: // @warning{default label in switch which covers all enumeration values}
break;
}

switch (f) {
default:
break;
}
}

9 changes: 9 additions & 0 deletions test/Stmt/switch_fallthrough_default.c2
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @warnings{no-unused}
module test;

func void foo(i32 a) {
switch (a) {
default: fallthrough; // @error{fallthrough in last case}
}
}

10 changes: 10 additions & 0 deletions test/Stmt/switch_fallthrough_last.c2
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @warnings{no-unused}
module test;

func void foo(i32 a) {
switch (a) {
case 1:
case 2: fallthrough; // @error{fallthrough in last case}
}
}

7 changes: 7 additions & 0 deletions test/Stmt/switch_fallthrough_noswitch.c2
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @warnings{no-unused}
module test;

func void foo(i32 a) {
fallthrough; // @error{fallthrough not allowed here}
}

13 changes: 13 additions & 0 deletions test/Stmt/switch_fallthrough_sub.c2
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @warnings{no-unused}
module test;

func void foo(i32 a) {
switch (a) {
case 1: fallthrough;
case 2:
if (a == 1) fallthrough; // @error{fallthrough not allowed here}
default:
break;
}
}

27 changes: 27 additions & 0 deletions test/Stmt/switch_ok.c2
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// @warnings{no-unused}

module test;

func void foo(i32 a) {
while (1) {
switch (a) {
case 1:
break;
}
}

switch (a) {
case 1: fallthrough;
case 2: fallthrough;
case 3:
break;
}

switch (a) {
case 1: fallthrough;
case 2:
if (a == 2) break;
default:
}
}

0 comments on commit 974c1a4

Please sign in to comment.