diff options
author | Akira Hatanaka <ahatanak@gmail.com> | 2024-06-25 08:33:05 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-25 08:33:05 -0700 |
commit | 2604830aacdd563715da030d0396b565e912436f (patch) | |
tree | 3d396e526dca83fe550fc71c19fabadc28b352f4 /clang/lib/Sema/SemaChecking.cpp | |
parent | 731db06a878f5c8cb29b36d526a54493677ea89f (diff) | |
download | llvm-2604830aacdd563715da030d0396b565e912436f.zip llvm-2604830aacdd563715da030d0396b565e912436f.tar.gz llvm-2604830aacdd563715da030d0396b565e912436f.tar.bz2 |
Add support for __builtin_verbose_trap (#79230)
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.
Diffstat (limited to 'clang/lib/Sema/SemaChecking.cpp')
-rw-r--r-- | clang/lib/Sema/SemaChecking.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 8798851..bce941c 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -183,6 +183,33 @@ bool Sema::checkArgCount(CallExpr *Call, unsigned DesiredArgCount) { << /*is non object*/ 0 << Call->getArg(1)->getSourceRange(); } +static bool checkBuiltinVerboseTrap(CallExpr *Call, Sema &S) { + bool HasError = false; + + for (unsigned I = 0; I < Call->getNumArgs(); ++I) { + Expr *Arg = Call->getArg(I); + + if (Arg->isValueDependent()) + continue; + + std::optional<std::string> ArgString = Arg->tryEvaluateString(S.Context); + int DiagMsgKind = -1; + // Arguments must be pointers to constant strings and cannot use '$'. + if (!ArgString.has_value()) + DiagMsgKind = 0; + else if (ArgString->find('$') != std::string::npos) + DiagMsgKind = 1; + + if (DiagMsgKind >= 0) { + S.Diag(Arg->getBeginLoc(), diag::err_builtin_verbose_trap_arg) + << DiagMsgKind << Arg->getSourceRange(); + HasError = true; + } + } + + return !HasError; +} + static bool convertArgumentToType(Sema &S, Expr *&Value, QualType Ty) { if (Value->isTypeDependent()) return false; @@ -3351,6 +3378,11 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID, case Builtin::BI__builtin_matrix_column_major_store: return BuiltinMatrixColumnMajorStore(TheCall, TheCallResult); + case Builtin::BI__builtin_verbose_trap: + if (!checkBuiltinVerboseTrap(TheCall, *this)) + return ExprError(); + break; + case Builtin::BI__builtin_get_device_side_mangled_name: { auto Check = [](CallExpr *TheCall) { if (TheCall->getNumArgs() != 1) |