Files
swift-mirror/unittests/SwiftDemangle/DemangleTest.cpp
Erik Eckstein 789646a15b Demangling: Make demangled names more readable and further reduce the size of the simplified demangled names
The goal here is to make the short demangling as short and readable as possible, also at the cost of omitting some information.
The assumption is that whenever the short demangling is displayed, there is a way for the user to also get the full demangled name if needed.

*) omit <where ...> because it does not give useful information anyway

Deserializer.deserialize<A where ...> () throws -> [A]
--> Deserializer.deserialize<A> () throws -> [A]

*) for multiple specialized functions only emit a single “specialized”

specialized specialized Constructible.create(A.Element) -> Constructible<A>
--> specialized Constructible.create(A.Element) -> Constructible<A>

*) Don’t print function argument types:

foo(Int, Double, named: Int)
--> foo(_:_:named:)

This is a trade-off, because it can lead to ambiguity if there are overloads with different types.

*) make contexts of closures, local functions, etc. more readable by using “<a> in <b>” syntax
This is also done for the full and not only for the simplified demangling.

Renderer.(renderInlines([Inline]) -> String).(closure #1)
--> closure #1 in Renderer.renderInlines

*) change spacing, so that it matches our coding style:

foo <A> (x : A)
--> foo<A>(x: A)
2017-04-13 08:43:28 -07:00

63 lines
2.3 KiB
C++

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include <stdlib.h>
#include "swift/SwiftDemangle/SwiftDemangle.h"
#include "gtest/gtest.h"
TEST(FunctionNameDemangleTests, CorrectlyDemangles) {
char OutputBuffer[128];
const char *FunctionName = "_TFC3foo3bar3basfT3zimCS_3zim_T_";
const char *DemangledName = "foo.bar.bas(zim: foo.zim) -> ()";
size_t Result = swift_demangle_getDemangledName(FunctionName, OutputBuffer,
sizeof(OutputBuffer));
EXPECT_STREQ(DemangledName, OutputBuffer);
EXPECT_EQ(Result, strlen(DemangledName));
// Make sure the SynthesizeSugarOnTypes option is functioning.
const char *FunctionNameWithSugar = "_TF4main3fooFT3argGSqGSaSi___T_";
const char *DemangledNameWithSugar = "main.foo(arg: [Swift.Int]?) -> ()";
Result = swift_demangle_getDemangledName(FunctionNameWithSugar, OutputBuffer,
sizeof(OutputBuffer));
EXPECT_STREQ(DemangledNameWithSugar, OutputBuffer);
EXPECT_EQ(Result, strlen(DemangledNameWithSugar));
}
TEST(FunctionNameDemangledTests, WorksWithNULLBuffer) {
const char *FunctionName = "_TFC3foo3bar3basfT3zimCS_3zim_T_";
const char *DemangledName = "foo.bar.bas(zim: foo.zim) -> ()";
// When given a null buffer, swift_demangle_getDemangledName should still be
// able to return the size of the demangled string.
size_t Result = swift_demangle_getDemangledName(FunctionName, nullptr, 0);
EXPECT_EQ(Result, strlen(DemangledName));
}
TEST(FunctionNameDemangleTests, IgnoresNonMangledInputs) {
const char *FunctionName = "printf";
char OutputBuffer[] = "0123456789abcdef";
size_t Result = swift_demangle_getDemangledName(FunctionName, OutputBuffer,
sizeof(OutputBuffer));
EXPECT_EQ(0U, Result);
EXPECT_STREQ("0123456789abcdef", OutputBuffer);
}