/home/arjun/llvm-project/llvm/lib/Support/StringSaver.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- StringSaver.cpp ---------------------------------------------------===// |
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 | | #include "llvm/Support/StringSaver.h" |
10 | | |
11 | | using namespace llvm; |
12 | | |
13 | 0 | StringRef StringSaver::save(StringRef S) { |
14 | 0 | char *P = Alloc.Allocate<char>(S.size() + 1); |
15 | 0 | if (!S.empty()) |
16 | 0 | memcpy(P, S.data(), S.size()); |
17 | 0 | P[S.size()] = '\0'; |
18 | 0 | return StringRef(P, S.size()); |
19 | 0 | } |
20 | | |
21 | 0 | StringRef UniqueStringSaver::save(StringRef S) { |
22 | 0 | auto R = Unique.insert(S); |
23 | 0 | if (R.second) // cache miss, need to actually save the string |
24 | 0 | *R.first = Strings.save(S); // safe replacement with equal value |
25 | 0 | return *R.first; |
26 | 0 | } |