OLD | NEW |
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 <map> | 5 #include <map> |
6 | 6 |
7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "tools/gn/commands.h" | 9 #include "tools/gn/commands.h" |
10 #include "tools/gn/input_file.h" | 10 #include "tools/gn/input_file.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 | 35 |
36 size_t cur = offset; | 36 size_t cur = offset; |
37 do { | 37 do { |
38 cur --; | 38 cur --; |
39 if (Tokenizer::IsNewline(data, cur)) | 39 if (Tokenizer::IsNewline(data, cur)) |
40 return cur + 1; // Want the first character *after* the newline. | 40 return cur + 1; // Want the first character *after* the newline. |
41 } while (cur > 0); | 41 } while (cur > 0); |
42 return 0; | 42 return 0; |
43 } | 43 } |
44 | 44 |
45 // Assumes DoesLineBeginWithComment(). | 45 // Assumes DoesLineBeginWithComment(), this strips the # character from the |
46 std::string StripCommentFromLine(const base::StringPiece& line) { | 46 // beginning and normalizes preceeding whitespace. |
47 std::string ret = line.as_string(); | 47 std::string StripHashFromLine(const base::StringPiece& line) { |
48 for (size_t i = 0; i < ret.size(); i++) { | 48 // Replace the # sign and everything before it with 3 spaces, so that a |
49 if (ret[i] == '#') { | 49 // normal comment that has a space after the # will be indented 4 spaces |
50 ret[i] = ' '; | 50 // (which makes our formatting come out nicely). If the comment is indented |
51 break; | 51 // from there, we want to preserve that indenting. |
52 } | 52 return " " + line.substr(line.find('#') + 1).as_string(); |
53 } | |
54 return ret; | |
55 } | 53 } |
56 | 54 |
57 // Tries to find the comment before the setting of the given value. | 55 // Tries to find the comment before the setting of the given value. |
58 void GetContextForValue(const Value& value, | 56 void GetContextForValue(const Value& value, |
59 std::string* location_str, | 57 std::string* location_str, |
60 std::string* comment) { | 58 std::string* comment) { |
61 Location location = value.origin()->GetRange().begin(); | 59 Location location = value.origin()->GetRange().begin(); |
62 const InputFile* file = location.file(); | 60 const InputFile* file = location.file(); |
63 if (!file) | 61 if (!file) |
64 return; | 62 return; |
65 | 63 |
66 *location_str = file->name().value() + ":" + | 64 *location_str = file->name().value() + ":" + |
67 base::IntToString(location.line_number()); | 65 base::IntToString(location.line_number()); |
68 | 66 |
69 const std::string& data = file->contents(); | 67 const std::string& data = file->contents(); |
70 size_t line_off = | 68 size_t line_off = |
71 Tokenizer::ByteOffsetOfNthLine(data, location.line_number()); | 69 Tokenizer::ByteOffsetOfNthLine(data, location.line_number()); |
72 | 70 |
73 while (line_off > 1) { | 71 while (line_off > 1) { |
74 line_off -= 2; // Back up to end of previous line. | 72 line_off -= 2; // Back up to end of previous line. |
75 size_t previous_line_offset = BackUpToLineBegin(data, line_off); | 73 size_t previous_line_offset = BackUpToLineBegin(data, line_off); |
76 | 74 |
77 base::StringPiece line(&data[previous_line_offset], | 75 base::StringPiece line(&data[previous_line_offset], |
78 line_off - previous_line_offset + 1); | 76 line_off - previous_line_offset + 1); |
79 if (!DoesLineBeginWithComment(line)) | 77 if (!DoesLineBeginWithComment(line)) |
80 break; | 78 break; |
81 | 79 |
82 comment->insert(0, StripCommentFromLine(line) + "\n"); | 80 comment->insert(0, StripHashFromLine(line) + "\n"); |
83 line_off = previous_line_offset; | 81 line_off = previous_line_offset; |
84 } | 82 } |
85 } | 83 } |
86 | 84 |
87 void PrintArgHelp(const base::StringPiece& name, const Value& value) { | 85 void PrintArgHelp(const base::StringPiece& name, const Value& value) { |
88 OutputString(name.as_string(), DECORATION_YELLOW); | 86 OutputString(name.as_string(), DECORATION_YELLOW); |
89 OutputString(" Default = " + value.ToString(true) + "\n"); | 87 OutputString(" Default = " + value.ToString(true) + "\n"); |
90 | 88 |
91 if (value.origin()) { | 89 if (value.origin()) { |
92 std::string location, comment; | 90 std::string location, comment; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 "Usage: \"gn args [arg name]\"").PrintToStdout(); | 141 "Usage: \"gn args [arg name]\"").PrintToStdout(); |
144 return 1; | 142 return 1; |
145 } | 143 } |
146 | 144 |
147 // List all arguments. First put them in a regular map so they're sorted. | 145 // List all arguments. First put them in a regular map so they're sorted. |
148 std::map<base::StringPiece, Value> sorted_args; | 146 std::map<base::StringPiece, Value> sorted_args; |
149 for (Scope::KeyValueMap::const_iterator i = build_args.begin(); | 147 for (Scope::KeyValueMap::const_iterator i = build_args.begin(); |
150 i != build_args.end(); ++i) | 148 i != build_args.end(); ++i) |
151 sorted_args.insert(*i); | 149 sorted_args.insert(*i); |
152 | 150 |
| 151 OutputString( |
| 152 "Available build arguments. Note that the which arguments are declared\n" |
| 153 "and their default values may depend on other arguments or the current\n" |
| 154 "platform and architecture. So setting some values may add, remove, or\n" |
| 155 "change the default value of other values.\n\n"); |
| 156 |
153 for (std::map<base::StringPiece, Value>::iterator i = sorted_args.begin(); | 157 for (std::map<base::StringPiece, Value>::iterator i = sorted_args.begin(); |
154 i != sorted_args.end(); ++i) { | 158 i != sorted_args.end(); ++i) { |
155 PrintArgHelp(i->first, i->second); | 159 PrintArgHelp(i->first, i->second); |
156 OutputString("\n"); | 160 OutputString("\n"); |
157 } | 161 } |
158 | 162 |
159 return 0; | 163 return 0; |
160 } | 164 } |
161 | 165 |
162 } // namespace commands | 166 } // namespace commands |
OLD | NEW |