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