LLVM  14.0.0git
StringSaver.h
Go to the documentation of this file.
1 //===- llvm/Support/StringSaver.h -------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_SUPPORT_STRINGSAVER_H
10 #define LLVM_SUPPORT_STRINGSAVER_H
11 
12 #include "llvm/ADT/DenseSet.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/ADT/Twine.h"
15 #include "llvm/Support/Allocator.h"
16 
17 namespace llvm {
18 
19 /// Saves strings in the provided stable storage and returns a
20 /// StringRef with a stable character pointer.
21 class StringSaver final {
22  BumpPtrAllocator &Alloc;
23 
24 public:
25  StringSaver(BumpPtrAllocator &Alloc) : Alloc(Alloc) {}
26 
27  // All returned strings are null-terminated: *save(S).end() == 0.
28  StringRef save(const char *S) { return save(StringRef(S)); }
30  StringRef save(const Twine &S) { return save(StringRef(S.str())); }
31  StringRef save(const std::string &S) { return save(StringRef(S)); }
32 };
33 
34 /// Saves strings in the provided stable storage and returns a StringRef with a
35 /// stable character pointer. Saving the same string yields the same StringRef.
36 ///
37 /// Compared to StringSaver, it does more work but avoids saving the same string
38 /// multiple times.
39 ///
40 /// Compared to StringPool, it performs fewer allocations but doesn't support
41 /// refcounting/deletion.
42 class UniqueStringSaver final {
43  StringSaver Strings;
45 
46 public:
47  UniqueStringSaver(BumpPtrAllocator &Alloc) : Strings(Alloc) {}
48 
49  // All returned strings are null-terminated: *save(S).end() == 0.
50  StringRef save(const char *S) { return save(StringRef(S)); }
52  StringRef save(const Twine &S) { return save(StringRef(S.str())); }
53  StringRef save(const std::string &S) { return save(StringRef(S)); }
54 };
55 
56 }
57 #endif
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
StringRef.h
Allocator.h
llvm::UniqueStringSaver::save
StringRef save(const Twine &S)
Definition: StringSaver.h:52
llvm::UniqueStringSaver
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer.
Definition: StringSaver.h:42
Twine.h
DenseSet.h
llvm::DenseSet< llvm::StringRef >
llvm::StringSaver::StringSaver
StringSaver(BumpPtrAllocator &Alloc)
Definition: StringSaver.h:25
llvm::BumpPtrAllocatorImpl
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:67
llvm::StringSaver::save
StringRef save(const std::string &S)
Definition: StringSaver.h:31
llvm::UniqueStringSaver::save
StringRef save(const char *S)
Definition: StringSaver.h:50
llvm::UniqueStringSaver::UniqueStringSaver
UniqueStringSaver(BumpPtrAllocator &Alloc)
Definition: StringSaver.h:47
llvm::StringSaver
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer.
Definition: StringSaver.h:21
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::UniqueStringSaver::save
StringRef save(const std::string &S)
Definition: StringSaver.h:53
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::StringSaver::save
StringRef save(const Twine &S)
Definition: StringSaver.h:30
llvm::StringSaver::save
StringRef save(const char *S)
Definition: StringSaver.h:28