diff options
author | Alp Toker <alp@nuanti.com> | 2014-01-27 04:07:17 +0000 |
---|---|---|
committer | Alp Toker <alp@nuanti.com> | 2014-01-27 04:07:17 +0000 |
commit | 042f41b0476c02a76e268bf049ad29dd2543188f (patch) | |
tree | f9041367ab15a8a1e957f10ac2fe8709c53eb455 /llvm/unittests/ADT/StringRefTest.cpp | |
parent | 054234faa8f026aac12b8a192d6464a485f3bc6a (diff) | |
download | llvm-042f41b0476c02a76e268bf049ad29dd2543188f.zip llvm-042f41b0476c02a76e268bf049ad29dd2543188f.tar.gz llvm-042f41b0476c02a76e268bf049ad29dd2543188f.tar.bz2 |
StringRef: Extend constexpr capabilities and introduce ConstStringRef
(1) Add llvm_expect(), an asserting macro that can be evaluated as a constexpr
expression as well as a runtime assert or compiler hint in release builds. This
technique can be used to construct functions that are both unevaluated and
compiled depending on usage.
(2) Update StringRef using llvm_expect() to preserve runtime assertions while
extending the same checks to static asserts in C++11 builds that support the
feature.
(3) Introduce ConstStringRef, a strong subclass of StringRef that references
compile-time constant strings. It's convertible to, but not from, ordinary
StringRef and thus can be used to add compile-time safety to various interfaces
in LLVM and clang that only accept fixed inputs such as diagnostic format
strings that tend to get misused.
llvm-svn: 200187
Diffstat (limited to 'llvm/unittests/ADT/StringRefTest.cpp')
-rw-r--r-- | llvm/unittests/ADT/StringRefTest.cpp | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/llvm/unittests/ADT/StringRefTest.cpp b/llvm/unittests/ADT/StringRefTest.cpp index 0ab8fcf..b240a87 100644 --- a/llvm/unittests/ADT/StringRefTest.cpp +++ b/llvm/unittests/ADT/StringRefTest.cpp @@ -531,4 +531,22 @@ TEST(StringRefTest, joinStrings) { EXPECT_TRUE(v2_join3); } +static void fn_stringref(StringRef str) { + EXPECT_TRUE(str == "hello"); +} +static void fn_conststringref(ConstStringRef str) { + fn_stringref(str); +} + +TEST(StringRefTest, constStringRef) { + LLVM_CONSTEXPR ConstStringRef csr("hello"); +#if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__) + LLVM_STATIC_ASSERT(csr[0] != csr[1], ""); + LLVM_STATIC_ASSERT(csr[2] == csr[3], ""); + LLVM_STATIC_ASSERT(csr.size() == 5, ""); +#endif + llvm_expect(csr[2] == csr[3]); + fn_conststringref(csr); +} + } // end anonymous namespace |