From 3b9d1af2e3a1b031554e3f3b9d0eda4aa55d8b15 Mon Sep 17 00:00:00 2001 From: PJ Reiniger Date: Sat, 7 May 2022 22:09:18 -0400 Subject: [PATCH 02/31] Remove StringRef, ArrayRef, and Optional --- llvm/include/llvm/ADT/SmallSet.h | 12 +-- llvm/include/llvm/ADT/SmallString.h | 76 +++++++++---------- llvm/include/llvm/ADT/StringMap.h | 34 ++++----- llvm/include/llvm/ADT/StringMapEntry.h | 23 +++--- llvm/include/llvm/Support/Chrono.h | 10 +-- llvm/include/llvm/Support/Compiler.h | 2 +- llvm/include/llvm/Support/ConvertUTF.h | 25 +++--- llvm/include/llvm/Support/ErrorHandling.h | 7 +- .../llvm/Support/SmallVectorMemoryBuffer.h | 6 +- llvm/include/llvm/Support/VersionTuple.h | 20 ++--- .../llvm/Support/Windows/WindowsSupport.h | 2 - llvm/include/llvm/Support/raw_ostream.h | 48 +++++++----- llvm/lib/Support/ConvertUTFWrapper.cpp | 32 ++++---- llvm/lib/Support/ErrorHandling.cpp | 15 ++-- llvm/lib/Support/StringMap.cpp | 12 +-- llvm/lib/Support/raw_ostream.cpp | 22 +++--- llvm/unittests/ADT/DenseMapTest.cpp | 25 ------ llvm/unittests/ADT/FunctionExtrasTest.cpp | 12 +-- llvm/unittests/ADT/HashingTest.cpp | 2 +- llvm/unittests/ADT/SmallStringTest.cpp | 50 ++++++------ llvm/unittests/ADT/SmallVectorTest.cpp | 20 +---- llvm/unittests/ADT/StringMapTest.cpp | 27 ++++--- llvm/unittests/Support/ConvertUTFTest.cpp | 37 +++++---- 23 files changed, 234 insertions(+), 285 deletions(-) diff --git a/llvm/include/llvm/ADT/SmallSet.h b/llvm/include/llvm/ADT/SmallSet.h index 0600e528ee69..e4c209c5f2a9 100644 --- a/llvm/include/llvm/ADT/SmallSet.h +++ b/llvm/include/llvm/ADT/SmallSet.h @@ -13,7 +13,6 @@ #ifndef LLVM_ADT_SMALLSET_H #define LLVM_ADT_SMALLSET_H -#include "llvm/ADT/None.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/iterator.h" @@ -21,6 +20,7 @@ #include "llvm/Support/type_traits.h" #include #include +#include #include #include #include @@ -177,16 +177,16 @@ public: /// concept. // FIXME: Add iterators that abstract over the small and large form, and then // return those here. - std::pair insert(const T &V) { + std::pair insert(const T &V) { if (!isSmall()) - return std::make_pair(None, Set.insert(V).second); + return std::make_pair(std::nullopt, Set.insert(V).second); VIterator I = vfind(V); if (I != Vector.end()) // Don't reinsert if it already exists. - return std::make_pair(None, false); + return std::make_pair(std::nullopt, false); if (Vector.size() < N) { Vector.push_back(V); - return std::make_pair(None, true); + return std::make_pair(std::nullopt, true); } // Otherwise, grow from vector to set. @@ -195,7 +195,7 @@ public: Vector.pop_back(); } Set.insert(V); - return std::make_pair(None, true); + return std::make_pair(std::nullopt, true); } template diff --git a/llvm/include/llvm/ADT/SmallString.h b/llvm/include/llvm/ADT/SmallString.h index 56b0639b98cc..8112741dd01d 100644 --- a/llvm/include/llvm/ADT/SmallString.h +++ b/llvm/include/llvm/ADT/SmallString.h @@ -14,8 +14,8 @@ #define LLVM_ADT_SMALLSTRING_H #include "llvm/ADT/SmallVector.h" -#include "llvm/ADT/StringRef.h" #include +#include namespace llvm { @@ -27,11 +27,11 @@ public: /// Default ctor - Initialize to empty. SmallString() = default; - /// Initialize from a StringRef. - SmallString(StringRef S) : SmallVector(S.begin(), S.end()) {} + /// Initialize from a std::string_view. + SmallString(std::string_view S) : SmallVector(S.begin(), S.end()) {} - /// Initialize by concatenating a list of StringRefs. - SmallString(std::initializer_list Refs) + /// Initialize by concatenating a list of std::string_views. + SmallString(std::initializer_list Refs) : SmallVector() { this->append(Refs); } @@ -46,13 +46,13 @@ public: using SmallVector::assign; - /// Assign from a StringRef. - void assign(StringRef RHS) { + /// Assign from a std::string_view. + void assign(std::string_view RHS) { SmallVectorImpl::assign(RHS.begin(), RHS.end()); } - /// Assign from a list of StringRefs. - void assign(std::initializer_list Refs) { + /// Assign from a list of std::string_views. + void assign(std::initializer_list Refs) { this->clear(); append(Refs); } @@ -63,19 +63,19 @@ public: using SmallVector::append; - /// Append from a StringRef. - void append(StringRef RHS) { + /// Append from a std::string_view. + void append(std::string_view RHS) { SmallVectorImpl::append(RHS.begin(), RHS.end()); } - /// Append from a list of StringRefs. - void append(std::initializer_list Refs) { + /// Append from a list of std::string_views. + void append(std::initializer_list Refs) { size_t SizeNeeded = this->size(); - for (const StringRef &Ref : Refs) + for (const std::string_view &Ref : Refs) SizeNeeded += Ref.size(); this->reserve(SizeNeeded); auto CurEnd = this->end(); - for (const StringRef &Ref : Refs) { + for (const std::string_view &Ref : Refs) { this->uninitialized_copy(Ref.begin(), Ref.end(), CurEnd); CurEnd += Ref.size(); } @@ -88,29 +88,29 @@ public: /// Check for string equality. This is more efficient than compare() when /// the relative ordering of inequal strings isn't needed. - bool equals(StringRef RHS) const { + bool equals(std::string_view RHS) const { return str().equals(RHS); } /// Check for string equality, ignoring case. - bool equals_insensitive(StringRef RHS) const { + bool equals_insensitive(std::string_view RHS) const { return str().equals_insensitive(RHS); } /// Compare two strings; the result is -1, 0, or 1 if this string is /// lexicographically less than, equal to, or greater than the \p RHS. - int compare(StringRef RHS) const { + int compare(std::string_view RHS) const { return str().compare(RHS); } /// compare_insensitive - Compare two strings, ignoring case. - int compare_insensitive(StringRef RHS) const { + int compare_insensitive(std::string_view RHS) const { return str().compare_insensitive(RHS); } /// compare_numeric - Compare two strings, treating sequences of digits as /// numbers. - int compare_numeric(StringRef RHS) const { + int compare_numeric(std::string_view RHS) const { return str().compare_numeric(RHS); } @@ -119,12 +119,12 @@ public: /// @{ /// startswith - Check if this string starts with the given \p Prefix. - bool startswith(StringRef Prefix) const { + bool startswith(std::string_view Prefix) const { return str().startswith(Prefix); } /// endswith - Check if this string ends with the given \p Suffix. - bool endswith(StringRef Suffix) const { + bool endswith(std::string_view Suffix) const { return str().endswith(Suffix); } @@ -144,7 +144,7 @@ public: /// /// \returns The index of the first occurrence of \p Str, or npos if not /// found. - size_t find(StringRef Str, size_t From = 0) const { + size_t find(std::string_view Str, size_t From = 0) const { return str().find(Str, From); } @@ -152,7 +152,7 @@ public: /// /// \returns The index of the last occurrence of \p C, or npos if not /// found. - size_t rfind(char C, size_t From = StringRef::npos) const { + size_t rfind(char C, size_t From = std::string_view::npos) const { return str().rfind(C, From); } @@ -160,7 +160,7 @@ public: /// /// \returns The index of the last occurrence of \p Str, or npos if not /// found. - size_t rfind(StringRef Str) const { + size_t rfind(std::string_view Str) const { return str().rfind(Str); } @@ -174,7 +174,7 @@ public: /// not found. /// /// Complexity: O(size() + Chars.size()) - size_t find_first_of(StringRef Chars, size_t From = 0) const { + size_t find_first_of(std::string_view Chars, size_t From = 0) const { return str().find_first_of(Chars, From); } @@ -188,13 +188,13 @@ public: /// \p Chars, or npos if not found. /// /// Complexity: O(size() + Chars.size()) - size_t find_first_not_of(StringRef Chars, size_t From = 0) const { + size_t find_first_not_of(std::string_view Chars, size_t From = 0) const { return str().find_first_not_of(Chars, From); } /// Find the last character in the string that is \p C, or npos if not /// found. - size_t find_last_of(char C, size_t From = StringRef::npos) const { + size_t find_last_of(char C, size_t From = std::string_view::npos) const { return str().find_last_of(C, From); } @@ -203,7 +203,7 @@ public: /// /// Complexity: O(size() + Chars.size()) size_t find_last_of( - StringRef Chars, size_t From = StringRef::npos) const { + std::string_view Chars, size_t From = std::string_view::npos) const { return str().find_last_of(Chars, From); } @@ -218,7 +218,7 @@ public: /// Return the number of non-overlapped occurrences of \p Str in the /// string. - size_t count(StringRef Str) const { + size_t count(std::string_view Str) const { return str().count(Str); } @@ -235,7 +235,7 @@ public: /// \param N The number of characters to included in the substring. If \p N /// exceeds the number of characters remaining in the string, the string /// suffix (starting with \p Start) will be returned. - StringRef substr(size_t Start, size_t N = StringRef::npos) const { + std::string_view substr(size_t Start, size_t N = std::string_view::npos) const { return str().substr(Start, N); } @@ -249,14 +249,14 @@ public: /// substring. If this is npos, or less than \p Start, or exceeds the /// number of characters remaining in the string, the string suffix /// (starting with \p Start) will be returned. - StringRef slice(size_t Start, size_t End) const { + std::string_view slice(size_t Start, size_t End) const { return str().slice(Start, End); } // Extra methods. - /// Explicit conversion to StringRef. - StringRef str() const { return StringRef(this->data(), this->size()); } + /// Explicit conversion to std::string_view. + std::string_view str() const { return std::string_view(this->begin(), this->size()); } // TODO: Make this const, if it's safe... const char* c_str() { @@ -265,20 +265,20 @@ public: return this->data(); } - /// Implicit conversion to StringRef. - operator StringRef() const { return str(); } + /// Implicit conversion to std::string_view. + operator std::string_view() const { return str(); } explicit operator std::string() const { return std::string(this->data(), this->size()); } // Extra operators. - SmallString &operator=(StringRef RHS) { + SmallString &operator=(std::string_view RHS) { this->assign(RHS); return *this; } - SmallString &operator+=(StringRef RHS) { + SmallString &operator+=(std::string_view RHS) { this->append(RHS.begin(), RHS.end()); return *this; } diff --git a/llvm/include/llvm/ADT/StringMap.h b/llvm/include/llvm/ADT/StringMap.h index a82afc9a817c..5f463cfef943 100644 --- a/llvm/include/llvm/ADT/StringMap.h +++ b/llvm/include/llvm/ADT/StringMap.h @@ -58,12 +58,12 @@ protected: /// specified bucket will be non-null. Otherwise, it will be null. In either /// case, the FullHashValue field of the bucket will be set to the hash value /// of the string. - unsigned LookupBucketFor(StringRef Key); + unsigned LookupBucketFor(std::string_view Key); /// FindKey - Look up the bucket that contains the specified key. If it exists /// in the map, return the bucket number of the key. Otherwise return -1. /// This does not modify the map. - int FindKey(StringRef Key) const; + int FindKey(std::string_view Key) const; /// RemoveKey - Remove the specified StringMapEntry from the table, but do not /// delete it. This aborts if the value isn't in the table. @@ -71,7 +71,7 @@ protected: /// RemoveKey - Remove the StringMapEntry for the specified key from the /// table, returning it. If the key is not in the table, this returns null. - StringMapEntryBase *RemoveKey(StringRef Key); + StringMapEntryBase *RemoveKey(std::string_view Key); /// Allocate the table with the specified number of buckets and otherwise /// setup the map as empty. @@ -124,7 +124,7 @@ public: : StringMapImpl(InitialSize, static_cast(sizeof(MapEntryTy))), Allocator(A) {} - StringMap(std::initializer_list> List) + StringMap(std::initializer_list> List) : StringMapImpl(List.size(), static_cast(sizeof(MapEntryTy))) { for (const auto &P : List) { insert(P); @@ -215,14 +215,14 @@ public: StringMapKeyIterator(end())); } - iterator find(StringRef Key) { + iterator find(std::string_view Key) { int Bucket = FindKey(Key); if (Bucket == -1) return end(); return iterator(TheTable + Bucket, true); } - const_iterator find(StringRef Key) const { + const_iterator find(std::string_view Key) const { int Bucket = FindKey(Key); if (Bucket == -1) return end(); @@ -231,7 +231,7 @@ public: /// lookup - Return the entry for the specified key, or a default /// constructed value if no such entry exists. - ValueTy lookup(StringRef Key) const { + ValueTy lookup(std::string_view Key) const { const_iterator it = find(Key); if (it != end()) return it->second; @@ -240,10 +240,10 @@ public: /// Lookup the ValueTy for the \p Key, or create a default constructed value /// if the key is not in the map. - ValueTy &operator[](StringRef Key) { return try_emplace(Key).first->second; } + ValueTy &operator[](std::string_view Key) { return try_emplace(Key).first->second; } /// count - Return 1 if the element is in the map, 0 otherwise. - size_type count(StringRef Key) const { return find(Key) == end() ? 0 : 1; } + size_type count(std::string_view Key) const { return find(Key) == end() ? 0 : 1; } template size_type count(const StringMapEntry &MapEntry) const { @@ -293,14 +293,14 @@ public: /// isn't already in the map. The bool component of the returned pair is true /// if and only if the insertion takes place, and the iterator component of /// the pair points to the element with key equivalent to the key of the pair. - std::pair insert(std::pair KV) { + std::pair insert(std::pair KV) { return try_emplace(KV.first, std::move(KV.second)); } /// Inserts an element or assigns to the current element if the key already /// exists. The return type is the same as try_emplace. template - std::pair insert_or_assign(StringRef Key, V &&Val) { + std::pair insert_or_assign(std::string_view Key, V &&Val) { auto Ret = try_emplace(Key, std::forward(Val)); if (!Ret.second) Ret.first->second = std::forward(Val); @@ -312,7 +312,7 @@ public: /// if and only if the insertion takes place, and the iterator component of /// the pair points to the element with key equivalent to the key of the pair. template - std::pair try_emplace(StringRef Key, ArgsTy &&... Args) { + std::pair try_emplace(std::string_view Key, ArgsTy &&... Args) { unsigned BucketNo = LookupBucketFor(Key); StringMapEntryBase *&Bucket = TheTable[BucketNo]; if (Bucket && Bucket != getTombstoneVal()) @@ -358,7 +358,7 @@ public: V.Destroy(Allocator); } - bool erase(StringRef Key) { + bool erase(std::string_view Key) { iterator I = find(Key); if (I == end()) return false; @@ -455,23 +455,23 @@ template class StringMapKeyIterator : public iterator_adaptor_base, StringMapConstIterator, - std::forward_iterator_tag, StringRef> { + std::forward_iterator_tag, std::string_view> { using base = iterator_adaptor_base, StringMapConstIterator, - std::forward_iterator_tag, StringRef>; + std::forward_iterator_tag, std::string_view>; public: StringMapKeyIterator() = default; explicit StringMapKeyIterator(StringMapConstIterator Iter) : base(std::move(Iter)) {} - StringRef &operator*() { + std::string_view &operator*() { Key = this->wrapped()->getKey(); return Key; } private: - StringRef Key; + std::string_view Key; }; } // end namespace llvm diff --git a/llvm/include/llvm/ADT/StringMapEntry.h b/llvm/include/llvm/ADT/StringMapEntry.h index 8bfad5540230..93e13b5bb16c 100644 --- a/llvm/include/llvm/ADT/StringMapEntry.h +++ b/llvm/include/llvm/ADT/StringMapEntry.h @@ -15,7 +15,8 @@ #ifndef LLVM_ADT_STRINGMAPENTRY_H #define LLVM_ADT_STRINGMAPENTRY_H -#include "llvm/ADT/StringRef.h" +#include +#include namespace llvm { @@ -34,13 +35,13 @@ protected: /// type-erase the allocator and put it in a source file. template static void *allocateWithKey(size_t EntrySize, size_t EntryAlign, - StringRef Key, AllocatorTy &Allocator); + std::string_view Key, AllocatorTy &Allocator); }; // Define out-of-line to dissuade inlining. template void *StringMapEntryBase::allocateWithKey(size_t EntrySize, size_t EntryAlign, - StringRef Key, + std::string_view Key, AllocatorTy &Allocator) { size_t KeyLength = Key.size(); @@ -82,13 +83,13 @@ public: void setValue(const ValueTy &V) { second = V; } }; -template <> class StringMapEntryStorage : public StringMapEntryBase { +template <> class StringMapEntryStorage : public StringMapEntryBase { public: - explicit StringMapEntryStorage(size_t keyLength, NoneType = None) + explicit StringMapEntryStorage(size_t keyLength, std::nullopt_t = std::nullopt) : StringMapEntryBase(keyLength) {} StringMapEntryStorage(StringMapEntryStorage &entry) = delete; - NoneType getValue() const { return None; } + std::nullopt_t getValue() const { return std::nullopt; } }; /// StringMapEntry - This is used to represent one value that is inserted into @@ -99,8 +100,8 @@ class StringMapEntry final : public StringMapEntryStorage { public: using StringMapEntryStorage::StringMapEntryStorage; - StringRef getKey() const { - return StringRef(getKeyData(), this->getKeyLength()); + std::string_view getKey() const { + return std::string_view(getKeyData(), this->getKeyLength()); } /// getKeyData - Return the start of the string data that is the key for this @@ -110,14 +111,14 @@ public: return reinterpret_cast(this + 1); } - StringRef first() const { - return StringRef(getKeyData(), this->getKeyLength()); + std::string_view first() const { + return std::string_view(getKeyData(), this->getKeyLength()); } /// Create a StringMapEntry for the specified key construct the value using /// \p InitiVals. template - static StringMapEntry *Create(StringRef key, AllocatorTy &allocator, + static StringMapEntry *Create(std::string_view key, AllocatorTy &allocator, InitTy &&... initVals) { return new (StringMapEntryBase::allocateWithKey( sizeof(StringMapEntry), alignof(StringMapEntry), key, allocator)) diff --git a/llvm/include/llvm/Support/Chrono.h b/llvm/include/llvm/Support/Chrono.h index 004519a883fd..2c2869de49b6 100644 --- a/llvm/include/llvm/Support/Chrono.h +++ b/llvm/include/llvm/Support/Chrono.h @@ -69,7 +69,7 @@ raw_ostream &operator<<(raw_ostream &OS, sys::TimePoint<> TP); template <> struct format_provider> { static void format(const sys::TimePoint<> &TP, llvm::raw_ostream &OS, - StringRef Style); + std::string_view Style); }; namespace detail { @@ -121,7 +121,7 @@ private: return duration_cast>(D).count(); } - static std::pair consumeUnit(StringRef &Style, + static std::pair consumeUnit(std::string_view &Style, const Dur &D) { using namespace std::chrono; if (Style.consume_front("ns")) @@ -139,7 +139,7 @@ private: return {D.count(), detail::unit::value}; } - static bool consumeShowUnit(StringRef &Style) { + static bool consumeShowUnit(std::string_view &Style) { if (Style.empty()) return true; if (Style.consume_front("-")) @@ -151,9 +151,9 @@ private: } public: - static void format(const Dur &D, llvm::raw_ostream &Stream, StringRef Style) { + static void format(const Dur &D, llvm::raw_ostream &Stream, std::string_view Style) { InternalRep count; - StringRef unit; + std::string_view unit; std::tie(count, unit) = consumeUnit(Style, D); bool show_unit = consumeShowUnit(Style); diff --git a/llvm/include/llvm/Support/Compiler.h b/llvm/include/llvm/Support/Compiler.h index 57052b596edb..f4d6612fe074 100644 --- a/llvm/include/llvm/Support/Compiler.h +++ b/llvm/include/llvm/Support/Compiler.h @@ -299,7 +299,7 @@ #endif /// LLVM_GSL_POINTER - Apply this to non-owning classes like -/// StringRef to enable lifetime warnings. +/// std::string_view to enable lifetime warnings. #if LLVM_HAS_CPP_ATTRIBUTE(gsl::Pointer) #define LLVM_GSL_POINTER [[gsl::Pointer]] #else diff --git a/llvm/include/llvm/Support/ConvertUTF.h b/llvm/include/llvm/Support/ConvertUTF.h index 1add185330fa..7f1527f51cdf 100644 --- a/llvm/include/llvm/Support/ConvertUTF.h +++ b/llvm/include/llvm/Support/ConvertUTF.h @@ -89,8 +89,11 @@ #ifndef LLVM_SUPPORT_CONVERTUTF_H #define LLVM_SUPPORT_CONVERTUTF_H +#include "wpi/span.h" + #include #include +#include #include // Wrap everything in namespace llvm so that programs can link with llvm and @@ -180,12 +183,10 @@ unsigned getNumBytesForUTF8(UTF8 firstByte); /*************************************************************************/ /* Below are LLVM-specific wrappers of the functions above. */ -template class ArrayRef; template class SmallVectorImpl; -class StringRef; /** - * Convert an UTF8 StringRef to UTF8, UTF16, or UTF32 depending on + * Convert an UTF8 string_view to UTF8, UTF16, or UTF32 depending on * WideCharWidth. The converted data is written to ResultPtr, which needs to * point to at least WideCharWidth * (Source.Size() + 1) bytes. On success, * ResultPtr will point one after the end of the copied string. On failure, @@ -193,14 +194,14 @@ class StringRef; * the first character which could not be converted. * \return true on success. */ -bool ConvertUTF8toWide(unsigned WideCharWidth, llvm::StringRef Source, +bool ConvertUTF8toWide(unsigned WideCharWidth, std::string_view Source, char *&ResultPtr, const UTF8 *&ErrorPtr); /** -* Converts a UTF-8 StringRef to a std::wstring. +* Converts a UTF-8 string_view to a std::wstring. * \return true on success. */ -bool ConvertUTF8toWide(llvm::StringRef Source, std::wstring &Result); +bool ConvertUTF8toWide(std::string_view Source, std::wstring &Result); /** * Converts a UTF-8 C-string to a std::wstring. @@ -258,7 +259,7 @@ inline ConversionResult convertUTF8Sequence(const UTF8 **source, * Returns true if a blob of text starts with a UTF-16 big or little endian byte * order mark. */ -bool hasUTF16ByteOrderMark(ArrayRef SrcBytes); +bool hasUTF16ByteOrderMark(span SrcBytes); /** * Converts a stream of raw bytes assumed to be UTF16 into a UTF8 std::string. @@ -267,7 +268,7 @@ bool hasUTF16ByteOrderMark(ArrayRef SrcBytes); * \param [out] Out Converted UTF-8 is stored here on success. * \returns true on success */ -bool convertUTF16ToUTF8String(ArrayRef SrcBytes, std::string &Out); +bool convertUTF16ToUTF8String(span SrcBytes, std::string &Out); /** * Converts a UTF16 string into a UTF8 std::string. @@ -276,22 +277,22 @@ bool convertUTF16ToUTF8String(ArrayRef SrcBytes, std::string &Out); * \param [out] Out Converted UTF-8 is stored here on success. * \returns true on success */ -bool convertUTF16ToUTF8String(ArrayRef Src, std::string &Out); +bool convertUTF16ToUTF8String(span Src, std::string &Out); /** * Converts a UTF-8 string into a UTF-16 string with native endianness. * * \returns true on success */ -bool convertUTF8ToUTF16String(StringRef SrcUTF8, +bool convertUTF8ToUTF16String(std::string_view SrcUTF8, SmallVectorImpl &DstUTF16); #if defined(_WIN32) namespace sys { namespace windows { -std::error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl &utf16); +std::error_code UTF8ToUTF16(std::string_view utf8, SmallVectorImpl &utf16); /// Convert to UTF16 from the current code page used in the system -std::error_code CurCPToUTF16(StringRef utf8, SmallVectorImpl &utf16); +std::error_code CurCPToUTF16(std::string_view utf8, SmallVectorImpl &utf16); std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len, SmallVectorImpl &utf8); /// Convert from UTF16 to the current code page used in the system diff --git a/llvm/include/llvm/Support/ErrorHandling.h b/llvm/include/llvm/Support/ErrorHandling.h index dd85a5892e01..411c78a81b24 100644 --- a/llvm/include/llvm/Support/ErrorHandling.h +++ b/llvm/include/llvm/Support/ErrorHandling.h @@ -16,10 +16,9 @@ #include "llvm/Support/Compiler.h" #include +#include namespace llvm { -class StringRef; - class Twine; /// An error handler callback. typedef void (*fatal_error_handler_t)(void *user_data, @@ -72,9 +71,7 @@ LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag = true); LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const std::string &reason, bool gen_crash_diag = true); -LLVM_ATTRIBUTE_NORETURN void report_fatal_error(StringRef reason, - bool gen_crash_diag = true); -LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const Twine &reason, +LLVM_ATTRIBUTE_NORETURN void report_fatal_error(std::string_view reason, bool gen_crash_diag = true); /// Installs a new bad alloc error handler that should be used whenever a diff --git a/llvm/include/llvm/Support/SmallVectorMemoryBuffer.h b/llvm/include/llvm/Support/SmallVectorMemoryBuffer.h index 9aa4e9aec266..924ed77510fe 100644 --- a/llvm/include/llvm/Support/SmallVectorMemoryBuffer.h +++ b/llvm/include/llvm/Support/SmallVectorMemoryBuffer.h @@ -42,8 +42,8 @@ public: } /// Construct a named SmallVectorMemoryBuffer from the given - /// SmallVector r-value and StringRef. - SmallVectorMemoryBuffer(SmallVectorImpl &&SV, StringRef Name) + /// SmallVector r-value and std::string_view. + SmallVectorMemoryBuffer(SmallVectorImpl &&SV, std::string_view Name) : SV(std::move(SV)), BufferName(std::string(Name)) { init(this->SV.begin(), this->SV.end(), false); } @@ -51,7 +51,7 @@ public: // Key function. ~SmallVectorMemoryBuffer() override; - StringRef getBufferIdentifier() const override { return BufferName; } + std::string_view getBufferIdentifier() const override { return BufferName; } BufferKind getBufferKind() const override { return MemoryBuffer_Malloc; } diff --git a/llvm/include/llvm/Support/VersionTuple.h b/llvm/include/llvm/Support/VersionTuple.h index a48ae0bf52bd..85f4f1a707c7 100644 --- a/llvm/include/llvm/Support/VersionTuple.h +++ b/llvm/include/llvm/Support/VersionTuple.h @@ -16,13 +16,12 @@ #include "llvm/ADT/DenseMapInfo.h" #include "llvm/ADT/Hashing.h" -#include "llvm/ADT/Optional.h" +#include #include #include namespace llvm { class raw_ostream; -class StringRef; /// Represents a version number in the form major[.minor[.subminor[.build]]]. class VersionTuple { @@ -69,23 +68,23 @@ public: unsigned getMajor() const { return Major; } /// Retrieve the minor version number, if provided. - Optional getMinor() const { + std::optional getMinor() const { if (!HasMinor) - return None; + return std::nullopt; return Minor; } /// Retrieve the subminor version number, if provided. - Optional getSubminor() const { + std::optional getSubminor() const { if (!HasSubminor) - return None; + return std::nullopt; return Subminor; } /// Retrieve the build version number, if provided. - Optional getBuild() const { + std::optional getBuild() const { if (!HasBuild) - return None; + return std::nullopt; return Build; } @@ -166,11 +165,6 @@ public: /// Retrieve a string representation of the version number. std::string getAsString() const; - - /// Try to parse the given string as a version number. - /// \returns \c true if the string does not match the regular expression - /// [0-9]+(\.[0-9]+){0,3} - bool tryParse(StringRef string); }; /// Print a version number. diff --git a/llvm/include/llvm/Support/Windows/WindowsSupport.h b/llvm/include/llvm/Support/Windows/WindowsSupport.h index a45eeaba4ad5..551bd199551e 100644 --- a/llvm/include/llvm/Support/Windows/WindowsSupport.h +++ b/llvm/include/llvm/Support/Windows/WindowsSupport.h @@ -35,8 +35,6 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringExtras.h" -#include "llvm/ADT/StringRef.h" -#include "llvm/ADT/Twine.h" #include "llvm/Config/llvm-config.h" // Get build system configuration settings #include "llvm/Support/Allocator.h" #include "llvm/Support/Chrono.h" diff --git a/llvm/include/llvm/Support/raw_ostream.h b/llvm/include/llvm/Support/raw_ostream.h index c669c2babad9..4f6ea873304f 100644 --- a/llvm/include/llvm/Support/raw_ostream.h +++ b/llvm/include/llvm/Support/raw_ostream.h @@ -14,8 +14,7 @@ #define LLVM_SUPPORT_RAW_OSTREAM_H #include "llvm/ADT/SmallVector.h" -#include "llvm/ADT/StringRef.h" -#include "llvm/Support/DataTypes.h" +#include "llvm/ADT/span.h" #include #include #include @@ -209,7 +208,22 @@ public: return *this; } - raw_ostream &operator<<(StringRef Str) { + raw_ostream &operator<<(span Arr) { + // Inline fast path, particularly for arrays with a known length. + size_t Size = Arr.size(); + + // Make sure we can use the fast path. + if (Size > (size_t)(OutBufEnd - OutBufCur)) + return write(Arr.data(), Size); + + if (Size) { + memcpy(OutBufCur, Arr.data(), Size); + OutBufCur += Size; + } + return *this; + } + + raw_ostream &operator<<(std::string_view Str) { // Inline fast path, particularly for strings with a known length. size_t Size = Str.size(); @@ -228,7 +242,7 @@ public: // Inline fast path, particularly for constant strings where a sufficiently // smart compiler will simplify strlen. - return this->operator<<(StringRef(Str)); + return this->operator<<(std::string_view(Str)); } raw_ostream &operator<<(const std::string &Str) { @@ -236,12 +250,6 @@ public: return write(Str.data(), Str.length()); } -#if __cplusplus > 201402L - raw_ostream &operator<<(const std::string_view &Str) { - return write(Str.data(), Str.length()); - } -#endif - raw_ostream &operator<<(const SmallVectorImpl &Str) { return write(Str.data(), Str.size()); } @@ -274,7 +282,7 @@ public: /// Output \p Str, turning '\\', '\t', '\n', '"', and anything that doesn't /// satisfy llvm::isPrint into an escape sequence. - raw_ostream &write_escaped(StringRef Str, bool UseHexEscapes = false); + raw_ostream &write_escaped(std::string_view Str, bool UseHexEscapes = false); raw_ostream &write(unsigned char C); raw_ostream &write(const char *Ptr, size_t Size); @@ -487,14 +495,14 @@ public: /// As a special case, if Filename is "-", then the stream will use /// STDOUT_FILENO instead of opening a file. This will not close the stdout /// descriptor. - raw_fd_ostream(StringRef Filename, std::error_code &EC); - raw_fd_ostream(StringRef Filename, std::error_code &EC, + raw_fd_ostream(std::string_view Filename, std::error_code &EC); + raw_fd_ostream(std::string_view Filename, std::error_code &EC, sys::fs::CreationDisposition Disp); - raw_fd_ostream(StringRef Filename, std::error_code &EC, + raw_fd_ostream(std::string_view Filename, std::error_code &EC, sys::fs::FileAccess Access); - raw_fd_ostream(StringRef Filename, std::error_code &EC, + raw_fd_ostream(std::string_view Filename, std::error_code &EC, sys::fs::OpenFlags Flags); - raw_fd_ostream(StringRef Filename, std::error_code &EC, + raw_fd_ostream(std::string_view Filename, std::error_code &EC, sys::fs::CreationDisposition Disp, sys::fs::FileAccess Access, sys::fs::OpenFlags Flags); @@ -597,7 +605,7 @@ public: /// Open the specified file for reading/writing/seeking. If an error occurs, /// information about the error is put into EC, and the stream should be /// immediately destroyed. - raw_fd_stream(StringRef Filename, std::error_code &EC); + raw_fd_stream(std::string_view Filename, std::error_code &EC); /// This reads the \p Size bytes into a buffer pointed by \p Ptr. /// @@ -677,8 +685,8 @@ public: void flush() = delete; - /// Return a StringRef for the vector contents. - StringRef str() const { return StringRef(OS.data(), OS.size()); } + /// Return a std::string_view for the vector contents. + std::string_view str() const { return std::string_view(OS.data(), OS.size()); } void reserveExtraSpace(uint64_t ExtraSize) override { OS.reserve(tell() + ExtraSize); @@ -731,7 +739,7 @@ class Error; /// for other names. For raw_fd_ostream instances, the stream writes to /// a temporary file. The final output file is atomically replaced with the /// temporary file after the \p Write function is finished. -Error writeToOutput(StringRef OutputFileName, +Error writeToOutput(std::string_view OutputFileName, std::function Write); } // end namespace llvm diff --git a/llvm/lib/Support/ConvertUTFWrapper.cpp b/llvm/lib/Support/ConvertUTFWrapper.cpp index d8d46712a593..090ebb0f5af8 100644 --- a/llvm/lib/Support/ConvertUTFWrapper.cpp +++ b/llvm/lib/Support/ConvertUTFWrapper.cpp @@ -6,24 +6,24 @@ // //===----------------------------------------------------------------------===// -#include "llvm/ADT/ArrayRef.h" -#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/span.h" #include "llvm/Support/ConvertUTF.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/SwapByteOrder.h" #include +#include #include namespace llvm { -bool ConvertUTF8toWide(unsigned WideCharWidth, llvm::StringRef Source, +bool ConvertUTF8toWide(unsigned WideCharWidth, std::string_view Source, char *&ResultPtr, const UTF8 *&ErrorPtr) { assert(WideCharWidth == 1 || WideCharWidth == 2 || WideCharWidth == 4); ConversionResult result = conversionOK; // Copy the character span over. if (WideCharWidth == 1) { - const UTF8 *Pos = reinterpret_cast(Source.begin()); - if (!isLegalUTF8String(&Pos, reinterpret_cast(Source.end()))) { + const UTF8 *Pos = reinterpret_cast(Source.data()); + if (!isLegalUTF8String(&Pos, reinterpret_cast(Source.data() + Source.size()))) { result = sourceIllegal; ErrorPtr = Pos; } else { @@ -77,13 +77,13 @@ bool ConvertCodePointToUTF8(unsigned Source, char *&ResultPtr) { return true; } -bool hasUTF16ByteOrderMark(ArrayRef S) { +bool hasUTF16ByteOrderMark(span S) { return (S.size() >= 2 && ((S[0] == '\xff' && S[1] == '\xfe') || (S[0] == '\xfe' && S[1] == '\xff'))); } -bool convertUTF16ToUTF8String(ArrayRef SrcBytes, std::string &Out) { +bool convertUTF16ToUTF8String(span SrcBytes, std::string &Out) { assert(Out.empty()); // Error out on an uneven byte count. @@ -134,14 +134,14 @@ bool convertUTF16ToUTF8String(ArrayRef SrcBytes, std::string &Out) { return true; } -bool convertUTF16ToUTF8String(ArrayRef Src, std::string &Out) +bool convertUTF16ToUTF8String(span Src, std::string &Out) { return convertUTF16ToUTF8String( - llvm::ArrayRef(reinterpret_cast(Src.data()), + span(reinterpret_cast(Src.data()), Src.size() * sizeof(UTF16)), Out); } -bool convertUTF8ToUTF16String(StringRef SrcUTF8, +bool convertUTF8ToUTF16String(std::string_view SrcUTF8, SmallVectorImpl &DstUTF16) { assert(DstUTF16.empty()); @@ -152,8 +152,8 @@ bool convertUTF8ToUTF16String(StringRef SrcUTF8, return true; } - const UTF8 *Src = reinterpret_cast(SrcUTF8.begin()); - const UTF8 *SrcEnd = reinterpret_cast(SrcUTF8.end()); + const UTF8 *Src = reinterpret_cast(SrcUTF8.data()); + const UTF8 *SrcEnd = reinterpret_cast(SrcUTF8.data() + SrcUTF8.size()); // Allocate the same number of UTF-16 code units as UTF-8 code units. Encoding // as UTF-16 should always require the same amount or less code units than the @@ -184,7 +184,7 @@ static_assert(sizeof(wchar_t) == 1 || sizeof(wchar_t) == 2 || "Expected wchar_t to be 1, 2, or 4 bytes"); template -static inline bool ConvertUTF8toWideInternal(llvm::StringRef Source, +static inline bool ConvertUTF8toWideInternal(std::string_view Source, TResult &Result) { // Even in the case of UTF-16, the number of bytes in a UTF-8 string is // at least as large as the number of elements in the resulting wide @@ -200,7 +200,7 @@ static inline bool ConvertUTF8toWideInternal(llvm::StringRef Source, return true; } -bool ConvertUTF8toWide(llvm::StringRef Source, std::wstring &Result) { +bool ConvertUTF8toWide(std::string_view Source, std::wstring &Result) { return ConvertUTF8toWideInternal(Source, Result); } @@ -209,7 +209,7 @@ bool ConvertUTF8toWide(const char *Source, std::wstring &Result) { Result.clear(); return true; } - return ConvertUTF8toWide(llvm::StringRef(Source), Result); + return ConvertUTF8toWide(std::string_view(Source), Result); } bool convertWideToUTF8(const std::wstring &Source, std::string &Result) { @@ -224,7 +224,7 @@ bool convertWideToUTF8(const std::wstring &Source, std::string &Result) { return true; } else if (sizeof(wchar_t) == 2) { return convertUTF16ToUTF8String( - llvm::ArrayRef(reinterpret_cast(Source.data()), + span(reinterpret_cast(Source.data()), Source.size()), Result); } else if (sizeof(wchar_t) == 4) { diff --git a/llvm/lib/Support/ErrorHandling.cpp b/llvm/lib/Support/ErrorHandling.cpp index ce6344284f06..19d1fd77af12 100644 --- a/llvm/lib/Support/ErrorHandling.cpp +++ b/llvm/lib/Support/ErrorHandling.cpp @@ -14,7 +14,6 @@ #include "llvm/Support/ErrorHandling.h" #include "llvm-c/ErrorHandling.h" #include "llvm/ADT/SmallVector.h" -#include "llvm/ADT/Twine.h" #include "llvm/Config/config.h" #include "llvm/Support/Debug.h" #include "llvm/Support/Errc.h" @@ -80,18 +79,14 @@ void llvm::remove_fatal_error_handler() { } void llvm::report_fatal_error(const char *Reason, bool GenCrashDiag) { - report_fatal_error(Twine(Reason), GenCrashDiag); + report_fatal_error(std::string_view(Reason), GenCrashDiag); } void llvm::report_fatal_error(const std::string &Reason, bool GenCrashDiag) { - report_fatal_error(Twine(Reason), GenCrashDiag); + report_fatal_error(std::string_view(Reason), GenCrashDiag); } -void llvm::report_fatal_error(StringRef Reason, bool GenCrashDiag) { - report_fatal_error(Twine(Reason), GenCrashDiag); -} - -void llvm::report_fatal_error(const Twine &Reason, bool GenCrashDiag) { +void llvm::report_fatal_error(std::string_view Reason, bool GenCrashDiag) { llvm::fatal_error_handler_t handler = nullptr; void* handlerData = nullptr; { @@ -105,7 +100,7 @@ void llvm::report_fatal_error(const Twine &Reason, bool GenCrashDiag) { } if (handler) { - handler(handlerData, Reason.str(), GenCrashDiag); + handler(handlerData, std::string{Reason}, GenCrashDiag); } else { // Blast the result out to stderr. We don't try hard to make sure this // succeeds (e.g. handling EINTR) and we can't use errs() here because @@ -113,7 +108,7 @@ void llvm::report_fatal_error(const Twine &Reason, bool GenCrashDiag) { SmallVector Buffer; raw_svector_ostream OS(Buffer); OS << "LLVM ERROR: " << Reason << "\n"; - StringRef MessageStr = OS.str(); + std::string_view MessageStr = OS.str(); ssize_t written = ::write(2, MessageStr.data(), MessageStr.size()); (void)written; // If something went wrong, we deliberately just give up. } diff --git a/llvm/lib/Support/StringMap.cpp b/llvm/lib/Support/StringMap.cpp index f65d3846623c..d9eeba619428 100644 --- a/llvm/lib/Support/StringMap.cpp +++ b/llvm/lib/Support/StringMap.cpp @@ -71,7 +71,7 @@ void StringMapImpl::init(unsigned InitSize) { /// specified bucket will be non-null. Otherwise, it will be null. In either /// case, the FullHashValue field of the bucket will be set to the hash value /// of the string. -unsigned StringMapImpl::LookupBucketFor(StringRef Name) { +unsigned StringMapImpl::LookupBucketFor(std::string_view Name) { unsigned HTSize = NumBuckets; if (HTSize == 0) { // Hash table unallocated so far? init(16); @@ -111,7 +111,7 @@ unsigned StringMapImpl::LookupBucketFor(StringRef Name) { // Do the comparison like this because Name isn't necessarily // null-terminated! char *ItemStr = (char *)BucketItem + ItemSize; - if (Name == StringRef(ItemStr, BucketItem->getKeyLength())) { + if (Name == std::string_view(ItemStr, BucketItem->getKeyLength())) { // We found a match! return BucketNo; } @@ -129,7 +129,7 @@ unsigned StringMapImpl::LookupBucketFor(StringRef Name) { /// FindKey - Look up the bucket that contains the specified key. If it exists /// in the map, return the bucket number of the key. Otherwise return -1. /// This does not modify the map. -int StringMapImpl::FindKey(StringRef Key) const { +int StringMapImpl::FindKey(std::string_view Key) const { unsigned HTSize = NumBuckets; if (HTSize == 0) return -1; // Really empty table? @@ -155,7 +155,7 @@ int StringMapImpl::FindKey(StringRef Key) const { // Do the comparison like this because NameStart isn't necessarily // null-terminated! char *ItemStr = (char *)BucketItem + ItemSize; - if (Key == StringRef(ItemStr, BucketItem->getKeyLength())) { + if (Key == std::string_view(ItemStr, BucketItem->getKeyLength())) { // We found a match! return BucketNo; } @@ -174,14 +174,14 @@ int StringMapImpl::FindKey(StringRef Key) const { /// delete it. This aborts if the value isn't in the table. void StringMapImpl::RemoveKey(StringMapEntryBase *V) { const char *VStr = (char *)V + ItemSize; - StringMapEntryBase *V2 = RemoveKey(StringRef(VStr, V->getKeyLength())); + StringMapEntryBase *V2 = RemoveKey(std::string_view(VStr, V->getKeyLength())); (void)V2; assert(V == V2 && "Didn't find key?"); } /// RemoveKey - Remove the StringMapEntry for the specified key from the /// table, returning it. If the key is not in the table, this returns null. -StringMapEntryBase *StringMapImpl::RemoveKey(StringRef Key) { +StringMapEntryBase *StringMapImpl::RemoveKey(std::string_view Key) { int Bucket = FindKey(Key); if (Bucket == -1) return nullptr; diff --git a/llvm/lib/Support/raw_ostream.cpp b/llvm/lib/Support/raw_ostream.cpp index d4e1c884d125..306cc981ed8f 100644 --- a/llvm/lib/Support/raw_ostream.cpp +++ b/llvm/lib/Support/raw_ostream.cpp @@ -160,7 +160,7 @@ raw_ostream &raw_ostream::write_uuid(const uuid_t UUID) { } -raw_ostream &raw_ostream::write_escaped(StringRef Str, +raw_ostream &raw_ostream::write_escaped(std::string_view Str, bool UseHexEscapes) { for (unsigned char c : Str) { switch (c) { @@ -564,7 +564,7 @@ void format_object_base::home() { // raw_fd_ostream //===----------------------------------------------------------------------===// -static int getFD(StringRef Filename, std::error_code &EC, +static int getFD(std::string_view Filename, std::error_code &EC, sys::fs::CreationDisposition Disp, sys::fs::FileAccess Access, sys::fs::OpenFlags Flags) { assert((Access & sys::fs::FA_Write) && @@ -590,25 +590,25 @@ static int getFD(StringRef Filename, std::error_code &EC, return FD; } -raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC) +raw_fd_ostream::raw_fd_ostream(std::string_view Filename, std::error_code &EC) : raw_fd_ostream(Filename, EC, sys::fs::CD_CreateAlways, sys::fs::FA_Write, sys::fs::OF_None) {} -raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC, +raw_fd_ostream::raw_fd_ostream(std::string_view Filename, std::error_code &EC, sys::fs::CreationDisposition Disp) : raw_fd_ostream(Filename, EC, Disp, sys::fs::FA_Write, sys::fs::OF_None) {} -raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC, +raw_fd_ostream::raw_fd_ostream(std::string_view Filename, std::error_code &EC, sys::fs::FileAccess Access) : raw_fd_ostream(Filename, EC, sys::fs::CD_CreateAlways, Access, sys::fs::OF_None) {} -raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC, +raw_fd_ostream::raw_fd_ostream(std::string_view Filename, std::error_code &EC, sys::fs::OpenFlags Flags) : raw_fd_ostream(Filename, EC, sys::fs::CD_CreateAlways, sys::fs::FA_Write, Flags) {} -raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC, +raw_fd_ostream::raw_fd_ostream(std::string_view Filename, std::error_code &EC, sys::fs::CreationDisposition Disp, sys::fs::FileAccess Access, sys::fs::OpenFlags Flags) @@ -698,7 +698,7 @@ raw_fd_ostream::~raw_fd_ostream() { // the input is UTF-8 or transcode from the local codepage to UTF-8 before // quoting it. If they don't, this may mess up the encoding, but this is still // probably the best compromise we can make. -static bool write_console_impl(int FD, StringRef Data) { +static bool write_console_impl(int FD, std::string_view Data) { SmallVector WideText; // Fall back to ::write if it wasn't valid UTF-8. @@ -741,7 +741,7 @@ void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) { // If this is a Windows console device, try re-encoding from UTF-8 to UTF-16 // and using WriteConsoleW. If that fails, fall back to plain write(). if (IsWindowsConsole) - if (write_console_impl(FD, StringRef(Ptr, Size))) + if (write_console_impl(FD, std::string_view(Ptr, Size))) return; #endif @@ -905,7 +905,7 @@ raw_ostream &llvm::nulls() { // File Streams //===----------------------------------------------------------------------===// -raw_fd_stream::raw_fd_stream(StringRef Filename, std::error_code &EC) +raw_fd_stream::raw_fd_stream(std::string_view Filename, std::error_code &EC) : raw_fd_ostream(getFD(Filename, EC, sys::fs::CD_CreateAlways, sys::fs::FA_Write | sys::fs::FA_Read, sys::fs::OF_None), @@ -988,7 +988,7 @@ void buffer_ostream::anchor() {} void buffer_unique_ostream::anchor() {} -Error llvm::writeToOutput(StringRef OutputFileName, +Error llvm::writeToOutput(std::string_view OutputFileName, std::function Write) { if (OutputFileName == "-") return Write(outs()); diff --git a/llvm/unittests/ADT/DenseMapTest.cpp b/llvm/unittests/ADT/DenseMapTest.cpp index 352270adec0f..a122f1fe3bdf 100644 --- a/llvm/unittests/ADT/DenseMapTest.cpp +++ b/llvm/unittests/ADT/DenseMapTest.cpp @@ -481,31 +481,6 @@ TEST(DenseMapCustomTest, ReserveTest) { } } -// Make sure DenseMap works with StringRef keys. -TEST(DenseMapCustomTest, StringRefTest) { - DenseMap M; - - M["a"] = 1; - M["b"] = 2; - M["c"] = 3; - - EXPECT_EQ(3u, M.size()); - EXPECT_EQ(1, M.lookup("a")); - EXPECT_EQ(2, M.lookup("b")); - EXPECT_EQ(3, M.lookup("c")); - - EXPECT_EQ(0, M.lookup("q")); - - // Test the empty string, spelled various ways. - EXPECT_EQ(0, M.lookup("")); - EXPECT_EQ(0, M.lookup(StringRef())); - EXPECT_EQ(0, M.lookup(StringRef("a", 0))); - M[""] = 42; - EXPECT_EQ(42, M.lookup("")); - EXPECT_EQ(42, M.lookup(StringRef())); - EXPECT_EQ(42, M.lookup(StringRef("a", 0))); -} - // Key traits that allows lookup with either an unsigned or char* key; // In the latter case, "a" == 0, "b" == 1 and so on. struct TestDenseMapInfo { diff --git a/llvm/unittests/ADT/FunctionExtrasTest.cpp b/llvm/unittests/ADT/FunctionExtrasTest.cpp index 3c6f179c190f..899690b8aae8 100644 --- a/llvm/unittests/ADT/FunctionExtrasTest.cpp +++ b/llvm/unittests/ADT/FunctionExtrasTest.cpp @@ -249,23 +249,23 @@ TEST(UniqueFunctionTest, Const) { // Overloaded call operator correctly resolved. struct ChooseCorrectOverload { - StringRef operator()() { return "non-const"; } - StringRef operator()() const { return "const"; } + std::string_view operator()() { return "non-const"; } + std::string_view operator()() const { return "const"; } }; - unique_function ChooseMutable = ChooseCorrectOverload(); + unique_function ChooseMutable = ChooseCorrectOverload(); ChooseCorrectOverload A; EXPECT_EQ("non-const", ChooseMutable()); EXPECT_EQ("non-const", A()); - unique_function ChooseConst = ChooseCorrectOverload(); + unique_function ChooseConst = ChooseCorrectOverload(); const ChooseCorrectOverload &X = A; EXPECT_EQ("const", ChooseConst()); EXPECT_EQ("const", X()); } // Test that overloads on unique_functions are resolved as expected. -std::string returns(StringRef) { return "not a function"; } +std::string returns(std::string_view) { return "not a function"; } std::string returns(unique_function F) { return "number"; } -std::string returns(unique_function F) { return "string"; } +std::string returns(unique_function F) { return "string"; } TEST(UniqueFunctionTest, SFINAE) { EXPECT_EQ("not a function", returns("boo!")); diff --git a/llvm/unittests/ADT/HashingTest.cpp b/llvm/unittests/ADT/HashingTest.cpp index d2cda3afdda0..3605bbdcdfe3 100644 --- a/llvm/unittests/ADT/HashingTest.cpp +++ b/llvm/unittests/ADT/HashingTest.cpp @@ -276,7 +276,7 @@ TEST(HashingTest, HashCombineRangeGoldenTest) { #endif }; for (unsigned i = 0; i < sizeof(golden_data)/sizeof(*golden_data); ++i) { - StringRef str = golden_data[i].s; + std::string_view str = golden_data[i].s; hash_code hash = hash_combine_range(str.begin(), str.end()); #if 0 // Enable this to generate paste-able text for the above structure. std::string member_str = "\"" + str.str() + "\","; diff --git a/llvm/unittests/ADT/SmallStringTest.cpp b/llvm/unittests/ADT/SmallStringTest.cpp index b207f582e919..bee3875d11c9 100644 --- a/llvm/unittests/ADT/SmallStringTest.cpp +++ b/llvm/unittests/ADT/SmallStringTest.cpp @@ -50,43 +50,43 @@ TEST_F(SmallStringTest, AssignRepeated) { } TEST_F(SmallStringTest, AssignIterPair) { - StringRef abc = "abc"; + std::string_view abc = "abc"; theString.assign(abc.begin(), abc.end()); EXPECT_EQ(3u, theString.size()); EXPECT_STREQ("abc", theString.c_str()); } -TEST_F(SmallStringTest, AssignStringRef) { - StringRef abc = "abc"; +TEST_F(SmallStringTest, AssignStringView) { + std::string_view abc = "abc"; theString.assign(abc); EXPECT_EQ(3u, theString.size()); EXPECT_STREQ("abc", theString.c_str()); } TEST_F(SmallStringTest, AssignSmallVector) { - StringRef abc = "abc"; + std::string_view abc = "abc"; SmallVector abcVec(abc.begin(), abc.end()); theString.assign(abcVec); EXPECT_EQ(3u, theString.size()); EXPECT_STREQ("abc", theString.c_str()); } -TEST_F(SmallStringTest, AssignStringRefs) { +TEST_F(SmallStringTest, AssignStringViews) { theString.assign({"abc", "def", "ghi"}); EXPECT_EQ(9u, theString.size()); EXPECT_STREQ("abcdefghi", theString.c_str()); } TEST_F(SmallStringTest, AppendIterPair) { - StringRef abc = "abc"; + std::string_view abc = "abc"; theString.append(abc.begin(), abc.end()); theString.append(abc.begin(), abc.end()); EXPECT_EQ(6u, theString.size()); EXPECT_STREQ("abcabc", theString.c_str()); } -TEST_F(SmallStringTest, AppendStringRef) { - StringRef abc = "abc"; +TEST_F(SmallStringTest, AppendStringView) { + std::string_view abc = "abc"; theString.append(abc); theString.append(abc); EXPECT_EQ(6u, theString.size()); @@ -94,7 +94,7 @@ TEST_F(SmallStringTest, AppendStringRef) { } TEST_F(SmallStringTest, AppendSmallVector) { - StringRef abc = "abc"; + std::string_view abc = "abc"; SmallVector abcVec(abc.begin(), abc.end()); theString.append(abcVec); theString.append(abcVec); @@ -102,11 +102,11 @@ TEST_F(SmallStringTest, AppendSmallVector) { EXPECT_STREQ("abcabc", theString.c_str()); } -TEST_F(SmallStringTest, AppendStringRefs) { +TEST_F(SmallStringTest, AppendStringViews) { theString.append({"abc", "def", "ghi"}); EXPECT_EQ(9u, theString.size()); EXPECT_STREQ("abcdefghi", theString.c_str()); - StringRef Jkl = "jkl"; + std::string_view Jkl = "jkl"; std::string Mno = "mno"; SmallString<4> Pqr("pqr"); const char *Stu = "stu"; @@ -115,15 +115,15 @@ TEST_F(SmallStringTest, AppendStringRefs) { EXPECT_STREQ("abcdefghijklmnopqrstu", theString.c_str()); } -TEST_F(SmallStringTest, StringRefConversion) { - StringRef abc = "abc"; +TEST_F(SmallStringTest, StringViewConversion) { + std::string_view abc = "abc"; theString.assign(abc.begin(), abc.end()); - StringRef theStringRef = theString; - EXPECT_EQ("abc", theStringRef); + std::string_view theStringView = theString; + EXPECT_EQ("abc", theStringView); } TEST_F(SmallStringTest, StdStringConversion) { - StringRef abc = "abc"; + std::string_view abc = "abc"; theString.assign(abc.begin(), abc.end()); std::string theStdString = std::string(theString); EXPECT_EQ("abc", theStdString); @@ -149,29 +149,29 @@ TEST_F(SmallStringTest, Slice) { TEST_F(SmallStringTest, Find) { theString = "hello"; EXPECT_EQ(2U, theString.find('l')); - EXPECT_EQ(StringRef::npos, theString.find('z')); - EXPECT_EQ(StringRef::npos, theString.find("helloworld")); + EXPECT_EQ(std::string_view::npos, theString.find('z')); + EXPECT_EQ(std::string_view::npos, theString.find("helloworld")); EXPECT_EQ(0U, theString.find("hello")); EXPECT_EQ(1U, theString.find("ello")); - EXPECT_EQ(StringRef::npos, theString.find("zz")); + EXPECT_EQ(std::string_view::npos, theString.find("zz")); EXPECT_EQ(2U, theString.find("ll", 2)); - EXPECT_EQ(StringRef::npos, theString.find("ll", 3)); + EXPECT_EQ(std::string_view::npos, theString.find("ll", 3)); EXPECT_EQ(0U, theString.find("")); EXPECT_EQ(3U, theString.rfind('l')); - EXPECT_EQ(StringRef::npos, theString.rfind('z')); - EXPECT_EQ(StringRef::npos, theString.rfind("helloworld")); + EXPECT_EQ(std::string_view::npos, theString.rfind('z')); + EXPECT_EQ(std::string_view::npos, theString.rfind("helloworld")); EXPECT_EQ(0U, theString.rfind("hello")); EXPECT_EQ(1U, theString.rfind("ello")); - EXPECT_EQ(StringRef::npos, theString.rfind("zz")); + EXPECT_EQ(std::string_view::npos, theString.rfind("zz")); EXPECT_EQ(2U, theString.find_first_of('l')); EXPECT_EQ(1U, theString.find_first_of("el")); - EXPECT_EQ(StringRef::npos, theString.find_first_of("xyz")); + EXPECT_EQ(std::string_view::npos, theString.find_first_of("xyz")); EXPECT_EQ(1U, theString.find_first_not_of('h')); EXPECT_EQ(4U, theString.find_first_not_of("hel")); - EXPECT_EQ(StringRef::npos, theString.find_first_not_of("hello")); + EXPECT_EQ(std::string_view::npos, theString.find_first_not_of("hello")); theString = "hellx xello hell ello world foo bar hello"; EXPECT_EQ(36U, theString.find("hello")); diff --git a/llvm/unittests/ADT/SmallVectorTest.cpp b/llvm/unittests/ADT/SmallVectorTest.cpp index 06b98efe66ef..1914f38fac6c 100644 --- a/llvm/unittests/ADT/SmallVectorTest.cpp +++ b/llvm/unittests/ADT/SmallVectorTest.cpp @@ -11,7 +11,7 @@ //===----------------------------------------------------------------------===// #include "llvm/ADT/SmallVector.h" -#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/span.h" #include "llvm/Support/Compiler.h" #include "gtest/gtest.h" #include @@ -1044,24 +1044,6 @@ TEST(SmallVectorTest, DefaultInlinedElements) { EXPECT_EQ(NestedV[0][0][0], 42); } -TEST(SmallVectorTest, InitializerList) { - SmallVector V1 = {}; - EXPECT_TRUE(V1.empty()); - V1 = {0, 0}; - EXPECT_TRUE(makeArrayRef(V1).equals({0, 0})); - V1 = {-1, -1}; - EXPECT_TRUE(makeArrayRef(V1).equals({-1, -1})); - - SmallVector V2 = {1, 2, 3, 4}; - EXPECT_TRUE(makeArrayRef(V2).equals({1, 2, 3, 4})); - V2.assign({4}); - EXPECT_TRUE(makeArrayRef(V2).equals({4})); - V2.append({3, 2}); - EXPECT_TRUE(makeArrayRef(V2).equals({4, 3, 2})); - V2.insert(V2.begin() + 1, 5); - EXPECT_TRUE(makeArrayRef(V2).equals({4, 5, 3, 2})); -} - template class SmallVectorReferenceInvalidationTest : public SmallVectorTestBase { protected: diff --git a/llvm/unittests/ADT/StringMapTest.cpp b/llvm/unittests/ADT/StringMapTest.cpp index 98fbd6e1df5a..4c4aec2184b7 100644 --- a/llvm/unittests/ADT/StringMapTest.cpp +++ b/llvm/unittests/ADT/StringMapTest.cpp @@ -7,7 +7,6 @@ //===----------------------------------------------------------------------===// #include "llvm/ADT/StringMap.h" -#include "llvm/ADT/Twine.h" #include "llvm/Support/DataTypes.h" #include "gtest/gtest.h" #include @@ -37,10 +36,10 @@ protected: // Lookup tests EXPECT_EQ(0u, testMap.count(testKey)); - EXPECT_EQ(0u, testMap.count(StringRef(testKeyFirst, testKeyLength))); + EXPECT_EQ(0u, testMap.count(std::string_view(testKeyFirst, testKeyLength))); EXPECT_EQ(0u, testMap.count(testKeyStr)); EXPECT_TRUE(testMap.find(testKey) == testMap.end()); - EXPECT_TRUE(testMap.find(StringRef(testKeyFirst, testKeyLength)) == + EXPECT_TRUE(testMap.find(std::string_view(testKeyFirst, testKeyLength)) == testMap.end()); EXPECT_TRUE(testMap.find(testKeyStr) == testMap.end()); } @@ -60,10 +59,10 @@ protected: // Lookup tests EXPECT_EQ(1u, testMap.count(testKey)); - EXPECT_EQ(1u, testMap.count(StringRef(testKeyFirst, testKeyLength))); + EXPECT_EQ(1u, testMap.count(std::string_view(testKeyFirst, testKeyLength))); EXPECT_EQ(1u, testMap.count(testKeyStr)); EXPECT_TRUE(testMap.find(testKey) == testMap.begin()); - EXPECT_TRUE(testMap.find(StringRef(testKeyFirst, testKeyLength)) == + EXPECT_TRUE(testMap.find(std::string_view(testKeyFirst, testKeyLength)) == testMap.begin()); EXPECT_TRUE(testMap.find(testKeyStr) == testMap.begin()); } @@ -103,10 +102,10 @@ TEST_F(StringMapTest, ConstEmptyMapTest) { // Lookup tests EXPECT_EQ(0u, constTestMap.count(testKey)); - EXPECT_EQ(0u, constTestMap.count(StringRef(testKeyFirst, testKeyLength))); + EXPECT_EQ(0u, constTestMap.count(std::string_view(testKeyFirst, testKeyLength))); EXPECT_EQ(0u, constTestMap.count(testKeyStr)); EXPECT_TRUE(constTestMap.find(testKey) == constTestMap.end()); - EXPECT_TRUE(constTestMap.find(StringRef(testKeyFirst, testKeyLength)) == + EXPECT_TRUE(constTestMap.find(std::string_view(testKeyFirst, testKeyLength)) == constTestMap.end()); EXPECT_TRUE(constTestMap.find(testKeyStr) == constTestMap.end()); } @@ -227,7 +226,7 @@ TEST_F(StringMapTest, StringMapEntryTest) { MallocAllocator Allocator; StringMap::value_type *entry = StringMap::value_type::Create( - StringRef(testKeyFirst, testKeyLength), Allocator, 1u); + std::string_view(testKeyFirst, testKeyLength), 1u); EXPECT_STREQ(testKey, entry->first().data()); EXPECT_EQ(1u, entry->second); entry->Destroy(Allocator); @@ -238,7 +237,7 @@ TEST_F(StringMapTest, InsertTest) { SCOPED_TRACE("InsertTest"); testMap.insert( StringMap::value_type::Create( - StringRef(testKeyFirst, testKeyLength), + std::string_view(testKeyFirst, testKeyLength), testMap.getAllocator(), 1u)); assertSingleItemMap(); } @@ -311,7 +310,7 @@ TEST_F(StringMapTest, IterMapKeys) { auto Keys = to_vector<4>(Map.keys()); llvm::sort(Keys); - SmallVector Expected = {"A", "B", "C", "D"}; + SmallVector Expected = {"A", "B", "C", "D"}; EXPECT_EQ(Expected, Keys); } @@ -353,13 +352,13 @@ private: TEST_F(StringMapTest, MoveOnly) { StringMap t; t.insert(std::make_pair("Test", MoveOnly(42))); - StringRef Key = "Test"; + std::string_view Key = "Test"; StringMapEntry::Create(Key, t.getAllocator(), MoveOnly(42)) ->Destroy(t.getAllocator()); } TEST_F(StringMapTest, CtorArg) { - StringRef Key = "Test"; + std::string_view Key = "Test"; MallocAllocator Allocator; StringMapEntry::Create(Key, Allocator, Immovable()) ->Destroy(Allocator); @@ -534,7 +533,7 @@ TEST(StringMapCustomTest, InitialSizeTest) { CountCtorCopyAndMove::Copy = 0; for (int i = 0; i < Size; ++i) Map.insert(std::pair( - std::piecewise_construct, std::forward_as_tuple(Twine(i).str()), + std::piecewise_construct, std::forward_as_tuple(std::to_string(i)), std::forward_as_tuple(i))); // After the initial move, the map will move the Elts in the Entry. EXPECT_EQ((unsigned)Size * 2, CountCtorCopyAndMove::Move); @@ -603,7 +602,7 @@ TEST(StringMapCustomTest, StringMapEntrySize) { else LargeValue = std::numeric_limits::max() + 1ULL; StringMapEntry LargeEntry(LargeValue); - StringRef Key = LargeEntry.getKey(); + std::string_view Key = LargeEntry.getKey(); EXPECT_EQ(LargeValue, Key.size()); // Test that the entry can hold at least max size_t. diff --git a/llvm/unittests/Support/ConvertUTFTest.cpp b/llvm/unittests/Support/ConvertUTFTest.cpp index 7bda6ea28ad6..9c798437a12d 100644 --- a/llvm/unittests/Support/ConvertUTFTest.cpp +++ b/llvm/unittests/Support/ConvertUTFTest.cpp @@ -7,7 +7,6 @@ //===----------------------------------------------------------------------===// #include "llvm/Support/ConvertUTF.h" -#include "llvm/ADT/ArrayRef.h" #include "gtest/gtest.h" #include #include @@ -17,7 +16,7 @@ using namespace llvm; TEST(ConvertUTFTest, ConvertUTF16LittleEndianToUTF8String) { // Src is the look of disapproval. alignas(UTF16) static const char Src[] = "\xff\xfe\xa0\x0c_\x00\xa0\x0c"; - ArrayRef Ref(Src, sizeof(Src) - 1); + span Ref(Src, sizeof(Src) - 1); std::string Result; bool Success = convertUTF16ToUTF8String(Ref, Result); EXPECT_TRUE(Success); @@ -28,7 +27,7 @@ TEST(ConvertUTFTest, ConvertUTF16LittleEndianToUTF8String) { TEST(ConvertUTFTest, ConvertUTF16BigEndianToUTF8String) { // Src is the look of disapproval. alignas(UTF16) static const char Src[] = "\xfe\xff\x0c\xa0\x00_\x0c\xa0"; - ArrayRef Ref(Src, sizeof(Src) - 1); + span Ref(Src, sizeof(Src) - 1); std::string Result; bool Success = convertUTF16ToUTF8String(Ref, Result); EXPECT_TRUE(Success); @@ -39,7 +38,7 @@ TEST(ConvertUTFTest, ConvertUTF16BigEndianToUTF8String) { TEST(ConvertUTFTest, ConvertUTF8ToUTF16String) { // Src is the look of disapproval. static const char Src[] = "\xe0\xb2\xa0_\xe0\xb2\xa0"; - StringRef Ref(Src, sizeof(Src) - 1); + std::string_view Ref(Src, sizeof(Src) - 1); SmallVector Result; bool Success = convertUTF8ToUTF16String(Ref, Result); EXPECT_TRUE(Success); @@ -51,37 +50,37 @@ TEST(ConvertUTFTest, ConvertUTF8ToUTF16String) { TEST(ConvertUTFTest, OddLengthInput) { std::string Result; - bool Success = convertUTF16ToUTF8String(makeArrayRef("xxxxx", 5), Result); + bool Success = convertUTF16ToUTF8String(span("xxxxx", 5), Result); EXPECT_FALSE(Success); } TEST(ConvertUTFTest, Empty) { std::string Result; - bool Success = convertUTF16ToUTF8String(llvm::ArrayRef(None), Result); + bool Success = convertUTF16ToUTF8String(span(), Result); EXPECT_TRUE(Success); EXPECT_TRUE(Result.empty()); } TEST(ConvertUTFTest, HasUTF16BOM) { - bool HasBOM = hasUTF16ByteOrderMark(makeArrayRef("\xff\xfe", 2)); + bool HasBOM = hasUTF16ByteOrderMark("\xff\xfe"); EXPECT_TRUE(HasBOM); - HasBOM = hasUTF16ByteOrderMark(makeArrayRef("\xfe\xff", 2)); + HasBOM = hasUTF16ByteOrderMark("\xfe\xff"); EXPECT_TRUE(HasBOM); - HasBOM = hasUTF16ByteOrderMark(makeArrayRef("\xfe\xff ", 3)); + HasBOM = hasUTF16ByteOrderMark("\xfe\xff "); EXPECT_TRUE(HasBOM); // Don't care about odd lengths. - HasBOM = hasUTF16ByteOrderMark(makeArrayRef("\xfe\xff\x00asdf", 6)); + HasBOM = hasUTF16ByteOrderMark("\xfe\xff\x00asdf"); EXPECT_TRUE(HasBOM); - HasBOM = hasUTF16ByteOrderMark(None); + HasBOM = hasUTF16ByteOrderMark(""); EXPECT_FALSE(HasBOM); - HasBOM = hasUTF16ByteOrderMark(makeArrayRef("\xfe", 1)); + HasBOM = hasUTF16ByteOrderMark("\xfe"); EXPECT_FALSE(HasBOM); } TEST(ConvertUTFTest, UTF16WrappersForConvertUTF16ToUTF8String) { // Src is the look of disapproval. alignas(UTF16) static const char Src[] = "\xff\xfe\xa0\x0c_\x00\xa0\x0c"; - ArrayRef SrcRef = makeArrayRef((const UTF16 *)Src, 4); + span SrcRef((const UTF16 *)Src, 4); std::string Result; bool Success = convertUTF16ToUTF8String(SrcRef, Result); EXPECT_TRUE(Success); @@ -98,7 +97,7 @@ TEST(ConvertUTFTest, ConvertUTF8toWide) { std::wstring Expected(L"\x0ca0_\x0ca0"); EXPECT_EQ(Expected, Result); Result.clear(); - Success = ConvertUTF8toWide(StringRef(Src, 7), Result); + Success = ConvertUTF8toWide(Src, Result); EXPECT_TRUE(Success); EXPECT_EQ(Expected, Result); } @@ -147,7 +146,7 @@ struct ConvertUTFResultContainer { }; std::pair> -ConvertUTF8ToUnicodeScalarsLenient(StringRef S) { +ConvertUTF8ToUnicodeScalarsLenient(std::string_view S) { const UTF8 *SourceStart = reinterpret_cast(S.data()); const UTF8 *SourceNext = SourceStart; @@ -164,7 +163,7 @@ ConvertUTF8ToUnicodeScalarsLenient(StringRef S) { } std::pair> -ConvertUTF8ToUnicodeScalarsPartialLenient(StringRef S) { +ConvertUTF8ToUnicodeScalarsPartialLenient(std::string_view S) { const UTF8 *SourceStart = reinterpret_cast(S.data()); const UTF8 *SourceNext = SourceStart; @@ -182,7 +181,7 @@ ConvertUTF8ToUnicodeScalarsPartialLenient(StringRef S) { ::testing::AssertionResult CheckConvertUTF8ToUnicodeScalars(ConvertUTFResultContainer Expected, - StringRef S, bool Partial = false) { + std::string_view S, bool Partial = false) { ConversionResult ErrorCode; std::vector Decoded; if (!Partial) @@ -277,7 +276,7 @@ TEST(ConvertUTFTest, UTF8ToUTF32Lenient) { // U+0000 NULL EXPECT_TRUE(CheckConvertUTF8ToUnicodeScalars( ConvertUTFResultContainer(conversionOK).withScalars(0x0000), - StringRef("\x00", 1))); + std::string_view("\x00", 1))); // U+0080 PADDING CHARACTER EXPECT_TRUE(CheckConvertUTF8ToUnicodeScalars( @@ -1051,7 +1050,7 @@ TEST(ConvertUTFTest, UTF8ToUTF32Lenient) { // U+0000 NULL EXPECT_TRUE(CheckConvertUTF8ToUnicodeScalars( ConvertUTFResultContainer(conversionOK).withScalars(0x0000), - StringRef("\x00", 1))); + std::string_view("\x00", 1))); // Overlong sequences of the above. EXPECT_TRUE(CheckConvertUTF8ToUnicodeScalars( -- 2.20.1.windows.1