mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-10-18 18:42:46 +02:00
[ADT] Add llvm::StringLiteral.
StringLiteral is a wrapper around a string literal useful for replacing global tables of char arrays with global tables of StringRefs that can initialized in a constexpr context, avoiding the invocation of a global constructor. Differential Revision: https://reviews.llvm.org/D27686 llvm-svn: 289551
This commit is contained in:
parent
86eb682529
commit
d429264556
@ -84,11 +84,8 @@ namespace llvm {
|
|||||||
|
|
||||||
/// Construct a string ref from a pointer and length.
|
/// Construct a string ref from a pointer and length.
|
||||||
LLVM_ATTRIBUTE_ALWAYS_INLINE
|
LLVM_ATTRIBUTE_ALWAYS_INLINE
|
||||||
/*implicit*/ StringRef(const char *data, size_t length)
|
/*implicit*/ constexpr StringRef(const char *data, size_t length)
|
||||||
: Data(data), Length(length) {
|
: Data(data), Length(length) {}
|
||||||
assert((data || length == 0) &&
|
|
||||||
"StringRef cannot be built from a NULL argument with non-null length");
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Construct a string ref from an std::string.
|
/// Construct a string ref from an std::string.
|
||||||
LLVM_ATTRIBUTE_ALWAYS_INLINE
|
LLVM_ATTRIBUTE_ALWAYS_INLINE
|
||||||
@ -839,6 +836,26 @@ namespace llvm {
|
|||||||
/// @}
|
/// @}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// A wrapper around a string literal that serves as a proxy for constructing
|
||||||
|
/// global tables of StringRefs with the length computed at compile time.
|
||||||
|
/// Using this class with a non-literal char array is considered undefined
|
||||||
|
/// behavior. To prevent this, it is recommended that StringLiteral *only*
|
||||||
|
/// be used in a constexpr context, as such:
|
||||||
|
///
|
||||||
|
/// constexpr StringLiteral S("test");
|
||||||
|
///
|
||||||
|
/// Note: There is a subtle behavioral difference in the constructor of
|
||||||
|
/// StringRef and StringLiteral, as illustrated below:
|
||||||
|
///
|
||||||
|
/// constexpr StringLiteral S("a\0b"); // S.size() == 3
|
||||||
|
/// StringRef S("a\0b"); // S.size() == 1
|
||||||
|
///
|
||||||
|
class StringLiteral : public StringRef {
|
||||||
|
public:
|
||||||
|
template <size_t N>
|
||||||
|
constexpr StringLiteral(const char (&Str)[N]) : StringRef(Str, N - 1) {}
|
||||||
|
};
|
||||||
|
|
||||||
/// @name StringRef Comparison Operators
|
/// @name StringRef Comparison Operators
|
||||||
/// @{
|
/// @{
|
||||||
|
|
||||||
|
@ -1002,4 +1002,10 @@ TEST(StringRefTest, DropWhileUntil) {
|
|||||||
EXPECT_EQ("", Taken);
|
EXPECT_EQ("", Taken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(StringRefTest, StringLiteral) {
|
||||||
|
constexpr StringLiteral Strings[] = {"Foo", "Bar"};
|
||||||
|
EXPECT_EQ(StringRef("Foo"), Strings[0]);
|
||||||
|
EXPECT_EQ(StringRef("Bar"), Strings[1]);
|
||||||
|
}
|
||||||
|
|
||||||
} // end anonymous namespace
|
} // end anonymous namespace
|
||||||
|
Loading…
Reference in New Issue
Block a user