mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-16 16:36:46 +00:00

The builtin causes the program to stop its execution abnormally and shows a human-readable description of the reason for the termination when a debugger is attached or in a symbolicated crash log. The motivation for the builtin is explained in the following RFC: https://discourse.llvm.org/t/rfc-adding-builtin-verbose-trap-string-literal/75845 clang's CodeGen lowers the builtin to `llvm.trap` and emits debugging information that represents an artificial inline frame whose name encodes the category and reason strings passed to the builtin.
48 lines
2.4 KiB
C++
48 lines
2.4 KiB
C++
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -fcxx-exceptions -verify %s
|
|
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -fcxx-exceptions -verify %s
|
|
|
|
#if !__has_builtin(__builtin_verbose_trap)
|
|
#error
|
|
#endif
|
|
|
|
constexpr char const* constCat1 = "cat";
|
|
char const* const constCat2 = "cat";
|
|
char const constCat3[] = "cat";
|
|
|
|
constexpr char const* constMsg1 = "hello";
|
|
char const* const constMsg2 = "hello";
|
|
char const constMsg3[] = "hello";
|
|
|
|
template <const char * const category, const char * const reason>
|
|
void f(const char * arg) {
|
|
__builtin_verbose_trap("cat1", "Arbitrary string literals can be used!");
|
|
__builtin_verbose_trap(" cat1 ", "Argument_must_not_be_null");
|
|
__builtin_verbose_trap("cat" "egory1", "hello" "world");
|
|
__builtin_verbose_trap(constCat1, constMsg1);
|
|
__builtin_verbose_trap(constCat2, constMsg2);
|
|
__builtin_verbose_trap("", "");
|
|
__builtin_verbose_trap(); // expected-error {{too few arguments}}
|
|
__builtin_verbose_trap(""); // expected-error {{too few arguments}}
|
|
__builtin_verbose_trap("", "", ""); // expected-error {{too many arguments}}
|
|
__builtin_verbose_trap("", 0); // expected-error {{argument to __builtin_verbose_trap must be a pointer to a constant string}}
|
|
__builtin_verbose_trap(1, ""); // expected-error {{cannot initialize a parameter of type 'const char *' with an rvalue of type 'int'}}
|
|
__builtin_verbose_trap(arg, ""); // expected-error {{argument to __builtin_verbose_trap must be a pointer to a constant string}}
|
|
__builtin_verbose_trap("cat$1", "hel$lo"); // expected-error 2 {{argument to __builtin_verbose_trap must not contain $}}
|
|
__builtin_verbose_trap(category, reason);
|
|
__builtin_verbose_trap(u8"cat1", u8"hello");
|
|
#if __cplusplus >= 202002L
|
|
// FIXME: Accept c++20 u8 string literals.
|
|
// expected-error@-3 {{cannot initialize a parameter of type 'const char *' with an lvalue of type 'const char8_t[5]'}}
|
|
#endif
|
|
__builtin_verbose_trap("", "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd");
|
|
}
|
|
|
|
template <const char * const category>
|
|
void f2() {
|
|
__builtin_verbose_trap(category, 1); // expected-error {{cannot initialize a parameter of type 'const char *' with an rvalue of type 'int'}}
|
|
}
|
|
|
|
void test() {
|
|
f<constCat3, constMsg3>(nullptr);
|
|
}
|