mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Port swiftSyntax to Windows
This commit is contained in:
@@ -44,28 +44,43 @@ class OwnedString {
|
|||||||
size_t Length;
|
size_t Length;
|
||||||
StringOwnership Ownership;
|
StringOwnership Ownership;
|
||||||
|
|
||||||
|
OwnedString(const char* Data, size_t Length, StringOwnership Ownership)
|
||||||
|
: Length(Length), Ownership(Ownership) {
|
||||||
|
assert(Length >= 0 && "expected length to be non-negative");
|
||||||
|
|
||||||
|
if (Ownership == StringOwnership::Copied && Data) {
|
||||||
|
assert(
|
||||||
|
Length <= strlen(Data) &&
|
||||||
|
"expected length to be a valid index, within the length of the string");
|
||||||
|
|
||||||
|
char *substring = static_cast<char *>(malloc(Length + 1));
|
||||||
|
assert(substring && "expected successful malloc of copy");
|
||||||
|
|
||||||
|
memcpy(substring, Data, Length);
|
||||||
|
substring[Length] = '\0';
|
||||||
|
|
||||||
|
this->Data = substring;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
this->Data = Data;
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
OwnedString()
|
OwnedString() : OwnedString(nullptr, 0, StringOwnership::Unowned) {}
|
||||||
: Data(nullptr), Length(0), Ownership(StringOwnership::Unowned) {}
|
|
||||||
|
|
||||||
OwnedString(const char *Data, size_t Length, StringOwnership Ownership)
|
OwnedString(const char *Data, size_t Length)
|
||||||
: Data(Ownership == StringOwnership::Copied ? strndup(Data, Length) : Data),
|
: OwnedString(Data, Length, StringOwnership::Unowned) {}
|
||||||
Length(Length), Ownership(Ownership) {}
|
|
||||||
|
|
||||||
OwnedString(StringRef Str, StringOwnership Ownership)
|
OwnedString(StringRef Str) : OwnedString(Str.data(), Str.size()) {}
|
||||||
: OwnedString(Ownership == StringOwnership::Copied
|
|
||||||
? strndup(Str.data(), Str.size())
|
|
||||||
: Str.data(),
|
|
||||||
Str.size(), Ownership) {}
|
|
||||||
|
|
||||||
OwnedString(const char *Data)
|
OwnedString(const char *Data) : OwnedString(StringRef(Data)) {}
|
||||||
: OwnedString(StringRef(Data), StringOwnership::Unowned) {}
|
|
||||||
|
|
||||||
OwnedString(const OwnedString &Other)
|
OwnedString(const OwnedString &Other)
|
||||||
: Data(Other.Ownership == StringOwnership::Copied
|
: OwnedString(Other.Data, Other.Length, Other.Ownership) {}
|
||||||
? strndup(Other.Data, Other.Length)
|
|
||||||
: Other.Data),
|
OwnedString copy() {
|
||||||
Length(Other.Length), Ownership(Other.Ownership) {}
|
return OwnedString(Data, Length, StringOwnership::Copied);
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the length of the string in bytes.
|
/// Returns the length of the string in bytes.
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
@@ -96,4 +111,3 @@ public:
|
|||||||
} // end namespace swift
|
} // end namespace swift
|
||||||
|
|
||||||
#endif // SWIFT_BASIC_OWNEDSTRING_H
|
#endif // SWIFT_BASIC_OWNEDSTRING_H
|
||||||
|
|
||||||
|
|||||||
@@ -737,8 +737,7 @@ syntax::RC<syntax::TokenSyntax> Lexer::fullLex() {
|
|||||||
TrailingTrivia.push_front(syntax::TriviaPiece::backtick());
|
TrailingTrivia.push_front(syntax::TriviaPiece::backtick());
|
||||||
}
|
}
|
||||||
auto Result = syntax::TokenSyntax::make(NextToken.getKind(),
|
auto Result = syntax::TokenSyntax::make(NextToken.getKind(),
|
||||||
OwnedString(NextToken.getText(),
|
OwnedString(NextToken.getText()).copy(),
|
||||||
StringOwnership::Copied),
|
|
||||||
syntax::SourcePresence::Present,
|
syntax::SourcePresence::Present,
|
||||||
{LeadingTrivia}, {TrailingTrivia});
|
{LeadingTrivia}, {TrailingTrivia});
|
||||||
LeadingTrivia.clear();
|
LeadingTrivia.clear();
|
||||||
@@ -1951,19 +1950,19 @@ Optional<syntax::TriviaPiece> Lexer::lexWhitespace(bool StopAtFirstNewline) {
|
|||||||
return syntax::TriviaPiece {
|
return syntax::TriviaPiece {
|
||||||
syntax::TriviaKind::Newline,
|
syntax::TriviaKind::Newline,
|
||||||
Length,
|
Length,
|
||||||
OwnedString(Start, Length, StringOwnership::Unowned),
|
OwnedString(Start, Length),
|
||||||
};
|
};
|
||||||
case ' ':
|
case ' ':
|
||||||
return syntax::TriviaPiece {
|
return syntax::TriviaPiece {
|
||||||
syntax::TriviaKind::Space,
|
syntax::TriviaKind::Space,
|
||||||
Length,
|
Length,
|
||||||
OwnedString(Start, Length, StringOwnership::Unowned),
|
OwnedString(Start, Length),
|
||||||
};
|
};
|
||||||
case '\t':
|
case '\t':
|
||||||
return syntax::TriviaPiece {
|
return syntax::TriviaPiece {
|
||||||
syntax::TriviaKind::Tab,
|
syntax::TriviaKind::Tab,
|
||||||
Length,
|
Length,
|
||||||
OwnedString(Start, Length, StringOwnership::Unowned),
|
OwnedString(Start, Length),
|
||||||
};
|
};
|
||||||
default:
|
default:
|
||||||
return None;
|
return None;
|
||||||
@@ -1982,7 +1981,7 @@ Optional<syntax::TriviaPiece> Lexer::lexSingleLineComment(syntax::TriviaKind Kin
|
|||||||
return Optional<syntax::TriviaPiece>({
|
return Optional<syntax::TriviaPiece>({
|
||||||
Kind,
|
Kind,
|
||||||
Length,
|
Length,
|
||||||
OwnedString(Start, Length, StringOwnership::Unowned)
|
OwnedString(Start, Length)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1997,7 +1996,7 @@ Lexer::lexBlockComment(syntax::TriviaKind Kind) {
|
|||||||
return Optional<syntax::TriviaPiece>({
|
return Optional<syntax::TriviaPiece>({
|
||||||
Kind,
|
Kind,
|
||||||
Length,
|
Length,
|
||||||
OwnedString(Start, Length, StringOwnership::Unowned)
|
OwnedString(Start, Length)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -296,8 +296,7 @@ LegacyASTTransformer::visitStructDecl(StructDecl *D,
|
|||||||
|
|
||||||
if (D->getNameLoc().isValid()) {
|
if (D->getNameLoc().isValid()) {
|
||||||
auto Identifier = findTokenSyntax(tok::identifier,
|
auto Identifier = findTokenSyntax(tok::identifier,
|
||||||
OwnedString(D->getName().str(),
|
OwnedString(D->getName().str()),
|
||||||
StringOwnership::Unowned),
|
|
||||||
SourceMgr, D->getNameLoc(), BufferID,
|
SourceMgr, D->getNameLoc(), BufferID,
|
||||||
Tokens);
|
Tokens);
|
||||||
StructBuilder.useIdentifier(Identifier);
|
StructBuilder.useIdentifier(Identifier);
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ add_swift_unittest(SwiftBasicTests
|
|||||||
FileSystemTest.cpp
|
FileSystemTest.cpp
|
||||||
ImmutablePointerSetTest.cpp
|
ImmutablePointerSetTest.cpp
|
||||||
OptionSetTest.cpp
|
OptionSetTest.cpp
|
||||||
|
OwnedStringTest.cpp
|
||||||
PointerIntEnumTest.cpp
|
PointerIntEnumTest.cpp
|
||||||
PrefixMapTest.cpp
|
PrefixMapTest.cpp
|
||||||
RangeTest.cpp
|
RangeTest.cpp
|
||||||
|
|||||||
194
unittests/Basic/OwnedStringTest.cpp
Normal file
194
unittests/Basic/OwnedStringTest.cpp
Normal file
@@ -0,0 +1,194 @@
|
|||||||
|
//===--- OwnedStringTest.cpp ----------------------------------------------===//
|
||||||
|
//
|
||||||
|
// 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 "swift/Basic/OwnedString.h"
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
|
using namespace swift;
|
||||||
|
|
||||||
|
TEST(OwnedStringTest, char_pointer_empty) {
|
||||||
|
const char *data = "";
|
||||||
|
const size_t length = strlen(data);
|
||||||
|
OwnedString ownedString(data);
|
||||||
|
|
||||||
|
EXPECT_EQ(length, ownedString.size());
|
||||||
|
EXPECT_TRUE(ownedString.empty());
|
||||||
|
|
||||||
|
OwnedString copy = ownedString.copy();
|
||||||
|
EXPECT_EQ(length, copy.size());
|
||||||
|
EXPECT_TRUE(copy.empty());
|
||||||
|
|
||||||
|
StringRef str = copy.str();
|
||||||
|
EXPECT_EQ("", str);
|
||||||
|
EXPECT_EQ(length, str.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(OwnedStringTest, char_pointer_non_empty) {
|
||||||
|
const char *data = "string";
|
||||||
|
const size_t length = strlen(data);
|
||||||
|
OwnedString ownedString(data);
|
||||||
|
|
||||||
|
EXPECT_EQ(length, ownedString.size());
|
||||||
|
EXPECT_FALSE(ownedString.empty());
|
||||||
|
|
||||||
|
OwnedString copy = ownedString.copy();
|
||||||
|
EXPECT_EQ(length, copy.size());
|
||||||
|
EXPECT_FALSE(copy.empty());
|
||||||
|
|
||||||
|
StringRef str = copy.str();
|
||||||
|
EXPECT_EQ("string", str);
|
||||||
|
EXPECT_EQ(length, strlen(str.data()));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(OwnedStringTest, char_pointer_length_equal) {
|
||||||
|
const char *data = "string";
|
||||||
|
size_t length = strlen(data);
|
||||||
|
OwnedString ownedString(data, length);
|
||||||
|
|
||||||
|
EXPECT_EQ(length, ownedString.size());
|
||||||
|
EXPECT_FALSE(ownedString.empty());
|
||||||
|
|
||||||
|
OwnedString copy = ownedString.copy();
|
||||||
|
EXPECT_EQ(length, copy.size());
|
||||||
|
EXPECT_FALSE(copy.empty());
|
||||||
|
|
||||||
|
// Make sure we correctly copied the data and that it is null
|
||||||
|
// terminated.
|
||||||
|
StringRef str = copy.str();
|
||||||
|
EXPECT_EQ("string", str);
|
||||||
|
EXPECT_EQ(length, strlen(str.data()));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(OwnedStringTest, char_pointer_length_nonzero) {
|
||||||
|
const char *data = "string";
|
||||||
|
const size_t length = 1;
|
||||||
|
OwnedString ownedString(data, length);
|
||||||
|
|
||||||
|
EXPECT_EQ(length, ownedString.size());
|
||||||
|
EXPECT_FALSE(ownedString.empty());
|
||||||
|
|
||||||
|
OwnedString copy = ownedString.copy();
|
||||||
|
EXPECT_EQ(length, copy.size());
|
||||||
|
EXPECT_FALSE(copy.empty());
|
||||||
|
|
||||||
|
// Make sure we correctly copied the data and that it is null
|
||||||
|
// terminated.
|
||||||
|
StringRef str = copy.str();
|
||||||
|
EXPECT_EQ("s", str);
|
||||||
|
EXPECT_EQ(1, strlen(str.data()));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(OwnedStringTest, char_pointer_length_zero) {
|
||||||
|
const char *data = "string";
|
||||||
|
const size_t length = 0;
|
||||||
|
OwnedString ownedString(data, length);
|
||||||
|
|
||||||
|
EXPECT_EQ(length, ownedString.size());
|
||||||
|
EXPECT_TRUE(ownedString.empty());
|
||||||
|
|
||||||
|
OwnedString copy = ownedString.copy();
|
||||||
|
EXPECT_EQ(length, copy.size());
|
||||||
|
EXPECT_TRUE(copy.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(OwnedStringTest, copy_original_new_different) {
|
||||||
|
// Initialize a mutable string.
|
||||||
|
const char *original = "string";
|
||||||
|
const size_t length = strlen(original);
|
||||||
|
char *data = static_cast<char *>(malloc(length + 1));
|
||||||
|
memcpy(data, original, length);
|
||||||
|
data[length] = '\0';
|
||||||
|
|
||||||
|
// Create an OwnedString.
|
||||||
|
OwnedString ownedString(data, length);
|
||||||
|
|
||||||
|
EXPECT_EQ(length, ownedString.size());
|
||||||
|
EXPECT_FALSE(ownedString.empty());
|
||||||
|
|
||||||
|
// Copy the string
|
||||||
|
OwnedString copy = ownedString.copy();
|
||||||
|
EXPECT_EQ(length, copy.size());
|
||||||
|
EXPECT_FALSE(copy.empty());
|
||||||
|
|
||||||
|
// Make sure we correctly copied the data and that it is null
|
||||||
|
// terminated.
|
||||||
|
StringRef str = copy.str();
|
||||||
|
EXPECT_EQ("string", str);
|
||||||
|
EXPECT_EQ(length, strlen(str.data()));
|
||||||
|
|
||||||
|
// Make sure updating the original pointer doesn't affect the copy.
|
||||||
|
data[0] = 'a';
|
||||||
|
|
||||||
|
EXPECT_EQ("string", str);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(OwnedStringTest, copy_constructor_original_not_copy) {
|
||||||
|
// Initialize a mutable string.
|
||||||
|
const char *original = "string";
|
||||||
|
const size_t length = strlen(original);
|
||||||
|
char *data = static_cast<char *>(malloc(length + 1));
|
||||||
|
memcpy(data, original, length);
|
||||||
|
data[length] = '\0';
|
||||||
|
|
||||||
|
// Create an OwnedString.
|
||||||
|
OwnedString ownedString(data, length);
|
||||||
|
|
||||||
|
EXPECT_EQ(length, ownedString.size());
|
||||||
|
EXPECT_FALSE(ownedString.empty());
|
||||||
|
|
||||||
|
// Copy the string
|
||||||
|
OwnedString copy = OwnedString(ownedString);
|
||||||
|
EXPECT_EQ(length, copy.size());
|
||||||
|
EXPECT_FALSE(copy.empty());
|
||||||
|
|
||||||
|
// Make sure we correctly copied the data and that it is null
|
||||||
|
// terminated.
|
||||||
|
StringRef str = copy.str();
|
||||||
|
EXPECT_EQ("string", str);
|
||||||
|
EXPECT_EQ(length, strlen(str.data()));
|
||||||
|
|
||||||
|
// Make sure updating the original pointer doesn't affect the copy.
|
||||||
|
data[0] = 'a';
|
||||||
|
|
||||||
|
EXPECT_EQ("atring", str);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(OwnedStringTest, copy_constructor_original_copy) {
|
||||||
|
// Initialize a mutable string.
|
||||||
|
const char *original = "string";
|
||||||
|
const size_t length = strlen(original);
|
||||||
|
char *data = static_cast<char *>(malloc(length + 1));
|
||||||
|
memcpy(data, original, length);
|
||||||
|
data[length] = '\0';
|
||||||
|
|
||||||
|
// Create an OwnedString.
|
||||||
|
OwnedString ownedString(data, length);
|
||||||
|
|
||||||
|
EXPECT_EQ(length, ownedString.size());
|
||||||
|
EXPECT_FALSE(ownedString.empty());
|
||||||
|
|
||||||
|
// Copy the string
|
||||||
|
OwnedString copy = OwnedString(ownedString.copy());
|
||||||
|
EXPECT_EQ(length, copy.size());
|
||||||
|
EXPECT_FALSE(copy.empty());
|
||||||
|
|
||||||
|
// Make sure we correctly copied the data and that it is null
|
||||||
|
// terminated.
|
||||||
|
StringRef str = copy.str();
|
||||||
|
EXPECT_EQ("string", str);
|
||||||
|
EXPECT_EQ(length, strlen(str.data()));
|
||||||
|
|
||||||
|
// Make sure updating the original pointer doesn't affect the copy.
|
||||||
|
data[0] = 'a';
|
||||||
|
|
||||||
|
EXPECT_EQ("string", str);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user