[lldb][NFC] Fix all formatting errors in .cpp file headers
Summary:
A *.cpp file header in LLDB (and in LLDB) should like this:
```
//===-- TestUtilities.cpp -------------------------------------------------===//
```
However in LLDB most of our source files have arbitrary changes to this format and
these changes are spreading through LLDB as folks usually just use the existing
source files as templates for their new files (most notably the unnecessary
editor language indicator `-*- C++ -*-` is spreading and in every review
someone is pointing out that this is wrong, resulting in people pointing out that this
is done in the same way in other files).
This patch removes most of these inconsistencies including the editor language indicators,
all the different missing/additional '-' characters, files that center the file name, missing
trailing `===//` (mostly caused by clang-format breaking the line).
Reviewers: aprantl, espindola, jfb, shafik, JDevlieghere
Reviewed By: JDevlieghere
Subscribers: dexonsmith, wuzish, emaste, sdardis, nemanjai, kbarton, MaskRay, atanasyan, arphaman, jfb, abidh, jsji, JDevlieghere, usaxena95, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D73258
2020-01-24 15:23:27 +08:00
|
|
|
//===-- ConstString.cpp ---------------------------------------------------===//
|
2010-06-09 00:52:24 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-06-09 00:52:24 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2016-03-11 07:57:12 +08:00
|
|
|
|
2017-02-03 05:39:50 +08:00
|
|
|
#include "lldb/Utility/ConstString.h"
|
2016-03-11 07:57:12 +08:00
|
|
|
|
2017-04-07 02:12:24 +08:00
|
|
|
#include "lldb/Utility/Stream.h"
|
2016-03-11 07:57:12 +08:00
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
2018-11-12 07:16:43 +08:00
|
|
|
#include "llvm/ADT/iterator.h"
|
|
|
|
#include "llvm/Support/Allocator.h"
|
|
|
|
#include "llvm/Support/DJB.h"
|
|
|
|
#include "llvm/Support/FormatProviders.h"
|
2015-10-14 18:38:22 +08:00
|
|
|
#include "llvm/Support/RWMutex.h"
|
2017-02-07 01:55:02 +08:00
|
|
|
#include "llvm/Support/Threading.h"
|
2017-04-07 02:12:24 +08:00
|
|
|
|
|
|
|
#include <array>
|
2018-11-12 07:16:43 +08:00
|
|
|
#include <utility>
|
2017-04-07 02:12:24 +08:00
|
|
|
|
2018-11-12 07:16:43 +08:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
2014-12-16 10:34:13 +08:00
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
using namespace lldb_private;
|
|
|
|
|
|
|
|
class Pool {
|
|
|
|
public:
|
2020-02-03 21:22:24 +08:00
|
|
|
/// The default BumpPtrAllocatorImpl slab size.
|
|
|
|
static const size_t AllocatorSlabSize = 4096;
|
|
|
|
static const size_t SizeThreshold = AllocatorSlabSize;
|
|
|
|
/// Every Pool has its own allocator which receives an equal share of
|
|
|
|
/// the ConstString allocations. This means that when allocating many
|
|
|
|
/// ConstStrings, every allocator sees only its small share of allocations and
|
|
|
|
/// assumes LLDB only allocated a small amount of memory so far. In reality
|
|
|
|
/// LLDB allocated a total memory that is N times as large as what the
|
|
|
|
/// allocator sees (where N is the number of string pools). This causes that
|
|
|
|
/// the BumpPtrAllocator continues a long time to allocate memory in small
|
|
|
|
/// chunks which only makes sense when allocating a small amount of memory
|
|
|
|
/// (which is true from the perspective of a single allocator). On some
|
|
|
|
/// systems doing all these small memory allocations causes LLDB to spend
|
|
|
|
/// a lot of time in malloc, so we need to force all these allocators to
|
|
|
|
/// behave like one allocator in terms of scaling their memory allocations
|
|
|
|
/// with increased demand. To do this we set the growth delay for each single
|
|
|
|
/// allocator to a rate so that our pool of allocators scales their memory
|
|
|
|
/// allocations similar to a single BumpPtrAllocatorImpl.
|
|
|
|
///
|
|
|
|
/// Currently we have 256 string pools and the normal growth delay of the
|
|
|
|
/// BumpPtrAllocatorImpl is 128 (i.e., the memory allocation size increases
|
|
|
|
/// every 128 full chunks), so by changing the delay to 1 we get a
|
|
|
|
/// total growth delay in our allocator collection of 256/1 = 256. This is
|
|
|
|
/// still only half as fast as a normal allocator but we can't go any faster
|
|
|
|
/// without decreasing the number of string pools.
|
|
|
|
static const size_t AllocatorGrowthDelay = 1;
|
|
|
|
typedef llvm::BumpPtrAllocatorImpl<llvm::MallocAllocator, AllocatorSlabSize,
|
|
|
|
SizeThreshold, AllocatorGrowthDelay>
|
|
|
|
Allocator;
|
2011-06-10 06:34:34 +08:00
|
|
|
typedef const char *StringPoolValueType;
|
2020-02-03 21:22:24 +08:00
|
|
|
typedef llvm::StringMap<StringPoolValueType, Allocator> StringPool;
|
2011-06-10 06:34:34 +08:00
|
|
|
typedef llvm::StringMapEntry<StringPoolValueType> StringPoolEntryType;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2011-06-10 06:34:34 +08:00
|
|
|
static StringPoolEntryType &
|
2010-06-09 00:52:24 +08:00
|
|
|
GetStringMapEntryFromKeyData(const char *keyData) {
|
2017-04-28 20:08:28 +08:00
|
|
|
return StringPoolEntryType::GetStringMapEntryFromKeyData(keyData);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2017-04-28 20:08:28 +08:00
|
|
|
static size_t GetConstCStringLength(const char *ccstr) {
|
2016-03-11 07:57:12 +08:00
|
|
|
if (ccstr != nullptr) {
|
2018-05-01 00:49:04 +08:00
|
|
|
// Since the entry is read only, and we derive the entry entirely from
|
|
|
|
// the pointer, we don't need the lock.
|
2015-10-22 19:14:31 +08:00
|
|
|
const StringPoolEntryType &entry = GetStringMapEntryFromKeyData(ccstr);
|
2010-06-09 00:52:24 +08:00
|
|
|
return entry.getKey().size();
|
|
|
|
}
|
2011-06-10 06:34:34 +08:00
|
|
|
return 0;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2011-06-10 06:34:34 +08:00
|
|
|
StringPoolValueType GetMangledCounterpart(const char *ccstr) const {
|
2016-03-11 07:57:12 +08:00
|
|
|
if (ccstr != nullptr) {
|
2015-10-22 19:14:31 +08:00
|
|
|
const uint8_t h = hash(llvm::StringRef(ccstr));
|
|
|
|
llvm::sys::SmartScopedReader<false> rlock(m_string_pools[h].m_mutex);
|
2011-06-10 06:34:34 +08:00
|
|
|
return GetStringMapEntryFromKeyData(ccstr).getValue();
|
|
|
|
}
|
2016-03-11 07:57:12 +08:00
|
|
|
return nullptr;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
const char *GetConstCString(const char *cstr) {
|
2016-03-11 07:57:12 +08:00
|
|
|
if (cstr != nullptr)
|
2010-06-22 23:28:34 +08:00
|
|
|
return GetConstCStringWithLength(cstr, strlen(cstr));
|
2015-10-14 18:38:22 +08:00
|
|
|
return nullptr;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2013-01-26 02:06:21 +08:00
|
|
|
const char *GetConstCStringWithLength(const char *cstr, size_t cstr_len) {
|
2016-03-11 07:57:12 +08:00
|
|
|
if (cstr != nullptr)
|
2015-10-14 18:38:22 +08:00
|
|
|
return GetConstCStringWithStringRef(llvm::StringRef(cstr, cstr_len));
|
|
|
|
return nullptr;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
<rdar://problem/11757916>
Make breakpoint setting by file and line much more efficient by only looking for inlined breakpoint locations if we are setting a breakpoint in anything but a source implementation file. Implementing this complex for a many reasons. Turns out that parsing compile units lazily had some issues with respect to how we need to do things with DWARF in .o files. So the fixes in the checkin for this makes these changes:
- Add a new setting called "target.inline-breakpoint-strategy" which can be set to "never", "always", or "headers". "never" will never try and set any inlined breakpoints (fastest). "always" always looks for inlined breakpoint locations (slowest, but most accurate). "headers", which is the default setting, will only look for inlined breakpoint locations if the breakpoint is set in what are consudered to be header files, which is realy defined as "not in an implementation source file".
- modify the breakpoint setting by file and line to check the current "target.inline-breakpoint-strategy" setting and act accordingly
- Modify compile units to be able to get their language and other info lazily. This allows us to create compile units from the debug map and not have to fill all of the details in, and then lazily discover this information as we go on debuggging. This is needed to avoid parsing all .o files when setting breakpoints in implementation only files (no inlines). Otherwise we would need to parse the .o file, the object file (mach-o in our case) and the symbol file (DWARF in the object file) just to see what the compile unit was.
- modify the "SymbolFileDWARFDebugMap" to subclass lldb_private::Module so that the virtual "GetObjectFile()" and "GetSymbolVendor()" functions can be intercepted when the .o file contenst are later lazilly needed. Prior to this fix, when we first instantiated the "SymbolFileDWARFDebugMap" class, we would also make modules, object files and symbol files for every .o file in the debug map because we needed to fix up the sections in the .o files with information that is in the executable debug map. Now we lazily do this in the DebugMapModule::GetObjectFile()
Cleaned up header includes a bit as well.
llvm-svn: 162860
2012-08-30 05:13:06 +08:00
|
|
|
const char *GetConstCStringWithStringRef(const llvm::StringRef &string_ref) {
|
|
|
|
if (string_ref.data()) {
|
2015-10-22 19:14:31 +08:00
|
|
|
const uint8_t h = hash(string_ref);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
{
|
2015-10-14 18:38:22 +08:00
|
|
|
llvm::sys::SmartScopedReader<false> rlock(m_string_pools[h].m_mutex);
|
|
|
|
auto it = m_string_pools[h].m_string_map.find(string_ref);
|
|
|
|
if (it != m_string_pools[h].m_string_map.end())
|
|
|
|
return it->getKeyData();
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2015-10-14 18:38:22 +08:00
|
|
|
llvm::sys::SmartScopedWriter<false> wlock(m_string_pools[h].m_mutex);
|
|
|
|
StringPoolEntryType &entry =
|
|
|
|
*m_string_pools[h]
|
|
|
|
.m_string_map.insert(std::make_pair(string_ref, nullptr))
|
2016-09-07 04:57:50 +08:00
|
|
|
.first;
|
2010-07-10 04:39:50 +08:00
|
|
|
return entry.getKeyData();
|
<rdar://problem/11757916>
Make breakpoint setting by file and line much more efficient by only looking for inlined breakpoint locations if we are setting a breakpoint in anything but a source implementation file. Implementing this complex for a many reasons. Turns out that parsing compile units lazily had some issues with respect to how we need to do things with DWARF in .o files. So the fixes in the checkin for this makes these changes:
- Add a new setting called "target.inline-breakpoint-strategy" which can be set to "never", "always", or "headers". "never" will never try and set any inlined breakpoints (fastest). "always" always looks for inlined breakpoint locations (slowest, but most accurate). "headers", which is the default setting, will only look for inlined breakpoint locations if the breakpoint is set in what are consudered to be header files, which is realy defined as "not in an implementation source file".
- modify the breakpoint setting by file and line to check the current "target.inline-breakpoint-strategy" setting and act accordingly
- Modify compile units to be able to get their language and other info lazily. This allows us to create compile units from the debug map and not have to fill all of the details in, and then lazily discover this information as we go on debuggging. This is needed to avoid parsing all .o files when setting breakpoints in implementation only files (no inlines). Otherwise we would need to parse the .o file, the object file (mach-o in our case) and the symbol file (DWARF in the object file) just to see what the compile unit was.
- modify the "SymbolFileDWARFDebugMap" to subclass lldb_private::Module so that the virtual "GetObjectFile()" and "GetSymbolVendor()" functions can be intercepted when the .o file contenst are later lazilly needed. Prior to this fix, when we first instantiated the "SymbolFileDWARFDebugMap" class, we would also make modules, object files and symbol files for every .o file in the debug map because we needed to fix up the sections in the .o files with information that is in the executable debug map. Now we lazily do this in the DebugMapModule::GetObjectFile()
Cleaned up header includes a bit as well.
llvm-svn: 162860
2012-08-30 05:13:06 +08:00
|
|
|
}
|
2016-03-11 07:57:12 +08:00
|
|
|
return nullptr;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
<rdar://problem/11757916>
Make breakpoint setting by file and line much more efficient by only looking for inlined breakpoint locations if we are setting a breakpoint in anything but a source implementation file. Implementing this complex for a many reasons. Turns out that parsing compile units lazily had some issues with respect to how we need to do things with DWARF in .o files. So the fixes in the checkin for this makes these changes:
- Add a new setting called "target.inline-breakpoint-strategy" which can be set to "never", "always", or "headers". "never" will never try and set any inlined breakpoints (fastest). "always" always looks for inlined breakpoint locations (slowest, but most accurate). "headers", which is the default setting, will only look for inlined breakpoint locations if the breakpoint is set in what are consudered to be header files, which is realy defined as "not in an implementation source file".
- modify the breakpoint setting by file and line to check the current "target.inline-breakpoint-strategy" setting and act accordingly
- Modify compile units to be able to get their language and other info lazily. This allows us to create compile units from the debug map and not have to fill all of the details in, and then lazily discover this information as we go on debuggging. This is needed to avoid parsing all .o files when setting breakpoints in implementation only files (no inlines). Otherwise we would need to parse the .o file, the object file (mach-o in our case) and the symbol file (DWARF in the object file) just to see what the compile unit was.
- modify the "SymbolFileDWARFDebugMap" to subclass lldb_private::Module so that the virtual "GetObjectFile()" and "GetSymbolVendor()" functions can be intercepted when the .o file contenst are later lazilly needed. Prior to this fix, when we first instantiated the "SymbolFileDWARFDebugMap" class, we would also make modules, object files and symbol files for every .o file in the debug map because we needed to fix up the sections in the .o files with information that is in the executable debug map. Now we lazily do this in the DebugMapModule::GetObjectFile()
Cleaned up header includes a bit as well.
llvm-svn: 162860
2012-08-30 05:13:06 +08:00
|
|
|
const char *
|
2018-08-06 16:27:59 +08:00
|
|
|
GetConstCStringAndSetMangledCounterPart(llvm::StringRef demangled,
|
2011-06-10 06:34:34 +08:00
|
|
|
const char *mangled_ccstr) {
|
2018-08-06 16:27:59 +08:00
|
|
|
const char *demangled_ccstr = nullptr;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2018-08-06 16:27:59 +08:00
|
|
|
{
|
|
|
|
const uint8_t h = hash(demangled);
|
|
|
|
llvm::sys::SmartScopedWriter<false> wlock(m_string_pools[h].m_mutex);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2018-08-14 19:07:18 +08:00
|
|
|
// Make or update string pool entry with the mangled counterpart
|
|
|
|
StringPool &map = m_string_pools[h].m_string_map;
|
|
|
|
StringPoolEntryType &entry = *map.try_emplace(demangled).first;
|
|
|
|
|
|
|
|
entry.second = mangled_ccstr;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2018-08-06 16:27:59 +08:00
|
|
|
// Extract the const version of the demangled_cstr
|
|
|
|
demangled_ccstr = entry.getKeyData();
|
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2018-08-06 16:27:59 +08:00
|
|
|
{
|
|
|
|
// Now assign the demangled const string as the counterpart of the
|
|
|
|
// mangled const string...
|
|
|
|
const uint8_t h = hash(llvm::StringRef(mangled_ccstr));
|
|
|
|
llvm::sys::SmartScopedWriter<false> wlock(m_string_pools[h].m_mutex);
|
|
|
|
GetStringMapEntryFromKeyData(mangled_ccstr).setValue(demangled_ccstr);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
2018-08-06 16:27:59 +08:00
|
|
|
|
|
|
|
// Return the constant demangled C string
|
|
|
|
return demangled_ccstr;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2015-10-22 19:14:31 +08:00
|
|
|
const char *GetConstTrimmedCStringWithLength(const char *cstr,
|
|
|
|
size_t cstr_len) {
|
|
|
|
if (cstr != nullptr) {
|
Fix strlen() of unbound array undefined behavior
LLDB testsuite fails when built by GCC8 on:
LLDB :: SymbolFile/DWARF/find-basic-namespace.cpp
This is because this code in LLDB codebase has undefined behavior:
#include <algorithm>
#include <string.h>
// lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp:1731
static struct section_64 {
char sectname[16];
char segname[16];
} sect64 = { {'_','_','a','p','p','l','e','_','n','a','m','e','s','p','a','c'}, "__DWARF" };
int main() {
return std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname));
}
It has been discussed as a (false) bugreport to GCC:
wrong-code: LLDB testcase fails: SymbolFile/DWARF/find-basic-namespace.cpp
https://bugzilla.redhat.com/show_bug.cgi?id=1672436
Differential Revision: https://reviews.llvm.org/D57781
llvm-svn: 353280
2019-02-06 16:44:13 +08:00
|
|
|
const size_t trimmed_len = strnlen(cstr, cstr_len);
|
2015-10-22 19:14:31 +08:00
|
|
|
return GetConstCStringWithLength(cstr, trimmed_len);
|
2011-06-10 06:34:34 +08:00
|
|
|
}
|
2015-10-14 18:38:22 +08:00
|
|
|
return nullptr;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// Return the size in bytes that this object and any items in its collection
|
|
|
|
// of uniqued strings + data count values takes in memory.
|
2010-06-09 00:52:24 +08:00
|
|
|
size_t MemorySize() const {
|
|
|
|
size_t mem_size = sizeof(Pool);
|
2015-10-14 18:38:22 +08:00
|
|
|
for (const auto &pool : m_string_pools) {
|
|
|
|
llvm::sys::SmartScopedReader<false> rlock(pool.m_mutex);
|
|
|
|
for (const auto &entry : pool.m_string_map)
|
|
|
|
mem_size += sizeof(StringPoolEntryType) + entry.getKey().size();
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
return mem_size;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
protected:
|
2015-10-22 19:14:31 +08:00
|
|
|
uint8_t hash(const llvm::StringRef &s) const {
|
2018-02-26 23:16:42 +08:00
|
|
|
uint32_t h = llvm::djbHash(s);
|
2015-10-14 18:38:22 +08:00
|
|
|
return ((h >> 24) ^ (h >> 16) ^ (h >> 8) ^ h) & 0xff;
|
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2015-10-14 18:38:22 +08:00
|
|
|
struct PoolEntry {
|
|
|
|
mutable llvm::sys::SmartRWMutex<false> m_mutex;
|
2019-10-17 08:02:32 +08:00
|
|
|
StringPool m_string_map;
|
2015-10-14 18:38:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
std::array<PoolEntry, 256> m_string_pools;
|
2010-06-09 00:52:24 +08:00
|
|
|
};
|
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// Frameworks and dylibs aren't supposed to have global C++ initializers so we
|
|
|
|
// hide the string pool in a static function so that it will get initialized on
|
|
|
|
// the first call to this static function.
|
2012-05-10 02:37:10 +08:00
|
|
|
//
|
2018-05-01 00:49:04 +08:00
|
|
|
// Note, for now we make the string pool a pointer to the pool, because we
|
|
|
|
// can't guarantee that some objects won't get destroyed after the global
|
|
|
|
// destructor chain is run, and trying to make sure no destructors touch
|
|
|
|
// ConstStrings is difficult. So we leak the pool instead.
|
2010-06-09 00:52:24 +08:00
|
|
|
static Pool &StringPool() {
|
2017-02-07 01:55:02 +08:00
|
|
|
static llvm::once_flag g_pool_initialization_flag;
|
2015-10-14 18:38:22 +08:00
|
|
|
static Pool *g_string_pool = nullptr;
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2017-02-07 01:55:02 +08:00
|
|
|
llvm::call_once(g_pool_initialization_flag,
|
2010-06-09 00:52:24 +08:00
|
|
|
[]() { g_string_pool = new Pool(); });
|
|
|
|
|
|
|
|
return *g_string_pool;
|
|
|
|
}
|
|
|
|
|
2012-08-23 01:17:09 +08:00
|
|
|
ConstString::ConstString(const char *cstr)
|
|
|
|
: m_string(StringPool().GetConstCString(cstr)) {}
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
ConstString::ConstString(const char *cstr, size_t cstr_len)
|
|
|
|
: m_string(StringPool().GetConstCStringWithLength(cstr, cstr_len)) {}
|
|
|
|
|
|
|
|
ConstString::ConstString(const llvm::StringRef &s)
|
2019-04-14 22:01:49 +08:00
|
|
|
: m_string(StringPool().GetConstCStringWithStringRef(s)) {}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2019-03-07 05:22:25 +08:00
|
|
|
bool ConstString::operator<(ConstString rhs) const {
|
2010-06-09 00:52:24 +08:00
|
|
|
if (m_string == rhs.m_string)
|
|
|
|
return false;
|
|
|
|
|
2017-04-28 20:08:28 +08:00
|
|
|
llvm::StringRef lhs_string_ref(GetStringRef());
|
|
|
|
llvm::StringRef rhs_string_ref(rhs.GetStringRef());
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
// If both have valid C strings, then return the comparison
|
|
|
|
if (lhs_string_ref.data() && rhs_string_ref.data())
|
2016-02-25 05:26:47 +08:00
|
|
|
return lhs_string_ref < rhs_string_ref;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-14 18:38:22 +08:00
|
|
|
// Else one of them was nullptr, so if LHS is nullptr then it is less than
|
|
|
|
return lhs_string_ref.data() == nullptr;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2019-03-07 05:22:25 +08:00
|
|
|
Stream &lldb_private::operator<<(Stream &s, ConstString str) {
|
2010-06-09 00:52:24 +08:00
|
|
|
const char *cstr = str.GetCString();
|
2016-03-11 07:57:12 +08:00
|
|
|
if (cstr != nullptr)
|
2010-06-09 00:52:24 +08:00
|
|
|
s << cstr;
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ConstString::GetLength() const {
|
2017-04-28 20:08:28 +08:00
|
|
|
return Pool::GetConstCStringLength(m_string);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2019-03-07 05:22:25 +08:00
|
|
|
bool ConstString::Equals(ConstString lhs, ConstString rhs,
|
2016-02-25 05:26:47 +08:00
|
|
|
const bool case_sensitive) {
|
|
|
|
if (lhs.m_string == rhs.m_string)
|
|
|
|
return true;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2016-02-25 05:26:47 +08:00
|
|
|
// Since the pointers weren't equal, and identical ConstStrings always have
|
2018-05-01 00:49:04 +08:00
|
|
|
// identical pointers, the result must be false for case sensitive equality
|
|
|
|
// test.
|
2016-02-25 05:26:47 +08:00
|
|
|
if (case_sensitive)
|
|
|
|
return false;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2016-02-25 05:26:47 +08:00
|
|
|
// perform case insensitive equality test
|
2017-04-28 20:08:28 +08:00
|
|
|
llvm::StringRef lhs_string_ref(lhs.GetStringRef());
|
|
|
|
llvm::StringRef rhs_string_ref(rhs.GetStringRef());
|
2016-02-25 05:26:47 +08:00
|
|
|
return lhs_string_ref.equals_lower(rhs_string_ref);
|
|
|
|
}
|
|
|
|
|
2019-03-07 05:22:25 +08:00
|
|
|
int ConstString::Compare(ConstString lhs, ConstString rhs,
|
2016-02-25 05:26:47 +08:00
|
|
|
const bool case_sensitive) {
|
2010-06-09 00:52:24 +08:00
|
|
|
// If the iterators are the same, this is the same string
|
2013-08-31 01:50:57 +08:00
|
|
|
const char *lhs_cstr = lhs.m_string;
|
|
|
|
const char *rhs_cstr = rhs.m_string;
|
2010-06-09 00:52:24 +08:00
|
|
|
if (lhs_cstr == rhs_cstr)
|
|
|
|
return 0;
|
|
|
|
if (lhs_cstr && rhs_cstr) {
|
2017-04-28 20:08:28 +08:00
|
|
|
llvm::StringRef lhs_string_ref(lhs.GetStringRef());
|
|
|
|
llvm::StringRef rhs_string_ref(rhs.GetStringRef());
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2016-02-25 05:26:47 +08:00
|
|
|
if (case_sensitive) {
|
|
|
|
return lhs_string_ref.compare(rhs_string_ref);
|
|
|
|
} else {
|
|
|
|
return lhs_string_ref.compare_lower(rhs_string_ref);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
if (lhs_cstr)
|
2016-03-11 07:57:12 +08:00
|
|
|
return +1; // LHS isn't nullptr but RHS is
|
2010-06-09 00:52:24 +08:00
|
|
|
else
|
2016-03-11 07:57:12 +08:00
|
|
|
return -1; // LHS is nullptr but RHS isn't
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConstString::Dump(Stream *s, const char *fail_value) const {
|
2016-03-11 07:57:12 +08:00
|
|
|
if (s != nullptr) {
|
2012-11-03 08:09:46 +08:00
|
|
|
const char *cstr = AsCString(fail_value);
|
2016-03-11 07:57:12 +08:00
|
|
|
if (cstr != nullptr)
|
2012-11-03 08:09:46 +08:00
|
|
|
s->PutCString(cstr);
|
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConstString::DumpDebug(Stream *s) const {
|
|
|
|
const char *cstr = GetCString();
|
|
|
|
size_t cstr_len = GetLength();
|
2016-03-11 07:57:12 +08:00
|
|
|
// Only print the parens if we have a non-nullptr string
|
2010-06-09 00:52:24 +08:00
|
|
|
const char *parens = cstr ? "\"" : "";
|
2014-04-04 12:06:10 +08:00
|
|
|
s->Printf("%*p: ConstString, string = %s%s%s, length = %" PRIu64,
|
|
|
|
static_cast<int>(sizeof(void *) * 2),
|
|
|
|
static_cast<const void *>(this), parens, cstr, parens,
|
|
|
|
static_cast<uint64_t>(cstr_len));
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConstString::SetCString(const char *cstr) {
|
|
|
|
m_string = StringPool().GetConstCString(cstr);
|
|
|
|
}
|
|
|
|
|
2012-08-23 01:17:09 +08:00
|
|
|
void ConstString::SetString(const llvm::StringRef &s) {
|
|
|
|
m_string = StringPool().GetConstCStringWithLength(s.data(), s.size());
|
|
|
|
}
|
|
|
|
|
2018-08-06 16:27:59 +08:00
|
|
|
void ConstString::SetStringWithMangledCounterpart(llvm::StringRef demangled,
|
2019-03-07 05:22:25 +08:00
|
|
|
ConstString mangled) {
|
2011-06-10 06:34:34 +08:00
|
|
|
m_string = StringPool().GetConstCStringAndSetMangledCounterPart(
|
|
|
|
demangled, mangled.m_string);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConstString::GetMangledCounterpart(ConstString &counterpart) const {
|
|
|
|
counterpart.m_string = StringPool().GetMangledCounterpart(m_string);
|
2013-10-04 06:27:29 +08:00
|
|
|
return (bool)counterpart;
|
2011-06-10 06:34:34 +08:00
|
|
|
}
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
void ConstString::SetCStringWithLength(const char *cstr, size_t cstr_len) {
|
|
|
|
m_string = StringPool().GetConstCStringWithLength(cstr, cstr_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConstString::SetTrimmedCStringWithLength(const char *cstr,
|
|
|
|
size_t cstr_len) {
|
|
|
|
m_string = StringPool().GetConstTrimmedCStringWithLength(cstr, cstr_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ConstString::StaticMemorySize() {
|
|
|
|
// Get the size of the static string pool
|
|
|
|
return StringPool().MemorySize();
|
|
|
|
}
|
2017-02-05 08:44:54 +08:00
|
|
|
|
|
|
|
void llvm::format_provider<ConstString>::format(const ConstString &CS,
|
|
|
|
llvm::raw_ostream &OS,
|
|
|
|
llvm::StringRef Options) {
|
2020-02-11 16:05:37 +08:00
|
|
|
format_provider<StringRef>::format(CS.GetStringRef(), OS, Options);
|
2017-02-05 08:44:54 +08:00
|
|
|
}
|