Path: blob/main/contrib/llvm-project/lldb/source/Utility/FileSpec.cpp
39587 views
//===-- FileSpec.cpp ------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/Utility/FileSpec.h"9#include "lldb/Utility/RegularExpression.h"10#include "lldb/Utility/Stream.h"1112#include "llvm/ADT/SmallString.h"13#include "llvm/ADT/SmallVector.h"14#include "llvm/ADT/StringExtras.h"15#include "llvm/ADT/StringRef.h"16#include "llvm/ADT/Twine.h"17#include "llvm/Support/ErrorOr.h"18#include "llvm/Support/FileSystem.h"19#include "llvm/Support/Program.h"20#include "llvm/Support/raw_ostream.h"21#include "llvm/TargetParser/Triple.h"2223#include <algorithm>24#include <optional>25#include <system_error>26#include <vector>2728#include <cassert>29#include <climits>30#include <cstdio>31#include <cstring>3233using namespace lldb;34using namespace lldb_private;3536namespace {3738static constexpr FileSpec::Style GetNativeStyle() {39#if defined(_WIN32)40return FileSpec::Style::windows;41#else42return FileSpec::Style::posix;43#endif44}4546bool PathStyleIsPosix(FileSpec::Style style) {47return llvm::sys::path::is_style_posix(style);48}4950const char *GetPathSeparators(FileSpec::Style style) {51return llvm::sys::path::get_separator(style).data();52}5354char GetPreferredPathSeparator(FileSpec::Style style) {55return GetPathSeparators(style)[0];56}5758void Denormalize(llvm::SmallVectorImpl<char> &path, FileSpec::Style style) {59if (PathStyleIsPosix(style))60return;6162std::replace(path.begin(), path.end(), '/', '\\');63}6465} // end anonymous namespace6667FileSpec::FileSpec() : m_style(GetNativeStyle()) {}6869// Default constructor that can take an optional full path to a file on disk.70FileSpec::FileSpec(llvm::StringRef path, Style style) : m_style(style) {71SetFile(path, style);72}7374FileSpec::FileSpec(llvm::StringRef path, const llvm::Triple &triple)75: FileSpec{path, triple.isOSWindows() ? Style::windows : Style::posix} {}7677namespace {78/// Safely get a character at the specified index.79///80/// \param[in] path81/// A full, partial, or relative path to a file.82///83/// \param[in] i84/// An index into path which may or may not be valid.85///86/// \return87/// The character at index \a i if the index is valid, or 0 if88/// the index is not valid.89inline char safeCharAtIndex(const llvm::StringRef &path, size_t i) {90if (i < path.size())91return path[i];92return 0;93}9495/// Check if a path needs to be normalized.96///97/// Check if a path needs to be normalized. We currently consider a98/// path to need normalization if any of the following are true99/// - path contains "/./"100/// - path contains "/../"101/// - path contains "//"102/// - path ends with "/"103/// Paths that start with "./" or with "../" are not considered to104/// need normalization since we aren't trying to resolve the path,105/// we are just trying to remove redundant things from the path.106///107/// \param[in] path108/// A full, partial, or relative path to a file.109///110/// \return111/// Returns \b true if the path needs to be normalized.112bool needsNormalization(const llvm::StringRef &path) {113if (path.empty())114return false;115// We strip off leading "." values so these paths need to be normalized116if (path[0] == '.')117return true;118for (auto i = path.find_first_of("\\/"); i != llvm::StringRef::npos;119i = path.find_first_of("\\/", i + 1)) {120const auto next = safeCharAtIndex(path, i+1);121switch (next) {122case 0:123// path separator char at the end of the string which should be124// stripped unless it is the one and only character125return i > 0;126case '/':127case '\\':128// two path separator chars in the middle of a path needs to be129// normalized130if (i > 0)131return true;132++i;133break;134135case '.': {136const auto next_next = safeCharAtIndex(path, i+2);137switch (next_next) {138default: break;139case 0: return true; // ends with "/."140case '/':141case '\\':142return true; // contains "/./"143case '.': {144const auto next_next_next = safeCharAtIndex(path, i+3);145switch (next_next_next) {146default: break;147case 0: return true; // ends with "/.."148case '/':149case '\\':150return true; // contains "/../"151}152break;153}154}155}156break;157158default:159break;160}161}162return false;163}164165166}167168void FileSpec::SetFile(llvm::StringRef pathname) { SetFile(pathname, m_style); }169170// Update the contents of this object with a new path. The path will be split171// up into a directory and filename and stored as uniqued string values for172// quick comparison and efficient memory usage.173void FileSpec::SetFile(llvm::StringRef pathname, Style style) {174Clear();175m_style = (style == Style::native) ? GetNativeStyle() : style;176177if (pathname.empty())178return;179180llvm::SmallString<128> resolved(pathname);181182// Normalize the path by removing ".", ".." and other redundant components.183if (needsNormalization(resolved))184llvm::sys::path::remove_dots(resolved, true, m_style);185186// Normalize back slashes to forward slashes187if (m_style == Style::windows)188std::replace(resolved.begin(), resolved.end(), '\\', '/');189190if (resolved.empty()) {191// If we have no path after normalization set the path to the current192// directory. This matches what python does and also a few other path193// utilities.194m_filename.SetString(".");195return;196}197198// Split path into filename and directory. We rely on the underlying char199// pointer to be nullptr when the components are empty.200llvm::StringRef filename = llvm::sys::path::filename(resolved, m_style);201if(!filename.empty())202m_filename.SetString(filename);203204llvm::StringRef directory = llvm::sys::path::parent_path(resolved, m_style);205if(!directory.empty())206m_directory.SetString(directory);207}208209void FileSpec::SetFile(llvm::StringRef path, const llvm::Triple &triple) {210return SetFile(path, triple.isOSWindows() ? Style::windows : Style::posix);211}212213// Convert to pointer operator. This allows code to check any FileSpec objects214// to see if they contain anything valid using code such as:215//216// if (file_spec)217// {}218FileSpec::operator bool() const { return m_filename || m_directory; }219220// Logical NOT operator. This allows code to check any FileSpec objects to see221// if they are invalid using code such as:222//223// if (!file_spec)224// {}225bool FileSpec::operator!() const { return !m_directory && !m_filename; }226227bool FileSpec::DirectoryEquals(const FileSpec &rhs) const {228const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();229return ConstString::Equals(m_directory, rhs.m_directory, case_sensitive);230}231232bool FileSpec::FileEquals(const FileSpec &rhs) const {233const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();234return ConstString::Equals(m_filename, rhs.m_filename, case_sensitive);235}236237// Equal to operator238bool FileSpec::operator==(const FileSpec &rhs) const {239return FileEquals(rhs) && DirectoryEquals(rhs);240}241242// Not equal to operator243bool FileSpec::operator!=(const FileSpec &rhs) const { return !(*this == rhs); }244245// Less than operator246bool FileSpec::operator<(const FileSpec &rhs) const {247return FileSpec::Compare(*this, rhs, true) < 0;248}249250// Dump a FileSpec object to a stream251Stream &lldb_private::operator<<(Stream &s, const FileSpec &f) {252f.Dump(s.AsRawOstream());253return s;254}255256// Clear this object by releasing both the directory and filename string values257// and making them both the empty string.258void FileSpec::Clear() {259m_directory.Clear();260m_filename.Clear();261PathWasModified();262}263264// Compare two FileSpec objects. If "full" is true, then both the directory and265// the filename must match. If "full" is false, then the directory names for266// "a" and "b" are only compared if they are both non-empty. This allows a267// FileSpec object to only contain a filename and it can match FileSpec objects268// that have matching filenames with different paths.269//270// Return -1 if the "a" is less than "b", 0 if "a" is equal to "b" and "1" if271// "a" is greater than "b".272int FileSpec::Compare(const FileSpec &a, const FileSpec &b, bool full) {273int result = 0;274275// case sensitivity of compare276const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();277278// If full is true, then we must compare both the directory and filename.279280// If full is false, then if either directory is empty, then we match on the281// basename only, and if both directories have valid values, we still do a282// full compare. This allows for matching when we just have a filename in one283// of the FileSpec objects.284285if (full || (a.m_directory && b.m_directory)) {286result = ConstString::Compare(a.m_directory, b.m_directory, case_sensitive);287if (result)288return result;289}290return ConstString::Compare(a.m_filename, b.m_filename, case_sensitive);291}292293bool FileSpec::Equal(const FileSpec &a, const FileSpec &b, bool full) {294if (full || (a.GetDirectory() && b.GetDirectory()))295return a == b;296297return a.FileEquals(b);298}299300bool FileSpec::Match(const FileSpec &pattern, const FileSpec &file) {301if (pattern.GetDirectory())302return pattern == file;303if (pattern.GetFilename())304return pattern.FileEquals(file);305return true;306}307308std::optional<FileSpec::Style>309FileSpec::GuessPathStyle(llvm::StringRef absolute_path) {310if (absolute_path.starts_with("/"))311return Style::posix;312if (absolute_path.starts_with(R"(\\)"))313return Style::windows;314if (absolute_path.size() >= 3 && llvm::isAlpha(absolute_path[0]) &&315(absolute_path.substr(1, 2) == R"(:\)" ||316absolute_path.substr(1, 2) == R"(:/)"))317return Style::windows;318return std::nullopt;319}320321// Dump the object to the supplied stream. If the object contains a valid322// directory name, it will be displayed followed by a directory delimiter, and323// the filename.324void FileSpec::Dump(llvm::raw_ostream &s) const {325std::string path{GetPath(true)};326s << path;327char path_separator = GetPreferredPathSeparator(m_style);328if (!m_filename && !path.empty() && path.back() != path_separator)329s << path_separator;330}331332FileSpec::Style FileSpec::GetPathStyle() const { return m_style; }333334void FileSpec::SetDirectory(ConstString directory) {335m_directory = directory;336PathWasModified();337}338339void FileSpec::SetDirectory(llvm::StringRef directory) {340m_directory = ConstString(directory);341PathWasModified();342}343344void FileSpec::SetFilename(ConstString filename) {345m_filename = filename;346PathWasModified();347}348349void FileSpec::SetFilename(llvm::StringRef filename) {350m_filename = ConstString(filename);351PathWasModified();352}353354void FileSpec::ClearFilename() {355m_filename.Clear();356PathWasModified();357}358359void FileSpec::ClearDirectory() {360m_directory.Clear();361PathWasModified();362}363364// Extract the directory and path into a fixed buffer. This is needed as the365// directory and path are stored in separate string values.366size_t FileSpec::GetPath(char *path, size_t path_max_len,367bool denormalize) const {368if (!path)369return 0;370371std::string result = GetPath(denormalize);372::snprintf(path, path_max_len, "%s", result.c_str());373return std::min(path_max_len - 1, result.length());374}375376std::string FileSpec::GetPath(bool denormalize) const {377llvm::SmallString<64> result;378GetPath(result, denormalize);379return static_cast<std::string>(result);380}381382ConstString FileSpec::GetPathAsConstString(bool denormalize) const {383return ConstString{GetPath(denormalize)};384}385386void FileSpec::GetPath(llvm::SmallVectorImpl<char> &path,387bool denormalize) const {388path.append(m_directory.GetStringRef().begin(),389m_directory.GetStringRef().end());390// Since the path was normalized and all paths use '/' when stored in these391// objects, we don't need to look for the actual syntax specific path392// separator, we just look for and insert '/'.393if (m_directory && m_filename && m_directory.GetStringRef().back() != '/' &&394m_filename.GetStringRef().back() != '/')395path.insert(path.end(), '/');396path.append(m_filename.GetStringRef().begin(),397m_filename.GetStringRef().end());398if (denormalize && !path.empty())399Denormalize(path, m_style);400}401402llvm::StringRef FileSpec::GetFileNameExtension() const {403return llvm::sys::path::extension(m_filename.GetStringRef(), m_style);404}405406ConstString FileSpec::GetFileNameStrippingExtension() const {407return ConstString(llvm::sys::path::stem(m_filename.GetStringRef(), m_style));408}409410// Return the size in bytes that this object takes in memory. This returns the411// size in bytes of this object, not any shared string values it may refer to.412size_t FileSpec::MemorySize() const {413return m_filename.MemorySize() + m_directory.MemorySize();414}415416FileSpec417FileSpec::CopyByAppendingPathComponent(llvm::StringRef component) const {418FileSpec ret = *this;419ret.AppendPathComponent(component);420return ret;421}422423FileSpec FileSpec::CopyByRemovingLastPathComponent() const {424llvm::SmallString<64> current_path;425GetPath(current_path, false);426if (llvm::sys::path::has_parent_path(current_path, m_style))427return FileSpec(llvm::sys::path::parent_path(current_path, m_style),428m_style);429return *this;430}431432void FileSpec::PrependPathComponent(llvm::StringRef component) {433llvm::SmallString<64> new_path(component);434llvm::SmallString<64> current_path;435GetPath(current_path, false);436llvm::sys::path::append(new_path,437llvm::sys::path::begin(current_path, m_style),438llvm::sys::path::end(current_path), m_style);439SetFile(new_path, m_style);440}441442void FileSpec::PrependPathComponent(const FileSpec &new_path) {443return PrependPathComponent(new_path.GetPath(false));444}445446void FileSpec::AppendPathComponent(llvm::StringRef component) {447llvm::SmallString<64> current_path;448GetPath(current_path, false);449llvm::sys::path::append(current_path, m_style, component);450SetFile(current_path, m_style);451}452453void FileSpec::AppendPathComponent(const FileSpec &new_path) {454return AppendPathComponent(new_path.GetPath(false));455}456457bool FileSpec::RemoveLastPathComponent() {458llvm::SmallString<64> current_path;459GetPath(current_path, false);460if (llvm::sys::path::has_parent_path(current_path, m_style)) {461SetFile(llvm::sys::path::parent_path(current_path, m_style));462return true;463}464return false;465}466467std::vector<llvm::StringRef> FileSpec::GetComponents() const {468std::vector<llvm::StringRef> components;469470auto dir_begin = llvm::sys::path::begin(m_directory.GetStringRef(), m_style);471auto dir_end = llvm::sys::path::end(m_directory.GetStringRef());472473for (auto iter = dir_begin; iter != dir_end; ++iter) {474if (*iter == "/" || *iter == ".")475continue;476477components.push_back(*iter);478}479480if (!m_filename.IsEmpty() && m_filename != "/" && m_filename != ".")481components.push_back(m_filename.GetStringRef());482483return components;484}485486/// Returns true if the filespec represents an implementation source487/// file (files with a ".c", ".cpp", ".m", ".mm" (many more)488/// extension).489///490/// \return491/// \b true if the filespec represents an implementation source492/// file, \b false otherwise.493bool FileSpec::IsSourceImplementationFile() const {494llvm::StringRef extension = GetFileNameExtension();495if (extension.empty())496return false;497498static RegularExpression g_source_file_regex(llvm::StringRef(499"^.([cC]|[mM]|[mM][mM]|[cC][pP][pP]|[cC]\\+\\+|[cC][xX][xX]|[cC][cC]|["500"cC][pP]|[sS]|[aA][sS][mM]|[fF]|[fF]77|[fF]90|[fF]95|[fF]03|[fF][oO]["501"rR]|[fF][tT][nN]|[fF][pP][pP]|[aA][dD][aA]|[aA][dD][bB]|[aA][dD][sS])"502"$"));503return g_source_file_regex.Execute(extension);504}505506bool FileSpec::IsRelative() const {507return !IsAbsolute();508}509510bool FileSpec::IsAbsolute() const {511// Check if we have cached if this path is absolute to avoid recalculating.512if (m_absolute != Absolute::Calculate)513return m_absolute == Absolute::Yes;514515m_absolute = Absolute::No;516517llvm::SmallString<64> path;518GetPath(path, false);519520if (!path.empty()) {521// We consider paths starting with ~ to be absolute.522if (path[0] == '~' || llvm::sys::path::is_absolute(path, m_style))523m_absolute = Absolute::Yes;524}525526return m_absolute == Absolute::Yes;527}528529void FileSpec::MakeAbsolute(const FileSpec &dir) {530if (IsRelative())531PrependPathComponent(dir);532}533534void llvm::format_provider<FileSpec>::format(const FileSpec &F,535raw_ostream &Stream,536StringRef Style) {537assert((Style.empty() || Style.equals_insensitive("F") ||538Style.equals_insensitive("D")) &&539"Invalid FileSpec style!");540541StringRef dir = F.GetDirectory().GetStringRef();542StringRef file = F.GetFilename().GetStringRef();543544if (dir.empty() && file.empty()) {545Stream << "(empty)";546return;547}548549if (Style.equals_insensitive("F")) {550Stream << (file.empty() ? "(empty)" : file);551return;552}553554// Style is either D or empty, either way we need to print the directory.555if (!dir.empty()) {556// Directory is stored in normalized form, which might be different than557// preferred form. In order to handle this, we need to cut off the558// filename, then denormalize, then write the entire denorm'ed directory.559llvm::SmallString<64> denormalized_dir = dir;560Denormalize(denormalized_dir, F.GetPathStyle());561Stream << denormalized_dir;562Stream << GetPreferredPathSeparator(F.GetPathStyle());563}564565if (Style.equals_insensitive("D")) {566// We only want to print the directory, so now just exit.567if (dir.empty())568Stream << "(empty)";569return;570}571572if (!file.empty())573Stream << file;574}575576577