|
| 1 | +//=== unittests/CodeGen/DemangleTrapReasonInDebugInfo.cpp -----------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "clang/CodeGen/ModuleBuilder.h" |
| 10 | +#include "llvm/ADT/StringRef.h" |
| 11 | +#include "gtest/gtest.h" |
| 12 | + |
| 13 | +using namespace clang::CodeGen; |
| 14 | + |
| 15 | +void CheckValidCommon(llvm::StringRef FuncName, const char *ExpectedCategory, |
| 16 | + const char *ExpectedMessage) { |
| 17 | + auto MaybeTrapReason = DemangleTrapReasonInDebugInfo(FuncName); |
| 18 | + ASSERT_TRUE(MaybeTrapReason.has_value()); |
| 19 | + auto [Category, Message] = MaybeTrapReason.value(); |
| 20 | + ASSERT_STREQ(Category.str().c_str(), ExpectedCategory); |
| 21 | + ASSERT_STREQ(Message.str().c_str(), ExpectedMessage); |
| 22 | +} |
| 23 | + |
| 24 | +void CheckInvalidCommon(llvm::StringRef FuncName) { |
| 25 | + auto MaybeTrapReason = DemangleTrapReasonInDebugInfo(FuncName); |
| 26 | + ASSERT_TRUE(!MaybeTrapReason.has_value()); |
| 27 | +} |
| 28 | + |
| 29 | +TEST(DemangleTrapReasonInDebugInfo, Valid) { |
| 30 | + std::string FuncName(ClangTrapPrefix); |
| 31 | + FuncName += "$trap category$trap message"; |
| 32 | + CheckValidCommon(FuncName, "trap category", "trap message"); |
| 33 | +} |
| 34 | + |
| 35 | +TEST(DemangleTrapReasonInDebugInfo, ValidEmptyCategory) { |
| 36 | + std::string FuncName(ClangTrapPrefix); |
| 37 | + FuncName += "$$trap message"; |
| 38 | + CheckValidCommon(FuncName, "", "trap message"); |
| 39 | +} |
| 40 | + |
| 41 | +TEST(DemangleTrapReasonInDebugInfo, ValidEmptyMessage) { |
| 42 | + std::string FuncName(ClangTrapPrefix); |
| 43 | + FuncName += "$trap category$"; |
| 44 | + CheckValidCommon(FuncName, "trap category", ""); |
| 45 | +} |
| 46 | + |
| 47 | +TEST(DemangleTrapReasonInDebugInfo, ValidAllEmpty) { |
| 48 | + // `__builtin_verbose_trap` actually allows this |
| 49 | + // currently. However, we should probably disallow this in Sema because having |
| 50 | + // an empty category and message completely defeats the point of using the |
| 51 | + // builtin (#165981). |
| 52 | + std::string FuncName(ClangTrapPrefix); |
| 53 | + FuncName += "$$"; |
| 54 | + CheckValidCommon(FuncName, "", ""); |
| 55 | +} |
| 56 | + |
| 57 | +TEST(DemangleTrapReasonInDebugInfo, InvalidOnlyPrefix) { |
| 58 | + std::string FuncName(ClangTrapPrefix); |
| 59 | + CheckInvalidCommon(FuncName); |
| 60 | +} |
| 61 | + |
| 62 | +TEST(DemangleTrapReasonInDebugInfo, Invalid) { |
| 63 | + std::string FuncName("foo"); |
| 64 | + CheckInvalidCommon(FuncName); |
| 65 | +} |
| 66 | + |
| 67 | +TEST(DemangleTrapReasonInDebugInfo, InvalidEmpty) { CheckInvalidCommon(""); } |
0 commit comments