LLVM 22.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"
17
18namespace llvm {
19
20/// Saves strings in the provided stable storage and returns a
21/// StringRef with a stable character pointer.
22class StringSaver final {
23 BumpPtrAllocator &Alloc;
24
25public:
26 StringSaver(BumpPtrAllocator &Alloc) : Alloc(Alloc) {}
27
28 BumpPtrAllocator &getAllocator() const { return Alloc; }
29
30 // All returned strings are null-terminated: *save(S).end() == 0.
31 StringRef save(const char *S) { return save(StringRef(S)); }
33 LLVM_ABI StringRef save(const Twine &S);
34 StringRef save(const std::string &S) { return save(StringRef(S)); }
35};
36
37/// Saves strings in the provided stable storage and returns a StringRef with a
38/// stable character pointer. Saving the same string yields the same StringRef.
39///
40/// Compared to StringSaver, it does more work but avoids saving the same string
41/// multiple times.
42///
43/// Compared to StringPool, it performs fewer allocations but doesn't support
44/// refcounting/deletion.
45class UniqueStringSaver final {
46 StringSaver Strings;
48
49public:
51
52 // All returned strings are null-terminated: *save(S).end() == 0.
53 StringRef save(const char *S) { return save(StringRef(S)); }
55 LLVM_ABI StringRef save(const Twine &S);
56 StringRef save(const std::string &S) { return save(StringRef(S)); }
57};
58
59} // namespace llvm
60#endif
This file defines the BumpPtrAllocator interface.
#define LLVM_ABI
Definition: Compiler.h:213
This file defines the DenseSet and SmallDenseSet classes.
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:67
Implements a dense probed hash-table based set.
Definition: DenseSet.h:263
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer.
Definition: StringSaver.h:22
BumpPtrAllocator & getAllocator() const
Definition: StringSaver.h:28
StringSaver(BumpPtrAllocator &Alloc)
Definition: StringSaver.h:26
StringRef save(const char *S)
Definition: StringSaver.h:31
StringRef save(const std::string &S)
Definition: StringSaver.h:34
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:82
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer.
Definition: StringSaver.h:45
UniqueStringSaver(BumpPtrAllocator &Alloc)
Definition: StringSaver.h:50
StringRef save(const std::string &S)
Definition: StringSaver.h:56
StringRef save(const char *S)
Definition: StringSaver.h:53
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18