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 "tools/gn/file_template.h" | 5 #include "tools/gn/file_template.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <iostream> | 8 #include <iostream> |
9 | 9 |
10 #include "tools/gn/escape.h" | 10 #include "tools/gn/escape.h" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 ParseInput(t, err); | 84 ParseInput(t, err); |
85 } | 85 } |
86 | 86 |
87 FileTemplate::FileTemplate(const std::vector<std::string>& t) | 87 FileTemplate::FileTemplate(const std::vector<std::string>& t) |
88 : has_substitutions_(false) { | 88 : has_substitutions_(false) { |
89 std::fill(types_required_, &types_required_[Subrange::NUM_TYPES], false); | 89 std::fill(types_required_, &types_required_[Subrange::NUM_TYPES], false); |
90 for (size_t i = 0; i < t.size(); i++) | 90 for (size_t i = 0; i < t.size(); i++) |
91 ParseOneTemplateString(t[i]); | 91 ParseOneTemplateString(t[i]); |
92 } | 92 } |
93 | 93 |
| 94 FileTemplate::FileTemplate(const std::vector<SourceFile>& t) |
| 95 : has_substitutions_(false) { |
| 96 std::fill(types_required_, &types_required_[Subrange::NUM_TYPES], false); |
| 97 for (size_t i = 0; i < t.size(); i++) |
| 98 ParseOneTemplateString(t[i].value()); |
| 99 } |
| 100 |
94 FileTemplate::~FileTemplate() { | 101 FileTemplate::~FileTemplate() { |
95 } | 102 } |
96 | 103 |
97 // static | 104 // static |
98 FileTemplate FileTemplate::GetForTargetOutputs(const Target* target) { | 105 FileTemplate FileTemplate::GetForTargetOutputs(const Target* target) { |
99 const Target::FileList& outputs = target->action_values().outputs(); | 106 const Target::FileList& outputs = target->action_values().outputs(); |
100 std::vector<std::string> output_template_args; | 107 std::vector<std::string> output_template_args; |
101 for (size_t i = 0; i < outputs.size(); i++) | 108 for (size_t i = 0; i < outputs.size(); i++) |
102 output_template_args.push_back(outputs[i].value()); | 109 output_template_args.push_back(outputs[i].value()); |
103 return FileTemplate(output_template_args); | 110 return FileTemplate(output_template_args); |
(...skipping 10 matching lines...) Expand all Loading... |
114 Err* err) const { | 121 Err* err) const { |
115 if (!sources.VerifyTypeIs(Value::LIST, err)) | 122 if (!sources.VerifyTypeIs(Value::LIST, err)) |
116 return; | 123 return; |
117 dest->reserve(sources.list_value().size() * templates_.container().size()); | 124 dest->reserve(sources.list_value().size() * templates_.container().size()); |
118 | 125 |
119 // Temporary holding place, allocate outside to re-use- buffer. | 126 // Temporary holding place, allocate outside to re-use- buffer. |
120 std::vector<std::string> string_output; | 127 std::vector<std::string> string_output; |
121 | 128 |
122 const std::vector<Value>& sources_list = sources.list_value(); | 129 const std::vector<Value>& sources_list = sources.list_value(); |
123 for (size_t i = 0; i < sources_list.size(); i++) { | 130 for (size_t i = 0; i < sources_list.size(); i++) { |
| 131 string_output.clear(); |
124 if (!sources_list[i].VerifyTypeIs(Value::STRING, err)) | 132 if (!sources_list[i].VerifyTypeIs(Value::STRING, err)) |
125 return; | 133 return; |
126 | 134 |
127 ApplyString(sources_list[i].string_value(), &string_output); | 135 ApplyString(sources_list[i].string_value(), &string_output); |
128 for (size_t out_i = 0; out_i < string_output.size(); out_i++) | 136 for (size_t out_i = 0; out_i < string_output.size(); out_i++) |
129 dest->push_back(Value(origin, string_output[out_i])); | 137 dest->push_back(Value(origin, string_output[out_i])); |
130 } | 138 } |
131 } | 139 } |
132 | 140 |
133 void FileTemplate::ApplyString(const std::string& str, | 141 void FileTemplate::ApplyString(const std::string& str, |
134 std::vector<std::string>* output) const { | 142 std::vector<std::string>* output) const { |
135 // Compute all substitutions needed so we can just do substitutions below. | 143 // Compute all substitutions needed so we can just do substitutions below. |
136 // We skip the LITERAL one since that varies each time. | 144 // We skip the LITERAL one since that varies each time. |
137 std::string subst[Subrange::NUM_TYPES]; | 145 std::string subst[Subrange::NUM_TYPES]; |
138 for (int i = 1; i < Subrange::NUM_TYPES; i++) { | 146 for (int i = 1; i < Subrange::NUM_TYPES; i++) { |
139 if (types_required_[i]) | 147 if (types_required_[i]) |
140 subst[i] = GetSubstitution(str, static_cast<Subrange::Type>(i)); | 148 subst[i] = GetSubstitution(str, static_cast<Subrange::Type>(i)); |
141 } | 149 } |
142 | 150 |
143 output->resize(templates_.container().size()); | 151 size_t first_output_index = output->size(); |
| 152 output->resize(output->size() + templates_.container().size()); |
144 for (size_t template_i = 0; | 153 for (size_t template_i = 0; |
145 template_i < templates_.container().size(); template_i++) { | 154 template_i < templates_.container().size(); template_i++) { |
146 const Template& t = templates_[template_i]; | 155 const Template& t = templates_[template_i]; |
147 (*output)[template_i].clear(); | 156 std::string& cur_output = (*output)[first_output_index + template_i]; |
148 for (size_t subrange_i = 0; subrange_i < t.container().size(); | 157 for (size_t subrange_i = 0; subrange_i < t.container().size(); |
149 subrange_i++) { | 158 subrange_i++) { |
150 if (t[subrange_i].type == Subrange::LITERAL) | 159 if (t[subrange_i].type == Subrange::LITERAL) |
151 (*output)[template_i].append(t[subrange_i].literal); | 160 cur_output.append(t[subrange_i].literal); |
152 else | 161 else |
153 (*output)[template_i].append(subst[t[subrange_i].type]); | 162 cur_output.append(subst[t[subrange_i].type]); |
154 } | 163 } |
155 } | 164 } |
156 } | 165 } |
157 | 166 |
158 void FileTemplate::WriteWithNinjaExpansions(std::ostream& out) const { | 167 void FileTemplate::WriteWithNinjaExpansions(std::ostream& out) const { |
159 EscapeOptions escape_options; | 168 EscapeOptions escape_options; |
160 escape_options.mode = ESCAPE_NINJA_SHELL; | 169 escape_options.mode = ESCAPE_NINJA_SHELL; |
161 escape_options.inhibit_quoting = true; | 170 escape_options.inhibit_quoting = true; |
162 | 171 |
163 for (size_t template_i = 0; | 172 for (size_t template_i = 0; |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 cur = next + arraysize(kSourceFilePart) - 1; | 304 cur = next + arraysize(kSourceFilePart) - 1; |
296 } else { | 305 } else { |
297 // If it's not a match, treat it like a one-char literal (this will be | 306 // If it's not a match, treat it like a one-char literal (this will be |
298 // rare, so it's not worth the bother to add to the previous literal) so | 307 // rare, so it's not worth the bother to add to the previous literal) so |
299 // we can keep going. | 308 // we can keep going. |
300 t.container().push_back(Subrange(Subrange::LITERAL, "{")); | 309 t.container().push_back(Subrange(Subrange::LITERAL, "{")); |
301 cur = next + 1; | 310 cur = next + 1; |
302 } | 311 } |
303 } | 312 } |
304 } | 313 } |
OLD | NEW |