Skip to content

Commit 58a88c1

Browse files
authored
Merge pull request #34608 from mikeash/fix-typelookuperror-constant-string
2 parents e1df94a + b9bed06 commit 58a88c1

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

include/swift/Demangling/TypeLookupError.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "swift/Basic/TaggedUnion.h"
2222
#include "swift/Runtime/Portability.h"
23+
#include <string.h>
2324

2425
namespace swift {
2526

@@ -115,8 +116,14 @@ class TypeLookupError {
115116
}
116117

117118
/// Construct a TypeLookupError that just returns a constant C string.
118-
TypeLookupError(const char *str)
119-
: TypeLookupError([=] { return const_cast<char *>(str); }) {}
119+
TypeLookupError(const char *str) {
120+
Context = const_cast<char *>(str);
121+
Fn = [](void *context, Command command, void *param) -> void * {
122+
// The context pointer is the string and works for both copying the string
123+
// and copying the context. Other commands don't need to do anything.
124+
return context;
125+
};
126+
}
120127

121128
/// Construct a TypeLookupError that creates a string using asprintf. The passed-in
122129
/// format string and arguments are passed directly to swift_asprintf when

unittests/Basic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ add_swift_unittest(SwiftBasicTests
3232
ThreadSafeRefCntPointerTest.cpp
3333
TransformRangeTest.cpp
3434
TreeScopedHashTableTest.cpp
35+
TypeLookupError.cpp
3536
UnicodeTest.cpp
3637
ValueEnumeratorTest.cpp
3738
${generated_tests}

unittests/Basic/TypeLookupError.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//===--- TypeLookupError.cpp - TypeLookupError Tests ----------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "swift/Demangling/TypeLookupError.h"
14+
#include "gtest/gtest.h"
15+
#include <vector>
16+
17+
using namespace swift;
18+
19+
TEST(TypeLookupError, ConstantString) {
20+
auto error = TypeLookupError("testing testing");
21+
char *str = error.copyErrorString();
22+
ASSERT_STREQ(str, "testing testing");
23+
error.freeErrorString(str);
24+
}
25+
26+
TEST(TypeLookupError, FormatString) {
27+
auto error = TypeLookupError("%d %d %d %d %d %d %d %d %d %d", 0, 1, 2, 3, 4,
28+
5, 6, 7, 8, 9, 10);
29+
char *str = error.copyErrorString();
30+
ASSERT_STREQ(str, "0 1 2 3 4 5 6 7 8 9");
31+
error.freeErrorString(str);
32+
}
33+
34+
TEST(TypeLookupError, Copying) {
35+
std::vector<TypeLookupError> vec;
36+
37+
{
38+
auto originalError = TypeLookupError("%d %d %d %d %d %d %d %d %d %d", 0, 1,
39+
2, 3, 4, 5, 6, 7, 8, 9, 10);
40+
for (int i = 0; i < 5; i++)
41+
vec.push_back(originalError);
42+
}
43+
44+
for (auto &error : vec) {
45+
char *str = error.copyErrorString();
46+
ASSERT_STREQ(str, "0 1 2 3 4 5 6 7 8 9");
47+
error.freeErrorString(str);
48+
}
49+
50+
auto extractedError = vec[4];
51+
vec.clear();
52+
char *str = extractedError.copyErrorString();
53+
ASSERT_STREQ(str, "0 1 2 3 4 5 6 7 8 9");
54+
extractedError.freeErrorString(str);
55+
}

0 commit comments

Comments
 (0)