2010-06-09 00:52:24 +08:00
|
|
|
//===-- FileSpec.cpp --------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-03-23 02:40:07 +08:00
|
|
|
#include "lldb/Utility/FileSpec.h"
|
2017-02-03 05:39:50 +08:00
|
|
|
#include "lldb/Utility/RegularExpression.h"
|
|
|
|
#include "lldb/Utility/Stream.h"
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2018-06-14 00:23:21 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2014-08-16 06:04:21 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2018-06-14 00:23:21 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/Support/ErrorOr.h"
|
2014-08-16 06:04:21 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/Program.h"
|
2018-06-14 00:23:21 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-04-07 02:12:24 +08:00
|
|
|
|
2018-11-12 07:16:43 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <system_error>
|
|
|
|
#include <vector>
|
2017-04-07 02:12:24 +08:00
|
|
|
|
2018-11-12 07:16:43 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2014-08-16 06:04:21 +08:00
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
2015-05-29 01:02:45 +08:00
|
|
|
namespace {
|
|
|
|
|
2018-05-14 22:52:47 +08:00
|
|
|
static constexpr FileSpec::Style GetNativeStyle() {
|
2018-04-10 21:33:45 +08:00
|
|
|
#if defined(_WIN32)
|
2018-05-14 22:52:47 +08:00
|
|
|
return FileSpec::Style::windows;
|
2017-03-09 01:56:08 +08:00
|
|
|
#else
|
2018-05-14 22:52:47 +08:00
|
|
|
return FileSpec::Style::posix;
|
2017-03-09 01:56:08 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-05-14 22:52:47 +08:00
|
|
|
bool PathStyleIsPosix(FileSpec::Style style) {
|
|
|
|
return (style == FileSpec::Style::posix ||
|
|
|
|
(style == FileSpec::Style::native &&
|
|
|
|
GetNativeStyle() == FileSpec::Style::posix));
|
2015-05-29 01:02:45 +08:00
|
|
|
}
|
|
|
|
|
2018-05-14 22:52:47 +08:00
|
|
|
const char *GetPathSeparators(FileSpec::Style style) {
|
2018-06-14 00:23:21 +08:00
|
|
|
return llvm::sys::path::get_separator(style).data();
|
2016-04-04 22:39:12 +08:00
|
|
|
}
|
|
|
|
|
2018-05-14 22:52:47 +08:00
|
|
|
char GetPreferredPathSeparator(FileSpec::Style style) {
|
|
|
|
return GetPathSeparators(style)[0];
|
2016-04-04 22:39:12 +08:00
|
|
|
}
|
|
|
|
|
2018-05-14 22:52:47 +08:00
|
|
|
void Denormalize(llvm::SmallVectorImpl<char> &path, FileSpec::Style style) {
|
|
|
|
if (PathStyleIsPosix(style))
|
2016-09-07 04:57:50 +08:00
|
|
|
return;
|
2015-05-29 01:02:45 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
std::replace(path.begin(), path.end(), '/', '\\');
|
2015-05-29 01:02:45 +08:00
|
|
|
}
|
2018-05-21 22:14:36 +08:00
|
|
|
|
2016-04-04 22:39:12 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2018-05-14 22:52:47 +08:00
|
|
|
FileSpec::FileSpec() : m_style(GetNativeStyle()) {}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2010-09-16 08:57:33 +08:00
|
|
|
//------------------------------------------------------------------
|
2018-05-01 00:49:04 +08:00
|
|
|
// Default constructor that can take an optional full path to a file on disk.
|
2010-09-16 08:57:33 +08:00
|
|
|
//------------------------------------------------------------------
|
2018-11-02 05:05:36 +08:00
|
|
|
FileSpec::FileSpec(llvm::StringRef path, Style style) : m_style(style) {
|
|
|
|
SetFile(path, style);
|
2010-09-16 08:57:33 +08:00
|
|
|
}
|
|
|
|
|
2018-11-02 05:05:36 +08:00
|
|
|
FileSpec::FileSpec(llvm::StringRef path, const llvm::Triple &Triple)
|
|
|
|
: FileSpec{path, Triple.isOSWindows() ? Style::windows : Style::posix} {}
|
2015-05-09 09:21:32 +08:00
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
// Copy constructor
|
|
|
|
//------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
FileSpec::FileSpec(const FileSpec &rhs)
|
|
|
|
: m_directory(rhs.m_directory), m_filename(rhs.m_filename),
|
2018-05-14 22:52:47 +08:00
|
|
|
m_is_resolved(rhs.m_is_resolved), m_style(rhs.m_style) {}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
// Copy constructor
|
|
|
|
//------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
FileSpec::FileSpec(const FileSpec *rhs) : m_directory(), m_filename() {
|
|
|
|
if (rhs)
|
|
|
|
*this = *rhs;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
2014-07-02 05:22:11 +08:00
|
|
|
// Virtual destructor in case anyone inherits from this class.
|
2010-06-09 00:52:24 +08:00
|
|
|
//------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
FileSpec::~FileSpec() {}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2018-06-25 18:11:53 +08:00
|
|
|
namespace {
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
/// Safely get a character at the specified index.
|
|
|
|
///
|
|
|
|
/// @param[in] path
|
|
|
|
/// A full, partial, or relative path to a file.
|
|
|
|
///
|
|
|
|
/// @param[in] i
|
|
|
|
/// An index into path which may or may not be valid.
|
|
|
|
///
|
|
|
|
/// @return
|
|
|
|
/// The character at index \a i if the index is valid, or 0 if
|
|
|
|
/// the index is not valid.
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
inline char safeCharAtIndex(const llvm::StringRef &path, size_t i) {
|
|
|
|
if (i < path.size())
|
|
|
|
return path[i];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
/// Check if a path needs to be normalized.
|
|
|
|
///
|
|
|
|
/// Check if a path needs to be normalized. We currently consider a
|
|
|
|
/// path to need normalization if any of the following are true
|
|
|
|
/// - path contains "/./"
|
|
|
|
/// - path contains "/../"
|
|
|
|
/// - path contains "//"
|
|
|
|
/// - path ends with "/"
|
|
|
|
/// Paths that start with "./" or with "../" are not considered to
|
|
|
|
/// need normalization since we aren't trying to resolve the path,
|
|
|
|
/// we are just trying to remove redundant things from the path.
|
|
|
|
///
|
|
|
|
/// @param[in] path
|
|
|
|
/// A full, partial, or relative path to a file.
|
|
|
|
///
|
|
|
|
/// @return
|
|
|
|
/// Returns \b true if the path needs to be normalized.
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
bool needsNormalization(const llvm::StringRef &path) {
|
|
|
|
if (path.empty())
|
|
|
|
return false;
|
|
|
|
// We strip off leading "." values so these paths need to be normalized
|
|
|
|
if (path[0] == '.')
|
|
|
|
return true;
|
|
|
|
for (auto i = path.find_first_of("\\/"); i != llvm::StringRef::npos;
|
|
|
|
i = path.find_first_of("\\/", i + 1)) {
|
|
|
|
const auto next = safeCharAtIndex(path, i+1);
|
|
|
|
switch (next) {
|
|
|
|
case 0:
|
|
|
|
// path separator char at the end of the string which should be
|
|
|
|
// stripped unless it is the one and only character
|
|
|
|
return i > 0;
|
|
|
|
case '/':
|
|
|
|
case '\\':
|
|
|
|
// two path separator chars in the middle of a path needs to be
|
|
|
|
// normalized
|
|
|
|
if (i > 0)
|
|
|
|
return true;
|
|
|
|
++i;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '.': {
|
|
|
|
const auto next_next = safeCharAtIndex(path, i+2);
|
|
|
|
switch (next_next) {
|
|
|
|
default: break;
|
|
|
|
case 0: return true; // ends with "/."
|
|
|
|
case '/':
|
|
|
|
case '\\':
|
|
|
|
return true; // contains "/./"
|
|
|
|
case '.': {
|
|
|
|
const auto next_next_next = safeCharAtIndex(path, i+3);
|
|
|
|
switch (next_next_next) {
|
|
|
|
default: break;
|
|
|
|
case 0: return true; // ends with "/.."
|
|
|
|
case '/':
|
|
|
|
case '\\':
|
|
|
|
return true; // contains "/../"
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
// Assignment operator.
|
|
|
|
//------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
const FileSpec &FileSpec::operator=(const FileSpec &rhs) {
|
|
|
|
if (this != &rhs) {
|
|
|
|
m_directory = rhs.m_directory;
|
|
|
|
m_filename = rhs.m_filename;
|
|
|
|
m_is_resolved = rhs.m_is_resolved;
|
2018-05-14 22:52:47 +08:00
|
|
|
m_style = rhs.m_style;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
return *this;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2018-11-02 05:05:36 +08:00
|
|
|
void FileSpec::SetFile(llvm::StringRef pathname) { SetFile(pathname, m_style); }
|
2018-06-14 06:08:14 +08:00
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
//------------------------------------------------------------------
|
2018-05-01 00:49:04 +08:00
|
|
|
// Update the contents of this object with a new path. The path will be split
|
|
|
|
// up into a directory and filename and stored as uniqued string values for
|
|
|
|
// quick comparison and efficient memory usage.
|
2010-06-09 00:52:24 +08:00
|
|
|
//------------------------------------------------------------------
|
2018-11-02 05:05:36 +08:00
|
|
|
void FileSpec::SetFile(llvm::StringRef pathname, Style style) {
|
2016-09-07 04:57:50 +08:00
|
|
|
m_filename.Clear();
|
|
|
|
m_directory.Clear();
|
|
|
|
m_is_resolved = false;
|
2018-05-14 22:52:47 +08:00
|
|
|
m_style = (style == Style::native) ? GetNativeStyle() : style;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2016-09-28 04:48:37 +08:00
|
|
|
if (pathname.empty())
|
2016-09-07 04:57:50 +08:00
|
|
|
return;
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2018-11-02 05:05:36 +08:00
|
|
|
llvm::SmallString<128> resolved(pathname);
|
2014-12-02 07:13:32 +08:00
|
|
|
|
2018-04-27 23:45:58 +08:00
|
|
|
// Normalize the path by removing ".", ".." and other redundant components.
|
2018-06-25 18:11:53 +08:00
|
|
|
if (needsNormalization(resolved))
|
|
|
|
llvm::sys::path::remove_dots(resolved, true, m_style);
|
2018-04-27 23:45:58 +08:00
|
|
|
|
|
|
|
// Normalize back slashes to forward slashes
|
2018-05-14 22:52:47 +08:00
|
|
|
if (m_style == Style::windows)
|
2018-04-27 23:45:58 +08:00
|
|
|
std::replace(resolved.begin(), resolved.end(), '\\', '/');
|
2016-04-14 17:38:06 +08:00
|
|
|
|
2018-05-18 00:12:38 +08:00
|
|
|
if (resolved.empty()) {
|
|
|
|
// If we have no path after normalization set the path to the current
|
|
|
|
// directory. This matches what python does and also a few other path
|
2018-06-14 00:23:21 +08:00
|
|
|
// utilities.
|
2018-05-18 00:12:38 +08:00
|
|
|
m_filename.SetString(".");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-14 00:23:21 +08:00
|
|
|
// Split path into filename and directory. We rely on the underlying char
|
|
|
|
// pointer to be nullptr when the components are empty.
|
|
|
|
llvm::StringRef filename = llvm::sys::path::filename(resolved, m_style);
|
2018-06-25 18:11:53 +08:00
|
|
|
if(!filename.empty())
|
2018-06-14 00:23:21 +08:00
|
|
|
m_filename.SetString(filename);
|
2018-11-02 05:05:36 +08:00
|
|
|
|
2018-06-14 00:23:21 +08:00
|
|
|
llvm::StringRef directory = llvm::sys::path::parent_path(resolved, m_style);
|
2018-06-25 18:11:53 +08:00
|
|
|
if(!directory.empty())
|
2018-06-14 00:23:21 +08:00
|
|
|
m_directory.SetString(directory);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2018-11-02 05:05:36 +08:00
|
|
|
void FileSpec::SetFile(llvm::StringRef path, const llvm::Triple &Triple) {
|
|
|
|
return SetFile(path, Triple.isOSWindows() ? Style::windows : Style::posix);
|
2015-05-30 03:52:37 +08:00
|
|
|
}
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
//----------------------------------------------------------------------
|
2018-05-01 00:49:04 +08:00
|
|
|
// Convert to pointer operator. This allows code to check any FileSpec objects
|
|
|
|
// to see if they contain anything valid using code such as:
|
2010-06-09 00:52:24 +08:00
|
|
|
//
|
|
|
|
// if (file_spec)
|
|
|
|
// {}
|
|
|
|
//----------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
FileSpec::operator bool() const { return m_filename || m_directory; }
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2018-05-01 00:49:04 +08:00
|
|
|
// Logical NOT operator. This allows code to check any FileSpec objects to see
|
|
|
|
// if they are invalid using code such as:
|
2010-06-09 00:52:24 +08:00
|
|
|
//
|
|
|
|
// if (!file_spec)
|
|
|
|
// {}
|
|
|
|
//----------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
bool FileSpec::operator!() const { return !m_directory && !m_filename; }
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool FileSpec::DirectoryEquals(const FileSpec &rhs) const {
|
|
|
|
const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();
|
|
|
|
return ConstString::Equals(m_directory, rhs.m_directory, case_sensitive);
|
Add support for reading line tables from PDB files.
PDB is Microsoft's debug information format, and although we
cannot yet generate it, we still must be able to consume it.
Reason for this is that debug information for system libraries
(e.g. kernel32, C Runtime Library, etc) only have debug info
in PDB format, so in order to be able to support debugging
of system code, we must support it.
Currently this code should compile on every platform, but on
non-Windows platforms the PDB plugin will return 0 capabilities,
meaning that for now PDB is only supported on Windows. This
may change in the future, but the API is designed in such a way
that this will require few (if any) changes on the LLDB side.
In the future we can just flip a switch and everything will
work.
This patch only adds support for line tables. It does not return
information about functions, types, global variables, or anything
else. This functionality will be added in a followup patch.
Differential Revision: http://reviews.llvm.org/D17363
Reviewed by: Greg Clayton
llvm-svn: 262528
2016-03-03 06:05:52 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool FileSpec::FileEquals(const FileSpec &rhs) const {
|
|
|
|
const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();
|
|
|
|
return ConstString::Equals(m_filename, rhs.m_filename, case_sensitive);
|
Add support for reading line tables from PDB files.
PDB is Microsoft's debug information format, and although we
cannot yet generate it, we still must be able to consume it.
Reason for this is that debug information for system libraries
(e.g. kernel32, C Runtime Library, etc) only have debug info
in PDB format, so in order to be able to support debugging
of system code, we must support it.
Currently this code should compile on every platform, but on
non-Windows platforms the PDB plugin will return 0 capabilities,
meaning that for now PDB is only supported on Windows. This
may change in the future, but the API is designed in such a way
that this will require few (if any) changes on the LLDB side.
In the future we can just flip a switch and everything will
work.
This patch only adds support for line tables. It does not return
information about functions, types, global variables, or anything
else. This functionality will be added in a followup patch.
Differential Revision: http://reviews.llvm.org/D17363
Reviewed by: Greg Clayton
llvm-svn: 262528
2016-03-03 06:05:52 +08:00
|
|
|
}
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
// Equal to operator
|
|
|
|
//------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
bool FileSpec::operator==(const FileSpec &rhs) const {
|
2018-11-02 05:05:36 +08:00
|
|
|
return FileEquals(rhs) && DirectoryEquals(rhs);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
// Not equal to operator
|
|
|
|
//------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
bool FileSpec::operator!=(const FileSpec &rhs) const { return !(*this == rhs); }
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
// Less than operator
|
|
|
|
//------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
bool FileSpec::operator<(const FileSpec &rhs) const {
|
|
|
|
return FileSpec::Compare(*this, rhs, true) < 0;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
// Dump a FileSpec object to a stream
|
|
|
|
//------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
Stream &lldb_private::operator<<(Stream &s, const FileSpec &f) {
|
|
|
|
f.Dump(&s);
|
|
|
|
return s;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
2018-05-01 00:49:04 +08:00
|
|
|
// Clear this object by releasing both the directory and filename string values
|
|
|
|
// and making them both the empty string.
|
2010-06-09 00:52:24 +08:00
|
|
|
//------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
void FileSpec::Clear() {
|
|
|
|
m_directory.Clear();
|
|
|
|
m_filename.Clear();
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
2018-05-01 00:49:04 +08:00
|
|
|
// Compare two FileSpec objects. If "full" is true, then both the directory and
|
|
|
|
// the filename must match. If "full" is false, then the directory names for
|
|
|
|
// "a" and "b" are only compared if they are both non-empty. This allows a
|
|
|
|
// FileSpec object to only contain a filename and it can match FileSpec objects
|
|
|
|
// that have matching filenames with different paths.
|
2010-06-09 00:52:24 +08:00
|
|
|
//
|
2018-05-01 00:49:04 +08:00
|
|
|
// Return -1 if the "a" is less than "b", 0 if "a" is equal to "b" and "1" if
|
|
|
|
// "a" is greater than "b".
|
2010-06-09 00:52:24 +08:00
|
|
|
//------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
int FileSpec::Compare(const FileSpec &a, const FileSpec &b, bool full) {
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
// case sensitivity of compare
|
|
|
|
const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();
|
|
|
|
|
|
|
|
// If full is true, then we must compare both the directory and filename.
|
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// If full is false, then if either directory is empty, then we match on the
|
|
|
|
// basename only, and if both directories have valid values, we still do a
|
|
|
|
// full compare. This allows for matching when we just have a filename in one
|
|
|
|
// of the FileSpec objects.
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
if (full || (a.m_directory && b.m_directory)) {
|
|
|
|
result = ConstString::Compare(a.m_directory, b.m_directory, case_sensitive);
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return ConstString::Compare(a.m_filename, b.m_filename, case_sensitive);
|
|
|
|
}
|
|
|
|
|
2018-04-27 23:45:58 +08:00
|
|
|
bool FileSpec::Equal(const FileSpec &a, const FileSpec &b, bool full) {
|
2016-09-07 04:57:50 +08:00
|
|
|
// case sensitivity of equality test
|
|
|
|
const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();
|
2018-06-14 00:23:21 +08:00
|
|
|
|
2018-06-25 18:11:53 +08:00
|
|
|
const bool filenames_equal = ConstString::Equals(a.m_filename,
|
|
|
|
b.m_filename,
|
|
|
|
case_sensitive);
|
2017-03-28 03:12:25 +08:00
|
|
|
|
2018-04-27 23:45:58 +08:00
|
|
|
if (!filenames_equal)
|
2018-06-14 00:23:21 +08:00
|
|
|
return false;
|
2017-03-28 03:12:25 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
if (!full && (a.GetDirectory().IsEmpty() || b.GetDirectory().IsEmpty()))
|
2017-03-28 03:12:25 +08:00
|
|
|
return filenames_equal;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2018-04-27 23:45:58 +08:00
|
|
|
return a == b;
|
2014-11-15 09:54:26 +08:00
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
2018-05-01 00:49:04 +08:00
|
|
|
// Dump the object to the supplied stream. If the object contains a valid
|
|
|
|
// directory name, it will be displayed followed by a directory delimiter, and
|
|
|
|
// the filename.
|
2010-06-09 00:52:24 +08:00
|
|
|
//------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
void FileSpec::Dump(Stream *s) const {
|
|
|
|
if (s) {
|
|
|
|
std::string path{GetPath(true)};
|
2016-11-03 04:34:10 +08:00
|
|
|
s->PutCString(path);
|
2018-05-14 22:52:47 +08:00
|
|
|
char path_separator = GetPreferredPathSeparator(m_style);
|
2016-09-07 04:57:50 +08:00
|
|
|
if (!m_filename && !path.empty() && path.back() != path_separator)
|
|
|
|
s->PutChar(path_separator);
|
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2018-05-14 22:52:47 +08:00
|
|
|
FileSpec::Style FileSpec::GetPathStyle() const { return m_style; }
|
2014-08-08 01:33:07 +08:00
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
// Directory string get accessor.
|
|
|
|
//------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
ConstString &FileSpec::GetDirectory() { return m_directory; }
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
// Directory string const get accessor.
|
|
|
|
//------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
const ConstString &FileSpec::GetDirectory() const { return m_directory; }
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
// Filename string get accessor.
|
|
|
|
//------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
ConstString &FileSpec::GetFilename() { return m_filename; }
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
// Filename string const get accessor.
|
|
|
|
//------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
const ConstString &FileSpec::GetFilename() const { return m_filename; }
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
2018-05-01 00:49:04 +08:00
|
|
|
// Extract the directory and path into a fixed buffer. This is needed as the
|
|
|
|
// directory and path are stored in separate string values.
|
2010-06-09 00:52:24 +08:00
|
|
|
//------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
size_t FileSpec::GetPath(char *path, size_t path_max_len,
|
|
|
|
bool denormalize) const {
|
|
|
|
if (!path)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
std::string result = GetPath(denormalize);
|
|
|
|
::snprintf(path, path_max_len, "%s", result.c_str());
|
|
|
|
return std::min(path_max_len - 1, result.length());
|
2011-10-18 05:45:27 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
std::string FileSpec::GetPath(bool denormalize) const {
|
|
|
|
llvm::SmallString<64> result;
|
|
|
|
GetPath(result, denormalize);
|
|
|
|
return std::string(result.begin(), result.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *FileSpec::GetCString(bool denormalize) const {
|
2018-12-13 08:15:17 +08:00
|
|
|
return ConstString{GetPath(denormalize)}.AsCString(nullptr);
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileSpec::GetPath(llvm::SmallVectorImpl<char> &path,
|
|
|
|
bool denormalize) const {
|
|
|
|
path.append(m_directory.GetStringRef().begin(),
|
|
|
|
m_directory.GetStringRef().end());
|
2018-04-27 23:45:58 +08:00
|
|
|
// Since the path was normalized and all paths use '/' when stored in these
|
|
|
|
// objects, we don't need to look for the actual syntax specific path
|
|
|
|
// separator, we just look for and insert '/'.
|
|
|
|
if (m_directory && m_filename && m_directory.GetStringRef().back() != '/' &&
|
|
|
|
m_filename.GetStringRef().back() != '/')
|
|
|
|
path.insert(path.end(), '/');
|
2016-09-07 04:57:50 +08:00
|
|
|
path.append(m_filename.GetStringRef().begin(),
|
|
|
|
m_filename.GetStringRef().end());
|
|
|
|
if (denormalize && !path.empty())
|
2018-05-14 22:52:47 +08:00
|
|
|
Denormalize(path, m_style);
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ConstString FileSpec::GetFileNameExtension() const {
|
2018-06-14 00:36:07 +08:00
|
|
|
return ConstString(
|
|
|
|
llvm::sys::path::extension(m_filename.GetStringRef(), m_style));
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ConstString FileSpec::GetFileNameStrippingExtension() const {
|
2018-06-14 00:36:07 +08:00
|
|
|
return ConstString(llvm::sys::path::stem(m_filename.GetStringRef(), m_style));
|
2011-10-18 05:45:27 +08:00
|
|
|
}
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
//------------------------------------------------------------------
|
2018-05-01 00:49:04 +08:00
|
|
|
// Return the size in bytes that this object takes in memory. This returns the
|
|
|
|
// size in bytes of this object, not any shared string values it may refer to.
|
2010-06-09 00:52:24 +08:00
|
|
|
//------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
size_t FileSpec::MemorySize() const {
|
|
|
|
return m_filename.MemorySize() + m_directory.MemorySize();
|
|
|
|
}
|
|
|
|
|
2016-09-28 04:48:37 +08:00
|
|
|
FileSpec
|
|
|
|
FileSpec::CopyByAppendingPathComponent(llvm::StringRef component) const {
|
2016-09-07 04:57:50 +08:00
|
|
|
FileSpec ret = *this;
|
2016-09-28 04:48:37 +08:00
|
|
|
ret.AppendPathComponent(component);
|
2016-09-07 04:57:50 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileSpec FileSpec::CopyByRemovingLastPathComponent() const {
|
2018-06-24 18:18:01 +08:00
|
|
|
llvm::SmallString<64> current_path;
|
|
|
|
GetPath(current_path, false);
|
|
|
|
if (llvm::sys::path::has_parent_path(current_path, m_style))
|
2018-11-02 05:05:36 +08:00
|
|
|
return FileSpec(llvm::sys::path::parent_path(current_path, m_style),
|
2018-06-24 18:18:01 +08:00
|
|
|
m_style);
|
|
|
|
return *this;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ConstString FileSpec::GetLastPathComponent() const {
|
2018-06-24 18:18:01 +08:00
|
|
|
llvm::SmallString<64> current_path;
|
|
|
|
GetPath(current_path, false);
|
|
|
|
return ConstString(llvm::sys::path::filename(current_path, m_style));
|
2017-01-16 18:07:02 +08:00
|
|
|
}
|
|
|
|
|
2016-09-28 04:48:37 +08:00
|
|
|
void FileSpec::PrependPathComponent(llvm::StringRef component) {
|
2018-06-24 18:18:01 +08:00
|
|
|
llvm::SmallString<64> new_path(component);
|
|
|
|
llvm::SmallString<64> current_path;
|
|
|
|
GetPath(current_path, false);
|
|
|
|
llvm::sys::path::append(new_path,
|
|
|
|
llvm::sys::path::begin(current_path, m_style),
|
|
|
|
llvm::sys::path::end(current_path), m_style);
|
2018-11-02 05:05:36 +08:00
|
|
|
SetFile(new_path, m_style);
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileSpec::PrependPathComponent(const FileSpec &new_path) {
|
|
|
|
return PrependPathComponent(new_path.GetPath(false));
|
|
|
|
}
|
|
|
|
|
2016-09-28 04:48:37 +08:00
|
|
|
void FileSpec::AppendPathComponent(llvm::StringRef component) {
|
2018-06-24 18:18:01 +08:00
|
|
|
llvm::SmallString<64> current_path;
|
|
|
|
GetPath(current_path, false);
|
|
|
|
llvm::sys::path::append(current_path, m_style, component);
|
2018-11-02 05:05:36 +08:00
|
|
|
SetFile(current_path, m_style);
|
2015-05-30 03:52:29 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void FileSpec::AppendPathComponent(const FileSpec &new_path) {
|
|
|
|
return AppendPathComponent(new_path.GetPath(false));
|
2015-06-05 08:28:06 +08:00
|
|
|
}
|
|
|
|
|
2018-05-30 21:03:16 +08:00
|
|
|
bool FileSpec::RemoveLastPathComponent() {
|
|
|
|
llvm::SmallString<64> current_path;
|
|
|
|
GetPath(current_path, false);
|
|
|
|
if (llvm::sys::path::has_parent_path(current_path, m_style)) {
|
2018-11-02 05:05:36 +08:00
|
|
|
SetFile(llvm::sys::path::parent_path(current_path, m_style));
|
2018-05-30 21:03:16 +08:00
|
|
|
return true;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2018-05-30 21:03:16 +08:00
|
|
|
return false;
|
2013-08-27 07:57:52 +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
|
|
|
//------------------------------------------------------------------
|
|
|
|
/// Returns true if the filespec represents an implementation source
|
|
|
|
/// file (files with a ".c", ".cpp", ".m", ".mm" (many more)
|
|
|
|
/// extension).
|
|
|
|
///
|
|
|
|
/// @return
|
|
|
|
/// \b true if the filespec represents an implementation source
|
|
|
|
/// file, \b false otherwise.
|
|
|
|
//------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
bool FileSpec::IsSourceImplementationFile() const {
|
|
|
|
ConstString extension(GetFileNameExtension());
|
2016-09-22 00:01:28 +08:00
|
|
|
if (!extension)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
static RegularExpression g_source_file_regex(llvm::StringRef(
|
2018-06-14 00:23:21 +08:00
|
|
|
"^.([cC]|[mM]|[mM][mM]|[cC][pP][pP]|[cC]\\+\\+|[cC][xX][xX]|[cC][cC]|["
|
2016-09-22 00:01:28 +08:00
|
|
|
"cC][pP]|[sS]|[aA][sS][mM]|[fF]|[fF]77|[fF]90|[fF]95|[fF]03|[fF][oO]["
|
|
|
|
"rR]|[fF][tT][nN]|[fF][pP][pP]|[aA][dD][aA]|[aA][dD][bB]|[aA][dD][sS])"
|
|
|
|
"$"));
|
|
|
|
return g_source_file_regex.Execute(extension.GetStringRef());
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2018-06-25 18:11:53 +08:00
|
|
|
bool FileSpec::IsRelative() const {
|
|
|
|
return !IsAbsolute();
|
|
|
|
}
|
2015-06-10 01:54:27 +08:00
|
|
|
|
2018-06-14 00:23:21 +08:00
|
|
|
bool FileSpec::IsAbsolute() const {
|
|
|
|
llvm::SmallString<64> current_path;
|
|
|
|
GetPath(current_path, false);
|
|
|
|
|
|
|
|
// Early return if the path is empty.
|
|
|
|
if (current_path.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// We consider paths starting with ~ to be absolute.
|
|
|
|
if (current_path[0] == '~')
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return llvm::sys::path::is_absolute(current_path, m_style);
|
|
|
|
}
|
2016-12-16 12:27:00 +08:00
|
|
|
|
DWARF: Add some support for non-native directory separators
Summary:
If we opened a file which was produced on system with different path
syntax, we would parse the paths from the debug info incorrectly.
The reason for that is that we would parse the paths as they were
native. For example this meant that on linux we would treat the entire
windows path as a single file name with no directory component, and then
we would concatenate that with the single directory component from the
DW_AT_comp_dir attribute. When parsing posix paths on windows, we would
at least get the directory separators right, but we still would treat
the posix paths as relative, and concatenate them where we shouldn't.
This patch attempts to remedy this by guessing the path syntax used in
each compile unit. (Unfortunately, there is no info in DWARF which would
give the definitive path style used by the produces, so guessing is all
we can do.) Currently, this guessing is based on the DW_AT_comp_dir
attribute of the compile unit, but this can be refined later if needed
(for example, the DW_AT_name of the compile unit may also contain some
useful info). This style is then used when parsing the line table of
that compile unit.
This patch is sufficient to make the line tables come out right, and
enable breakpoint setting by file name work correctly. Setting a
breakpoint by full path still has some kinks (specifically, using a
windows-style full path will not work on linux because the path will be
parsed as a linux path), but this will require larger changes in how
breakpoint setting works.
Reviewers: clayborg, zturner, JDevlieghere
Subscribers: aprantl, lldb-commits
Differential Revision: https://reviews.llvm.org/D56543
llvm-svn: 351328
2019-01-16 20:30:41 +08:00
|
|
|
void FileSpec::MakeAbsolute(const FileSpec &dir) {
|
|
|
|
if (IsRelative())
|
|
|
|
PrependPathComponent(dir);
|
|
|
|
}
|
|
|
|
|
2016-12-16 12:27:00 +08:00
|
|
|
void llvm::format_provider<FileSpec>::format(const FileSpec &F,
|
|
|
|
raw_ostream &Stream,
|
|
|
|
StringRef Style) {
|
|
|
|
assert(
|
|
|
|
(Style.empty() || Style.equals_lower("F") || Style.equals_lower("D")) &&
|
|
|
|
"Invalid FileSpec style!");
|
|
|
|
|
|
|
|
StringRef dir = F.GetDirectory().GetStringRef();
|
|
|
|
StringRef file = F.GetFilename().GetStringRef();
|
|
|
|
|
|
|
|
if (dir.empty() && file.empty()) {
|
|
|
|
Stream << "(empty)";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Style.equals_lower("F")) {
|
|
|
|
Stream << (file.empty() ? "(empty)" : file);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Style is either D or empty, either way we need to print the directory.
|
|
|
|
if (!dir.empty()) {
|
2018-05-01 00:49:04 +08:00
|
|
|
// Directory is stored in normalized form, which might be different than
|
|
|
|
// preferred form. In order to handle this, we need to cut off the
|
|
|
|
// filename, then denormalize, then write the entire denorm'ed directory.
|
2016-12-16 12:27:00 +08:00
|
|
|
llvm::SmallString<64> denormalized_dir = dir;
|
2018-05-14 22:52:47 +08:00
|
|
|
Denormalize(denormalized_dir, F.GetPathStyle());
|
2016-12-16 12:27:00 +08:00
|
|
|
Stream << denormalized_dir;
|
2018-05-14 22:52:47 +08:00
|
|
|
Stream << GetPreferredPathSeparator(F.GetPathStyle());
|
2016-12-16 12:27:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Style.equals_lower("D")) {
|
|
|
|
// We only want to print the directory, so now just exit.
|
|
|
|
if (dir.empty())
|
|
|
|
Stream << "(empty)";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file.empty())
|
|
|
|
Stream << file;
|
|
|
|
}
|