128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley// RUN: %clang_cc1 -fborland-extensions -fsyntax-only -verify %s
228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley#define JOIN2(x,y) x ## y
428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley#define JOIN(x,y) JOIN2(x,y)
528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley#define TEST2(name) JOIN(name,__LINE__)
628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley#define TEST TEST2(test)
728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleytypedef int DWORD;
828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley#pragma sysheader begin
1028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
1128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleystruct EXCEPTION_INFO{};
1228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
1328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyint __exception_code();
1428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleystruct EXCEPTION_INFO* __exception_info();
1528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyvoid __abnormal_termination();
1628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
1728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley#define GetExceptionCode __exception_code
1828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley#define GetExceptionInformation __exception_info
1928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley#define AbnormalTermination __abnormal_termination
2028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
2128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley#pragma sysheader end
2228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
231aaa249734b9c87e4ad5ed8b4e8cfd26ee921741Douglas GregorDWORD FilterExpression(int); // expected-note{{declared here}}
2428bbe4b8acc338476fe0825769b41fb32b423c72John WiegleyDWORD FilterExceptionInformation(struct EXCEPTION_INFO*);
2528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
2628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyconst char * NotFilterExpression();
2728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
2828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyvoid TEST() {
2928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __try {
3028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    __try {
3128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley      __try {
3228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley      }
3328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley      __finally{
3428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley      }
3528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    }
3628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    __finally{
3728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    }
3828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
3928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __finally{
4028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
4128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley}
4228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
4328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyvoid TEST() {
4428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __try {
4528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
4628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
4728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley}  // expected-error{{expected '__except' or '__finally' block}}
4828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
4928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyvoid TEST() {
501aaa249734b9c87e4ad5ed8b4e8cfd26ee921741Douglas Gregor  __except ( FilterExpression() ) { // expected-warning{{implicit declaration of function '__except' is invalid in C99}} \
511aaa249734b9c87e4ad5ed8b4e8cfd26ee921741Douglas Gregor    // expected-error{{too few arguments to function call, expected 1, have 0}}
5228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
5328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
5428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley}
5528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
5628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyvoid TEST() {
5728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __finally { } // expected-error{{}}
5828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley}
5928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
6028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyvoid TEST() {
6128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __try{
6228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    int try_scope = 0;
6328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  } // TODO: expected expression is an extra error
6428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __except( try_scope ? 1 : -1 ) // expected-error{{undeclared identifier 'try_scope'}} expected-error{{expected expression}}
6528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  {}
6628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley}
6728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
6828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyvoid TEST() {
6928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __try {
7028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
7128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
7228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  // TODO: Why are there two errors?
7328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __except( ) { // expected-error{{expected expression}} expected-error{{expected expression}}
7428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
7528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley}
7628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
7728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyvoid TEST() {
7828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __try {
7928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
8028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
8128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __except ( FilterExpression(GetExceptionCode()) ) {
8228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
8328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
8428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
8528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __try {
8628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
8728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
8828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __except( FilterExpression(__exception_code()) ) {
8928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
9028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
9128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
9228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __try {
9328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
9428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
9528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __except( FilterExceptionInformation(__exception_info()) ) {
9628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
9728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
9828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
9928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __try {
10028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
10128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
10228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __except(FilterExceptionInformation( GetExceptionInformation() ) ) {
10328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
10428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
10528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley}
10628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
10728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyvoid TEST() {
10828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __try {
10928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
11028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
11128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __except ( NotFilterExpression() ) { // expected-error{{filter expression type should be an integral value not 'const char *'}}
11228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
11328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
11428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley}
11528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
11628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyvoid TEST() {
11728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  int function_scope = 0;
11828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __try {
11928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    int try_scope = 0;
12028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
12128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __except ( FilterExpression(GetExceptionCode()) ) {
12228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    (void)function_scope;
12328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    (void)try_scope; // expected-error{{undeclared identifier}}
12428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
12528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley}
12628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
12728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyvoid TEST() {
12828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  int function_scope = 0;
12928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __try {
13028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    int try_scope = 0;
13128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
13228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __finally {
13328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    (void)function_scope;
13428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    (void)try_scope; // expected-error{{undeclared identifier}}
13528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
13628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley}
13728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
13828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyvoid TEST() {
13928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  int function_scope = 0;
14028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __try {
14128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
14228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
14328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __except( function_scope ? 1 : -1 ) {}
14428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley}
14528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
14628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyvoid TEST() {
14728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __try {
14828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    (void)AbnormalTermination;  // expected-error{{only allowed in __finally block}}
14928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    (void)__abnormal_termination; // expected-error{{only allowed in __finally block}}
15028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
15128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __except( 1 ) {
15228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    (void)AbnormalTermination;  // expected-error{{only allowed in __finally block}}
15328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    (void)__abnormal_termination; // expected-error{{only allowed in __finally block}}
15428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
15528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
15628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __try {
15728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
15828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __finally {
15928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    AbnormalTermination();
16028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    __abnormal_termination();
16128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
16228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley}
16328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
16428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyvoid TEST() {
16528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  (void)__exception_code;       // expected-error{{only allowed in __except block}}
16628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  (void)__exception_info;       // expected-error{{only allowed in __except filter expression}}
16728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  (void)__abnormal_termination; // expected-error{{only allowed in __finally block}}
16828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
16928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  (void)GetExceptionCode();     // expected-error{{only allowed in __except block}}
17028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  (void)GetExceptionInformation(); // expected-error{{only allowed in __except filter expression}}
17128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  (void)AbnormalTermination();  // expected-error{{only allowed in __finally block}}
17228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley}
173ef8225444452a1486bd721f3285301fe84643b00Stephen Hines
174ef8225444452a1486bd721f3285301fe84643b00Stephen Hinesvoid test_seh_leave_stmt() {
175ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  __leave; // expected-error{{'__leave' statement not in __try block}}
176ef8225444452a1486bd721f3285301fe84643b00Stephen Hines
177ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  __try {
178ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    __leave;
179ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    __leave 4; // expected-error{{expected ';' after __leave statement}}
180ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  } __except(1) {
181ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    __leave; // expected-error{{'__leave' statement not in __try block}}
182ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  }
183ef8225444452a1486bd721f3285301fe84643b00Stephen Hines
184ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  __try {
185ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    __leave;
186ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  } __finally {
187ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    __leave; // expected-error{{'__leave' statement not in __try block}}
188ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  }
189ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  __leave; // expected-error{{'__leave' statement not in __try block}}
190ef8225444452a1486bd721f3285301fe84643b00Stephen Hines}
191ef8225444452a1486bd721f3285301fe84643b00Stephen Hines
192