Explorar o código

Insert fn for void functions (#596)

Jon Meow %!s(int64=4) %!d(string=hai) anos
pai
achega
9bdabda851

+ 7 - 2
migrate_cpp/cpp_refactoring/fn_inserter.cpp

@@ -13,8 +13,13 @@ namespace Carbon {
 FnInserter::FnInserter(std::map<std::string, Replacements>& 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) {

+ 15 - 5
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.

+ 1 - 1
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_;

+ 1 - 1
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_;

+ 1 - 1
third_party/examples/woff2/carbon/src/file.carbon

@@ -24,7 +24,7 @@ fn auto GetFileContent(filename: const std::string &) -> string {
     std::istreambuf_iterator<char>());
 }
 
-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<char>(ofs));

+ 2 - 2
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);
 }

+ 1 - 1
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 {

+ 3 - 3
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<uint16_t*>(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;

+ 10 - 10
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<uint8_t> *, data: const uint8_t *, len: size_t) {
+fn WriteBytes(out: std::vector<uint8_t> *, data: const uint8_t *, len: size_t) {
   if (len == 0) { return;
 }
   var offset: size_t;
@@ -31,18 +31,18 @@ void WriteBytes(out: std::vector<uint8_t> *, data: const uint8_t *, len: size_t)
   memcpy(&(*out)[offset], data, len);
 }
 
-void WriteBytes(out: std::vector<uint8_t> *, in: const std::vector<uint8_t> &) {
+fn WriteBytes(out: std::vector<uint8_t> *, in: const std::vector<uint8_t> &) {
   for (unsigned char i var __begin2: std::__wrap_iter<const unsigned char *> var __range2: const std::vector<unsigned char> &) {
     out->push_back(i);
   }
 }
 
-void WriteUShort(out: std::vector<uint8_t> *, value: int) {
+fn WriteUShort(out: std::vector<uint8_t> *, value: int) {
   out->push_back(value >> 8);
   out->push_back(value & 255);
 }
 
-void WriteLong(out: std::vector<uint8_t> *, value: int) {
+fn WriteLong(out: std::vector<uint8_t> *, 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<uint8_t> *) {
+  fn GetTransformedGlyfBytes(result: std::vector<uint8_t> *) {
     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;

+ 3 - 3
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<uint8_t> *, value: int);
-void Store255UShort(val: int, offset: size_t *, dst: uint8_t *);
+fn Write255UShort(out: std::vector<uint8_t> *, 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
 

+ 3 - 3
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<uint8_t> *, value: int) {
+fn Write255UShort(out: std::vector<uint8_t> *, value: int) {
   if (value < 253) {
     out->push_back(value);
   } else if (value < 506) {
@@ -38,7 +38,7 @@ void Write255UShort(out: std::vector<uint8_t> *, 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<uint8_t>;
   Write255UShort(&packed, val);
   for (uint8_t packed_byte var __begin1: std::__wrap_iter<unsigned char *> var __range1: std::vector<unsigned char> &) {
@@ -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;

+ 1 - 1
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;

+ 1 - 1
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

+ 1 - 1
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_;