[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 08:23:27 +01:00
|
|
|
//===-- PathMappingList.cpp -----------------------------------------------===//
|
2010-06-08 16:52:24 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00: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-08 16:52:24 +00:00
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2016-02-18 00:10:17 +00:00
|
|
|
#include <climits>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
2018-11-01 17:09:25 +00:00
|
|
|
#include "lldb/Host/FileSystem.h"
|
2016-08-09 23:06:08 +00:00
|
|
|
#include "lldb/Host/PosixApi.h"
|
2010-06-09 09:32:42 +00:00
|
|
|
#include "lldb/Target/PathMappingList.h"
|
2017-03-22 18:40:07 +00:00
|
|
|
#include "lldb/Utility/FileSpec.h"
|
2017-05-12 04:51:55 +00:00
|
|
|
#include "lldb/Utility/Status.h"
|
2017-02-02 21:39:50 +00:00
|
|
|
#include "lldb/Utility/Stream.h"
|
2018-11-01 17:09:25 +00:00
|
|
|
#include "lldb/lldb-private-enumerations.h"
|
2010-06-08 16:52:24 +00:00
|
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
|
2018-05-21 14:14:36 +00:00
|
|
|
namespace {
|
|
|
|
|
// We must normalize our path pairs that we store because if we don't then
|
|
|
|
|
// things won't always work. We found a case where if we did:
|
|
|
|
|
// (lldb) settings set target.source-map . /tmp
|
|
|
|
|
// We would store a path pairs of "." and "/tmp" as raw strings. If the debug
|
|
|
|
|
// info contains "./foo/bar.c", the path will get normalized to "foo/bar.c".
|
|
|
|
|
// When PathMappingList::RemapPath() is called, it expects the path to start
|
|
|
|
|
// with the raw path pair, which doesn't work anymore because the paths have
|
|
|
|
|
// been normalized when the debug info was loaded. So we need to store
|
|
|
|
|
// nomalized path pairs to ensure things match up.
|
2019-03-06 21:22:25 +00:00
|
|
|
ConstString NormalizePath(ConstString path) {
|
2018-05-21 14:14:36 +00:00
|
|
|
// If we use "path" to construct a FileSpec, it will normalize the path for
|
|
|
|
|
// us. We then grab the string and turn it back into a ConstString.
|
2018-11-01 21:05:36 +00:00
|
|
|
return ConstString(FileSpec(path.GetStringRef()).GetPath());
|
2018-05-21 14:14:36 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-06-08 16:52:24 +00:00
|
|
|
// PathMappingList constructor
|
2012-03-15 21:01:31 +00:00
|
|
|
PathMappingList::PathMappingList() : m_pairs() {}
|
|
|
|
|
|
2013-03-14 22:52:17 +00:00
|
|
|
PathMappingList::PathMappingList(ChangedCallback callback, void *callback_baton)
|
|
|
|
|
: m_pairs(), m_callback(callback), m_callback_baton(callback_baton),
|
|
|
|
|
m_mod_id(0) {}
|
2010-06-08 16:52:24 +00:00
|
|
|
|
2011-04-23 02:04:55 +00:00
|
|
|
PathMappingList::PathMappingList(const PathMappingList &rhs)
|
2016-02-18 00:10:17 +00:00
|
|
|
: m_pairs(rhs.m_pairs), m_callback(nullptr), m_callback_baton(nullptr),
|
|
|
|
|
m_mod_id(0) {}
|
2011-04-23 02:04:55 +00:00
|
|
|
|
|
|
|
|
const PathMappingList &PathMappingList::operator=(const PathMappingList &rhs) {
|
|
|
|
|
if (this != &rhs) {
|
|
|
|
|
m_pairs = rhs.m_pairs;
|
2016-02-18 00:10:17 +00:00
|
|
|
m_callback = nullptr;
|
|
|
|
|
m_callback_baton = nullptr;
|
2013-03-14 22:52:17 +00:00
|
|
|
m_mod_id = rhs.m_mod_id;
|
2011-04-23 02:04:55 +00:00
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-18 00:10:17 +00:00
|
|
|
PathMappingList::~PathMappingList() = default;
|
2010-06-08 16:52:24 +00:00
|
|
|
|
2019-03-06 21:22:25 +00:00
|
|
|
void PathMappingList::Append(ConstString path,
|
|
|
|
|
ConstString replacement, bool notify) {
|
2013-03-14 22:52:17 +00:00
|
|
|
++m_mod_id;
|
2018-05-21 14:14:36 +00:00
|
|
|
m_pairs.emplace_back(pair(NormalizePath(path), NormalizePath(replacement)));
|
2010-06-08 16:52:24 +00:00
|
|
|
if (notify && m_callback)
|
|
|
|
|
m_callback(*this, m_callback_baton);
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-15 21:01:31 +00:00
|
|
|
void PathMappingList::Append(const PathMappingList &rhs, bool notify) {
|
2013-03-14 22:52:17 +00:00
|
|
|
++m_mod_id;
|
2012-03-15 21:01:31 +00:00
|
|
|
if (!rhs.m_pairs.empty()) {
|
|
|
|
|
const_iterator pos, end = rhs.m_pairs.end();
|
|
|
|
|
for (pos = rhs.m_pairs.begin(); pos != end; ++pos)
|
|
|
|
|
m_pairs.push_back(*pos);
|
|
|
|
|
if (notify && m_callback)
|
|
|
|
|
m_callback(*this, m_callback_baton);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-06 21:22:25 +00:00
|
|
|
void PathMappingList::Insert(ConstString path,
|
|
|
|
|
ConstString replacement, uint32_t index,
|
2010-06-08 16:52:24 +00:00
|
|
|
bool notify) {
|
2013-03-14 22:52:17 +00:00
|
|
|
++m_mod_id;
|
2010-06-08 16:52:24 +00:00
|
|
|
iterator insert_iter;
|
|
|
|
|
if (index >= m_pairs.size())
|
|
|
|
|
insert_iter = m_pairs.end();
|
|
|
|
|
else
|
|
|
|
|
insert_iter = m_pairs.begin() + index;
|
2018-05-21 14:14:36 +00:00
|
|
|
m_pairs.emplace(insert_iter, pair(NormalizePath(path),
|
|
|
|
|
NormalizePath(replacement)));
|
2010-06-08 16:52:24 +00:00
|
|
|
if (notify && m_callback)
|
|
|
|
|
m_callback(*this, m_callback_baton);
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-06 21:22:25 +00:00
|
|
|
bool PathMappingList::Replace(ConstString path,
|
|
|
|
|
ConstString replacement, uint32_t index,
|
2012-08-22 17:17:09 +00:00
|
|
|
bool notify) {
|
|
|
|
|
if (index >= m_pairs.size())
|
|
|
|
|
return false;
|
2013-03-14 22:52:17 +00:00
|
|
|
++m_mod_id;
|
2018-05-21 14:14:36 +00:00
|
|
|
m_pairs[index] = pair(NormalizePath(path), NormalizePath(replacement));
|
2012-08-22 17:17:09 +00:00
|
|
|
if (notify && m_callback)
|
|
|
|
|
m_callback(*this, m_callback_baton);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-02 17:24:07 +00:00
|
|
|
bool PathMappingList::Remove(size_t index, bool notify) {
|
|
|
|
|
if (index >= m_pairs.size())
|
2010-06-08 16:52:24 +00:00
|
|
|
return false;
|
|
|
|
|
|
2013-03-14 22:52:17 +00:00
|
|
|
++m_mod_id;
|
2010-06-08 16:52:24 +00:00
|
|
|
iterator iter = m_pairs.begin() + index;
|
|
|
|
|
m_pairs.erase(iter);
|
|
|
|
|
if (notify && m_callback)
|
|
|
|
|
m_callback(*this, m_callback_baton);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-12 21:59:28 +00:00
|
|
|
// For clients which do not need the pair index dumped, pass a pair_index >= 0
|
|
|
|
|
// to only dump the indicated pair.
|
|
|
|
|
void PathMappingList::Dump(Stream *s, int pair_index) {
|
2010-06-08 16:52:24 +00:00
|
|
|
unsigned int numPairs = m_pairs.size();
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2011-12-12 21:59:28 +00:00
|
|
|
if (pair_index < 0) {
|
|
|
|
|
unsigned int index;
|
|
|
|
|
for (index = 0; index < numPairs; ++index)
|
|
|
|
|
s->Printf("[%d] \"%s\" -> \"%s\"\n", index,
|
|
|
|
|
m_pairs[index].first.GetCString(),
|
|
|
|
|
m_pairs[index].second.GetCString());
|
|
|
|
|
} else {
|
2014-04-02 03:51:35 +00:00
|
|
|
if (static_cast<unsigned int>(pair_index) < numPairs)
|
2011-12-12 21:59:28 +00:00
|
|
|
s->Printf("%s -> %s", m_pairs[pair_index].first.GetCString(),
|
|
|
|
|
m_pairs[pair_index].second.GetCString());
|
2010-06-08 16:52:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PathMappingList::Clear(bool notify) {
|
2013-03-14 22:52:17 +00:00
|
|
|
if (!m_pairs.empty())
|
|
|
|
|
++m_mod_id;
|
2010-06-08 16:52:24 +00:00
|
|
|
m_pairs.clear();
|
|
|
|
|
if (notify && m_callback)
|
|
|
|
|
m_callback(*this, m_callback_baton);
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-06 21:22:25 +00:00
|
|
|
bool PathMappingList::RemapPath(ConstString path,
|
2012-03-15 21:01:31 +00:00
|
|
|
ConstString &new_path) const {
|
2021-06-25 13:49:01 -07:00
|
|
|
if (llvm::Optional<FileSpec> remapped = RemapPath(path.GetStringRef())) {
|
|
|
|
|
new_path.SetString(remapped->GetPath());
|
2018-05-21 14:14:36 +00:00
|
|
|
return true;
|
2016-09-06 20:57:50 +00:00
|
|
|
}
|
2012-03-19 22:22:41 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-29 15:24:36 -07:00
|
|
|
/// Append components to path, applying style.
|
|
|
|
|
static void AppendPathComponents(FileSpec &path, llvm::StringRef components,
|
|
|
|
|
llvm::sys::path::Style style) {
|
|
|
|
|
auto component = llvm::sys::path::begin(components, style);
|
|
|
|
|
auto e = llvm::sys::path::end(components);
|
|
|
|
|
while (component != e &&
|
|
|
|
|
llvm::sys::path::is_separator(*component->data(), style))
|
|
|
|
|
++component;
|
|
|
|
|
for (; component != e; ++component)
|
|
|
|
|
path.AppendPathComponent(*component);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 13:49:01 -07:00
|
|
|
llvm::Optional<FileSpec>
|
2021-07-23 16:21:05 -07:00
|
|
|
PathMappingList::RemapPath(llvm::StringRef mapping_path,
|
|
|
|
|
bool only_if_exists) const {
|
|
|
|
|
if (m_pairs.empty() || mapping_path.empty())
|
2021-06-25 13:49:01 -07:00
|
|
|
return {};
|
2018-05-21 14:14:36 +00:00
|
|
|
LazyBool path_is_relative = eLazyBoolCalculate;
|
2021-07-23 16:21:05 -07:00
|
|
|
|
2018-05-21 14:14:36 +00:00
|
|
|
for (const auto &it : m_pairs) {
|
2021-07-23 16:21:05 -07:00
|
|
|
llvm::StringRef prefix = it.first.GetStringRef();
|
|
|
|
|
// We create a copy of mapping_path because StringRef::consume_from
|
|
|
|
|
// effectively modifies the instance itself.
|
|
|
|
|
llvm::StringRef path = mapping_path;
|
2018-05-21 14:14:36 +00:00
|
|
|
if (!path.consume_front(prefix)) {
|
|
|
|
|
// Relative paths won't have a leading "./" in them unless "." is the
|
|
|
|
|
// only thing in the relative path so we need to work around "."
|
|
|
|
|
// carefully.
|
|
|
|
|
if (prefix != ".")
|
|
|
|
|
continue;
|
|
|
|
|
// We need to figure out if the "path" argument is relative. If it is,
|
|
|
|
|
// then we should remap, else skip this entry.
|
|
|
|
|
if (path_is_relative == eLazyBoolCalculate) {
|
2018-11-01 21:05:36 +00:00
|
|
|
path_is_relative =
|
|
|
|
|
FileSpec(path).IsRelative() ? eLazyBoolYes : eLazyBoolNo;
|
2018-05-21 14:14:36 +00:00
|
|
|
}
|
|
|
|
|
if (!path_is_relative)
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2018-11-01 21:05:36 +00:00
|
|
|
FileSpec remapped(it.second.GetStringRef());
|
2021-06-29 15:24:36 -07:00
|
|
|
auto orig_style = FileSpec::GuessPathStyle(prefix).getValueOr(
|
|
|
|
|
llvm::sys::path::Style::native);
|
|
|
|
|
AppendPathComponents(remapped, path, orig_style);
|
2021-07-23 16:21:05 -07:00
|
|
|
if (!only_if_exists || FileSystem::Instance().Exists(remapped))
|
|
|
|
|
return remapped;
|
2016-09-06 20:57:50 +00:00
|
|
|
}
|
2021-06-25 13:49:01 -07:00
|
|
|
return {};
|
2016-09-06 20:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
2018-05-21 14:14:36 +00:00
|
|
|
bool PathMappingList::ReverseRemapPath(const FileSpec &file, FileSpec &fixed) const {
|
|
|
|
|
std::string path = file.GetPath();
|
|
|
|
|
llvm::StringRef path_ref(path);
|
2010-06-08 16:52:24 +00:00
|
|
|
for (const auto &it : m_pairs) {
|
2018-05-21 14:14:36 +00:00
|
|
|
if (!path_ref.consume_front(it.second.GetStringRef()))
|
|
|
|
|
continue;
|
2021-06-29 15:24:36 -07:00
|
|
|
auto orig_file = it.first.GetStringRef();
|
|
|
|
|
auto orig_style = FileSpec::GuessPathStyle(orig_file).getValueOr(
|
|
|
|
|
llvm::sys::path::Style::native);
|
|
|
|
|
fixed.SetFile(orig_file, orig_style);
|
|
|
|
|
AppendPathComponents(fixed, path_ref, orig_style);
|
2018-05-21 14:14:36 +00:00
|
|
|
return true;
|
2016-09-06 20:57:50 +00:00
|
|
|
}
|
2010-06-08 16:52:24 +00:00
|
|
|
return false;
|
2016-03-04 11:26:44 +00:00
|
|
|
}
|
|
|
|
|
|
2021-06-29 15:14:31 -07:00
|
|
|
llvm::Optional<FileSpec> PathMappingList::FindFile(const FileSpec &orig_spec) const {
|
2021-07-23 16:21:05 -07:00
|
|
|
if (auto remapped = RemapPath(orig_spec.GetPath(), /*only_if_exists=*/true))
|
|
|
|
|
return remapped;
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2021-06-29 14:59:12 -07:00
|
|
|
return {};
|
2012-03-15 21:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-06 21:22:25 +00:00
|
|
|
bool PathMappingList::Replace(ConstString path,
|
|
|
|
|
ConstString new_path, bool notify) {
|
2011-04-23 02:04:55 +00:00
|
|
|
uint32_t idx = FindIndexForPath(path);
|
|
|
|
|
if (idx < m_pairs.size()) {
|
2013-03-14 22:52:17 +00:00
|
|
|
++m_mod_id;
|
2011-04-23 02:04:55 +00:00
|
|
|
m_pairs[idx].second = new_path;
|
|
|
|
|
if (notify && m_callback)
|
|
|
|
|
m_callback(*this, m_callback_baton);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-06 21:22:25 +00:00
|
|
|
bool PathMappingList::Remove(ConstString path, bool notify) {
|
2011-04-23 02:04:55 +00:00
|
|
|
iterator pos = FindIteratorForPath(path);
|
|
|
|
|
if (pos != m_pairs.end()) {
|
2013-03-14 22:52:17 +00:00
|
|
|
++m_mod_id;
|
2011-04-23 02:04:55 +00:00
|
|
|
m_pairs.erase(pos);
|
|
|
|
|
if (notify && m_callback)
|
|
|
|
|
m_callback(*this, m_callback_baton);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PathMappingList::const_iterator
|
2019-03-06 21:22:25 +00:00
|
|
|
PathMappingList::FindIteratorForPath(ConstString path) const {
|
2011-04-23 02:04:55 +00:00
|
|
|
const_iterator pos;
|
|
|
|
|
const_iterator begin = m_pairs.begin();
|
|
|
|
|
const_iterator end = m_pairs.end();
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2011-04-23 02:04:55 +00:00
|
|
|
for (pos = begin; pos != end; ++pos) {
|
|
|
|
|
if (pos->first == path)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return pos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PathMappingList::iterator
|
2019-03-06 21:22:25 +00:00
|
|
|
PathMappingList::FindIteratorForPath(ConstString path) {
|
2011-04-23 02:04:55 +00:00
|
|
|
iterator pos;
|
|
|
|
|
iterator begin = m_pairs.begin();
|
|
|
|
|
iterator end = m_pairs.end();
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2011-04-23 02:04:55 +00:00
|
|
|
for (pos = begin; pos != end; ++pos) {
|
|
|
|
|
if (pos->first == path)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return pos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PathMappingList::GetPathsAtIndex(uint32_t idx, ConstString &path,
|
|
|
|
|
ConstString &new_path) const {
|
|
|
|
|
if (idx < m_pairs.size()) {
|
|
|
|
|
path = m_pairs[idx].first;
|
|
|
|
|
new_path = m_pairs[idx].second;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-06 21:22:25 +00:00
|
|
|
uint32_t PathMappingList::FindIndexForPath(ConstString orig_path) const {
|
2018-05-21 14:14:36 +00:00
|
|
|
const ConstString path = NormalizePath(orig_path);
|
2011-04-23 02:04:55 +00:00
|
|
|
const_iterator pos;
|
|
|
|
|
const_iterator begin = m_pairs.begin();
|
|
|
|
|
const_iterator end = m_pairs.end();
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2011-04-23 02:04:55 +00:00
|
|
|
for (pos = begin; pos != end; ++pos) {
|
|
|
|
|
if (pos->first == path)
|
|
|
|
|
return std::distance(begin, pos);
|
|
|
|
|
}
|
|
|
|
|
return UINT32_MAX;
|
|
|
|
|
}
|