From 9bdabda851714ce1519f6ebfed3f06be10ce6bf2 Mon Sep 17 00:00:00 2001 From: Jon Meow <46229924+jonmeow@users.noreply.github.com> Date: Thu, 24 Jun 2021 10:16:21 -0700 Subject: [PATCH] Insert fn for void functions (#596) --- migrate_cpp/cpp_refactoring/fn_inserter.cpp | 9 +++++++-- .../cpp_refactoring/fn_inserter_test.cpp | 20 ++++++++++++++----- .../woff2/carbon/include/woff2/output.carbon | 2 +- .../examples/woff2/carbon/src/buffer.carbon | 2 +- .../examples/woff2/carbon/src/file.carbon | 2 +- .../woff2/carbon/src/glyph.impl.carbon | 4 ++-- .../woff2/carbon/src/normalize.impl.carbon | 2 +- .../woff2/carbon/src/store_bytes.carbon | 6 +++--- .../woff2/carbon/src/transform.impl.carbon | 20 +++++++++---------- .../woff2/carbon/src/variable_length.carbon | 6 +++--- .../carbon/src/variable_length.impl.carbon | 6 +++--- .../woff2/carbon/src/woff2_dec.impl.carbon | 2 +- .../woff2/carbon/src/woff2_enc.impl.carbon | 2 +- .../woff2/carbon/src/woff2_out.impl.carbon | 2 +- 14 files changed, 50 insertions(+), 35 deletions(-) diff --git a/migrate_cpp/cpp_refactoring/fn_inserter.cpp b/migrate_cpp/cpp_refactoring/fn_inserter.cpp index 17c44ab5bfcc..7eaea4241c1b 100644 --- a/migrate_cpp/cpp_refactoring/fn_inserter.cpp +++ b/migrate_cpp/cpp_refactoring/fn_inserter.cpp @@ -13,8 +13,13 @@ namespace Carbon { FnInserter::FnInserter(std::map& in_replacements, cam::MatchFinder* finder) : Matcher(in_replacements) { - finder->addMatcher(cam::functionDecl(cam::hasTrailingReturn()).bind(Label), - this); + finder->addMatcher( + cam::functionDecl(cam::anyOf(cam::hasTrailingReturn(), + cam::returns(cam::asString("void"))), + cam::unless(cam::anyOf(cam::cxxConstructorDecl(), + cam::cxxDestructorDecl()))) + .bind(Label), + this); } void FnInserter::run(const cam::MatchFinder::MatchResult& result) { diff --git a/migrate_cpp/cpp_refactoring/fn_inserter_test.cpp b/migrate_cpp/cpp_refactoring/fn_inserter_test.cpp index fd4affda17e3..ee88c7454520 100644 --- a/migrate_cpp/cpp_refactoring/fn_inserter_test.cpp +++ b/migrate_cpp/cpp_refactoring/fn_inserter_test.cpp @@ -30,13 +30,12 @@ TEST_F(FnInserterTest, Inline) { } TEST_F(FnInserterTest, Void) { - // TODO: void needs to be handled. constexpr char Before[] = "void A();"; - ExpectReplacement(Before, Before); + constexpr char After[] = "fn A();"; + ExpectReplacement(Before, After); } TEST_F(FnInserterTest, Methods) { - // TODO: void needs to be handled. // TODO: Need to re-lex tokens, this should probably be "fn virtual" for now. constexpr char Before[] = R"cpp( class Shape { @@ -58,13 +57,13 @@ TEST_F(FnInserterTest, Methods) { constexpr char After[] = R"( class Shape { public: - virtual void Draw() = 0; + fn void Draw() = 0; fn auto NumSides() -> int = 0; }; class Circle : public Shape { public: - void Draw() override; + fn Draw() override; fn NumSides() -> int override; fn Radius() -> double { return radius_; } @@ -75,6 +74,17 @@ TEST_F(FnInserterTest, Methods) { ExpectReplacement(Before, After); } +TEST_F(FnInserterTest, ConstructorDestructor) { + constexpr char Before[] = R"cpp( + class Shape { + public: + Shape() {} + ~Shape() {} + }; + )cpp"; + ExpectReplacement(Before, Before); +} + TEST_F(FnInserterTest, LegacyReturn) { // Code should be migrated to trailing returns by clang-tidy, so this is okay // to miss. diff --git a/third_party/examples/woff2/carbon/include/woff2/output.carbon b/third_party/examples/woff2/carbon/include/woff2/output.carbon index 35cb56703b39..49cfc3085e29 100644 --- a/third_party/examples/woff2/carbon/include/woff2/output.carbon +++ b/third_party/examples/woff2/carbon/include/woff2/output.carbon @@ -57,7 +57,7 @@ class WOFF2StringOut : public WOFF2Out { fn Write(buf: const void *, offset: size_t, n: size_t) -> bool override; fn Size() -> size_t override { return offset_; } fn MaxSize() -> size_t { return max_size_; } - void SetMaxSize(max_size: size_t); + fn SetMaxSize(max_size: size_t); private: std::string* buf_; size_t max_size_; diff --git a/third_party/examples/woff2/carbon/src/buffer.carbon b/third_party/examples/woff2/carbon/src/buffer.carbon index baa26a59e105..ffb8e9fdb424 100644 --- a/third_party/examples/woff2/carbon/src/buffer.carbon +++ b/third_party/examples/woff2/carbon/src/buffer.carbon @@ -151,7 +151,7 @@ class Buffer { [[nodiscard]] fn offset() const -> size_t { return offset_; } [[nodiscard]] fn length() const -> size_t { return length_; } - void set_offset(newoffset: size_t) { offset_ = newoffset; } + fn set_offset(newoffset: size_t) { offset_ = newoffset; } private: const uint8_t * const buffer_; diff --git a/third_party/examples/woff2/carbon/src/file.carbon b/third_party/examples/woff2/carbon/src/file.carbon index 8e830b08d1f7..662084280d6b 100644 --- a/third_party/examples/woff2/carbon/src/file.carbon +++ b/third_party/examples/woff2/carbon/src/file.carbon @@ -24,7 +24,7 @@ fn auto GetFileContent(filename: const std::string &) -> string { std::istreambuf_iterator()); } -inline void SetFileContents(filename: const std::string &, start: string::iterator, +fn void SetFileContents(filename: const std::string &, start: string::iterator, end: string::iterator) { var ofs: std::ofstream; std::copy(start, end, std::ostream_iterator(ofs)); diff --git a/third_party/examples/woff2/carbon/src/glyph.impl.carbon b/third_party/examples/woff2/carbon/src/glyph.impl.carbon index 03c8dac07dba..2eab0873ad2d 100644 --- a/third_party/examples/woff2/carbon/src/glyph.impl.carbon +++ b/third_party/examples/woff2/carbon/src/glyph.impl.carbon @@ -212,14 +212,14 @@ fn ReadGlyph(data: const uint8_t *, len: size_t, glyph: woff2::Glyph *) -> bool namespace { -void StoreBbox(glyph: const woff2::Glyph &, offset: size_t *, dst: uint8_t *) { +fn StoreBbox(glyph: const woff2::Glyph &, offset: size_t *, dst: uint8_t *) { Store16(glyph.x_min, offset, dst); Store16(glyph.y_min, offset, dst); Store16(glyph.x_max, offset, dst); Store16(glyph.y_max, offset, dst); } -void StoreInstructions(glyph: const woff2::Glyph &, offset: size_t *, dst: uint8_t *) { +fn StoreInstructions(glyph: const woff2::Glyph &, offset: size_t *, dst: uint8_t *) { Store16(glyph.instructions_size, offset, dst); StoreBytes(glyph.instructions_data, glyph.instructions_size, offset, dst); } diff --git a/third_party/examples/woff2/carbon/src/normalize.impl.carbon b/third_party/examples/woff2/carbon/src/normalize.impl.carbon index 458945900a83..7acca894419d 100644 --- a/third_party/examples/woff2/carbon/src/normalize.impl.carbon +++ b/third_party/examples/woff2/carbon/src/normalize.impl.carbon @@ -24,7 +24,7 @@ namespace woff2 { namespace { -void StoreLoca(index_fmt: int, value: uint32_t, offset: size_t *, dst: uint8_t *) { +fn StoreLoca(index_fmt: int, value: uint32_t, offset: size_t *, dst: uint8_t *) { if (index_fmt == 0) { Store16(value >> 1, offset, dst); } else { diff --git a/third_party/examples/woff2/carbon/src/store_bytes.carbon b/third_party/examples/woff2/carbon/src/store_bytes.carbon index d8d95ac1deec..02cae360f8c3 100644 --- a/third_party/examples/woff2/carbon/src/store_bytes.carbon +++ b/third_party/examples/woff2/carbon/src/store_bytes.carbon @@ -39,14 +39,14 @@ fn auto Store16(dst: uint8_t *, offset: size_t, x: int) -> size_t { return offset + 2; } -inline void StoreU32(val: uint32_t, offset: size_t *, dst: uint8_t *) { +fn void StoreU32(val: uint32_t, offset: size_t *, dst: uint8_t *) { dst[(*offset)++] = val >> 24; dst[(*offset)++] = val >> 16; dst[(*offset)++] = val >> 8; dst[(*offset)++] = val; } -inline void Store16(val: int, offset: size_t *, dst: uint8_t *) { +fn void Store16(val: int, offset: size_t *, dst: uint8_t *) { #if defined(WOFF_LITTLE_ENDIAN) *reinterpret_cast(dst + *offset) = ((val & 0xFF) << 8) | ((val & 0xFF00) >> 8); @@ -60,7 +60,7 @@ inline void Store16(val: int, offset: size_t *, dst: uint8_t *) { #endif } -inline void StoreBytes(data: const uint8_t *, len: size_t, +fn void StoreBytes(data: const uint8_t *, len: size_t, offset: size_t *, dst: uint8_t *) { memcpy(&dst[*offset], data, len); *offset += len; diff --git a/third_party/examples/woff2/carbon/src/transform.impl.carbon b/third_party/examples/woff2/carbon/src/transform.impl.carbon index 46fe015bb449..0c77ea5d11cd 100644 --- a/third_party/examples/woff2/carbon/src/transform.impl.carbon +++ b/third_party/examples/woff2/carbon/src/transform.impl.carbon @@ -23,7 +23,7 @@ namespace { var FLAG_ARG_1_AND_2_ARE_WORDS: const int; var FLAG_WE_HAVE_INSTRUCTIONS: const int; -void WriteBytes(out: std::vector *, data: const uint8_t *, len: size_t) { +fn WriteBytes(out: std::vector *, data: const uint8_t *, len: size_t) { if (len == 0) { return; } var offset: size_t; @@ -31,18 +31,18 @@ void WriteBytes(out: std::vector *, data: const uint8_t *, len: size_t) memcpy(&(*out)[offset], data, len); } -void WriteBytes(out: std::vector *, in: const std::vector &) { +fn WriteBytes(out: std::vector *, in: const std::vector &) { for (unsigned char i var __begin2: std::__wrap_iter var __range2: const std::vector &) { out->push_back(i); } } -void WriteUShort(out: std::vector *, value: int) { +fn WriteUShort(out: std::vector *, value: int) { out->push_back(value >> 8); out->push_back(value & 255); } -void WriteLong(out: std::vector *, value: int) { +fn WriteLong(out: std::vector *, value: int) { out->push_back((value >> 24) & 255); out->push_back((value >> 16) & 255); out->push_back((value >> 8) & 255); @@ -69,7 +69,7 @@ class GlyfEncoder { return true; } - void GetTransformedGlyfBytes(result: std::vector *) { + fn GetTransformedGlyfBytes(result: std::vector *) { WriteLong(result, 0); // version WriteUShort(result, n_glyphs_); WriteUShort(result, 0); // index_format, will be set later @@ -91,7 +91,7 @@ class GlyfEncoder { } private: - void WriteInstructions(glyph: const woff2::Glyph &) { + fn WriteInstructions(glyph: const woff2::Glyph &) { Write255UShort(&glyph_stream_, glyph.instructions_size); WriteBytes(&instruction_stream_, glyph.instructions_data, glyph.instructions_size); @@ -135,7 +135,7 @@ class GlyfEncoder { return false; } - void WriteSimpleGlyph(glyph_id: int, glyph: const woff2::Glyph &) { + fn WriteSimpleGlyph(glyph_id: int, glyph: const woff2::Glyph &) { var num_contours: int; WriteUShort(&n_contour_stream_, num_contours); if (ShouldWriteSimpleGlyphBbox(glyph)) { @@ -163,7 +163,7 @@ class GlyfEncoder { } } - void WriteCompositeGlyph(glyph_id: int, glyph: const woff2::Glyph &) { + fn WriteCompositeGlyph(glyph_id: int, glyph: const woff2::Glyph &) { WriteUShort(&n_contour_stream_, -1); WriteBbox(glyph_id, glyph); WriteBytes(&composite_stream_, @@ -174,7 +174,7 @@ class GlyfEncoder { } } - void WriteBbox(glyph_id: int, glyph: const woff2::Glyph &) { + fn WriteBbox(glyph_id: int, glyph: const woff2::Glyph &) { bbox_bitmap_[glyph_id >> 3] |= 0x80 >> (glyph_id & 7); WriteUShort(&bbox_stream_, glyph.x_min); WriteUShort(&bbox_stream_, glyph.y_min); @@ -182,7 +182,7 @@ class GlyfEncoder { WriteUShort(&bbox_stream_, glyph.y_max); } - void WriteTriplet(on_curve: bool, x: int, y: int) { + fn WriteTriplet(on_curve: bool, x: int, y: int) { var abs_x: int; var abs_y: int; var on_curve_bit: int; diff --git a/third_party/examples/woff2/carbon/src/variable_length.carbon b/third_party/examples/woff2/carbon/src/variable_length.carbon index a1602537a795..86d99241111e 100644 --- a/third_party/examples/woff2/carbon/src/variable_length.carbon +++ b/third_party/examples/woff2/carbon/src/variable_length.carbon @@ -17,12 +17,12 @@ namespace woff2 { fn Size255UShort(value: uint16_t) -> size_t; fn Read255UShort(buf: woff2::Buffer *, value: unsigned int *) -> bool; -void Write255UShort(out: std::vector *, value: int); -void Store255UShort(val: int, offset: size_t *, dst: uint8_t *); +fn Write255UShort(out: std::vector *, value: int); +fn Store255UShort(val: int, offset: size_t *, dst: uint8_t *); fn Base128Size(n: size_t) -> size_t; fn ReadBase128(buf: woff2::Buffer *, value: uint32_t *) -> bool; -void StoreBase128(len: size_t, offset: size_t *, dst: uint8_t *); +fn StoreBase128(len: size_t, offset: size_t *, dst: uint8_t *); } // namespace woff2 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 9a79655d5c19..7e7f06b2209f 100644 --- a/third_party/examples/woff2/carbon/src/variable_length.impl.carbon +++ b/third_party/examples/woff2/carbon/src/variable_length.impl.carbon @@ -22,7 +22,7 @@ fn Size255UShort(value: uint16_t) -> size_t { return result; } -void Write255UShort(out: std::vector *, value: int) { +fn Write255UShort(out: std::vector *, value: int) { if (value < 253) { out->push_back(value); } else if (value < 506) { @@ -38,7 +38,7 @@ void Write255UShort(out: std::vector *, value: int) { } } -void Store255UShort(val: int, offset: size_t *, dst: uint8_t *) { +fn Store255UShort(val: int, offset: size_t *, dst: uint8_t *) { var packed: std::vector; Write255UShort(&packed, val); for (uint8_t packed_byte var __begin1: std::__wrap_iter var __range1: std::vector &) { @@ -115,7 +115,7 @@ fn Base128Size(n: size_t) -> size_t { return size; } -void StoreBase128(len: size_t, offset: size_t *, dst: uint8_t *) { +fn StoreBase128(len: size_t, offset: size_t *, dst: uint8_t *) { var size: size_t; for (var i: size_t; i < size; ++i) { var b: int; 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 cf6cff2d97ec..092dcdee359e 100644 --- a/third_party/examples/woff2/carbon/src/woff2_dec.impl.carbon +++ b/third_party/examples/woff2/carbon/src/woff2_dec.impl.carbon @@ -292,7 +292,7 @@ fn StorePoints(n_points: unsigned int, points: const woff2::Point *, // Compute the bounding box of the coordinates, and store into a glyf buffer. // A precondition is that there are at least 10 bytes available. // dst should point to the beginning of a 'glyf' record. -void ComputeBbox(n_points: unsigned int, points: const woff2::Point *, dst: uint8_t *) { +fn ComputeBbox(n_points: unsigned int, points: const woff2::Point *, dst: uint8_t *) { var x_min: int; var y_min: int; var x_max: int; 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 0c82ac54121c..545e8b6c182d 100644 --- a/third_party/examples/woff2/carbon/src/woff2_enc.impl.carbon +++ b/third_party/examples/woff2/carbon/src/woff2_enc.impl.carbon @@ -71,7 +71,7 @@ fn KnownTableIndex(tag: uint32_t) -> int { return 63; } -void StoreTableEntry(table: const woff2::Table &, offset: size_t *, dst: uint8_t *) { +fn StoreTableEntry(table: const woff2::Table &, offset: size_t *, dst: uint8_t *) { var flag_byte: uint8_t; dst[(*offset)++] = flag_byte; // The index here is treated as a set of flag bytes because diff --git a/third_party/examples/woff2/carbon/src/woff2_out.impl.carbon b/third_party/examples/woff2/carbon/src/woff2_out.impl.carbon index 2a07a39528c0..9980990bb8b8 100644 --- a/third_party/examples/woff2/carbon/src/woff2_out.impl.carbon +++ b/third_party/examples/woff2/carbon/src/woff2_out.impl.carbon @@ -38,7 +38,7 @@ fn WOFF2StringOut::Write(buf: const void *, offset: size_t, n: size_t) -> bool { return true; } -void WOFF2StringOut::SetMaxSize(max_size: size_t) { +fn WOFF2StringOut::SetMaxSize(max_size: size_t) { max_size_ = max_size; if (offset_ > max_size_) { offset_ = max_size_;