Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(373)

Side by Side Diff: tools/gn/source_dir.cc

Issue 1155713006: GN: Make file/dir resolving return errors. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "tools/gn/source_dir.h" 5 #include "tools/gn/source_dir.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "tools/gn/filesystem_utils.h" 8 #include "tools/gn/filesystem_utils.h"
9 #include "tools/gn/source_file.h" 9 #include "tools/gn/source_file.h"
10 10
11 namespace { 11 namespace {
12 12
13 void AssertValueSourceDirString(const std::string& s) { 13 void AssertValueSourceDirString(const std::string& s) {
14 if (!s.empty()) { 14 if (!s.empty()) {
15 #if defined(OS_WIN) 15 #if defined(OS_WIN)
16 DCHECK(s[0] == '/' || 16 DCHECK(s[0] == '/' ||
17 (s.size() > 2 && s[0] != '/' && s[1] == ':' && IsSlash(s[2]))); 17 (s.size() > 2 && s[0] != '/' && s[1] == ':' && IsSlash(s[2])));
18 #else 18 #else
19 DCHECK(s[0] == '/'); 19 DCHECK(s[0] == '/');
20 #endif 20 #endif
21 DCHECK(EndsWithSlash(s)); 21 DCHECK(EndsWithSlash(s)) << s;
22 } 22 }
23 } 23 }
24 24
25 } // namespace 25 } // namespace
26 26
27 SourceDir::SourceDir() { 27 SourceDir::SourceDir() {
28 } 28 }
29 29
30 SourceDir::SourceDir(const base::StringPiece& p) 30 SourceDir::SourceDir(const base::StringPiece& p)
31 : value_(p.data(), p.size()) { 31 : value_(p.data(), p.size()) {
32 if (!EndsWithSlash(value_)) 32 if (!EndsWithSlash(value_))
33 value_.push_back('/'); 33 value_.push_back('/');
34 AssertValueSourceDirString(value_); 34 AssertValueSourceDirString(value_);
35 } 35 }
36 36
37 SourceDir::SourceDir(SwapIn, std::string* s) { 37 SourceDir::SourceDir(SwapIn, std::string* s) {
38 value_.swap(*s); 38 value_.swap(*s);
39 if (!EndsWithSlash(value_)) 39 if (!EndsWithSlash(value_))
40 value_.push_back('/'); 40 value_.push_back('/');
41 AssertValueSourceDirString(value_); 41 AssertValueSourceDirString(value_);
42 } 42 }
43 43
44 SourceDir::~SourceDir() { 44 SourceDir::~SourceDir() {
45 } 45 }
46 46
47 SourceFile SourceDir::ResolveRelativeFile( 47 SourceFile SourceDir::ResolveRelativeFile(
48 const base::StringPiece& p, 48 const Value& p,
49 Err* err,
49 const base::StringPiece& source_root) const { 50 const base::StringPiece& source_root) const {
50 SourceFile ret; 51 SourceFile ret;
51 52 if (!p.VerifyTypeIs(Value::STRING, err))
52 DCHECK(source_root.empty() || !source_root.ends_with("/")); 53 return ret;
53 54
54 // It's an error to resolve an empty string or one that is a directory 55 // It's an error to resolve an empty string or one that is a directory
55 // (indicated by a trailing slash) because this is the function that expects 56 // (indicated by a trailing slash) because this is the function that expects
56 // to return a file. 57 // to return a file.
57 if (p.empty() || (p.size() > 0 && p[p.size() - 1] == '/')) 58 const std::string& str = p.string_value();
58 return SourceFile(); 59 if (str.empty()) {
59 if (p.size() >= 2 && p[0] == '/' && p[1] == '/') { 60 *err = Err(p, "Empty file path.",
61 "You can't use empty strings as file paths. That's just wrong.");
62 return ret;
63 } else if (str[str.size() - 1] == '/') {
64 *err = Err(p, "File path ends in a slash.",
65 "You specified the path\n " + str + "\n"
66 "and it ends in a slash, indicating you think it's a directory."
67 "\nBut here you're supposed to be listing a file.");
68 return ret;
69 }
70
71 if (str.size() >= 2 && str[0] == '/' && str[1] == '/') {
60 // Source-relative. 72 // Source-relative.
61 ret.value_.assign(p.data(), p.size()); 73 ret.value_.assign(str.data(), str.size());
62 NormalizePath(&ret.value_); 74 NormalizePath(&ret.value_);
63 return ret; 75 return ret;
64 } else if (IsPathAbsolute(p)) { 76 } else if (IsPathAbsolute(str)) {
65 if (source_root.empty() || 77 if (source_root.empty() ||
66 !MakeAbsolutePathRelativeIfPossible(source_root, p, &ret.value_)) { 78 !MakeAbsolutePathRelativeIfPossible(source_root, str, &ret.value_)) {
67 #if defined(OS_WIN) 79 #if defined(OS_WIN)
68 // On Windows we'll accept "C:\foo" as an absolute path, which we want 80 // On Windows we'll accept "C:\foo" as an absolute path, which we want
69 // to convert to "/C:..." here. 81 // to convert to "/C:..." here.
70 if (p[0] != '/') 82 if (str[0] != '/')
71 ret.value_ = "/"; 83 ret.value_ = "/";
72 #endif 84 #endif
73 ret.value_.append(p.data(), p.size()); 85 ret.value_.append(str.data(), str.size());
74 } 86 }
75 NormalizePath(&ret.value_); 87 NormalizePath(&ret.value_);
76 return ret; 88 return ret;
77 } 89 }
78 90
79 if (!source_root.empty()) { 91 if (!source_root.empty()) {
80 std::string absolute = 92 std::string absolute =
81 FilePathToUTF8(Resolve(UTF8ToFilePath(source_root)).AppendASCII( 93 FilePathToUTF8(Resolve(UTF8ToFilePath(source_root)).AppendASCII(
82 p.as_string()).value()); 94 str).value());
83 NormalizePath(&absolute); 95 NormalizePath(&absolute);
84 if (!MakeAbsolutePathRelativeIfPossible(source_root, absolute, 96 if (!MakeAbsolutePathRelativeIfPossible(source_root, absolute,
85 &ret.value_)) { 97 &ret.value_)) {
86 #if defined(OS_WIN) 98 #if defined(OS_WIN)
87 // On Windows we'll accept "C:\foo" as an absolute path, which we want 99 // On Windows we'll accept "C:\foo" as an absolute path, which we want
88 // to convert to "/C:..." here. 100 // to convert to "/C:..." here.
89 if (absolute[0] != '/') 101 if (absolute[0] != '/')
90 ret.value_ = "/"; 102 ret.value_ = "/";
91 #endif 103 #endif
92 ret.value_.append(absolute.data(), absolute.size()); 104 ret.value_.append(absolute.data(), absolute.size());
93 } 105 }
94 return ret; 106 return ret;
95 } 107 }
96 108
97 // With no source_root_, there's nothing we can do about 109 // With no source_root_, there's nothing we can do about
98 // e.g. p=../../../path/to/file and value_=//source and we'll 110 // e.g. p=../../../path/to/file and value_=//source and we'll
99 // errornously return //file. 111 // errornously return //file.
100 ret.value_.reserve(value_.size() + p.size()); 112 ret.value_.reserve(value_.size() + str.size());
101 ret.value_.assign(value_); 113 ret.value_.assign(value_);
102 ret.value_.append(p.data(), p.size()); 114 ret.value_.append(str.data(), str.size());
103 115
104 NormalizePath(&ret.value_); 116 NormalizePath(&ret.value_);
105 return ret; 117 return ret;
106 } 118 }
107 119
108 SourceDir SourceDir::ResolveRelativeDir( 120 SourceDir SourceDir::ResolveRelativeDir(
109 const base::StringPiece& p, 121 const Value& p,
122 Err* err,
123 const base::StringPiece& source_root) const {
124 if (!p.VerifyTypeIs(Value::STRING, err))
125 return SourceDir();
126 return ResolveRelativeDir(p, p.string_value(), err, source_root);
127 }
128
129 SourceDir SourceDir::ResolveRelativeDir(
130 const Value& blame_but_dont_use,
131 const base::StringPiece& str,
132 Err* err,
110 const base::StringPiece& source_root) const { 133 const base::StringPiece& source_root) const {
111 SourceDir ret; 134 SourceDir ret;
112 135
113 DCHECK(source_root.empty() || !source_root.ends_with("/")); 136 if (str.empty()) {
137 *err = Err(blame_but_dont_use, "Empty directory path.",
138 "You can't use empty strings as directoriers. "
scottmg 2015/06/03 21:01:59 "directories"
139 "That's just wrong.");
140 return ret;
141 }
114 142
115 if (p.empty()) 143 if (str.size() >= 2 && str[0] == '/' && str[1] == '/') {
116 return ret;
117 if (p.size() >= 2 && p[0] == '/' && p[1] == '/') {
118 // Source-relative. 144 // Source-relative.
119 ret.value_.assign(p.data(), p.size()); 145 ret.value_.assign(str.data(), str.size());
120 if (!EndsWithSlash(ret.value_)) 146 if (!EndsWithSlash(ret.value_))
121 ret.value_.push_back('/'); 147 ret.value_.push_back('/');
122 NormalizePath(&ret.value_); 148 NormalizePath(&ret.value_);
123 return ret; 149 return ret;
124 } else if (IsPathAbsolute(p)) { 150 } else if (IsPathAbsolute(str)) {
125 if (source_root.empty() || 151 if (source_root.empty() ||
126 !MakeAbsolutePathRelativeIfPossible(source_root, p, &ret.value_)) { 152 !MakeAbsolutePathRelativeIfPossible(source_root, str, &ret.value_)) {
127 #if defined(OS_WIN) 153 #if defined(OS_WIN)
128 if (p[0] != '/') // See the file case for why we do this check. 154 if (str[0] != '/') // See the file case for why we do this check.
129 ret.value_ = "/"; 155 ret.value_ = "/";
130 #endif 156 #endif
131 ret.value_.append(p.data(), p.size()); 157 ret.value_.append(str.data(), str.size());
132 } 158 }
133 NormalizePath(&ret.value_); 159 NormalizePath(&ret.value_);
134 if (!EndsWithSlash(ret.value_)) 160 if (!EndsWithSlash(ret.value_))
135 ret.value_.push_back('/'); 161 ret.value_.push_back('/');
136 return ret; 162 return ret;
137 } 163 }
138 164
139 if (!source_root.empty()) { 165 if (!source_root.empty()) {
140 std::string absolute = 166 std::string absolute =
141 FilePathToUTF8(Resolve(UTF8ToFilePath(source_root)).AppendASCII( 167 FilePathToUTF8(Resolve(UTF8ToFilePath(source_root)).AppendASCII(
142 p.as_string()).value()); 168 str.as_string()).value());
143 NormalizePath(&absolute); 169 NormalizePath(&absolute);
144 if (!MakeAbsolutePathRelativeIfPossible(source_root, absolute, 170 if (!MakeAbsolutePathRelativeIfPossible(source_root, absolute,
145 &ret.value_)) { 171 &ret.value_)) {
146 #if defined(OS_WIN) 172 #if defined(OS_WIN)
147 if (absolute[0] != '/') // See the file case for why we do this check. 173 if (absolute[0] != '/') // See the file case for why we do this check.
148 ret.value_ = "/"; 174 ret.value_ = "/";
149 #endif 175 #endif
150 ret.value_.append(absolute.data(), absolute.size()); 176 ret.value_.append(absolute.data(), absolute.size());
151 } 177 }
152 if (!EndsWithSlash(ret.value_)) 178 if (!EndsWithSlash(ret.value_))
153 ret.value_.push_back('/'); 179 ret.value_.push_back('/');
154 return ret; 180 return ret;
155 } 181 }
156 182
157 ret.value_.reserve(value_.size() + p.size()); 183 ret.value_.reserve(value_.size() + str.size());
158 ret.value_.assign(value_); 184 ret.value_.assign(value_);
159 ret.value_.append(p.data(), p.size()); 185 ret.value_.append(str.data(), str.size());
160 186
161 NormalizePath(&ret.value_); 187 NormalizePath(&ret.value_);
162 if (!EndsWithSlash(ret.value_)) 188 if (!EndsWithSlash(ret.value_))
163 ret.value_.push_back('/'); 189 ret.value_.push_back('/');
164 AssertValueSourceDirString(ret.value_); 190 AssertValueSourceDirString(ret.value_);
165 191
166 return ret; 192 return ret;
167 } 193 }
168 194
169 base::FilePath SourceDir::Resolve(const base::FilePath& source_root) const { 195 base::FilePath SourceDir::Resolve(const base::FilePath& source_root) const {
(...skipping 14 matching lines...) Expand all
184 // String the double-leading slash for source-relative paths. 210 // String the double-leading slash for source-relative paths.
185 converted.assign(&value_[2], value_.size() - 2); 211 converted.assign(&value_[2], value_.size() - 2);
186 return source_root.Append(UTF8ToFilePath(converted)) 212 return source_root.Append(UTF8ToFilePath(converted))
187 .NormalizePathSeparatorsTo('/'); 213 .NormalizePathSeparatorsTo('/');
188 } 214 }
189 215
190 void SourceDir::SwapValue(std::string* v) { 216 void SourceDir::SwapValue(std::string* v) {
191 value_.swap(*v); 217 value_.swap(*v);
192 AssertValueSourceDirString(value_); 218 AssertValueSourceDirString(value_);
193 } 219 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698