From 6fa42a7f3c9790cecb87cf158b3babbe1284fe09 Mon Sep 17 00:00:00 2001 From: Jon Meow <46229924+jonmeow@users.noreply.github.com> Date: Mon, 12 Jul 2021 16:20:44 -0700 Subject: [PATCH] For-range `in` replacement logic (#631) This replaces GetAstMatcher with AddMatcher because cxxForRangeStmt is a StatementMatcher. addMatcher has multiple definitions (https://clang.llvm.org/doxygen/classclang_1_1ast__matchers_1_1MatchFinder.html) and so this approach allows using the right addMatcher without writing per-call overloads. To handle the `var`, I'm considering something like moving VarDecl logic into a VarMatcherBase so that I can just use CXXForRangeStmt's getLoopVariable. The problem is a for-range statement has multiple VarDecls, and getLoopVariable may be the easiest way to identify the real one. --- migrate_cpp/cpp_refactoring/BUILD | 19 +++++++ migrate_cpp/cpp_refactoring/fn_inserter.cpp | 15 +++--- migrate_cpp/cpp_refactoring/fn_inserter.h | 4 +- migrate_cpp/cpp_refactoring/for_range.cpp | 29 +++++++++++ migrate_cpp/cpp_refactoring/for_range.h | 31 +++++++++++ .../cpp_refactoring/for_range_test.cpp | 52 +++++++++++++++++++ migrate_cpp/cpp_refactoring/main.cpp | 2 + migrate_cpp/cpp_refactoring/matcher.h | 7 +-- migrate_cpp/cpp_refactoring/matcher_manager.h | 2 +- migrate_cpp/cpp_refactoring/var_decl.cpp | 10 ++-- migrate_cpp/cpp_refactoring/var_decl.h | 4 +- .../woff2/carbon/src/font.impl.carbon | 16 +++--- .../woff2/carbon/src/glyph.impl.carbon | 10 ++-- .../woff2/carbon/src/normalize.impl.carbon | 14 ++--- .../woff2/carbon/src/transform.impl.carbon | 12 ++--- .../carbon/src/variable_length.impl.carbon | 2 +- .../woff2/carbon/src/woff2_dec.impl.carbon | 18 +++---- .../woff2/carbon/src/woff2_enc.impl.carbon | 34 ++++++------ 18 files changed, 212 insertions(+), 69 deletions(-) create mode 100644 migrate_cpp/cpp_refactoring/for_range.cpp create mode 100644 migrate_cpp/cpp_refactoring/for_range.h create mode 100644 migrate_cpp/cpp_refactoring/for_range_test.cpp diff --git a/migrate_cpp/cpp_refactoring/BUILD b/migrate_cpp/cpp_refactoring/BUILD index 8c8bcddb31bc..b5800b48ccef 100644 --- a/migrate_cpp/cpp_refactoring/BUILD +++ b/migrate_cpp/cpp_refactoring/BUILD @@ -11,6 +11,7 @@ cc_binary( srcs = ["main.cpp"], deps = [ ":fn_inserter", + ":for_range", ":var_decl", "@llvm-project//clang:tooling", ], @@ -62,6 +63,24 @@ cc_test( ], ) +cc_library( + name = "for_range", + srcs = ["for_range.cpp"], + hdrs = ["for_range.h"], + deps = [":matcher"], +) + +cc_test( + name = "for_range_test", + srcs = ["for_range_test.cpp"], + deps = [ + ":for_range", + ":matcher_test_base", + "@llvm-project//clang:tooling", + "@llvm-project//llvm:gtest_main", + ], +) + cc_library( name = "var_decl", srcs = ["var_decl.cpp"], diff --git a/migrate_cpp/cpp_refactoring/fn_inserter.cpp b/migrate_cpp/cpp_refactoring/fn_inserter.cpp index 1a147372502b..76558b649a8a 100644 --- a/migrate_cpp/cpp_refactoring/fn_inserter.cpp +++ b/migrate_cpp/cpp_refactoring/fn_inserter.cpp @@ -35,12 +35,15 @@ void FnInserter::Run() { AddReplacement(range, new_text); } -auto FnInserterFactory::GetAstMatcher() -> cam::DeclarationMatcher { - return cam::functionDecl(cam::anyOf(cam::hasTrailingReturn(), - cam::returns(cam::asString("void"))), - cam::unless(cam::anyOf(cam::cxxConstructorDecl(), - cam::cxxDestructorDecl()))) - .bind(Label); +void FnInserterFactory::AddMatcher(cam::MatchFinder* finder, + cam::MatchFinder::MatchCallback* callback) { + finder->addMatcher( + cam::functionDecl(cam::anyOf(cam::hasTrailingReturn(), + cam::returns(cam::asString("void"))), + cam::unless(cam::anyOf(cam::cxxConstructorDecl(), + cam::cxxDestructorDecl()))) + .bind(Label), + callback); } } // namespace Carbon diff --git a/migrate_cpp/cpp_refactoring/fn_inserter.h b/migrate_cpp/cpp_refactoring/fn_inserter.h index 8d7c9bd0a68e..fe8fcdeb3652 100644 --- a/migrate_cpp/cpp_refactoring/fn_inserter.h +++ b/migrate_cpp/cpp_refactoring/fn_inserter.h @@ -18,7 +18,9 @@ class FnInserter : public Matcher { class FnInserterFactory : public MatcherFactoryBase { public: - auto GetAstMatcher() -> clang::ast_matchers::DeclarationMatcher override; + void AddMatcher( + clang::ast_matchers::MatchFinder* finder, + clang::ast_matchers::MatchFinder::MatchCallback* callback) override; }; } // namespace Carbon diff --git a/migrate_cpp/cpp_refactoring/for_range.cpp b/migrate_cpp/cpp_refactoring/for_range.cpp new file mode 100644 index 000000000000..371b7dc21c42 --- /dev/null +++ b/migrate_cpp/cpp_refactoring/for_range.cpp @@ -0,0 +1,29 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#include "migrate_cpp/cpp_refactoring/for_range.h" + +#include "clang/ASTMatchers/ASTMatchers.h" + +namespace cam = ::clang::ast_matchers; + +namespace Carbon { + +static constexpr char Label[] = "ForRange"; + +void ForRange::Run() { + const auto& stmt = GetNodeAsOrDie(Label); + + // Wrap `in` with spaces so that `for (auto i:items)` has valid results. + AddReplacement(clang::CharSourceRange::getTokenRange(stmt.getColonLoc(), + stmt.getColonLoc()), + " in "); +} + +void ForRangeFactory::AddMatcher(cam::MatchFinder* finder, + cam::MatchFinder::MatchCallback* callback) { + finder->addMatcher(cam::cxxForRangeStmt().bind(Label), callback); +} + +} // namespace Carbon diff --git a/migrate_cpp/cpp_refactoring/for_range.h b/migrate_cpp/cpp_refactoring/for_range.h new file mode 100644 index 000000000000..d8feb9fff3b9 --- /dev/null +++ b/migrate_cpp/cpp_refactoring/for_range.h @@ -0,0 +1,31 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#ifndef MIGRATE_CPP_CPP_REFACTORING_FOR_RANGE_H_ +#define MIGRATE_CPP_CPP_REFACTORING_FOR_RANGE_H_ + +#include "migrate_cpp/cpp_refactoring/matcher.h" + +namespace Carbon { + +// Updates variable declarations for `var name: Type`. +class ForRange : public Matcher { + public: + using Matcher::Matcher; + void Run() override; + + private: + auto GetTypeStr(const clang::VarDecl& decl) -> std::string; +}; + +class ForRangeFactory : public MatcherFactoryBase { + public: + void AddMatcher( + clang::ast_matchers::MatchFinder* finder, + clang::ast_matchers::MatchFinder::MatchCallback* callback) override; +}; + +} // namespace Carbon + +#endif // MIGRATE_CPP_CPP_REFACTORING_FOR_RANGE_H_ diff --git a/migrate_cpp/cpp_refactoring/for_range_test.cpp b/migrate_cpp/cpp_refactoring/for_range_test.cpp new file mode 100644 index 000000000000..c0da25a3b20e --- /dev/null +++ b/migrate_cpp/cpp_refactoring/for_range_test.cpp @@ -0,0 +1,52 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#include "migrate_cpp/cpp_refactoring/for_range.h" + +#include "migrate_cpp/cpp_refactoring/matcher_test_base.h" + +namespace Carbon { +namespace { + +class ForRangeTest : public MatcherTestBase {}; + +TEST_F(ForRangeTest, Basic) { + constexpr char Before[] = R"cpp( + void Foo() { + int items[] = {1}; + for (int i : items) { + } + } + )cpp"; + constexpr char After[] = R"( + void Foo() { + int items[] = {1}; + for (int i in items) { + } + } + )"; + ExpectReplacement(Before, After); +} + +TEST_F(ForRangeTest, NoSpace) { + // Do not mark `cpp` so that clang-format won't "fix" the `:` spacing. + constexpr char Before[] = R"( + void Foo() { + int items[] = {1}; + for (int i:items) { + } + } + )"; + constexpr char After[] = R"( + void Foo() { + int items[] = {1}; + for (int i in items) { + } + } + )"; + ExpectReplacement(Before, After); +} + +} // namespace +} // namespace Carbon diff --git a/migrate_cpp/cpp_refactoring/main.cpp b/migrate_cpp/cpp_refactoring/main.cpp index 623fbfc21394..e22262cc088a 100644 --- a/migrate_cpp/cpp_refactoring/main.cpp +++ b/migrate_cpp/cpp_refactoring/main.cpp @@ -5,6 +5,7 @@ #include "clang/Tooling/CommonOptionsParser.h" #include "clang/Tooling/Refactoring.h" #include "migrate_cpp/cpp_refactoring/fn_inserter.h" +#include "migrate_cpp/cpp_refactoring/for_range.h" #include "migrate_cpp/cpp_refactoring/matcher_manager.h" #include "migrate_cpp/cpp_refactoring/var_decl.h" @@ -35,6 +36,7 @@ auto main(int argc, const char** argv) -> int { // Set up AST matcher callbacks. Carbon::MatcherManager matchers(&tool.getReplacements()); matchers.Register(std::make_unique()); + matchers.Register(std::make_unique()); matchers.Register(std::make_unique()); return tool.runAndSave( diff --git a/migrate_cpp/cpp_refactoring/matcher.h b/migrate_cpp/cpp_refactoring/matcher.h index c61c55c9c850..4ef3bbef1dd6 100644 --- a/migrate_cpp/cpp_refactoring/matcher.h +++ b/migrate_cpp/cpp_refactoring/matcher.h @@ -71,9 +71,10 @@ class MatcherFactory { const clang::ast_matchers::MatchFinder::MatchResult* match_result, Matcher::ReplacementMap* replacements) -> std::unique_ptr = 0; - // Returns the AST matcher which determines when the Matcher is instantiated - // and run. - virtual auto GetAstMatcher() -> clang::ast_matchers::DeclarationMatcher = 0; + // Adds the Matcher to the finder with the provided callback. + virtual void AddMatcher( + clang::ast_matchers::MatchFinder* finder, + clang::ast_matchers::MatchFinder::MatchCallback* callback) = 0; }; // A convenience factory that implements CreateMatcher for Matchers that have a diff --git a/migrate_cpp/cpp_refactoring/matcher_manager.h b/migrate_cpp/cpp_refactoring/matcher_manager.h index 683c2be6f57e..72effe31b918 100644 --- a/migrate_cpp/cpp_refactoring/matcher_manager.h +++ b/migrate_cpp/cpp_refactoring/matcher_manager.h @@ -34,7 +34,7 @@ class MatcherManager { std::unique_ptr in_factory, Matcher::ReplacementMap* in_replacements) : factory(std::move(in_factory)), replacements(in_replacements) { - finder->addMatcher(factory->GetAstMatcher(), this); + factory->AddMatcher(finder, this); } void run(const clang::ast_matchers::MatchFinder::MatchResult& match_result) diff --git a/migrate_cpp/cpp_refactoring/var_decl.cpp b/migrate_cpp/cpp_refactoring/var_decl.cpp index 5911c2a2a9ea..9a16ffedfd45 100644 --- a/migrate_cpp/cpp_refactoring/var_decl.cpp +++ b/migrate_cpp/cpp_refactoring/var_decl.cpp @@ -131,10 +131,12 @@ void VarDecl::Run() { clang::CharSourceRange::getCharRange(replace_start, replace_end), after); } -auto VarDeclFactory::GetAstMatcher() -> cam::DeclarationMatcher { - return cam::varDecl(cam::unless(cam::hasParent(cam::declStmt( - cam::hasParent(cam::cxxForRangeStmt()))))) - .bind(Label); +void VarDeclFactory::AddMatcher(cam::MatchFinder* finder, + cam::MatchFinder::MatchCallback* callback) { + finder->addMatcher(cam::varDecl(cam::unless(cam::hasParent(cam::declStmt( + cam::hasParent(cam::cxxForRangeStmt()))))) + .bind(Label), + callback); } } // namespace Carbon diff --git a/migrate_cpp/cpp_refactoring/var_decl.h b/migrate_cpp/cpp_refactoring/var_decl.h index 4e89470e494e..9adaac4e114c 100644 --- a/migrate_cpp/cpp_refactoring/var_decl.h +++ b/migrate_cpp/cpp_refactoring/var_decl.h @@ -21,7 +21,9 @@ class VarDecl : public Matcher { class VarDeclFactory : public MatcherFactoryBase { public: - auto GetAstMatcher() -> clang::ast_matchers::DeclarationMatcher override; + void AddMatcher( + clang::ast_matchers::MatchFinder* finder, + clang::ast_matchers::MatchFinder::MatchCallback* callback) override; }; } // namespace Carbon diff --git a/third_party/examples/woff2/carbon/src/font.impl.carbon b/third_party/examples/woff2/carbon/src/font.impl.carbon index 6f89d54b1363..21573db93e19 100644 --- a/third_party/examples/woff2/carbon/src/font.impl.carbon +++ b/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 { var output_order: std::vector; - for (const auto& i : tables) { + for (const auto& i in tables) { var table: const Font::Table& = i.second; // This is a transformed table, we will write it together with the // original version. @@ -90,7 +90,7 @@ fn ReadTrueTypeFont(file: Buffer*, data: const uint8_t*, len: size_t, // Check that tables are non-overlapping. var last_offset: uint32_t = 12UL + 16UL * font->num_tables; - for (const auto& i : intervals) { + for (const auto& i in intervals) { if (i.first < last_offset || i.first + i.second < i.first) { return FONT_COMPRESSION_FAILURE(); } @@ -116,7 +116,7 @@ fn ReadCollectionFont(file: Buffer*, data: const uint8_t*, len: size_t, return FONT_COMPRESSION_FAILURE(); } - for (auto& entry : font->tables) { + for (auto& entry in font->tables) { var table: Font::Table& = entry.second; if (all_tables->find(table.offset) == all_tables->end()) { @@ -154,7 +154,7 @@ fn ReadTrueTypeCollection(file: Buffer*, data: const uint8_t*, len: size_t, var font_it: auto = font_collection->fonts.begin(); var all_tables: std::map; - for (const auto offset : offsets) { + for (const auto offset in offsets) { file->set_offset(offset); var font: Font& = *font_it++; if (!ReadCollectionFont(file, data, len, &font, &all_tables)) { @@ -197,7 +197,7 @@ fn ReadFontCollection(data: const uint8_t*, len: size_t, fn FontFileSize(font: const Font&) -> size_t { var max_offset: size_t = 12ULL + 16ULL * font.num_tables; - for (const auto& i : font.tables) { + for (const auto& i in font.tables) { var table: const Font::Table& = i.second; var padding_size: size_t = (4 - (table.length & 3)) & 3; var end_offset: size_t = (padding_size + table.offset) + table.length; @@ -208,7 +208,7 @@ fn FontFileSize(font: const Font&) -> size_t { fn FontCollectionFileSize(font_collection: const FontCollection&) -> size_t { var max_offset: size_t = 0; - for (auto& font : font_collection.fonts) { + for (auto& font in font_collection.fonts) { // font file size actually just finds max offset max_offset = std::max(max_offset, FontFileSize(font)); } @@ -272,7 +272,7 @@ fn WriteFont(font: const Font&, offset: size_t*, dst: uint8_t*, Store16(max_pow2, offset, dst); Store16(range_shift, offset, dst); - for (const auto& i : font.tables) { + for (const auto& i in font.tables) { if (!WriteTable(i.second, offset, dst, dst_size)) { return false; } @@ -308,7 +308,7 @@ fn WriteFontCollection(font_collection: const FontCollection&, dst: uint8_t*, } // Write fonts and their offsets. - for (const auto & font : font_collection.fonts) { + for (const auto & font in font_collection.fonts) { StoreU32(offset, &offset_table, dst); if (!WriteFont(font, &offset, dst, dst_size)) { return false; diff --git a/third_party/examples/woff2/carbon/src/glyph.impl.carbon b/third_party/examples/woff2/carbon/src/glyph.impl.carbon index 17bf3eb0a08b..754a116cc858 100644 --- a/third_party/examples/woff2/carbon/src/glyph.impl.carbon +++ b/third_party/examples/woff2/carbon/src/glyph.impl.carbon @@ -226,7 +226,7 @@ fn StoreInstructions(glyph: const Glyph&, offset: size_t*, dst: uint8_t*) { fn StoreEndPtsOfContours(glyph: const Glyph&, offset: size_t*, dst: uint8_t*) -> bool { var end_point: int = -1; - for (const auto& contour : glyph.contours) { + for (const auto& contour in glyph.contours) { end_point += contour.size(); if (contour.size() > std::numeric_limits::max() || end_point > std::numeric_limits::max()) { @@ -247,8 +247,8 @@ fn StorePoints(glyph: const Glyph&, offset: size_t*, var y_bytes: size_t = 0; // Store the flags and calculate the total size of the x and y coordinates. - for (const auto& contour : glyph.contours) { - for (const auto& point : contour) { + for (const auto& contour in glyph.contours) { + for (const auto& point in contour) { var flag: int = point.on_curve ? kFLAG_ONCURVE : 0; var dx: int = point.x - last_x; var dy: int = point.y - last_y; @@ -305,8 +305,8 @@ fn StorePoints(glyph: const Glyph&, offset: size_t*, var y_offset: size_t = *offset + x_bytes; last_x = 0; last_y = 0; - for (const auto& contour : glyph.contours) { - for (const auto& point : contour) { + for (const auto& contour in glyph.contours) { + for (const auto& point in contour) { var dx: int = point.x - last_x; var dy: int = point.y - last_y; if (dx == 0) { diff --git a/third_party/examples/woff2/carbon/src/normalize.impl.carbon b/third_party/examples/woff2/carbon/src/normalize.impl.carbon index 8ff5cd01d45e..2b573ba0c566 100644 --- a/third_party/examples/woff2/carbon/src/normalize.impl.carbon +++ b/third_party/examples/woff2/carbon/src/normalize.impl.carbon @@ -165,7 +165,7 @@ fn NormalizeGlyphs(font: Font*) -> bool { fn NormalizeOffsets(font: Font*) -> bool { var offset: uint32_t = 12 + 16 * font->num_tables; - for (auto tag : font->OutputOrderedTags()) { + for (auto tag in font->OutputOrderedTags()) { var table: auto& = font->tables[tag]; table.offset = offset; offset += Round4(table.length); @@ -182,7 +182,7 @@ fn ComputeHeaderChecksum(font: const Font&) -> uint32_t { var range_shift: uint16_t = (font.num_tables << 4) - search_range; checksum += (font.num_tables << 16 | search_range); checksum += (max_pow2 << 16 | range_shift); - for (const auto& i : font.tables) { + for (const auto& i in font.tables) { var table: const Font::Table* = &i.second; if (table->IsReused()) { table = table->reuse_of; @@ -214,7 +214,7 @@ fn FixChecksums(font: Font*) -> bool { StoreU32(0, &offset, head_buf); var file_checksum: uint32_t = 0; var head_checksum: uint32_t = 0; - for (auto& i : font->tables) { + for (auto& i in font->tables) { var table: Font::Table* = &i.second; if (table->IsReused()) { table = table->reuse_of; @@ -275,7 +275,7 @@ fn NormalizeFontCollection(font_collection: FontCollection*) -> bool { var offset: uint32_t = CollectionHeaderSize(font_collection->header_version, font_collection->fonts.size()); - for (auto& font : font_collection->fonts) { + for (auto& font in font_collection->fonts) { if (!NormalizeWithoutFixingChecksums(&font)) { #ifdef FONT_COMPRESSION_BIN fprintf(stderr, "Font normalization failed.\n"); @@ -286,8 +286,8 @@ fn NormalizeFontCollection(font_collection: FontCollection*) -> bool { } // Start table offsets after TTC Header and Sfnt Headers - for (auto& font : font_collection->fonts) { - for (auto tag : font.OutputOrderedTags()) { + for (auto& font in font_collection->fonts) { + for (auto tag in font.OutputOrderedTags()) { var table: Font::Table& = font.tables[tag]; if (table.IsReused()) { table.offset = table.reuse_of->offset; @@ -299,7 +299,7 @@ fn NormalizeFontCollection(font_collection: FontCollection*) -> bool { } // Now we can fix the checksums - for (auto& font : font_collection->fonts) { + for (auto& font in font_collection->fonts) { if (!FixChecksums(&font)) { #ifdef FONT_COMPRESSION_BIN fprintf(stderr, "Failed to fix checksums\n"); diff --git a/third_party/examples/woff2/carbon/src/transform.impl.carbon b/third_party/examples/woff2/carbon/src/transform.impl.carbon index dca7cfbe7d45..3235d08323d0 100644 --- a/third_party/examples/woff2/carbon/src/transform.impl.carbon +++ b/third_party/examples/woff2/carbon/src/transform.impl.carbon @@ -32,7 +32,7 @@ fn WriteBytes(out: std::vector*, data: const uint8_t*, len: size_t) { } fn WriteBytes(out: std::vector*, in: const std::vector&) { - for (unsigned char i : in) { + for (unsigned char i in in) { out->push_back(i); } } @@ -106,8 +106,8 @@ class GlyfEncoder { var y_min: int16_t = glyph.contours[0][0].y; var x_max: int16_t = x_min; var y_max: int16_t = y_min; - for (const auto& contour : glyph.contours) { - for (const auto& point : contour) { + for (const auto& contour in glyph.contours) { + for (const auto& point in 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: Font*) -> bool { transformed_hmtx->buffer.reserve(transformed_size); var out: std::vector* = &transformed_hmtx->buffer; WriteBytes(out, &flags, 1); - for (uint16_t advance_width : advance_widths) { + for (uint16_t advance_width in advance_widths) { WriteUShort(out, advance_width); } if (!remove_proportional_lsb) { - for (int16_t lsb : proportional_lsbs) { + for (int16_t lsb in proportional_lsbs) { WriteUShort(out, lsb); } } if (!remove_monospace_lsb) { - for (int16_t lsb : monospace_lsbs) { + for (int16_t lsb in monospace_lsbs) { WriteUShort(out, lsb); } } diff --git a/third_party/examples/woff2/carbon/src/variable_length.impl.carbon b/third_party/examples/woff2/carbon/src/variable_length.impl.carbon index 4d49398acb2c..77c6c98f3a8b 100644 --- a/third_party/examples/woff2/carbon/src/variable_length.impl.carbon +++ b/third_party/examples/woff2/carbon/src/variable_length.impl.carbon @@ -41,7 +41,7 @@ fn Write255UShort(out: std::vector*, value: int) { fn Store255UShort(val: int, offset: size_t*, dst: uint8_t*) { var packed: std::vector; Write255UShort(&packed, val); - for (uint8_t packed_byte : packed) { + for (uint8_t packed_byte in packed) { dst[(*offset)++] = packed_byte; } } diff --git a/third_party/examples/woff2/carbon/src/woff2_dec.impl.carbon b/third_party/examples/woff2/carbon/src/woff2_dec.impl.carbon index a452ceb8ef44..07496bfc989f 100644 --- a/third_party/examples/woff2/carbon/src/woff2_dec.impl.carbon +++ b/third_party/examples/woff2/carbon/src/woff2_dec.impl.carbon @@ -383,7 +383,7 @@ fn StoreLoca(loca_values: const std::vector&, index_format: int, var loca_content: std::vector(loca_size * offset_size); var dst: uint8_t* = &loca_content[0]; var offset: size_t = 0; - for (unsigned int value : loca_values) { + for (unsigned int value in loca_values) { if (index_format) { offset = StoreU32(dst, offset, value); } else { @@ -651,7 +651,7 @@ fn ReconstructGlyf(data: const uint8_t*, glyf_table: Table*, } fn FindTable(tables: std::vector*, tag: uint32_t) -> Table* { - for (Table* table : *tables) { + for (Table* table in *tables) { if (table->tag == tag) { return table; } @@ -870,7 +870,7 @@ fn ComputeOffsetToFirstTable(hdr: const WOFF2Header&) -> uint64_t { if (hdr.header_version) { offset = CollectionHeaderSize(hdr.header_version, hdr.ttc_fonts.size()) + kSfntHeaderSize * hdr.ttc_fonts.size(); - for (const auto& ttc_font : hdr.ttc_fonts) { + for (const auto& ttc_font in hdr.ttc_fonts) { offset += kSfntEntrySize * ttc_font.table_indices.size(); } } @@ -880,11 +880,11 @@ fn ComputeOffsetToFirstTable(hdr: const WOFF2Header&) -> uint64_t { fn Tables(hdr: WOFF2Header*, font_index: size_t) -> std::vector { var tables: std::vector; if (PREDICT_FALSE(hdr->header_version)) { - for (auto index : hdr->ttc_fonts[font_index].table_indices) { + for (auto index in hdr->ttc_fonts[font_index].table_indices) { tables.push_back(&hdr->tables[index]); } } else { - for (auto& table : hdr->tables) { + for (auto& table in hdr->tables) { tables.push_back(&table); } } @@ -1234,13 +1234,13 @@ fn WriteHeaders(metadata: RebuildMetadata*, var sorted_tables: std::vector(hdr->tables); if (hdr->header_version) { // collection; we have to sort the table offset vector in each font - for (auto& ttc_font : hdr->ttc_fonts) { + for (auto& ttc_font in hdr->ttc_fonts) { var sorted_index_by_tag: std::map; - for (auto table_index : ttc_font.table_indices) { + for (auto table_index in ttc_font.table_indices) { sorted_index_by_tag[hdr->tables[table_index].tag] = table_index; } var index: uint16_t = 0; - for (auto& i : sorted_index_by_tag) { + for (auto& i in sorted_index_by_tag) { ttc_font.table_indices[index++] = i.second; } } @@ -1282,7 +1282,7 @@ fn WriteHeaders(metadata: RebuildMetadata*, offset = StoreOffsetTable(result, offset, ttc_font.flavor, ttc_font.table_indices.size()); - for (const auto table_index : ttc_font.table_indices) { + for (const auto table_index in ttc_font.table_indices) { var tag: uint32_t = hdr->tables[table_index].tag; metadata->font_infos[i].table_entry_by_tag[tag] = offset; offset = StoreTableEntry(result, offset, tag); diff --git a/third_party/examples/woff2/carbon/src/woff2_enc.impl.carbon b/third_party/examples/woff2/carbon/src/woff2_enc.impl.carbon index 182e154f2812..29ab963375e7 100644 --- a/third_party/examples/woff2/carbon/src/woff2_enc.impl.carbon +++ b/third_party/examples/woff2/carbon/src/woff2_enc.impl.carbon @@ -103,7 +103,7 @@ fn ComputeWoff2Length(font_collection: const FontCollection&, extended_metadata_length: size_t) -> size_t { var size: size_t = kWoff2HeaderSize; - for (const auto& table : tables) { + for (const auto& table in tables) { size += TableEntrySize(table); } @@ -114,9 +114,9 @@ fn ComputeWoff2Length(font_collection: const FontCollection&, size += 4 * font_collection.fonts.size(); // UInt32 flavor for each - for (const auto& font : font_collection.fonts) { + for (const auto& font in font_collection.fonts) { size += Size255UShort(font.tables.size()); // 255UInt16 numTables - for (const auto& entry : font.tables) { + for (const auto& entry in font.tables) { var table: const Font::Table& = entry.second; // no collection entry for xform table if (table.tag & 0x80808080) { continue; @@ -140,7 +140,7 @@ fn ComputeWoff2Length(font_collection: const FontCollection&, fn ComputeUncompressedLength(font: const Font&) -> size_t { // sfnt header + offset table var size: size_t = 12 + 16 * font.num_tables; - for (const auto& entry : font.tables) { + for (const auto& entry in font.tables) { var table: const Font::Table& = entry.second; if (table.tag & 0x80808080) { continue; // xform tables don't stay } @@ -157,7 +157,7 @@ fn ComputeUncompressedLength(font_collection: const FontCollection&) -> size_t { } var size: size_t = CollectionHeaderSize(font_collection.header_version, font_collection.fonts.size()); - for (const auto& font : font_collection.fonts) { + for (const auto& font in font_collection.fonts) { size += ComputeUncompressedLength(font); } return size; @@ -165,7 +165,7 @@ fn ComputeUncompressedLength(font_collection: const FontCollection&) -> size_t { fn ComputeTotalTransformLength(font: const Font&) -> size_t { var total: size_t = 0; - for (const auto& i : font.tables) { + for (const auto& i in font.tables) { var table: const Font::Table& = i.second; if (table.IsReused()) { continue; @@ -199,7 +199,7 @@ fn CompressedBufferSize(original_size: uint32_t) -> uint32_t { } fn TransformFontCollection(font_collection: FontCollection*) -> bool { - for (auto& font : font_collection->fonts) { + for (auto& font in font_collection->fonts) { if (!TransformGlyfAndLocaTables(&font)) { #ifdef FONT_COMPRESSION_BIN fprintf(stderr, "glyf/loca transformation failed.\n"); @@ -237,7 +237,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 : font_collection.fonts) { + for (auto& font in font_collection.fonts) { var glyf_table: Font::Table* = font.FindTable(kGlyfTableTag); var loca_table: Font::Table* = font.FindTable(kLocaTableTag); if (glyf_table) { @@ -256,7 +256,7 @@ fn ConvertTTFToWOFF2(data: const uint8_t*, length: size_t, // then this function will also return false. var total_transform_length: size_t = 0; - for (const auto& font : font_collection.fonts) { + for (const auto& font in font_collection.fonts) { total_transform_length += ComputeTotalTransformLength(font); } var compression_buffer_size: size_t = CompressedBufferSize(total_transform_length); @@ -266,8 +266,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(total_transform_length); var transform_offset: size_t = 0; - for (const auto& font : font_collection.fonts) { - for (const auto tag : font.OutputOrderedTags()) { + for (const auto& font in font_collection.fonts) { + for (const auto tag in font.OutputOrderedTags()) { var original: const Font::Table& = font.tables.at(tag); if (original.IsReused()) { continue; } @@ -322,9 +322,9 @@ fn ConvertTTFToWOFF2(data: const uint8_t*, length: size_t, var tables: std::vector
; var index_by_tag_offset: std::map, uint16_t>; - for (const auto& font : font_collection.fonts) { + for (const auto& font in font_collection.fonts) { - for (const auto tag : font.OutputOrderedTags()) { + for (const auto tag in font.OutputOrderedTags()) { var src_table: const Font::Table& = font.tables.at(tag); if (src_table.IsReused()) { continue; @@ -403,7 +403,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 : tables) { + for (const auto& table in tables) { StoreTableEntry(table, &offset, result); } @@ -411,10 +411,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 : font_collection.fonts) { + for (const Font& font in font_collection.fonts) { var num_tables: uint16_t = 0; - for (const auto& entry : font.tables) { + for (const auto& entry in font.tables) { var table: const Font::Table& = entry.second; if (table.tag & 0x80808080) { continue; // don't write xform tables } @@ -423,7 +423,7 @@ fn ConvertTTFToWOFF2(data: const uint8_t*, length: size_t, Store255UShort(num_tables, &offset, result); StoreU32(font.flavor, &offset, result); - for (const auto& entry : font.tables) { + for (const auto& entry in font.tables) { var table: const Font::Table& = entry.second; if (table.tag & 0x80808080) { continue; // don't write xform tables }