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 12 matching lines...) Expand all Loading... |
23 bool Visibility::Set(const SourceDir& current_dir, | 23 bool Visibility::Set(const SourceDir& current_dir, |
24 const Value& value, | 24 const Value& value, |
25 Err* err) { | 25 Err* err) { |
26 patterns_.clear(); | 26 patterns_.clear(); |
27 | 27 |
28 if (!value.VerifyTypeIs(Value::LIST, err)) { | 28 if (!value.VerifyTypeIs(Value::LIST, err)) { |
29 CHECK(err->has_error()); | 29 CHECK(err->has_error()); |
30 return false; | 30 return false; |
31 } | 31 } |
32 | 32 |
33 const std::vector<Value>& list = value.list_value(); | 33 for (const auto& item : value.list_value()) { |
34 for (size_t i = 0; i < list.size(); i++) { | 34 patterns_.push_back(LabelPattern::GetPattern(current_dir, item, err)); |
35 patterns_.push_back(LabelPattern::GetPattern(current_dir, list[i], err)); | |
36 if (err->has_error()) | 35 if (err->has_error()) |
37 return false; | 36 return false; |
38 } | 37 } |
39 return true; | 38 return true; |
40 } | 39 } |
41 | 40 |
42 void Visibility::SetPublic() { | 41 void Visibility::SetPublic() { |
43 patterns_.clear(); | 42 patterns_.clear(); |
44 patterns_.push_back( | 43 patterns_.push_back( |
45 LabelPattern(LabelPattern::RECURSIVE_DIRECTORY, SourceDir(), | 44 LabelPattern(LabelPattern::RECURSIVE_DIRECTORY, SourceDir(), |
46 std::string(), Label())); | 45 std::string(), Label())); |
47 } | 46 } |
48 | 47 |
49 void Visibility::SetPrivate(const SourceDir& current_dir) { | 48 void Visibility::SetPrivate(const SourceDir& current_dir) { |
50 patterns_.clear(); | 49 patterns_.clear(); |
51 patterns_.push_back( | 50 patterns_.push_back( |
52 LabelPattern(LabelPattern::DIRECTORY, current_dir, std::string(), | 51 LabelPattern(LabelPattern::DIRECTORY, current_dir, std::string(), |
53 Label())); | 52 Label())); |
54 } | 53 } |
55 | 54 |
56 bool Visibility::CanSeeMe(const Label& label) const { | 55 bool Visibility::CanSeeMe(const Label& label) const { |
57 for (size_t i = 0; i < patterns_.size(); i++) { | 56 for (const auto& pattern : patterns_) { |
58 if (patterns_[i].Matches(label)) | 57 if (pattern.Matches(label)) |
59 return true; | 58 return true; |
60 } | 59 } |
61 return false; | 60 return false; |
62 } | 61 } |
63 | 62 |
64 std::string Visibility::Describe(int indent, bool include_brackets) const { | 63 std::string Visibility::Describe(int indent, bool include_brackets) const { |
65 std::string outer_indent_string(indent, ' '); | 64 std::string outer_indent_string(indent, ' '); |
66 | 65 |
67 if (patterns_.empty()) | 66 if (patterns_.empty()) |
68 return outer_indent_string + "[] (no visibility)\n"; | 67 return outer_indent_string + "[] (no visibility)\n"; |
69 | 68 |
70 std::string result; | 69 std::string result; |
71 | 70 |
72 std::string inner_indent_string = outer_indent_string; | 71 std::string inner_indent_string = outer_indent_string; |
73 if (include_brackets) { | 72 if (include_brackets) { |
74 result += outer_indent_string + "[\n"; | 73 result += outer_indent_string + "[\n"; |
75 // Indent the insides more if brackets are requested. | 74 // Indent the insides more if brackets are requested. |
76 inner_indent_string += " "; | 75 inner_indent_string += " "; |
77 } | 76 } |
78 | 77 |
79 for (size_t i = 0; i < patterns_.size(); i++) | 78 for (const auto& pattern : patterns_) |
80 result += inner_indent_string + patterns_[i].Describe() + "\n"; | 79 result += inner_indent_string + pattern.Describe() + "\n"; |
81 | 80 |
82 if (include_brackets) | 81 if (include_brackets) |
83 result += outer_indent_string + "]\n"; | 82 result += outer_indent_string + "]\n"; |
84 return result; | 83 return result; |
85 } | 84 } |
86 | 85 |
87 // static | 86 // static |
88 bool Visibility::CheckItemVisibility(const Item* from, | 87 bool Visibility::CheckItemVisibility(const Item* from, |
89 const Item* to, | 88 const Item* to, |
90 Err* err) { | 89 Err* err) { |
(...skipping 11 matching lines...) Expand all Loading... |
102 | 101 |
103 // static | 102 // static |
104 bool Visibility::FillItemVisibility(Item* item, Scope* scope, Err* err) { | 103 bool Visibility::FillItemVisibility(Item* item, Scope* scope, Err* err) { |
105 const Value* vis_value = scope->GetValue(variables::kVisibility, true); | 104 const Value* vis_value = scope->GetValue(variables::kVisibility, true); |
106 if (vis_value) | 105 if (vis_value) |
107 item->visibility().Set(scope->GetSourceDir(), *vis_value, err); | 106 item->visibility().Set(scope->GetSourceDir(), *vis_value, err); |
108 else // Default to public. | 107 else // Default to public. |
109 item->visibility().SetPublic(); | 108 item->visibility().SetPublic(); |
110 return !err->has_error(); | 109 return !err->has_error(); |
111 } | 110 } |
OLD | NEW |