This patch refactors the trap reason demangling logic in `lldb_private::VerboseTrapFrameRecognizer::RecognizeFrame` into a new public function `clang::CodeGen::DemangleTrapReasonInDebugInfo`. There are two reasons for doing this: 1. In a future patch the logic for demangling needs to be used somewhere else in LLDB and thus the logic needs refactoring to avoid duplicating code. 2. The logic for demangling shouldn't really be in LLDB anyway because it's a Clang implementation detail and thus the logic really belongs inside Clang, not LLDB. Unit tests have been added for the new function that demonstrate how to use the new API. The function names recognized by VerboseTrapFrameRecognizer are identical to before. However, this patch isn't NFC because: * The `lldbTarget` library now links against `clangCodeGen` which it didn't previously. * The LLDB logging output is a little different now. The previous code tried to log failures for an invalid regex pattern and for the `Regex::match` API not returning the correct number of matches. These failure conditions are unreachable via unit testing so they have been made assertions failures inside the `DemangleTrapReasonInDebugInfo` implementation instead of trying to log them in LLDB. rdar://163230807
68 lines
2.3 KiB
C++
68 lines
2.3 KiB
C++
//=== 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(""); }
|