Procházet zdrojové kódy

Don't use VarDecl replacements on range for loops. (#604)

Jon Meow před 4 roky
rodič
revize
8e31e46c66

+ 5 - 1
migrate_cpp/cpp_refactoring/var_decl.cpp

@@ -14,7 +14,11 @@ namespace Carbon {
 VarDecl::VarDecl(std::map<std::string, Replacements>& in_replacements,
                  cam::MatchFinder* finder)
     : Matcher(in_replacements) {
-  finder->addMatcher(cam::varDecl().bind(Label), this);
+  // Skip range-based for loops in this var-inserter.
+  finder->addMatcher(cam::varDecl(cam::unless(cam::hasParent(cam::declStmt(
+                                      cam::hasParent(cam::cxxForRangeStmt())))))
+                         .bind(Label),
+                     this);
 }
 
 // Returns a string for the type.

+ 6 - 5
migrate_cpp/cpp_refactoring/var_decl_test.cpp

@@ -129,7 +129,8 @@ TEST_F(VarDeclTest, ParamsConst) {
 }
 
 TEST_F(VarDeclTest, ParamStruct) {
-  // This is to ensure the 'struct' keyword doesn't get added to the call type.
+  // This is to ensure the 'struct' keyword doesn't get added to the qualified
+  // type.
   constexpr char Before[] = R"cpp(
     struct Circle {};
     auto Draw(int times, const Circle& circle) -> bool;
@@ -155,20 +156,20 @@ TEST_F(VarDeclTest, Member) {
   ExpectReplacement(Before, Before);
 }
 
-TEST_F(VarDeclTest, DISABLED_RangeFor) {
-  // TODO: Handle range based for loops. Test shouldn't be enabled without
-  // addressing this because the output is quirky and fragile.
+TEST_F(VarDeclTest, RangeFor) {
   constexpr char Before[] = R"cpp(
     void Foo() {
       int items[] = {1};
       for (int i : items) {
+        int j;
       }
     }
   )cpp";
   constexpr char After[] = R"(
     void Foo() {
-      var items: int[] = {1};
+      var items: int [1];
       for (int i : items) {
+        var j: int;
       }
     }
   )";

+ 8 - 8
third_party/examples/woff2/carbon/src/font.impl.carbon

@@ -31,7 +31,7 @@ fn Font::FindTable(tag: uint32_t) const -> const Font::Table* {
 fn Font::OutputOrderedTags() const -> std::vector<uint32_t> {
   var output_order: std::vector<uint32_t>;
 
-  for (const auto& i var __begin1: : var __range1: tablestables) {
+  for (const auto& i : tables) {
     var table: const Font::Table &;
     // This is a transformed table, we will write it together with the
     // original version.
@@ -88,7 +88,7 @@ fn ReadTrueTypeFont(file: woff2::Buffer *, data: const uint8_t *, len: size_t,
 
   // Check that tables are non-overlapping.
   var last_offset: uint32_t;
-  for (const auto& i var __begin1: : var __range1: intervalsintervals) {
+  for (const auto& i : intervals) {
     if (i.first < last_offset || i.first + i.second < i.first) {
       return FONT_COMPRESSION_FAILURE();
     }
@@ -114,7 +114,7 @@ fn ReadCollectionFont(file: woff2::Buffer *, data: const uint8_t *, len: size_t,
     return FONT_COMPRESSION_FAILURE();
   }
 
-  for (auto& entry var __begin1: : var __range1: fontfont) {
+  for (auto& entry : font->tables) {
     var table: Font::Table &;
 
     if (all_tables->find(table.offset) == all_tables->end()) {
@@ -152,7 +152,7 @@ fn ReadTrueTypeCollection(file: woff2::Buffer *, data: const uint8_t *, len: siz
     var font_it: auto;
 
     var all_tables: std::map<uint32_t, Font::Table *>;
-    for (const auto offset var __begin1: : var __range1: offsetsoffsets) {
+    for (const auto offset : offsets) {
       file->set_offset(offset);
       var font: woff2::Font &;
       if (!ReadCollectionFont(file, data, len, &font, &all_tables)) {
@@ -195,7 +195,7 @@ fn ReadFontCollection(data: const uint8_t *, len: size_t,
 
 fn FontFileSize(font: const woff2::Font &) -> size_t {
   var max_offset: size_t;
-  for (const auto& i var __begin1: : var __range1: fontfont) {
+  for (const auto& i : font.tables) {
     var table: const Font::Table &;
     var padding_size: size_t;
     var end_offset: size_t;
@@ -206,7 +206,7 @@ fn FontFileSize(font: const woff2::Font &) -> size_t {
 
 fn FontCollectionFileSize(font_collection: const woff2::FontCollection &) -> size_t {
   var max_offset: size_t;
-  for (auto& font var __begin1: : var __range1: font_collectionfont_collection) {
+  for (auto& font : font_collection.fonts) {
     // font file size actually just finds max offset
     max_offset = std::max(max_offset, FontFileSize(font));
   }
@@ -270,7 +270,7 @@ fn WriteFont(font: const woff2::Font &, offset: size_t *, dst: uint8_t *,
   Store16(max_pow2, offset, dst);
   Store16(range_shift, offset, dst);
 
-  for (const auto& i var __begin1: : var __range1: fontfont) {
+  for (const auto& i : font.tables) {
     if (!WriteTable(i.second, offset, dst, dst_size)) {
       return false;
     }
@@ -306,7 +306,7 @@ fn WriteFontCollection(font_collection: const woff2::FontCollection &, dst: uint
   }
 
   // Write fonts and their offsets.
-  for (const auto & font var __begin1: : var __range1: font_collectionfont_collection) {
+  for (const auto & font : font_collection.fonts) {
     StoreU32(offset, &offset_table, dst);
     if (!WriteFont(font, &offset, dst, dst_size)) {
       return false;

+ 5 - 5
third_party/examples/woff2/carbon/src/glyph.impl.carbon

@@ -226,7 +226,7 @@ fn StoreInstructions(glyph: const woff2::Glyph &, offset: size_t *, dst: uint8_t
 
 fn StoreEndPtsOfContours(glyph: const woff2::Glyph &, offset: size_t *, dst: uint8_t *) -> bool {
   var end_point: int;
-  for (const auto& contour var __begin2: : var __range2: glyphglyph) {
+  for (const auto& contour : glyph.contours) {
     end_point += contour.size();
     if (contour.size() > std::numeric_limits<uint16_t>::max() ||
         end_point > std::numeric_limits<uint16_t>::max()) {
@@ -247,8 +247,8 @@ fn StorePoints(glyph: const woff2::Glyph &, offset: size_t *,
   var y_bytes: size_t;
 
   // Store the flags and calculate the total size of the x and y coordinates.
-  for (const auto& contour var __begin2: : var __range2: glyphglyph) {
-    for (const auto& point var __begin3: : var __range3: contourcontour) {
+  for (const auto& contour : glyph.contours) {
+    for (const auto& point : contour) {
       var flag: int;
       var dx: int;
       var dy: int;
@@ -305,8 +305,8 @@ fn StorePoints(glyph: const woff2::Glyph &, offset: size_t *,
   var y_offset: size_t;
   last_x = 0;
   last_y = 0;
-  for (const auto& contour var __begin2: : var __range2: glyphglyph) {
-    for (const auto& point var __begin3: : var __range3: contourcontour) {
+  for (const auto& contour : glyph.contours) {
+    for (const auto& point : contour) {
       var dx: int;
       var dy: int;
       if (dx == 0) {

+ 7 - 7
third_party/examples/woff2/carbon/src/normalize.impl.carbon

@@ -165,7 +165,7 @@ fn NormalizeGlyphs(font: woff2::Font *) -> bool {
 
 fn NormalizeOffsets(font: woff2::Font *) -> bool {
   var offset: uint32_t;
-  for (auto tag var __begin1: : var __range1: fontfont) {
+  for (auto tag : font->OutputOrderedTags()) {
     var table: auto&;
     table.offset = offset;
     offset += Round4(table.length);
@@ -182,7 +182,7 @@ fn ComputeHeaderChecksum(font: const woff2::Font &) -> uint32_t {
   var range_shift: uint16_t;
   checksum += (font.num_tables << 16 | search_range);
   checksum += (max_pow2 << 16 | range_shift);
-  for (const auto& i var __begin2: : var __range2: fontfont) {
+  for (const auto& i : font.tables) {
     var table: const Font::Table *;
     if (table->IsReused()) {
       table = table->reuse_of;
@@ -214,7 +214,7 @@ fn FixChecksums(font: woff2::Font *) -> bool {
   StoreU32(0, &offset, head_buf);
   var file_checksum: uint32_t;
   var head_checksum: uint32_t;
-  for (auto& i var __begin1: : var __range1: fontfont) {
+  for (auto& i : font->tables) {
     var table: Font::Table *;
     if (table->IsReused()) {
       table = table->reuse_of;
@@ -274,7 +274,7 @@ fn NormalizeFontCollection(font_collection: woff2::FontCollection *) -> bool {
   }
 
   var offset: uint32_t;
-  for (auto& font var __begin1: : var __range1: font_collectionfont_collection) {
+  for (auto& font : font_collection->fonts) {
     if (!NormalizeWithoutFixingChecksums(&font)) {
 #ifdef FONT_COMPRESSION_BIN
       fprintf(stderr, "Font normalization failed.\n");
@@ -285,8 +285,8 @@ fn NormalizeFontCollection(font_collection: woff2::FontCollection *) -> bool {
   }
 
   // Start table offsets after TTC Header and Sfnt Headers
-  for (auto& font var __begin1: : var __range1: font_collectionfont_collection) {
-    for (auto tag var __begin2: : var __range2: fontfont) {
+  for (auto& font : font_collection->fonts) {
+    for (auto tag : font.OutputOrderedTags()) {
       var table: Font::Table &;
       if (table.IsReused()) {
         table.offset = table.reuse_of->offset;
@@ -298,7 +298,7 @@ fn NormalizeFontCollection(font_collection: woff2::FontCollection *) -> bool {
   }
 
   // Now we can fix the checksums
-  for (auto& font var __begin1: : var __range1: font_collectionfont_collection) {
+  for (auto& font : font_collection->fonts) {
     if (!FixChecksums(&font)) {
 #ifdef FONT_COMPRESSION_BIN
       fprintf(stderr, "Failed to fix checksums\n");

+ 6 - 6
third_party/examples/woff2/carbon/src/transform.impl.carbon

@@ -32,7 +32,7 @@ fn WriteBytes(out: std::vector<uint8_t> *, data: const uint8_t *, len: size_t) {
 }
 
 fn WriteBytes(out: std::vector<uint8_t> *, in: const std::vector<uint8_t> &) {
-  for (unsigned char i var __begin2: : var __range2: inin) {
+  for (unsigned char i : in) {
     out->push_back(i);
   }
 }
@@ -106,8 +106,8 @@ class GlyfEncoder {
     var y_min: int16_t;
     var x_max: int16_t;
     var y_max: int16_t;
-    for (const auto& contour var __begin2: : var __range2: glyphglyph) {
-      for (const auto& point var __begin3: : var __range3: contourcontour) {
+    for (const auto& contour : glyph.contours) {
+      for (const auto& point : contour) {
         if (point.x < x_min) { x_min = point.x;
 }
         if (point.x > x_max) { x_max = point.x;
@@ -394,17 +394,17 @@ fn TransformHmtxTable(font: woff2::Font *) -> bool {
   transformed_hmtx->buffer.reserve(transformed_size);
   var out: std::vector<uint8_t> *;
   WriteBytes(out, &flags, 1);
-  for (uint16_t advance_width var __begin1: : var __range1: advance_widthsadvance_widths) {
+  for (uint16_t advance_width : advance_widths) {
     WriteUShort(out, advance_width);
   }
 
   if (!remove_proportional_lsb) {
-    for (int16_t lsb var __begin2: : var __range2: proportional_lsbsproportional_lsbs) {
+    for (int16_t lsb : proportional_lsbs) {
       WriteUShort(out, lsb);
     }
   }
   if (!remove_monospace_lsb) {
-    for (int16_t lsb var __begin2: : var __range2: monospace_lsbsmonospace_lsbs) {
+    for (int16_t lsb : monospace_lsbs) {
       WriteUShort(out, lsb);
     }
   }

+ 1 - 1
third_party/examples/woff2/carbon/src/variable_length.impl.carbon

@@ -41,7 +41,7 @@ fn Write255UShort(out: std::vector<uint8_t> *, value: int) {
 fn Store255UShort(val: int, offset: size_t *, dst: uint8_t *) {
   var packed: std::vector<uint8_t>;
   Write255UShort(&packed, val);
-  for (uint8_t packed_byte var __begin1: : var __range1: packedpacked) {
+  for (uint8_t packed_byte : packed) {
     dst[(*offset)++] = packed_byte;
   }
 }

+ 9 - 9
third_party/examples/woff2/carbon/src/woff2_dec.impl.carbon

@@ -382,7 +382,7 @@ fn StoreLoca(loca_values: const std::vector<uint32_t> &, index_format: int,
   var loca_content: std::vector<uint8_t>;
   var dst: uint8_t *;
   var offset: size_t;
-  for (unsigned int value var __begin2: : var __range2: loca_valuesloca_values) {
+  for (unsigned int value : loca_values) {
     if (index_format) {
       offset = StoreU32(dst, offset, value);
     } else {
@@ -647,7 +647,7 @@ fn ReconstructGlyf(data: const uint8_t *, glyf_table: woff2::Table *,
 }
 
 fn FindTable(tables: std::vector<Table *> *, tag: uint32_t) -> Table* {
-  for (Table* table var __begin2: : var __range2: **) {
+  for (Table* table : *tables) {
     if (table->tag == tag) {
       return table;
     }
@@ -864,7 +864,7 @@ fn ComputeOffsetToFirstTable(hdr: const woff2::(anonymous namespace)::WOFF2Heade
   if (hdr.header_version) {
     offset = CollectionHeaderSize(hdr.header_version, hdr.ttc_fonts.size())
       + kSfntHeaderSize * hdr.ttc_fonts.size();
-    for (const auto& ttc_font var __begin3: : var __range3: hdrhdr) {
+    for (const auto& ttc_font : hdr.ttc_fonts) {
       offset += kSfntEntrySize * ttc_font.table_indices.size();
     }
   }
@@ -874,11 +874,11 @@ fn ComputeOffsetToFirstTable(hdr: const woff2::(anonymous namespace)::WOFF2Heade
 fn Tables(hdr: woff2::(anonymous namespace)::WOFF2Header *, font_index: size_t) -> std::vector<Table*> {
   var tables: std::vector<Table *>;
   if (PREDICT_FALSE(hdr->header_version)) {
-    for (auto index var __begin3: : var __range3: hdrhdr) {
+    for (auto index : hdr->ttc_fonts[font_index].table_indices) {
       tables.push_back(&hdr->tables[index]);
     }
   } else {
-    for (auto& table var __begin3: : var __range3: hdrhdr) {
+    for (auto& table : hdr->tables) {
       tables.push_back(&table);
     }
   }
@@ -1227,13 +1227,13 @@ fn WriteHeaders(metadata: woff2::(anonymous namespace)::RebuildMetadata *,
   var sorted_tables: std::vector<Table>;
   if (hdr->header_version) {
     // collection; we have to sort the table offset vector in each font
-    for (auto& ttc_font var __begin3: : var __range3: hdrhdr) {
+    for (auto& ttc_font : hdr->ttc_fonts) {
       var sorted_index_by_tag: std::map<uint32_t, uint16_t>;
-      for (auto table_index var __begin4: : var __range4: ttc_fontttc_font) {
+      for (auto table_index : ttc_font.table_indices) {
         sorted_index_by_tag[hdr->tables[table_index].tag] = table_index;
       }
       var index: uint16_t;
-      for (auto& i var __begin4: : var __range4: sorted_index_by_tagsorted_index_by_tag) {
+      for (auto& i : sorted_index_by_tag) {
         ttc_font.table_indices[index++] = i.second;
       }
     }
@@ -1275,7 +1275,7 @@ fn WriteHeaders(metadata: woff2::(anonymous namespace)::RebuildMetadata *,
       offset = StoreOffsetTable(result, offset, ttc_font.flavor,
                                 ttc_font.table_indices.size());
 
-      for (const auto table_index var __begin4: : var __range4: ttc_fontttc_font) {
+      for (const auto table_index : ttc_font.table_indices) {
         var tag: uint32_t;
         metadata->font_infos[i].table_entry_by_tag[tag] = offset;
         offset = StoreTableEntry(result, offset, tag);

+ 17 - 17
third_party/examples/woff2/carbon/src/woff2_enc.impl.carbon

@@ -103,7 +103,7 @@ fn ComputeWoff2Length(font_collection: const woff2::FontCollection &,
                           extended_metadata_length: size_t) -> size_t {
   var size: size_t;
 
-  for (const auto& table var __begin2: : var __range2: tablestables) {
+  for (const auto& table : tables) {
     size += TableEntrySize(table);
   }
 
@@ -114,9 +114,9 @@ fn ComputeWoff2Length(font_collection: const woff2::FontCollection &,
 
     size += 4 * font_collection.fonts.size();  // UInt32 flavor for each
 
-    for (const auto& font var __begin3: : var __range3: font_collectionfont_collection) {
+    for (const auto& font : font_collection.fonts) {
       size += Size255UShort(font.tables.size());  // 255UInt16 numTables
-      for (const auto& entry var __begin4: : var __range4: fontfont) {
+      for (const auto& entry : font.tables) {
         var table: const Font::Table &;
         // no collection entry for xform table
         if (table.tag & 0x80808080) { continue;
@@ -140,7 +140,7 @@ fn ComputeWoff2Length(font_collection: const woff2::FontCollection &,
 fn ComputeUncompressedLength(font: const woff2::Font &) -> size_t {
   // sfnt header + offset table
   var size: size_t;
-  for (const auto& entry var __begin2: : var __range2: fontfont) {
+  for (const auto& entry : font.tables) {
     var table: const Font::Table &;
     if (table.tag & 0x80808080) { continue;  // xform tables don't stay
 }
@@ -156,7 +156,7 @@ fn ComputeUncompressedLength(font_collection: const woff2::FontCollection &) ->
     return ComputeUncompressedLength(font_collection.fonts[0]);
   }
   var size: size_t;
-  for (const auto& font var __begin2: : var __range2: font_collectionfont_collection) {
+  for (const auto& font : font_collection.fonts) {
     size += ComputeUncompressedLength(font);
   }
   return size;
@@ -164,7 +164,7 @@ fn ComputeUncompressedLength(font_collection: const woff2::FontCollection &) ->
 
 fn ComputeTotalTransformLength(font: const woff2::Font &) -> size_t {
   var total: size_t;
-  for (const auto& i var __begin2: : var __range2: fontfont) {
+  for (const auto& i : font.tables) {
     var table: const Font::Table &;
     if (table.IsReused()) {
       continue;
@@ -198,7 +198,7 @@ fn CompressedBufferSize(original_size: uint32_t) -> uint32_t {
 }
 
 fn TransformFontCollection(font_collection: woff2::FontCollection *) -> bool {
-  for (auto& font var __begin1: : var __range1: font_collectionfont_collection) {
+  for (auto& font : font_collection->fonts) {
     if (!TransformGlyfAndLocaTables(&font)) {
 #ifdef FONT_COMPRESSION_BIN
       fprintf(stderr, "glyf/loca transformation failed.\n");
@@ -236,7 +236,7 @@ fn ConvertTTFToWOFF2(data: const uint8_t *, length: size_t,
     return FONT_COMPRESSION_FAILURE();
   } else {
     // glyf/loca use 11 to flag "not transformed"
-    for (auto& font var __begin2: : var __range2: font_collectionfont_collection) {
+    for (auto& font : font_collection.fonts) {
       var glyf_table: Font::Table *;
       var loca_table: Font::Table *;
       if (glyf_table) {
@@ -255,7 +255,7 @@ fn ConvertTTFToWOFF2(data: const uint8_t *, length: size_t,
   // then this function will also return false.
 
   var total_transform_length: size_t;
-  for (const auto& font var __begin1: : var __range1: font_collectionfont_collection) {
+  for (const auto& font : font_collection.fonts) {
     total_transform_length += ComputeTotalTransformLength(font);
   }
   var compression_buffer_size: size_t;
@@ -265,8 +265,8 @@ fn ConvertTTFToWOFF2(data: const uint8_t *, length: size_t,
   // Collect all transformed data into one place in output order.
   var transform_buf: std::vector<uint8_t>;
   var transform_offset: size_t;
-  for (const auto& font var __begin1: : var __range1: font_collectionfont_collection) {
-    for (const auto tag var __begin2: : var __range2: fontfont) {
+  for (const auto& font : font_collection.fonts) {
+    for (const auto tag : font.OutputOrderedTags()) {
       var original: const Font::Table &;
       if (original.IsReused()) { continue;
 }
@@ -320,9 +320,9 @@ fn ConvertTTFToWOFF2(data: const uint8_t *, length: size_t,
   var tables: std::vector<Table>;
   var index_by_tag_offset: std::map<std::pair<uint32_t, uint32_t>, uint16_t>;
 
-  for (const auto& font var __begin1: : var __range1: font_collectionfont_collection) {
+  for (const auto& font : font_collection.fonts) {
 
-    for (const auto tag var __begin2: : var __range2: fontfont) {
+    for (const auto tag : font.OutputOrderedTags()) {
       var src_table: const Font::Table &;
       if (src_table.IsReused()) {
         continue;
@@ -398,7 +398,7 @@ fn ConvertTTFToWOFF2(data: const uint8_t *, length: size_t,
   // end of woff2 header
 
   // table directory (http://www.w3.org/TR/WOFF2/#table_dir_format)
-  for (const auto& table var __begin1: : var __range1: tablestables) {
+  for (const auto& table : tables) {
     StoreTableEntry(table, &offset, result);
   }
 
@@ -406,10 +406,10 @@ fn ConvertTTFToWOFF2(data: const uint8_t *, length: size_t,
   if (font_collection.flavor == kTtcFontFlavor) {
     StoreU32(font_collection.header_version, &offset, result);
     Store255UShort(font_collection.fonts.size(), &offset, result);
-    for (const Font& font var __begin2: : var __range2: font_collectionfont_collection) {
+    for (const Font& font : font_collection.fonts) {
 
       var num_tables: uint16_t;
-      for (const auto& entry var __begin3: : var __range3: fontfont) {
+      for (const auto& entry : font.tables) {
         var table: const Font::Table &;
         if (table.tag & 0x80808080) { continue;  // don't write xform tables
 }
@@ -418,7 +418,7 @@ fn ConvertTTFToWOFF2(data: const uint8_t *, length: size_t,
       Store255UShort(num_tables, &offset, result);
 
       StoreU32(font.flavor, &offset, result);
-      for (const auto& entry var __begin3: : var __range3: fontfont) {
+      for (const auto& entry : font.tables) {
         var table: const Font::Table &;
         if (table.tag & 0x80808080) { continue;  // don't write xform tables
 }