1b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar// RUN: %clang_cc1 -triple x86_64-windows -fsyntax-only -verify -fborland-extensions -fcxx-exceptions %s
228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley// This test is from http://docwiki.embarcadero.com/RADStudio/en/Try
428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyint puts(const char *);
628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleytemplate<typename T>
828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyint printf(const char *, T);
928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
1028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyconst char * strdup(const char *);
1128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
1228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyvoid free(const void *);
1328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
1428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley#define EXCEPTION_EXECUTE_HANDLER 1
1528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
1628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyclass Exception
1728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley{
1828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleypublic:
1928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  Exception(const char* s = "Unknown"){what = strdup(s);      }
2028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  Exception(const Exception& e ){what = strdup(e.what); }
2128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  ~Exception()                   {free(what);         }
2228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  const char* msg() const             {return what;           }
2328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyprivate:
2428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  const char* what;
2528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley};
2628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
2728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyint main()
2828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley{
2928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  float e, f, g;
3028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  try
3128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  {
3228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    try
3328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    {
3428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley      f = 1.0;
3528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley      g = 0.0;
3628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley      try
3728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley      {
3828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley        puts("Another exception:");
3928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
4028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley        e = f / g;
4128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley      }
4228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley      __except(EXCEPTION_EXECUTE_HANDLER)
4328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley      {
4428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley        puts("Caught a C-based exception.");
4528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley        throw(Exception("Hardware error: Divide by 0"));
4628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley      }
4728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    }
4828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    catch(const Exception& e)
4928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    {
5028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley      printf("Caught C++ Exception: %s :\n", e.msg());
5128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    }
5228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
5328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  __finally
5428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  {
5528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    puts("C++ allows __finally too!");
5628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
5728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  return e;
5828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley}
597752a741093888ea62e88777929fdaa4c23d0264David Majnemer
607752a741093888ea62e88777929fdaa4c23d0264David Majnemernamespace PR17584 {
617752a741093888ea62e88777929fdaa4c23d0264David Majnemertemplate <typename>
627752a741093888ea62e88777929fdaa4c23d0264David Majnemervoid Except() {
637752a741093888ea62e88777929fdaa4c23d0264David Majnemer  __try {
647752a741093888ea62e88777929fdaa4c23d0264David Majnemer  } __except(true) {
657752a741093888ea62e88777929fdaa4c23d0264David Majnemer  }
667752a741093888ea62e88777929fdaa4c23d0264David Majnemer}
677752a741093888ea62e88777929fdaa4c23d0264David Majnemer
687752a741093888ea62e88777929fdaa4c23d0264David Majnemertemplate <typename>
697752a741093888ea62e88777929fdaa4c23d0264David Majnemervoid Finally() {
707752a741093888ea62e88777929fdaa4c23d0264David Majnemer  __try {
717752a741093888ea62e88777929fdaa4c23d0264David Majnemer  } __finally {
727752a741093888ea62e88777929fdaa4c23d0264David Majnemer  }
737752a741093888ea62e88777929fdaa4c23d0264David Majnemer}
747752a741093888ea62e88777929fdaa4c23d0264David Majnemer
757752a741093888ea62e88777929fdaa4c23d0264David Majnemertemplate void Except<void>();
767752a741093888ea62e88777929fdaa4c23d0264David Majnemertemplate void Finally<void>();
777752a741093888ea62e88777929fdaa4c23d0264David Majnemer
787752a741093888ea62e88777929fdaa4c23d0264David Majnemer}
79c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines
80c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hinesvoid test___leave() {
81c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  // Most tests are in __try.c.
82c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines
83c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  // Clang accepts try with __finally. MSVC doesn't. (Maybe a Borland thing?)
84c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  // __leave in mixed blocks isn't supported.
85c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  try {
86c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines    __leave; // expected-error{{'__leave' statement not in __try block}}
87c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  } __finally {
88c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  }
89c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines}
90