| 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/substitution_pattern.h" | 5 #include "tools/gn/substitution_pattern.h" |
| 6 | 6 |
| 7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "tools/gn/build_settings.h" |
| 8 #include "tools/gn/err.h" | 9 #include "tools/gn/err.h" |
| 10 #include "tools/gn/filesystem_utils.h" |
| 9 #include "tools/gn/value.h" | 11 #include "tools/gn/value.h" |
| 10 | 12 |
| 11 SubstitutionPattern::Subrange::Subrange() | 13 SubstitutionPattern::Subrange::Subrange() |
| 12 : type(SUBSTITUTION_LITERAL) { | 14 : type(SUBSTITUTION_LITERAL) { |
| 13 } | 15 } |
| 14 | 16 |
| 15 SubstitutionPattern::Subrange::Subrange(SubstitutionType t, | 17 SubstitutionPattern::Subrange::Subrange(SubstitutionType t, |
| 16 const std::string& l) | 18 const std::string& l) |
| 17 : type(t), | 19 : type(t), |
| 18 literal(l) { | 20 literal(l) { |
| 19 } | 21 } |
| 20 | 22 |
| 21 SubstitutionPattern::Subrange::~Subrange() { | 23 SubstitutionPattern::Subrange::~Subrange() { |
| 22 } | 24 } |
| 23 | 25 |
| 24 SubstitutionPattern::SubstitutionPattern() { | 26 SubstitutionPattern::SubstitutionPattern() : origin_(NULL) { |
| 25 } | 27 } |
| 26 | 28 |
| 27 SubstitutionPattern::~SubstitutionPattern() { | 29 SubstitutionPattern::~SubstitutionPattern() { |
| 28 } | 30 } |
| 29 | 31 |
| 30 bool SubstitutionPattern::Parse(const Value& value, Err* err) { | 32 bool SubstitutionPattern::Parse(const Value& value, Err* err) { |
| 31 if (!value.VerifyTypeIs(Value::STRING, err)) | 33 if (!value.VerifyTypeIs(Value::STRING, err)) |
| 32 return false; | 34 return false; |
| 33 return Parse(value.string_value(), value.origin(), err); | 35 return Parse(value.string_value(), value.origin(), err); |
| 34 } | 36 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 // to exactly indicate the error location is tricky. | 75 // to exactly indicate the error location is tricky. |
| 74 *err = Err(origin, "Unknown substitution pattern", | 76 *err = Err(origin, "Unknown substitution pattern", |
| 75 "Found a {{ at offset " + | 77 "Found a {{ at offset " + |
| 76 base::SizeTToString(next) + | 78 base::SizeTToString(next) + |
| 77 " and did not find a known substitution following it."); | 79 " and did not find a known substitution following it."); |
| 78 ranges_.clear(); | 80 ranges_.clear(); |
| 79 return false; | 81 return false; |
| 80 } | 82 } |
| 81 } | 83 } |
| 82 | 84 |
| 85 origin_ = origin; |
| 86 |
| 83 // Fill required types vector. | 87 // Fill required types vector. |
| 84 bool required_type_bits[SUBSTITUTION_NUM_TYPES] = {0}; | 88 SubstitutionBits bits; |
| 85 FillRequiredTypes(required_type_bits); | 89 FillRequiredTypes(&bits); |
| 86 | 90 bits.FillVector(&required_types_); |
| 87 for (size_t i = SUBSTITUTION_FIRST_PATTERN; i < SUBSTITUTION_NUM_TYPES; i++) { | |
| 88 if (required_type_bits[i]) | |
| 89 required_types_.push_back(static_cast<SubstitutionType>(i)); | |
| 90 } | |
| 91 return true; | 91 return true; |
| 92 } | 92 } |
| 93 | 93 |
| 94 std::string SubstitutionPattern::AsString() const { | 94 std::string SubstitutionPattern::AsString() const { |
| 95 std::string result; | 95 std::string result; |
| 96 for (size_t i = 0; i < ranges_.size(); i++) { | 96 for (size_t i = 0; i < ranges_.size(); i++) { |
| 97 if (ranges_[i].type == SUBSTITUTION_LITERAL) | 97 if (ranges_[i].type == SUBSTITUTION_LITERAL) |
| 98 result.append(ranges_[i].literal); | 98 result.append(ranges_[i].literal); |
| 99 else | 99 else |
| 100 result.append(kSubstitutionNames[ranges_[i].type]); | 100 result.append(kSubstitutionNames[ranges_[i].type]); |
| 101 } | 101 } |
| 102 return result; | 102 return result; |
| 103 } | 103 } |
| 104 | 104 |
| 105 void SubstitutionPattern::FillRequiredTypes( | 105 void SubstitutionPattern::FillRequiredTypes(SubstitutionBits* bits) const { |
| 106 bool required_types[SUBSTITUTION_NUM_TYPES]) const { | |
| 107 for (size_t i = 0; i < ranges_.size(); i++) { | 106 for (size_t i = 0; i < ranges_.size(); i++) { |
| 108 if (ranges_[i].type != SUBSTITUTION_LITERAL) | 107 if (ranges_[i].type != SUBSTITUTION_LITERAL) |
| 109 required_types[static_cast<size_t>(ranges_[i].type)] = true; | 108 bits->used[static_cast<size_t>(ranges_[i].type)] = true; |
| 110 } | 109 } |
| 111 } | 110 } |
| 111 |
| 112 bool SubstitutionPattern::IsInOutputDir(const BuildSettings* build_settings, |
| 113 Err* err) const { |
| 114 if (ranges_.empty()) { |
| 115 *err = Err(origin_, "This is empty but I was expecting an output file."); |
| 116 return false; |
| 117 } |
| 118 |
| 119 if (ranges_[0].type == SUBSTITUTION_LITERAL) { |
| 120 // If the first thing is a literal, it must start with the output dir. |
| 121 if (!EnsureStringIsInOutputDir( |
| 122 build_settings->build_dir(), |
| 123 ranges_[0].literal, origin_, err)) |
| 124 return false; |
| 125 } else { |
| 126 // Otherwise, the first subrange must be a pattern that expands to |
| 127 // something in the output directory. |
| 128 if (!SubstitutionIsInOutputDir(ranges_[0].type)) { |
| 129 *err = Err(origin_, |
| 130 "File is not inside output directory.", |
| 131 "The given file should be in the output directory. Normally you\n" |
| 132 "would specify\n\"$target_out_dir/foo\" or " |
| 133 "\"{{source_gen_dir}}/foo\"."); |
| 134 return false; |
| 135 } |
| 136 } |
| 137 |
| 138 return true; |
| 139 } |
| OLD | NEW |