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.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@200187 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Alp Toker
2014-01-27 04:07:17 +00:00
parent 1943ce9b6e
commit bad91954cf
4 changed files with 58 additions and 22 deletions

View File

@@ -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