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