119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman//===- llvm/Support/PathV2.h - Path Operating System Concept ----*- C++ -*-===// 219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// The LLVM Compiler Infrastructure 419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// This file is distributed under the University of Illinois Open Source 619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// License. See LICENSE.TXT for details. 719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman//===----------------------------------------------------------------------===// 919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 1019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// This file declares the llvm::sys::path namespace. It is designed after 1119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// TR2/boost filesystem (v3), but modified to remove exception handling and the 1219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// path class. 1319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 1419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman//===----------------------------------------------------------------------===// 1519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 1619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#ifndef LLVM_SUPPORT_PATHV2_H 1719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#define LLVM_SUPPORT_PATHV2_H 1819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 1919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/ADT/SmallString.h" 2019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/ADT/Twine.h" 2119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/Support/DataTypes.h" 2219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include <iterator> 2319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 2419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumannamespace llvm { 2519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumannamespace sys { 2619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumannamespace path { 2719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 2819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @name Lexical Component Iterator 2919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @{ 3019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 3119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Path iterator. 3219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 3319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// This is a bidirectional iterator that iterates over the individual 3419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// components in \a path. The forward traversal order is as follows: 3519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// * The root-name element, if present. 3619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// * The root-directory element, if present. 3719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// * Each successive filename element, if present. 3819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// * Dot, if one or more trailing non-root slash characters are present. 3919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// The backwards traversal order is the reverse of forward traversal. 4019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 4119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// Iteration examples. Each component is separated by ',': 4219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// / => / 4319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// /foo => /,foo 4419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// foo/ => foo,. 4519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// /foo/bar => /,foo,bar 4619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// ../ => ..,. 4719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// C:\foo\bar => C:,/,foo,bar 4819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 4919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanclass const_iterator { 5019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman StringRef Path; //< The entire path. 5119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman StringRef Component; //< The current component. Not necessarily in Path. 5219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman size_t Position; //< The iterators current position within Path. 5319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 5419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // An end iterator has Position = Path.size() + 1. 5519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman friend const_iterator begin(StringRef path); 5619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman friend const_iterator end(StringRef path); 5719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 5819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanpublic: 5919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman typedef const StringRef value_type; 6019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman typedef ptrdiff_t difference_type; 6119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman typedef value_type &reference; 6219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman typedef value_type *pointer; 6319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman typedef std::bidirectional_iterator_tag iterator_category; 6419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 6519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman reference operator*() const { return Component; } 6619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman pointer operator->() const { return &Component; } 6719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman const_iterator &operator++(); // preincrement 6819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman const_iterator &operator++(int); // postincrement 6919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman const_iterator &operator--(); // predecrement 7019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman const_iterator &operator--(int); // postdecrement 7119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman bool operator==(const const_iterator &RHS) const; 7219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman bool operator!=(const const_iterator &RHS) const; 7319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 7419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman /// @brief Difference in bytes between this and RHS. 7519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ptrdiff_t operator-(const const_iterator &RHS) const; 7619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}; 7719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 7819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumantypedef std::reverse_iterator<const_iterator> reverse_iterator; 7919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 8019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Get begin iterator over \a path. 8119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path Input path. 8219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @returns Iterator initialized with the first component of \a path. 8319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanconst_iterator begin(StringRef path); 8419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 8519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Get end iterator over \a path. 8619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path Input path. 8719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @returns Iterator initialized to the end of \a path. 8819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanconst_iterator end(StringRef path); 8919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 9019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Get reverse begin iterator over \a path. 9119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path Input path. 9219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @returns Iterator initialized with the first reverse component of \a path. 9319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumaninline reverse_iterator rbegin(StringRef path) { 9419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return reverse_iterator(end(path)); 9519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman} 9619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 9719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Get reverse end iterator over \a path. 9819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path Input path. 9919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @returns Iterator initialized to the reverse end of \a path. 10019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumaninline reverse_iterator rend(StringRef path) { 10119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return reverse_iterator(begin(path)); 10219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman} 10319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 10419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @} 10519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @name Lexical Modifiers 10619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @{ 10719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 10819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Remove the last component from \a path unless it is the root dir. 10919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 11019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// directory/filename.cpp => directory/ 11119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// directory/ => directory 11219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// / => / 11319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 11419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path A path that is modified to not have a file component. 11519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid remove_filename(SmallVectorImpl<char> &path); 11619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 11719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Replace the file extension of \a path with \a extension. 11819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 11919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// ./filename.cpp => ./filename.extension 12019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// ./filename => ./filename.extension 12119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// ./ => ./.extension 12219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 12319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path A path that has its extension replaced with \a extension. 12419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param extension The extension to be added. It may be empty. It may also 12519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// optionally start with a '.', if it does not, one will be 12619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// prepended. 12719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid replace_extension(SmallVectorImpl<char> &path, const Twine &extension); 12819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 12919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Append to path. 13019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 13119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// /foo + bar/f => /foo/bar/f 13219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// /foo/ + bar/f => /foo/bar/f 13319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// foo + bar/f => foo/bar/f 13419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 13519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path Set to \a path + \a component. 13619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param component The component to be appended to \a path. 13719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid append(SmallVectorImpl<char> &path, const Twine &a, 13819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman const Twine &b = "", 13919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman const Twine &c = "", 14019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman const Twine &d = ""); 14119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 14219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Append to path. 14319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 14419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// /foo + [bar,f] => /foo/bar/f 14519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// /foo/ + [bar,f] => /foo/bar/f 14619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// foo + [bar,f] => foo/bar/f 14719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 14819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path Set to \a path + [\a begin, \a end). 14919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param begin Start of components to append. 15019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param end One past the end of components to append. 15119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid append(SmallVectorImpl<char> &path, 15219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman const_iterator begin, const_iterator end); 15319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 15419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @} 15519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @name Transforms (or some other better name) 15619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @{ 15719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 15819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// Convert path to the native form. This is used to give paths to users and 15919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// operating system calls in the platform's normal way. For example, on Windows 16019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// all '/' are converted to '\'. 16119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 16219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path A path that is transformed to native format. 16319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param result Holds the result of the transformation. 16419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid native(const Twine &path, SmallVectorImpl<char> &result); 16519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 16619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @} 16719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @name Lexical Observers 16819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @{ 16919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 17019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Get root name. 17119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 17219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// //net/hello => //net 17319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// c:/hello => c: (on Windows, on other platforms nothing) 17419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// /hello => <empty> 17519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 17619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path Input path. 17719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @result The root name of \a path if it has one, otherwise "". 17819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanconst StringRef root_name(StringRef path); 17919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 18019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Get root directory. 18119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 18219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// /goo/hello => / 18319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// c:/hello => / 18419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// d/file.txt => <empty> 18519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 18619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path Input path. 18719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @result The root directory of \a path if it has one, otherwise 18819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// "". 18919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanconst StringRef root_directory(StringRef path); 19019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 19119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Get root path. 19219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 19319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// Equivalent to root_name + root_directory. 19419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 19519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path Input path. 19619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @result The root path of \a path if it has one, otherwise "". 19719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanconst StringRef root_path(StringRef path); 19819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 19919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Get relative path. 20019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 20119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// C:\hello\world => hello\world 20219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// foo/bar => foo/bar 20319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// /foo/bar => foo/bar 20419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 20519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path Input path. 20619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @result The path starting after root_path if one exists, otherwise "". 20719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanconst StringRef relative_path(StringRef path); 20819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 20919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Get parent path. 21019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 21119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// / => <empty> 21219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// /foo => / 21319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// foo/../bar => foo/.. 21419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 21519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path Input path. 21619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @result The parent path of \a path if one exists, otherwise "". 21719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanconst StringRef parent_path(StringRef path); 21819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 21919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Get filename. 22019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 22119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// /foo.txt => foo.txt 22219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// . => . 22319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// .. => .. 22419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// / => / 22519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 22619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path Input path. 22719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @result The filename part of \a path. This is defined as the last component 22819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// of \a path. 22919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanconst StringRef filename(StringRef path); 23019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 23119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Get stem. 23219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 23319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// If filename contains a dot but not solely one or two dots, result is the 23419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// substring of filename ending at (but not including) the last dot. Otherwise 23519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// it is filename. 23619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 23719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// /foo/bar.txt => bar 23819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// /foo/bar => bar 23919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// /foo/.txt => <empty> 24019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// /foo/. => . 24119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// /foo/.. => .. 24219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 24319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path Input path. 24419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @result The stem of \a path. 24519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanconst StringRef stem(StringRef path); 24619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 24719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Get extension. 24819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 24919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// If filename contains a dot but not solely one or two dots, result is the 25019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// substring of filename starting at (and including) the last dot, and ending 25119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// at the end of \a path. Otherwise "". 25219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 25319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// /foo/bar.txt => .txt 25419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// /foo/bar => <empty> 25519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// /foo/.txt => .txt 25619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 25719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path Input path. 25819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @result The extension of \a path. 25919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanconst StringRef extension(StringRef path); 26019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 26119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Check whether the given char is a path separator on the host OS. 26219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 26319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param value a character 26419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @result true if \a value is a path separator character on the host OS 26519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool is_separator(char value); 26619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 26719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Get the typical temporary directory for the system, e.g., 26819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// "/var/tmp" or "C:/TEMP" 26919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 27019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param erasedOnReboot Whether to favor a path that is erased on reboot 27119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// rather than one that potentially persists longer. This parameter will be 27219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// ignored if the user or system has set the typical environment variable 27319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// (e.g., TEMP on Windows, TMPDIR on *nix) to specify a temporary directory. 27419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 27519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param Result Holds the resulting path name. 27619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result); 27719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 27819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Has root name? 27919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 28019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// root_name != "" 28119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 28219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path Input path. 28319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @result True if the path has a root name, false otherwise. 28419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool has_root_name(const Twine &path); 28519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 28619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Has root directory? 28719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 28819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// root_directory != "" 28919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 29019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path Input path. 29119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @result True if the path has a root directory, false otherwise. 29219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool has_root_directory(const Twine &path); 29319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 29419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Has root path? 29519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 29619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// root_path != "" 29719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 29819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path Input path. 29919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @result True if the path has a root path, false otherwise. 30019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool has_root_path(const Twine &path); 30119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 30219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Has relative path? 30319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 30419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// relative_path != "" 30519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 30619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path Input path. 30719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @result True if the path has a relative path, false otherwise. 30819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool has_relative_path(const Twine &path); 30919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 31019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Has parent path? 31119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 31219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// parent_path != "" 31319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 31419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path Input path. 31519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @result True if the path has a parent path, false otherwise. 31619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool has_parent_path(const Twine &path); 31719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 31819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Has filename? 31919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 32019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// filename != "" 32119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 32219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path Input path. 32319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @result True if the path has a filename, false otherwise. 32419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool has_filename(const Twine &path); 32519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 32619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Has stem? 32719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 32819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// stem != "" 32919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 33019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path Input path. 33119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @result True if the path has a stem, false otherwise. 33219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool has_stem(const Twine &path); 33319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 33419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Has extension? 33519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 33619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// extension != "" 33719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 33819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path Input path. 33919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @result True if the path has a extension, false otherwise. 34019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool has_extension(const Twine &path); 34119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 34219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Is path absolute? 34319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 34419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path Input path. 34519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @result True if the path is absolute, false if it is not. 34619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool is_absolute(const Twine &path); 34719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 34819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @brief Is path relative? 34919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 35019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @param path Input path. 35119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @result True if the path is relative, false if it is not. 35219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool is_relative(const Twine &path); 35319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 35419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman} // end namespace path 35519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman} // end namespace sys 35619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman} // end namespace llvm 35719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 35819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#endif 359