Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:34

0001 //===- llvm/Support/StringSaver.h -------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_SUPPORT_STRINGSAVER_H
0010 #define LLVM_SUPPORT_STRINGSAVER_H
0011 
0012 #include "llvm/ADT/DenseSet.h"
0013 #include "llvm/ADT/StringRef.h"
0014 #include "llvm/ADT/Twine.h"
0015 #include "llvm/Support/Allocator.h"
0016 
0017 namespace llvm {
0018 
0019 /// Saves strings in the provided stable storage and returns a
0020 /// StringRef with a stable character pointer.
0021 class StringSaver final {
0022   BumpPtrAllocator &Alloc;
0023 
0024 public:
0025   StringSaver(BumpPtrAllocator &Alloc) : Alloc(Alloc) {}
0026 
0027   BumpPtrAllocator &getAllocator() const { return Alloc; }
0028 
0029   // All returned strings are null-terminated: *save(S).end() == 0.
0030   StringRef save(const char *S) { return save(StringRef(S)); }
0031   StringRef save(StringRef S);
0032   StringRef save(const Twine &S);
0033   StringRef save(const std::string &S) { return save(StringRef(S)); }
0034 };
0035 
0036 /// Saves strings in the provided stable storage and returns a StringRef with a
0037 /// stable character pointer. Saving the same string yields the same StringRef.
0038 ///
0039 /// Compared to StringSaver, it does more work but avoids saving the same string
0040 /// multiple times.
0041 ///
0042 /// Compared to StringPool, it performs fewer allocations but doesn't support
0043 /// refcounting/deletion.
0044 class UniqueStringSaver final {
0045   StringSaver Strings;
0046   llvm::DenseSet<llvm::StringRef> Unique;
0047 
0048 public:
0049   UniqueStringSaver(BumpPtrAllocator &Alloc) : Strings(Alloc) {}
0050 
0051   // All returned strings are null-terminated: *save(S).end() == 0.
0052   StringRef save(const char *S) { return save(StringRef(S)); }
0053   StringRef save(StringRef S);
0054   StringRef save(const Twine &S);
0055   StringRef save(const std::string &S) { return save(StringRef(S)); }
0056 };
0057 
0058 } // namespace llvm
0059 #endif