From 6e0d1cb30957a636c53158d3089e6fb88348a57a Mon Sep 17 00:00:00 2001
From: Daniel Dunbar
@@ -424,6 +430,106 @@ are lots of examples in the LLVM source base.
Although LLVM generally does not do much string manipulation, we do have +several important APIs which take string. Several important examples are the +Value class -- which has names for instructions, functions, etc. -- and the +StringMap class which is used extensively in LLVM and Clang.
+ +These are generic classes, and they need to be able to accept strings which +may have embedded null characters. Therefore, they cannot simply take +a const char *, and taking a const std::string& requires +clients to perform a heap allocation which is usually unnecessary. Instead, +many LLVM APIs use a const StringRef& or a const Twine& for +passing strings efficiently.
+ +The StringRef data type represents a reference to a constant string +(a character array and a length) and supports the common operations available +on std:string, but does not require heap allocation.
+ +It can be implicitly constructed using either a C style null-terminated string +or an std::string, or explicitly with a character pointer and length. +For example, the StringRef find function is declared as: +and clients can call it using any one of:
+ ++ Map.find("foo"); // Lookup "foo" + Map.find(std::string("bar")); // Lookup "bar" + Map.find(StringRef("\0baz", 4)); // Lookup "\0baz" ++
Similarly, APIs which need to return a string may return a StringRef +instance, which can be used directly or converted to an std::string +using the str member function. See +"llvm/ADT/StringRef.h" +for more information.
+ +You should rarely use the StringRef class directly, because it contains +pointers to external memory it is not generally safe to store an instance of the +class (since the external storage may be freed).
+ +The Twine class is an efficient way for APIs to accept concatenated +strings. For example, a common LLVM paradigm is to name one instruction based on +the name of another instruction with a suffix, for example:
+ ++ New = CmpInst::Create(..., SO->getName() + ".cmp"); ++
The Twine class is effectively a +lightweight rope +which points to temporary (stack allocated) objects. Twines can be implicitly +constructed as the result of the plus operator applied to strings (i.e., a C +strings, an std::string, or a StringRef). The twine delays the +actual concatentation of strings until it is actually required, at which point +it can be efficiently rendered directly into a character array. This avoids +unnecessary heap allocation involved in constructing the temporary results of +string concatenation. See +"llvm/ADT/Twine.h" +for more information.
+ +As with a StringRef, Twine objects point to external memory +and should almost never be stored or mentioned directly. They are intended +solely for use when defining a function which should be able to efficiently +accept concatenated strings.
+ +