Skip to content

Commit b8a8799

Browse files
committed
[Demangler] Tidy up a bit, and add line numbers to ManglingErrors.
Because DEMANGLER_ASSERT() might cause the remanglers to return a ManglingError with the code ManglingError::AssertionFailed, it's useful to have a line number in the ManglingError as well as the other information. This is also potentially helpful for other cases where the code is used multiple times in the remanglers. rdar://79725187
1 parent 227b438 commit b8a8799

File tree

7 files changed

+203
-177
lines changed

7 files changed

+203
-177
lines changed

include/swift/Demangling/Demangle.h

+9-6
Original file line numberDiff line numberDiff line change
@@ -542,14 +542,17 @@ struct SWIFT_NODISCARD ManglingError {
542542

543543
Code code;
544544
NodePointer node;
545+
unsigned line;
545546

546547
ManglingError() : code(Uninitialized), node(nullptr) {}
547-
ManglingError(Code c) : code(c), node(nullptr) {}
548-
ManglingError(Code c, NodePointer n) : code(c), node(n) {}
548+
ManglingError(Code c) : code(c), node(nullptr), line(0) {}
549+
ManglingError(Code c, NodePointer n, unsigned l) : code(c), node(n), line(l) {}
549550

550551
bool isSuccess() const { return code == Success; }
551552
};
552553

554+
#define MANGLING_ERROR(c,n) ManglingError((c), (n), __LINE__)
555+
553556
/// Used as a return type for mangling functions that may fail
554557
template <typename T>
555558
class SWIFT_NODISCARD ManglingErrorOr {
@@ -559,17 +562,17 @@ class SWIFT_NODISCARD ManglingErrorOr {
559562

560563
public:
561564
ManglingErrorOr() : err_() {}
562-
ManglingErrorOr(ManglingError::Code code, NodePointer node = nullptr)
563-
: err_(code, node) {}
565+
ManglingErrorOr(ManglingError::Code code,
566+
NodePointer node = nullptr,
567+
unsigned line = 0)
568+
: err_(code, node, line) {}
564569
ManglingErrorOr(const ManglingError &err) : err_(err) {}
565570
ManglingErrorOr(const T &t) : err_(ManglingError::Success), value_(t) {}
566571
ManglingErrorOr(T &&t) : err_(ManglingError::Success), value_(std::move(t)) {}
567572

568573
bool isSuccess() const { return err_.code == ManglingError::Success; }
569574

570575
const ManglingError &error() const { return err_; }
571-
ManglingError::Code errorCode() const { return err_.code; }
572-
NodePointer errorNode() const { return err_.node; }
573576

574577
const T &result() const {
575578
assert(isSuccess());

include/swift/Demangling/TypeDecoder.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -611,8 +611,9 @@ class TypeDecoder {
611611
auto mangling = Demangle::mangleNode(Node);
612612
if (!mangling.isSuccess()) {
613613
return MAKE_NODE_TYPE_ERROR(Node,
614-
"failed to mangle node (%d)",
615-
mangling.errorCode());
614+
"failed to mangle node (%d:%u)",
615+
mangling.error().code,
616+
mangling.error().line);
616617
}
617618
return Builder.createBuiltinType(Node->getText().str(), mangling.result());
618619
}

lib/Demangling/DemanglerAssert.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
#define SWIFT_DEMANGLING_ASSERT_H
2020

2121
#if SWIFT_RUNTIME
22-
#define DEMANGLER_ASSERT(expr, node) \
23-
do { \
24-
if (!(expr)) \
25-
return ManglingError(ManglingError::AssertionFailed, node); \
26-
} while (0)
22+
#define DEMANGLER_ASSERT(expr, node) \
23+
do { \
24+
if (!(expr)) \
25+
return ManglingError(ManglingError::AssertionFailed, (node), __LINE__); \
26+
} while (0)
2727
#else
28-
#define DEMANGLER_ASSERT(expr, node) assert(expr)
28+
#define DEMANGLER_ASSERT(expr, node) assert(expr)
2929
#endif
3030

3131
#endif // SWIFT_DEMANGLING_ASSERT_H

0 commit comments

Comments
 (0)