Initial update to VMCore to use Twines for string arguments.

- The only meat here is in Value.{h,cpp} the rest is essential 'const
   std::string &' -> 'const Twine &'.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77048 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Daniel Dunbar
2009-07-25 04:41:11 +00:00
parent a66297af30
commit 6e0d1cb309
19 changed files with 392 additions and 276 deletions

View File

@@ -29,6 +29,12 @@
<ul>
<li><a href="#isa">The <tt>isa&lt;&gt;</tt>, <tt>cast&lt;&gt;</tt>
and <tt>dyn_cast&lt;&gt;</tt> templates</a> </li>
<li><a href="#string_apis">Passing strings (the <tt>StringRef</tt>
and <tt>Twine</tt> classes)</li>
<ul>
<li><a href="#StringRef">The <tt>StringRef</tt> class</a> </li>
<li><a href="#Twine">The <tt>Twine</tt> class</a> </li>
</ul>
<li><a href="#DEBUG">The <tt>DEBUG()</tt> macro and <tt>-debug</tt>
option</a>
<ul>
@@ -424,6 +430,106 @@ are lots of examples in the LLVM source base.</p>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection">
<a name="string_apis">Passing strings (the <tt>StringRef</tt>
and <tt>Twine</tt> classes)</a>
</div>
<div class="doc_text">
<p>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.</p>
<p>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 <tt>const char *</tt>, and taking a <tt>const std::string&</tt> requires
clients to perform a heap allocation which is usually unnecessary. Instead,
many LLVM APIs use a <tt>const StringRef&</tt> or a <tt>const Twine&</tt> for
passing strings efficiently.</p>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection">
<a name="StringRef">The <tt>StringRef</tt> class</a>
</div>
<div class="doc_text">
<p>The <tt>StringRef</tt> data type represents a reference to a constant string
(a character array and a length) and supports the common operations available
on <tt>std:string</tt>, but does not require heap allocation.</p>
It can be implicitly constructed using either a C style null-terminated string
or an <tt>std::string</tt>, or explicitly with a character pointer and length.
For example, the <tt>StringRef</tt> find function is declared as:</p>
<div class="doc_code">
iterator find(const StringRef &Key);
</div>
<p>and clients can call it using any one of:</p>
<div class="doc_code">
<pre>
Map.find("foo"); <i>// Lookup "foo"</i>
Map.find(std::string("bar")); <i>// Lookup "bar"</i>
Map.find(StringRef("\0baz", 4)); <i>// Lookup "\0baz"</i>
</pre>
</div>
<p>Similarly, APIs which need to return a string may return a <tt>StringRef</tt>
instance, which can be used directly or converted to an <tt>std::string</tt>
using the <tt>str</tt> member function. See
"<tt><a href="/doxygen/classllvm_1_1StringRef_8h-source.html">llvm/ADT/StringRef.h</a></tt>"
for more information.</p>
<p>You should rarely use the <tt>StringRef</tt> 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).</p>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection">
<a name="Twine">The <tt>Twine</tt> class</a>
</div>
<div class="doc_text">
<p>The <tt>Twine</tt> 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:</p>
<div class="doc_code">
<pre>
New = CmpInst::Create(<i>...</i>, SO->getName() + ".cmp");
</pre>
</div>
<p>The <tt>Twine</tt> class is effectively a
lightweight <a href="http://en.wikipedia.org/wiki/Rope_(computer_science)">rope</a>
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 <tt>std::string</tt>, or a <tt>StringRef</tt>). 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
"<tt><a href="/doxygen/classllvm_1_1Twine_8h-source.html">llvm/ADT/Twine.h</a></tt>"
for more information.</p></tt>
<p>As with a <tt>StringRef</tt>, <tt>Twine</tt> 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.</p>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection">
<a name="DEBUG">The <tt>DEBUG()</tt> macro and <tt>-debug</tt> option</a>

View File

@@ -460,8 +460,14 @@ API changes are:</p>
<li><tt>SCEVHandle</tt> no longer exists, because reference counting is no
longer done for <tt>SCEV*</tt> objects, instead <tt>const SCEV*</tt> should be
used.</li>
<li>Many APIs, notably <tt>llvm::Value</tt>, now use the <tt>StringRef</tt>
and <tt>Twine</tt> classes instead of passing <tt>const char*</tt>
or <tt>std::string</tt>, as described in
the <a href="ProgrammersManual.html#string_apis">Programmer's Manual</a>. Most
clients should be uneffected by this transition.</li>
<li>llvm-dis now fails if output file exists, instead of dumping to stdout.
FIXME: describe any other tool changes due to the raw_fd_ostream change</li>
FIXME: describe any other tool changes due to the raw_fd_ostream change. FIXME:
This is not an API change, maybe there should be a tool changes section?</li>
<li>temporarely due to Context API change passes should call doInitialization()
method of the pass they inherit from, otherwise Context is NULL.
FIXME: remove this entry when this is no longer needed.<li>