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
,
28 if (!value
.VerifyTypeIs(Value::LIST
, err
)) {
29 CHECK(err
->has_error());
33 for (const auto& item
: value
.list_value()) {
34 patterns_
.push_back(LabelPattern::GetPattern(current_dir
, item
, err
));
41 void Visibility::SetPublic() {
44 LabelPattern(LabelPattern::RECURSIVE_DIRECTORY
, SourceDir(),
45 std::string(), Label()));
48 void Visibility::SetPrivate(const SourceDir
& current_dir
) {
51 LabelPattern(LabelPattern::DIRECTORY
, current_dir
, std::string(),
55 bool Visibility::CanSeeMe(const Label
& label
) const {
56 for (const auto& pattern
: patterns_
) {
57 if (pattern
.Matches(label
))
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";
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";
82 result
+= outer_indent_string
+ "]\n";
87 bool Visibility::CheckItemVisibility(const Item
* from
,
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));
103 bool Visibility::FillItemVisibility(Item
* item
, Scope
* scope
, Err
* err
) {
104 const Value
* vis_value
= scope
->GetValue(variables::kVisibility
, true);
106 item
->visibility().Set(scope
->GetSourceDir(), *vis_value
, err
);
107 else // Default to public.
108 item
->visibility().SetPublic();
109 return !err
->has_error();