Files
TheRock/compiler/amd-llvm/clang-tools-extra/test/clang-tidy/checkers/misc/throw-by-value-catch-by-reference.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

150 lines
4.3 KiB
C++

// RUN: %check_clang_tidy %s misc-throw-by-value-catch-by-reference %t -- -- -fcxx-exceptions
#include <utility>
class logic_error {
public:
logic_error(const char *message) {}
};
typedef logic_error *logic_ptr;
typedef logic_ptr logic_double_typedef;
int lastException;
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 std::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
}