1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef TOOLS_GN_SOURCE_DIR_H_ 6 #define TOOLS_GN_SOURCE_DIR_H_ 7 8 #include <stddef.h> 9 10 #include <algorithm> 11 #include <string> 12 #include <string_view> 13 14 #include "base/files/file_path.h" 15 #include "base/logging.h" 16 17 class Err; 18 class SourceFile; 19 class Value; 20 21 // Represents a directory within the source tree. Source dirs begin and end in 22 // slashes. 23 // 24 // If there is one slash at the beginning, it will mean a system-absolute file 25 // path. On Windows, absolute system paths will be of the form "/C:/foo/bar". 26 // 27 // Two slashes at the beginning indicate a path relative to the source root. 28 class SourceDir { 29 public: 30 SourceDir() = default; 31 32 SourceDir(const std::string& s); 33 explicit SourceDir(std::string&& s); 34 35 // Resolves a file or dir name (based on as_file parameter) relative 36 // to this source directory. Will return an empty string on error 37 // and set the give *err pointer (required). Empty input is always an error. 38 // 39 // Passed non null v_value will be used to resolve path (in cases where 40 // a substring has been extracted from the value, as with label resolution). 41 // In this use case parameter v is used to generate proper error. 42 // 43 // If source_root is supplied, these functions will additionally handle the 44 // case where the input is a system-absolute but still inside the source 45 // tree. This is the case for some external tools. 46 std::string ResolveRelativeAs( 47 bool as_file, 48 const Value& v, 49 Err* err, 50 const std::string_view& source_root = std::string_view(), 51 const std::string* v_value = nullptr) const; 52 53 // Like ResolveRelativeAs above, but allows to produce result 54 // without overhead for string conversion (on input value). 55 template <typename StringType> 56 std::string ResolveRelativeAs( 57 bool as_file, 58 const Value& blame_input_value, 59 const StringType& input_value, 60 Err* err, 61 const std::string_view& source_root = std::string_view()) const; 62 63 // Wrapper for ResolveRelativeAs. 64 SourceFile ResolveRelativeFile( 65 const Value& p, 66 Err* err, 67 const std::string_view& source_root = std::string_view()) const; 68 69 // Wrapper for ResolveRelativeAs. 70 template <typename StringType> 71 SourceDir ResolveRelativeDir( 72 const Value& blame_input_value, 73 const StringType& input_value, 74 Err* err, 75 const std::string_view& source_root = std::string_view()) const { 76 SourceDir ret; 77 ret.value_ = ResolveRelativeAs<StringType>(false, blame_input_value, 78 input_value, err, source_root); 79 return ret; 80 } 81 82 // Wrapper for ResolveRelativeDir where input_value equals to 83 // v.string_value(). 84 SourceDir ResolveRelativeDir( 85 const Value& v, 86 Err* err, 87 const std::string_view& source_root = std::string_view()) const; 88 89 // Resolves this source file relative to some given source root. Returns 90 // an empty file path on error. 91 base::FilePath Resolve(const base::FilePath& source_root) const; 92 is_null()93 bool is_null() const { return value_.empty(); } value()94 const std::string& value() const { return value_; } 95 96 // Returns true if this path starts with a "//" which indicates a path 97 // from the source root. is_source_absolute()98 bool is_source_absolute() const { 99 return value_.size() >= 2 && value_[0] == '/' && value_[1] == '/'; 100 } 101 102 // Returns true if this path starts with a single slash which indicates a 103 // system-absolute path. is_system_absolute()104 bool is_system_absolute() const { return !is_source_absolute(); } 105 106 // Returns a source-absolute path starting with only one slash at the 107 // beginning (normally source-absolute paths start with two slashes to mark 108 // them as such). This is normally used when concatenating directories 109 // together. 110 // 111 // This function asserts that the directory is actually source-absolute. The 112 // return value points into our buffer. SourceAbsoluteWithOneSlash()113 std::string_view SourceAbsoluteWithOneSlash() const { 114 CHECK(is_source_absolute()); 115 return std::string_view(&value_[1], value_.size() - 1); 116 } 117 118 // Returns a path that does not end with a slash. 119 // 120 // This function simply returns the reference to the value if the path is a 121 // root, e.g. "/" or "//". SourceWithNoTrailingSlash()122 std::string_view SourceWithNoTrailingSlash() const { 123 if (value_.size() > 2) 124 return std::string_view(&value_[0], value_.size() - 1); 125 return std::string_view(value_); 126 } 127 128 void SwapValue(std::string* v); 129 130 bool operator==(const SourceDir& other) const { 131 return value_ == other.value_; 132 } 133 bool operator!=(const SourceDir& other) const { return !operator==(other); } 134 bool operator<(const SourceDir& other) const { return value_ < other.value_; } 135 136 private: 137 friend class SourceFile; 138 std::string value_; 139 }; 140 141 namespace std { 142 143 template <> 144 struct hash<SourceDir> { 145 std::size_t operator()(const SourceDir& v) const { 146 hash<std::string> h; 147 return h(v.value()); 148 } 149 }; 150 151 } // namespace std 152 153 #endif // TOOLS_GN_SOURCE_DIR_H_ 154