Updated patch according to Richard Smith's notes.
http://llvm-reviews.chandlerc.com/D2018
CHANGE SINCE LAST DIFF
http://llvm-reviews.chandlerc.com/D2018?vs=5133&id=5699#toc
Files:
include/clang/Sema/Scope.h
lib/Parse/ParseStmt.cpp
lib/Sema/Scope.cpp
test/Parser/bad-control.c
test/Sema/statements.c
Index: include/clang/Sema/Scope.h
===================================================================
--- include/clang/Sema/Scope.h
+++ include/clang/Sema/Scope.h
@@ -342,6 +342,16 @@
/// Init - This is used by the parser to implement scope caching.
///
void Init(Scope *parent, unsigned flags);
+
+ /// \brief Sets up the specified scope flags and adjusts the scope state
+ /// variables accordingly.
+ ///
+ void AddFlags(unsigned Flags);
+
+ /// \brief Clears the specified scope flags and adjusts the scope state
+ /// variables accordingly.
+ ///
+ void RemoveFlags(unsigned Flags);
};
} // end namespace clang
Index: lib/Parse/ParseStmt.cpp
===================================================================
--- lib/Parse/ParseStmt.cpp
+++ lib/Parse/ParseStmt.cpp
@@ -1175,7 +1175,7 @@
// while, for, and switch statements are local to the if, while, for, or
// switch statement (including the controlled statement).
//
- unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
+ unsigned ScopeFlags = Scope::SwitchScope;
if (C99orCXX)
ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
ParseScope SwitchScope(this, ScopeFlags);
@@ -1213,6 +1213,7 @@
// See comments in ParseIfStatement for why we create a scope for the
// condition and a new scope for substatement in C++.
//
+ getCurScope()->AddFlags(Scope::BreakScope);
ParseScope InnerScope(this, Scope::DeclScope,
C99orCXX && Tok.isNot(tok::l_brace));
@@ -1264,12 +1265,9 @@
// while, for, and switch statements are local to the if, while, for, or
// switch statement (including the controlled statement).
//
- unsigned ScopeFlags;
+ unsigned ScopeFlags = 0;
if (C99orCXX)
- ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
- Scope::DeclScope | Scope::ControlScope;
- else
- ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
+ ScopeFlags = Scope::DeclScope | Scope::ControlScope;
ParseScope WhileScope(this, ScopeFlags);
// Parse the condition.
@@ -1291,6 +1289,7 @@
// See comments in ParseIfStatement for why we create a scope for the
// condition and a new scope for substatement in C++.
//
+ getCurScope()->AddFlags(Scope::BreakScope | Scope::ContinueScope);
ParseScope InnerScope(this, Scope::DeclScope,
C99orCXX && Tok.isNot(tok::l_brace));
@@ -1342,6 +1341,7 @@
// Pop the body scope if needed.
InnerScope.Exit();
+ getCurScope()->RemoveFlags(Scope::BreakScope | Scope::ContinueScope);
if (Tok.isNot(tok::kw_while)) {
if (!Body.isInvalid()) {
@@ -1424,12 +1424,9 @@
// Names declared in the for-init-statement are in the same declarative-region
// as those declared in the condition.
//
- unsigned ScopeFlags;
+ unsigned ScopeFlags = 0;
if (C99orCXXorObjC)
- ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
- Scope::DeclScope | Scope::ControlScope;
- else
- ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
+ ScopeFlags = Scope::DeclScope | Scope::ControlScope;
ParseScope ForScope(this, ScopeFlags);
@@ -1578,12 +1575,15 @@
}
// Parse the third part of the for specifier.
+ getCurScope()->AddFlags(Scope::BreakScope | Scope::ContinueScope);
if (Tok.isNot(tok::r_paren)) { // for (...;...;)
ExprResult Third = ParseExpression();
// FIXME: The C++11 standard doesn't actually say that this is a
// discarded-value expression, but it clearly should be.
ThirdPart = Actions.MakeFullDiscardedValueExpr(Third.take());
}
+ } else {
+ getCurScope()->AddFlags(Scope::BreakScope | Scope::ContinueScope);
}
// Match the ')'.
T.consumeClose();
Index: lib/Sema/Scope.cpp
===================================================================
--- lib/Sema/Scope.cpp
+++ lib/Sema/Scope.cpp
@@ -69,3 +69,34 @@
}
return false;
}
+
+void Scope::AddFlags(unsigned FlagsToSet) {
+ assert((FlagsToSet & ~(BreakScope | ContinueScope)) == 0 ||
+ "Unsupported scope flags");
+ assert ((Flags & ControlScope) != 0 || "Must be control scope");
+ if (FlagsToSet & BreakScope) {
+ assert((Flags & BreakScope) == 0 || "Already set");
+ BreakParent = this;
+ }
+ if (FlagsToSet & ContinueScope) {
+ assert((Flags & ContinueScope) == 0 || "Already set");
+ ContinueParent = this;
+ }
+ Flags |= FlagsToSet;
+}
+
+void Scope::RemoveFlags(unsigned FlagsToClear) {
+ assert((FlagsToClear & ~(BreakScope | ContinueScope)) == 0 ||
+ "Unsupported scope flags");
+ if (FlagsToClear & BreakScope) {
+ assert((Flags & ControlScope) != 0 || "Must be control scope");
+ assert((Flags & BreakScope) != 0 || "Already cleared");
+ BreakParent = 0;
+ }
+ if (FlagsToClear & ContinueScope) {
+ assert ((Flags & ControlScope) != 0 || "Must be control scope");
+ assert((Flags & ContinueScope) != 0 || "Already cleared");
+ ContinueParent = 0;
+ }
+ Flags &= ~FlagsToClear;
+}
Index: test/Parser/bad-control.c
===================================================================
--- test/Parser/bad-control.c
+++ test/Parser/bad-control.c
@@ -7,3 +7,98 @@
void foo2() {
continue; /* expected-error {{'continue' statement not in loop statement}} */
}
+
+int pr8880() {
+ int first = 1;
+ for ( ; ({ if (first) { first = 0; continue; } 0; }); ) /* expected-error {{'continue' statement not in loop statement}} */
+ return 0;
+ return 1;
+}
+
+int pr8880_1() {
+ int first = 1;
+ for ( ; ({ if (first) { first = 0; break; } 0; }); ) /* expected-error {{'break' statement not in loop or switch statement}} */
+ return 0;
+ return 1;
+}
+
+int pr8880_2 (int a) {
+ int first = a;
+ while(({ if (first) { first = 0; continue; } 0; })) /* expected-error {{'continue' statement not in loop statement}} */
+ return a;
+}
+
+int pr8880_3 (int a) {
+ int first = a;
+ while(({ if (first) { first = 0; break; } 0; })) /* expected-error {{'break' statement not in loop or switch statement}} */
+ return a;
+}
+
+int pr8880_4 (int a) {
+ int first = a;
+ do {
+ return a;
+ } while(({ if (first) { first = 0; continue; } 0; })); /* expected-error {{'continue' statement not in loop statement}} */
+}
+
+int pr8880_5 (int a) {
+ int first = a;
+ do {
+ return a;
+ } while(({ if (first) { first = 0; break; } 0; })); /* expected-error {{'break' statement not in loop or switch statement}} */
+}
+
+int pr8880_6 (int a) {
+ int first = a;
+ switch(({ if (first) { first = 0; break; } a; })) { /* expected-error {{'break' statement not in loop or switch statement}} */
+ case 2: return a;
+ default: return 0;
+ }
+ return 1;
+}
+
+// GCC compatibility: break and continue in the 3rd expression of for are parts of loop body,
+// break and continue in cond and init expressions refer to enclosing statement.
+void pr8880_7() {
+ int i;
+ for (i = 0 ; i != 10 ; i++ ) {
+ for ( ; ; ({ ++i; continue; })) {
+ }
+ }
+}
+
+void pr8880_8() {
+ int i;
+ for (i = 0 ; i != 10 ; i++ )
+ for ( ; ; ({ ++i; break; })) {
+ }
+}
+
+void pr8880_9(int x, int y) {
+ switch(x) {
+ case 1:
+ while(({if (y) break; y;})) {}
+ }
+}
+
+void pr8880_10(int x, int y) {
+ while(x > 0) {
+ switch(({if(y) break; y;})) {
+ case 2: x=0;
+ }
+ }
+}
+
+void pr8880_11() {
+ int i;
+ for (i = 0 ; i != 10 ; i++ ) {
+ while(({if (i) break; i;})) {}
+ }
+}
+
+void pr8880_12() {
+ int i, j;
+ for (i = 0 ; i != 10 ; i++ ) {
+ for (j=0; ({if (i) continue; i;}); j++) {}
+ }
+}
Index: test/Sema/statements.c
===================================================================
--- test/Sema/statements.c
+++ test/Sema/statements.c
@@ -95,7 +95,7 @@
// was causing a crash in the CFG builder.
int test_pr8880() {
int first = 1;
- for ( ; ({ if (first) { first = 0; continue; } 0; }); )
+ for ( ; ({ if (first) { first = 0; continue; } 0; }); ) // expected-error {{'continue' statement not in loop statement}}
return 0;
return 1;
}
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits