OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/visibility.h" | 5 #include "tools/gn/visibility.h" |
6 | 6 |
7 #include "base/strings/string_piece.h" | 7 #include "base/strings/string_piece.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "tools/gn/err.h" | 9 #include "tools/gn/err.h" |
10 #include "tools/gn/filesystem_utils.h" | 10 #include "tools/gn/filesystem_utils.h" |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 } | 90 } |
91 | 91 |
92 bool Visibility::CanSeeMe(const Label& label) const { | 92 bool Visibility::CanSeeMe(const Label& label) const { |
93 for (size_t i = 0; i < patterns_.size(); i++) { | 93 for (size_t i = 0; i < patterns_.size(); i++) { |
94 if (patterns_[i].Matches(label)) | 94 if (patterns_[i].Matches(label)) |
95 return true; | 95 return true; |
96 } | 96 } |
97 return false; | 97 return false; |
98 } | 98 } |
99 | 99 |
100 std::string Visibility::Describe() const { | 100 std::string Visibility::Describe(int indent, bool include_brackets) const { |
| 101 std::string outer_indent_string(indent, ' '); |
| 102 |
101 if (patterns_.empty()) | 103 if (patterns_.empty()) |
102 return std::string("[] (no visibility)"); | 104 return outer_indent_string + "[] (no visibility)\n"; |
103 std::string result = "[\n"; | 105 |
| 106 std::string result; |
| 107 |
| 108 std::string inner_indent_string = outer_indent_string; |
| 109 if (include_brackets) { |
| 110 result += outer_indent_string + "[\n"; |
| 111 // Indent the insides more if brackets are requested. |
| 112 inner_indent_string += " "; |
| 113 } |
104 | 114 |
105 for (size_t i = 0; i < patterns_.size(); i++) { | 115 for (size_t i = 0; i < patterns_.size(); i++) { |
106 switch (patterns_[i].type()) { | 116 switch (patterns_[i].type()) { |
107 case VisPattern::MATCH: | 117 case VisPattern::MATCH: |
108 result += " " + patterns_[i].dir().value() + ":" + | 118 result += inner_indent_string + |
| 119 DirectoryWithNoLastSlash(patterns_[i].dir()) + ":" + |
109 patterns_[i].name() + "\n"; | 120 patterns_[i].name() + "\n"; |
110 break; | 121 break; |
111 case VisPattern::DIRECTORY: | 122 case VisPattern::DIRECTORY: |
112 result += " " + DirectoryWithNoLastSlash(patterns_[i].dir()) + | 123 result += inner_indent_string + |
113 ":*\n"; | 124 DirectoryWithNoLastSlash(patterns_[i].dir()) + ":*\n"; |
114 break; | 125 break; |
115 case VisPattern::RECURSIVE_DIRECTORY: | 126 case VisPattern::RECURSIVE_DIRECTORY: |
116 result += " " + patterns_[i].dir().value() + "*\n"; | 127 result += inner_indent_string + patterns_[i].dir().value() + "*\n"; |
117 break; | 128 break; |
118 } | 129 } |
119 } | 130 } |
120 | 131 |
121 result += "]"; | 132 if (include_brackets) |
| 133 result += outer_indent_string + "]\n"; |
122 return result; | 134 return result; |
123 } | 135 } |
124 | 136 |
125 // static | 137 // static |
126 Visibility::VisPattern Visibility::GetPattern(const SourceDir& current_dir, | 138 Visibility::VisPattern Visibility::GetPattern(const SourceDir& current_dir, |
127 const Value& value, | 139 const Value& value, |
128 Err* err) { | 140 Err* err) { |
129 if (!value.VerifyTypeIs(Value::STRING, err)) | 141 if (!value.VerifyTypeIs(Value::STRING, err)) |
130 return VisPattern(); | 142 return VisPattern(); |
131 | 143 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 // static | 249 // static |
238 bool Visibility::CheckItemVisibility(const Item* from, | 250 bool Visibility::CheckItemVisibility(const Item* from, |
239 const Item* to, | 251 const Item* to, |
240 Err* err) { | 252 Err* err) { |
241 if (!to->visibility().CanSeeMe(from->label())) { | 253 if (!to->visibility().CanSeeMe(from->label())) { |
242 std::string to_label = to->label().GetUserVisibleName(false); | 254 std::string to_label = to->label().GetUserVisibleName(false); |
243 *err = Err(from->defined_from(), "Dependency not allowed.", | 255 *err = Err(from->defined_from(), "Dependency not allowed.", |
244 "The item " + from->label().GetUserVisibleName(false) + "\n" | 256 "The item " + from->label().GetUserVisibleName(false) + "\n" |
245 "can not depend on " + to_label + "\n" | 257 "can not depend on " + to_label + "\n" |
246 "because it is not in " + to_label + "'s visibility list: " + | 258 "because it is not in " + to_label + "'s visibility list: " + |
247 to->visibility().Describe()); | 259 to->visibility().Describe(0, true)); |
248 return false; | 260 return false; |
249 } | 261 } |
250 return true; | 262 return true; |
251 } | 263 } |
252 | 264 |
253 // static | 265 // static |
254 bool Visibility::FillItemVisibility(Item* item, Scope* scope, Err* err) { | 266 bool Visibility::FillItemVisibility(Item* item, Scope* scope, Err* err) { |
255 const Value* vis_value = scope->GetValue(variables::kVisibility, true); | 267 const Value* vis_value = scope->GetValue(variables::kVisibility, true); |
256 if (vis_value) | 268 if (vis_value) |
257 item->visibility().Set(scope->GetSourceDir(), *vis_value, err); | 269 item->visibility().Set(scope->GetSourceDir(), *vis_value, err); |
258 else // Default to public. | 270 else // Default to public. |
259 item->visibility().SetPublic(); | 271 item->visibility().SetPublic(); |
260 return !err->has_error(); | 272 return !err->has_error(); |
261 } | 273 } |
OLD | NEW |