comparison include/llvm/Support/StringSaver.h @ 147:c2174574ed3a

LLVM 10
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 16:55:33 +0900
parents 803732b1fca8
children
comparison
equal deleted inserted replaced
134:3a76565eade5 147:c2174574ed3a
1 //===- llvm/Support/StringSaver.h -------------------------------*- C++ -*-===// 1 //===- llvm/Support/StringSaver.h -------------------------------*- C++ -*-===//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // 4 // See https://llvm.org/LICENSE.txt for license information.
5 // This file is distributed under the University of Illinois Open Source 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 // License. See LICENSE.TXT for details.
7 // 6 //
8 //===----------------------------------------------------------------------===// 7 //===----------------------------------------------------------------------===//
9 8
10 #ifndef LLVM_SUPPORT_STRINGSAVER_H 9 #ifndef LLVM_SUPPORT_STRINGSAVER_H
11 #define LLVM_SUPPORT_STRINGSAVER_H 10 #define LLVM_SUPPORT_STRINGSAVER_H
12 11
12 #include "llvm/ADT/DenseSet.h"
13 #include "llvm/ADT/StringRef.h" 13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/ADT/Twine.h" 14 #include "llvm/ADT/Twine.h"
15 #include "llvm/Support/Allocator.h" 15 #include "llvm/Support/Allocator.h"
16 16
17 namespace llvm { 17 namespace llvm {
18 18
19 /// \brief Saves strings in the inheritor's stable storage and returns a 19 /// Saves strings in the provided stable storage and returns a
20 /// StringRef with a stable character pointer. 20 /// StringRef with a stable character pointer.
21 class StringSaver final { 21 class StringSaver final {
22 BumpPtrAllocator &Alloc; 22 BumpPtrAllocator &Alloc;
23 23
24 public: 24 public:
25 StringSaver(BumpPtrAllocator &Alloc) : Alloc(Alloc) {} 25 StringSaver(BumpPtrAllocator &Alloc) : Alloc(Alloc) {}
26
27 // All returned strings are null-terminated: *save(S).end() == 0.
26 StringRef save(const char *S) { return save(StringRef(S)); } 28 StringRef save(const char *S) { return save(StringRef(S)); }
27 StringRef save(StringRef S); 29 StringRef save(StringRef S);
28 StringRef save(const Twine &S) { return save(StringRef(S.str())); } 30 StringRef save(const Twine &S) { return save(StringRef(S.str())); }
29 StringRef save(const std::string &S) { return save(StringRef(S)); } 31 StringRef save(const std::string &S) { return save(StringRef(S)); }
30 }; 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;
44 llvm::DenseSet<llvm::StringRef> Unique;
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)); }
51 StringRef 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
31 } 56 }
32 #endif 57 #endif