Skip to content

[C++20] Remove u8 string literal prefixes. #67707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions include/swift/Basic/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#ifndef SWIFT_BASIC_COMPILER_H
#define SWIFT_BASIC_COMPILER_H

#include <stddef.h>

#if defined(_MSC_VER) && !defined(__clang__)
#define SWIFT_COMPILER_IS_MSVC 1
#else
Expand Down Expand Up @@ -190,4 +192,21 @@
#define ENUM_EXTENSIBILITY_ATTR(arg)
#endif

// The 'u8' string literal prefix creates `char` types on C++14/17 but
// `char8_t` types on C++20. To support compiling in both modes
// simultaneously, wrap Unicode literals in `SWIFT_UTF8("...")` to ensure
// that they are interpreted by the compiler as UTF-8 but always return
// `char` types.
#if defined(__cplusplus)
#if defined(__cpp_char8_t)
inline constexpr char operator""_swift_u8(char8_t c) { return c; }
inline const char *operator""_swift_u8(const char8_t *p, std::size_t) {
return reinterpret_cast<const char *>(p);
}
#define SWIFT_UTF8(literal) u8##literal##_swift_u8
#else // !defined(__cpp_char8_t)
#define SWIFT_UTF8(literal) u8##literal
#endif // defined(__cpp_char8_t)
#endif // defined(__cplusplus)

#endif // SWIFT_BASIC_COMPILER_H
5 changes: 3 additions & 2 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "swift/AST/SubstitutionMap.h"
#include "swift/AST/TypeLoc.h"
#include "swift/AST/TypeRepr.h"
#include "swift/Basic/Compiler.h"
#include "clang/AST/Type.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/SmallPtrSet.h"
Expand Down Expand Up @@ -2037,8 +2038,8 @@ Identifier GenericTypeParamType::getName() const {
llvm::SmallString<10> nameBuf;
llvm::raw_svector_ostream os(nameBuf);

static const char *tau = u8"\u03C4_";
static const char *tau = SWIFT_UTF8("\u03C4_");

os << tau << getDepth() << '_' << getIndex();
Identifier name = C.getIdentifier(os.str());
names.insert({depthIndex, name});
Expand Down
3 changes: 2 additions & 1 deletion lib/Basic/Unicode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

#include "swift/Basic/Unicode.h"
#include "swift/Basic/Compiler.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/ConvertUTF.h"
Expand Down Expand Up @@ -135,7 +136,7 @@ std::string swift::unicode::sanitizeUTF8(StringRef Text) {
Builder.reserve(Text.size());
const llvm::UTF8* Data = reinterpret_cast<const llvm::UTF8*>(Text.begin());
const llvm::UTF8* End = reinterpret_cast<const llvm::UTF8*>(Text.end());
StringRef Replacement = u8"\ufffd";
StringRef Replacement = SWIFT_UTF8("\ufffd");
while (Data < End) {
auto Step = llvm::getNumBytesForUTF8(*Data);
if (Data + Step > End) {
Expand Down
3 changes: 2 additions & 1 deletion lib/IRGen/IRGenDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "swift/AST/ModuleLoader.h"
#include "swift/AST/Pattern.h"
#include "swift/AST/TypeDifferenceVisitor.h"
#include "swift/Basic/Compiler.h"
#include "swift/Basic/Dwarf.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Basic/Version.h"
Expand Down Expand Up @@ -3034,7 +3035,7 @@ void IRGenDebugInfoImpl::emitTypeMetadata(IRGenFunction &IGF,
return;

llvm::SmallString<8> Buf;
static const char *Tau = u8"\u03C4";
static const char *Tau = SWIFT_UTF8("\u03C4");
llvm::raw_svector_ostream OS(Buf);
OS << '$' << Tau << '_' << Depth << '_' << Index;
uint64_t PtrWidthInBits = CI.getTargetInfo().getPointerWidth(0);
Expand Down
28 changes: 17 additions & 11 deletions unittests/IDE/FuzzyStringMatcherTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

#include "swift/IDE/FuzzyStringMatcher.h"
#include "swift/Basic/Compiler.h"
#include "gtest/gtest.h"

using FuzzyStringMatcher = swift::ide::FuzzyStringMatcher;
Expand Down Expand Up @@ -53,26 +54,31 @@ TEST(FuzzyStringMatcher, SingleCharacterMatching) {

TEST(FuzzyStringMatcher, UnicodeMatching) {
// Single code point matching.
EXPECT_TRUE(FuzzyStringMatcher(u8"\u2602a\U0002000Bz")
.matchesCandidate(u8"\u2602A\U0002000BZ"));
EXPECT_TRUE(FuzzyStringMatcher(SWIFT_UTF8("\u2602a\U0002000Bz"))
.matchesCandidate(SWIFT_UTF8("\u2602A\U0002000BZ")));

// Same-order combining marks.
EXPECT_TRUE(FuzzyStringMatcher(u8"a\u0323\u0307")
.matchesCandidate(u8"A\u0323\u0307"));
EXPECT_TRUE(FuzzyStringMatcher(SWIFT_UTF8("a\u0323\u0307"))
.matchesCandidate(SWIFT_UTF8("A\u0323\u0307")));

// FIXME: Canonical equivalence. These should be the same.
EXPECT_FALSE(FuzzyStringMatcher(u8"a\u0307\u0323")
.matchesCandidate(u8"A\u0323\u0307"));
EXPECT_FALSE(FuzzyStringMatcher(u8"a\u00C5").matchesCandidate(u8"A\u030A"));
EXPECT_FALSE(FuzzyStringMatcher(SWIFT_UTF8("a\u0307\u0323"))
.matchesCandidate(SWIFT_UTF8("A\u0323\u0307")));
EXPECT_FALSE(FuzzyStringMatcher(SWIFT_UTF8("a\u00C5"))
.matchesCandidate(SWIFT_UTF8("A\u030A")));

// FIXME: Compatibility equivalence. It would be good to make these the same
// too, since we're fuzzy matching.
EXPECT_FALSE(FuzzyStringMatcher(u8"fi").matchesCandidate(u8"\uFB01"));
EXPECT_FALSE(FuzzyStringMatcher(u8"25").matchesCandidate(u8"2\u2075"));
EXPECT_FALSE(FuzzyStringMatcher(SWIFT_UTF8("fi"))
.matchesCandidate(SWIFT_UTF8("\uFB01")));
EXPECT_FALSE(FuzzyStringMatcher(SWIFT_UTF8("25"))
.matchesCandidate(SWIFT_UTF8("2\u2075")));

// FIXME: Case-insensitivity in non-ASCII characters.
EXPECT_FALSE(FuzzyStringMatcher(u8"\u00E0").matchesCandidate(u8"\u00C0"));
EXPECT_FALSE(FuzzyStringMatcher(u8"ss").matchesCandidate(u8"\u00DF"));
EXPECT_FALSE(FuzzyStringMatcher(SWIFT_UTF8("\u00E0"))
.matchesCandidate(SWIFT_UTF8("\u00C0")));
EXPECT_FALSE(FuzzyStringMatcher(SWIFT_UTF8("ss"))
.matchesCandidate(SWIFT_UTF8("\u00DF")));
}

TEST(FuzzyStringMatcher, BasicScoring) {
Expand Down