-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[Clang][LLDB] Refactor trap reason demangling out of LLDB and into Clang #165996
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
delcypher
merged 1 commit into
llvm:main
from
delcypher:dliew/demangle-trap-reason-refactor
Nov 5, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| //=== unittests/CodeGen/DemangleTrapReasonInDebugInfo.cpp -----------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "clang/CodeGen/ModuleBuilder.h" | ||
| #include "llvm/ADT/StringRef.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| using namespace clang::CodeGen; | ||
|
|
||
| void CheckValidCommon(llvm::StringRef FuncName, const char *ExpectedCategory, | ||
| const char *ExpectedMessage) { | ||
| auto MaybeTrapReason = DemangleTrapReasonInDebugInfo(FuncName); | ||
| ASSERT_TRUE(MaybeTrapReason.has_value()); | ||
| auto [Category, Message] = MaybeTrapReason.value(); | ||
| ASSERT_STREQ(Category.str().c_str(), ExpectedCategory); | ||
| ASSERT_STREQ(Message.str().c_str(), ExpectedMessage); | ||
| } | ||
|
|
||
| void CheckInvalidCommon(llvm::StringRef FuncName) { | ||
| auto MaybeTrapReason = DemangleTrapReasonInDebugInfo(FuncName); | ||
| ASSERT_TRUE(!MaybeTrapReason.has_value()); | ||
| } | ||
|
|
||
| TEST(DemangleTrapReasonInDebugInfo, Valid) { | ||
| std::string FuncName(ClangTrapPrefix); | ||
| FuncName += "$trap category$trap message"; | ||
| CheckValidCommon(FuncName, "trap category", "trap message"); | ||
| } | ||
|
|
||
| TEST(DemangleTrapReasonInDebugInfo, ValidEmptyCategory) { | ||
| std::string FuncName(ClangTrapPrefix); | ||
| FuncName += "$$trap message"; | ||
| CheckValidCommon(FuncName, "", "trap message"); | ||
| } | ||
|
|
||
| TEST(DemangleTrapReasonInDebugInfo, ValidEmptyMessage) { | ||
| std::string FuncName(ClangTrapPrefix); | ||
| FuncName += "$trap category$"; | ||
| CheckValidCommon(FuncName, "trap category", ""); | ||
| } | ||
|
|
||
| TEST(DemangleTrapReasonInDebugInfo, ValidAllEmpty) { | ||
| // `__builtin_verbose_trap` actually allows this | ||
| // currently. However, we should probably disallow this in Sema because having | ||
| // an empty category and message completely defeats the point of using the | ||
| // builtin (#165981). | ||
| std::string FuncName(ClangTrapPrefix); | ||
| FuncName += "$$"; | ||
| CheckValidCommon(FuncName, "", ""); | ||
| } | ||
|
|
||
| TEST(DemangleTrapReasonInDebugInfo, InvalidOnlyPrefix) { | ||
| std::string FuncName(ClangTrapPrefix); | ||
| CheckInvalidCommon(FuncName); | ||
| } | ||
|
|
||
| TEST(DemangleTrapReasonInDebugInfo, Invalid) { | ||
| std::string FuncName("foo"); | ||
| CheckInvalidCommon(FuncName); | ||
| } | ||
|
|
||
| TEST(DemangleTrapReasonInDebugInfo, InvalidEmpty) { CheckInvalidCommon(""); } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,33 +95,14 @@ VerboseTrapFrameRecognizer::RecognizeFrame(lldb::StackFrameSP frame_sp) { | |
| if (func_name.empty()) | ||
| return {}; | ||
|
|
||
| static auto trap_regex = | ||
| llvm::Regex(llvm::formatv("^{0}\\$(.*)\\$(.*)$", ClangTrapPrefix).str()); | ||
| SmallVector<llvm::StringRef, 3> matches; | ||
| std::string regex_err_msg; | ||
| if (!trap_regex.match(func_name, &matches, ®ex_err_msg)) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM Looks like this error handling was bogus |
||
| LLDB_LOGF(GetLog(LLDBLog::Unwind), | ||
| "Failed to parse match trap regex for '%s': %s", func_name.data(), | ||
| regex_err_msg.c_str()); | ||
|
|
||
| return {}; | ||
| } | ||
|
|
||
| // For `__clang_trap_msg$category$message$` we expect 3 matches: | ||
| // 1. entire string | ||
| // 2. category | ||
| // 3. message | ||
| if (matches.size() != 3) { | ||
| LLDB_LOGF(GetLog(LLDBLog::Unwind), | ||
| "Unexpected function name format. Expected '<trap prefix>$<trap " | ||
| "category>$<trap message>'$ but got: '%s'.", | ||
| func_name.data()); | ||
|
|
||
| auto maybe_trap_reason = | ||
| clang::CodeGen::DemangleTrapReasonInDebugInfo(func_name); | ||
| if (!maybe_trap_reason.has_value()) { | ||
| LLDB_LOGF(GetLog(LLDBLog::Unwind), "Failed to demangle '%s' as trap reason", | ||
| func_name.str().c_str()); | ||
| return {}; | ||
| } | ||
|
|
||
| auto category = matches[1]; | ||
| auto message = matches[2]; | ||
| auto [category, message] = maybe_trap_reason.value(); | ||
|
|
||
| std::string stop_reason = | ||
| category.empty() ? "<empty category>" : category.str(); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moving this into Clang makes sense to me, thanks!