LLVM  8.0.1
StringSaver.h
Go to the documentation of this file.
1 //===- llvm/Support/StringSaver.h -------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_SUPPORT_STRINGSAVER_H
11 #define LLVM_SUPPORT_STRINGSAVER_H
12 
13 #include "llvm/ADT/DenseSet.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/Support/Allocator.h"
17 
18 namespace llvm {
19 
20 /// Saves strings in the provided stable storage and returns a
21 /// StringRef with a stable character pointer.
22 class StringSaver final {
23  BumpPtrAllocator &Alloc;
24 
25 public:
26  StringSaver(BumpPtrAllocator &Alloc) : Alloc(Alloc) {}
27 
28  // All returned strings are null-terminated: *save(S).end() == 0.
29  StringRef save(const char *S) { return save(StringRef(S)); }
31  StringRef save(const Twine &S) { return save(StringRef(S.str())); }
32  StringRef save(const std::string &S) { return save(StringRef(S)); }
33 };
34 
35 /// Saves strings in the provided stable storage and returns a StringRef with a
36 /// stable character pointer. Saving the same string yields the same StringRef.
37 ///
38 /// Compared to StringSaver, it does more work but avoids saving the same string
39 /// multiple times.
40 ///
41 /// Compared to StringPool, it performs fewer allocations but doesn't support
42 /// refcounting/deletion.
43 class UniqueStringSaver final {
44  StringSaver Strings;
46 
47 public:
48  UniqueStringSaver(BumpPtrAllocator &Alloc) : Strings(Alloc) {}
49 
50  // All returned strings are null-terminated: *save(S).end() == 0.
51  StringRef save(const char *S) { return save(StringRef(S)); }
53  StringRef save(const Twine &S) { return save(StringRef(S.str())); }
54  StringRef save(const std::string &S) { return save(StringRef(S)); }
55 };
56 
57 }
58 #endif
This class represents lattice values for constants.
Definition: AllocatorList.h:24
UniqueStringSaver(BumpPtrAllocator &Alloc)
Definition: StringSaver.h:48
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
StringSaver(BumpPtrAllocator &Alloc)
Definition: StringSaver.h:26
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer...
Definition: StringSaver.h:43
StringRef save(const char *S)
Definition: StringSaver.h:51
StringRef save(const Twine &S)
Definition: StringSaver.h:31
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:141
StringRef save(const std::string &S)
Definition: StringSaver.h:54
StringRef save(const char *S)
Definition: StringSaver.h:29
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer...
Definition: StringSaver.h:22
StringRef save(const Twine &S)
Definition: StringSaver.h:53
std::string str() const
Return the twine contents as a std::string.
Definition: Twine.cpp:18
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
StringRef save(const std::string &S)
Definition: StringSaver.h:32