Add ICU message format support
[chromium-blink-merge.git] / tools / gn / visibility.cc
bloba61a5cd9c45f7effb57fd053be21ddaf29a6122e
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
3 // found in the LICENSE file.
5 #include "tools/gn/visibility.h"
7 #include "base/strings/string_piece.h"
8 #include "base/strings/string_util.h"
9 #include "tools/gn/err.h"
10 #include "tools/gn/filesystem_utils.h"
11 #include "tools/gn/item.h"
12 #include "tools/gn/label.h"
13 #include "tools/gn/scope.h"
14 #include "tools/gn/value.h"
15 #include "tools/gn/variables.h"
17 Visibility::Visibility() {
20 Visibility::~Visibility() {
23 bool Visibility::Set(const SourceDir& current_dir,
24 const Value& value,
25 Err* err) {
26 patterns_.clear();
28 if (!value.VerifyTypeIs(Value::LIST, err)) {
29 CHECK(err->has_error());
30 return false;
33 for (const auto& item : value.list_value()) {
34 patterns_.push_back(LabelPattern::GetPattern(current_dir, item, err));
35 if (err->has_error())
36 return false;
38 return true;
41 void Visibility::SetPublic() {
42 patterns_.clear();
43 patterns_.push_back(
44 LabelPattern(LabelPattern::RECURSIVE_DIRECTORY, SourceDir(),
45 std::string(), Label()));
48 void Visibility::SetPrivate(const SourceDir& current_dir) {
49 patterns_.clear();
50 patterns_.push_back(
51 LabelPattern(LabelPattern::DIRECTORY, current_dir, std::string(),
52 Label()));
55 bool Visibility::CanSeeMe(const Label& label) const {
56 for (const auto& pattern : patterns_) {
57 if (pattern.Matches(label))
58 return true;
60 return false;
63 std::string Visibility::Describe(int indent, bool include_brackets) const {
64 std::string outer_indent_string(indent, ' ');
66 if (patterns_.empty())
67 return outer_indent_string + "[] (no visibility)\n";
69 std::string result;
71 std::string inner_indent_string = outer_indent_string;
72 if (include_brackets) {
73 result += outer_indent_string + "[\n";
74 // Indent the insides more if brackets are requested.
75 inner_indent_string += " ";
78 for (const auto& pattern : patterns_)
79 result += inner_indent_string + pattern.Describe() + "\n";
81 if (include_brackets)
82 result += outer_indent_string + "]\n";
83 return result;
86 // static
87 bool Visibility::CheckItemVisibility(const Item* from,
88 const Item* to,
89 Err* err) {
90 if (!to->visibility().CanSeeMe(from->label())) {
91 std::string to_label = to->label().GetUserVisibleName(false);
92 *err = Err(from->defined_from(), "Dependency not allowed.",
93 "The item " + from->label().GetUserVisibleName(false) + "\n"
94 "can not depend on " + to_label + "\n"
95 "because it is not in " + to_label + "'s visibility list: " +
96 to->visibility().Describe(0, true));
97 return false;
99 return true;
102 // static
103 bool Visibility::FillItemVisibility(Item* item, Scope* scope, Err* err) {
104 const Value* vis_value = scope->GetValue(variables::kVisibility, true);
105 if (vis_value)
106 item->visibility().Set(scope->GetSourceDir(), *vis_value, err);
107 else // Default to public.
108 item->visibility().SetPublic();
109 return !err->has_error();