| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 
 | // RUN: %check_clang_tidy %s misc-throw-by-value-catch-by-reference %t -- -- -fcxx-exceptions
class logic_error {
public:
  logic_error(const char *message) {}
};
typedef logic_error *logic_ptr;
typedef logic_ptr logic_double_typedef;
int lastException;
template <class T> struct remove_reference { typedef T type; };
template <class T> struct remove_reference<T &> { typedef T type; };
template <class T> struct remove_reference<T &&> { typedef T type; };
template <typename T> typename remove_reference<T>::type &&move(T &&arg) {
  return static_cast<typename remove_reference<T>::type &&>(arg);
}
logic_error CreateException() { return logic_error("created"); }
void testThrowFunc() {
  throw new logic_error("by pointer");
  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: throw expression throws a pointer; it should throw a non-pointer value instead [misc-throw-by-value-catch-by-reference]
  logic_ptr tmp = new logic_error("by pointer");
  throw tmp;
  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: throw expression should throw anonymous temporary values instead [misc-throw-by-value-catch-by-reference]
  // CHECK-MESSAGES: :[[@LINE-2]]:9: warning: throw expression throws a pointer; it should throw a non-pointer value instead [misc-throw-by-value-catch-by-reference]
  throw logic_error("by value");
  auto *literal = "test";
  throw logic_error(literal);
  throw "test string literal";
  throw L"throw wide string literal";
  const char *characters = 0;
  throw characters;
  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: throw expression should throw anonymous temporary values instead [misc-throw-by-value-catch-by-reference]
  // CHECK-MESSAGES: :[[@LINE-2]]:9: warning: throw expression throws a pointer; it should throw a non-pointer value instead [misc-throw-by-value-catch-by-reference]
  logic_error lvalue("lvalue");
  throw lvalue;
  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: throw expression should throw anonymous temporary values instead [misc-throw-by-value-catch-by-reference]
  throw move(lvalue);
  int &ex = lastException;
  throw ex;
  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: throw expression should throw anonymous temporary values instead [misc-throw-by-value-catch-by-reference]
  throw CreateException();
}
void throwReferenceFunc(logic_error &ref) { throw ref; }
void catchByPointer() {
  try {
    testThrowFunc();
  } catch (logic_error *e) {
    // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: catch handler catches a pointer value; should throw a non-pointer value and catch by reference instead [misc-throw-by-value-catch-by-reference]
  }
}
void catchByValue() {
  try {
    testThrowFunc();
  } catch (logic_error e) {
    // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]
  }
}
void catchByReference() {
  try {
    testThrowFunc();
  } catch (logic_error &e) {
  }
}
void catchByConstReference() {
  try {
    testThrowFunc();
  } catch (const logic_error &e) {
  }
}
void catchTypedef() {
  try {
    testThrowFunc();
  } catch (logic_ptr) {
    // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: catch handler catches a pointer value; should throw a non-pointer value and catch by reference instead [misc-throw-by-value-catch-by-reference]
  }
}
void catchAll() {
  try {
    testThrowFunc();
  } catch (...) {
  }
}
void catchLiteral() {
  try {
    testThrowFunc();
  } catch (const char *) {
  } catch (const wchar_t *) {
    // disabled for now until it is clear
    // how to enable them in the test
    //} catch (const char16_t*) {
    //} catch (const char32_t*) {
  }
}
// catching fundamentals should not warn
void catchFundamental() {
  try {
    testThrowFunc();
  } catch (int) {
  } catch (double) {
  } catch (unsigned long) {
  }
}
struct TrivialType {
  double x;
  double y;
};
void catchTrivial() {
  try {
    testThrowFunc();
  } catch (TrivialType) {
  }
}
typedef logic_error &fine;
void additionalTests() {
  try {
  } catch (int i) {  // ok
    throw i;         // ok
  } catch (fine e) { // ok
    throw e;         // ok
  } catch (logic_error *e) {
    // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: catch handler catches a pointer value; should throw a non-pointer value and catch by reference instead [misc-throw-by-value-catch-by-reference]
    throw e;      // ok, despite throwing a pointer
  } catch (...) { // ok
    throw;        // ok
  }
}
struct S {};
S &returnByReference();
S returnByValue();
void f() {
  throw returnByReference(); // Should diagnose
  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: throw expression should throw anonymous temporary values instead [misc-throw-by-value-catch-by-reference]
  throw returnByValue(); // Should not diagnose
}
 |