LLDB mainline
Identifier.cpp
Go to the documentation of this file.
1//===-- Identifier.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 "Identifier.h"
10
11#include "llvm/Support/Compiler.h"
12#include "llvm/Support/ErrorHandling.h"
13
14#include <cstring>
15
16using namespace lldb_private;
17using namespace lldb_private::clike;
18
19Identifier IdentifierMap::get(llvm::StringRef name) {
20 auto it = m_names.find(name);
21 if (it != m_names.end())
22 return Identifier(*it);
23
24 // Copy the string into our own storage.
25 // TODO: Would it be beneficial to add a null terminator into the buffer?
26 char *storage = m_string_storage.Allocate<char>(name.size());
27 std::memcpy(storage, name.data(), name.size());
28 llvm::StringRef owned(storage, name.size());
29 m_names.insert(owned);
30 return Identifier(owned);
31}
32
34 // The caller promises the backing storage outlives this map.
35 m_names.insert(name);
36 return Identifier(name);
37}
38
40#if LLVM_ADDRESS_SANITIZER_BUILD
41 // Verify every identifier still points at live memory.
42 for (llvm::StringRef name : m_names) {
43 if (!name.empty() &&
44 __asan_region_is_poisoned(const_cast<char *>(name.data()), name.size()))
45 llvm::report_fatal_error("IdentifierMap holds a freed string "
46 "(misuse of getWithStaticStorageStr?)");
47 }
48#endif
49}
llvm::DenseSet< llvm::StringRef > m_names
Set of all created Identifiers strings.
Definition Identifier.h:59
Identifier get(llvm::StringRef name)
Returns an Identifier for name.
llvm::BumpPtrAllocator m_string_storage
Owns the copies made by get().
Definition Identifier.h:57
Identifier getWithStaticStorageStr(llvm::StringRef name)
Returns an Identifier for name.
Represents a name in TypeSystemClike.
Definition Identifier.h:24
A class that represents a running process on the host machine.