Skip to content

Commit 59cfe35

Browse files
committed
[Demangling] Improve DEMANGLE_ASSERT, enable some disabled tests.
Defining DEMANGLE_ASSERT as assert() is simple but messes up the file/line information from assert(). Make a dedicated assertion failure function and use that instead to fix. Also enable some tests that were disabled because they triggered abort() calls in the remanglers. rdar://79725187
1 parent 22f8e13 commit 59cfe35

File tree

3 files changed

+51
-14
lines changed

3 files changed

+51
-14
lines changed

lib/Demangling/Demangler.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
#include "swift/Demangling/Demangler.h"
18-
#include "swift/Demangling/ManglingUtils.h"
18+
#include "DemanglerAssert.h"
1919
#include "swift/Demangling/ManglingMacros.h"
20+
#include "swift/Demangling/ManglingUtils.h"
2021
#include "swift/Demangling/Punycode.h"
2122
#include "swift/Strings.h"
23+
#include <stdio.h>
24+
#include <stdlib.h>
2225

2326
using namespace swift;
2427
using namespace Mangle;
@@ -87,6 +90,14 @@ static bool isRequirement(Node::Kind kind) {
8790
// Public utility functions //
8891
//////////////////////////////////
8992

93+
SWIFT_NORETURN void swift::Demangle::failAssert(const char *file, unsigned line,
94+
NodePointer node,
95+
const char *expr) {
96+
fprintf(stderr, "%s:%u: assertion failed for Node %p: %s", file, line, node,
97+
expr);
98+
abort();
99+
}
100+
90101
bool swift::Demangle::isContext(Node::Kind kind) {
91102
switch (kind) {
92103
#define NODE(ID)

lib/Demangling/DemanglerAssert.h

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- DemanglerAssert.h - Assertions for de/re-mangling ----------------===//
1+
//===--- DemanglerAssert.h - Assertions for de/re-mangling ------*- C++ -*-===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
@@ -18,14 +18,43 @@
1818
#ifndef SWIFT_DEMANGLING_ASSERT_H
1919
#define SWIFT_DEMANGLING_ASSERT_H
2020

21+
#include "swift/Demangling/Demangle.h"
22+
#include "swift/Demangling/NamespaceMacros.h"
23+
2124
#if SWIFT_RUNTIME
25+
26+
// In the runtime, DEMANGLER_ASSERT() returns an error
2227
#define DEMANGLER_ASSERT(expr, node) \
2328
do { \
2429
if (!(expr)) \
2530
return ManglingError(ManglingError::AssertionFailed, (node), __LINE__); \
2631
} while (0)
32+
33+
#elif !defined(NDEBUG)
34+
35+
// If NDEBUG is not defined, DEMANGLER_ASSERT() works like assert()
36+
#define DEMANGLER_ASSERT(expr, node) \
37+
do { \
38+
if (!(expr)) \
39+
swift::Demangle::failAssert(__FILE__, __LINE__, node, #expr); \
40+
} while (0)
41+
2742
#else
28-
#define DEMANGLER_ASSERT(expr, node) assert(expr)
29-
#endif
43+
44+
// Otherwise, DEMANGLER_ASSERT() does nothing
45+
#define DEMANGLER_ASSERT(expr, node)
46+
47+
#endif // SWIFT_RUNTIME
48+
49+
namespace swift {
50+
namespace Demangle {
51+
SWIFT_BEGIN_INLINE_NAMESPACE
52+
53+
SWIFT_NORETURN void failAssert(const char *file, unsigned line,
54+
NodePointer node, const char *expr);
55+
56+
SWIFT_END_INLINE_NAMESPACE
57+
} // end namespace Demangle
58+
} // end namespace swift
3059

3160
#endif // SWIFT_DEMANGLING_ASSERT_H

test/Demangle/Inputs/objc-getclass.txt

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
# These were found by fuzzing getObjCClassByMangledName
22

33
# rdar://63485806
4-
# This results in an abort(), whereas it should be an error; rdar://79725187
5-
# covers improving error handling; until that's done, disable this test case
6-
7-
# 3…KySSyGSkySySSGiG3(KˇˇˇˇˇˇˇˇˇˇˇˇˇˇCwKySSiKySS
8-
# SSmSySyySGGSGyGSyySyySySSGGSGyS78iSLccSGSyySSySSGGccLcV1yVS~^§!zzzzzzzzzzzzhzzzzzSLzSEzzzzzzzzzzzzzzzzzxxxxx8K_S0ttnIx4_
9-
# ˇyySySyySySyGnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnSgZ1laSgSg
10-
# SSx3…KySyySGSSG_S2ItLHPˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇMd7KLPlKSSFTc•OSgS
11-
# 3…KySSiGSeySySSGiGSiySySSGiG3(KˇˇˇˇˇˇˇˇˇˇˇˇˇˇCwKySSiKySS
12-
# 3…KySSiGSyySySSGiG3(KˇˇˇˇˇˇˇˇˇˇˇˇˇˇCwKySSiKySS
13-
# SyySyySSySyyGSyySyyGGSGyGSyySySyySySSGGSGGˇˇˇˇˇS4S_SmˇˇAGmmmmmmmmmtLHPL(LHPTVdLHV
4+
3…KySSyGSkySySSGiG3(KˇˇˇˇˇˇˇˇˇˇˇˇˇˇCwKySSiKySS
5+
SSmSySyySGGSGyGSyySyySySSGGSGyS78iSLccSGSyySSySSGGccLcV1yVS~^§!zzzzzzzzzzzzhzzzzzSLzSEzzzzzzzzzzzzzzzzzxxxxx8K_S0ttnIx4_
6+
ˇyySySyySySyGnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnSgZ1laSgSg
7+
SSx3…KySyySGSSG_S2ItLHPˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇMd7KLPlKSSFTc•OSgS
8+
3…KySSiGSeySySSGiGSiySySSGiG3(KˇˇˇˇˇˇˇˇˇˇˇˇˇˇCwKySSiKySS
9+
3…KySSiGSyySySSGiG3(KˇˇˇˇˇˇˇˇˇˇˇˇˇˇCwKySSiKySS
10+
SyySyySSySyyGSyySyyGGSGyGSyySySyySySSGGSGGˇˇˇˇˇS4S_SmˇˇAGmmmmmmmmmtLHPL(LHPTVdLHV
1411

1512
# rdar://63488139
1613
1_SxSt_S4KSgS9OSgRSLAPALÂ

0 commit comments

Comments
 (0)