From 51a7f7b3056b59433bcdf7d47b7bba821c362825 Mon Sep 17 00:00:00 2001 From: Tommy Blunt Date: Wed, 2 Oct 2024 11:46:19 -0700 Subject: [PATCH] Fix missing documentation Fix an issue where generated code would sometimes be missing documentation. This was causing missing enum class comments in C++, missing enum comments in Swift, and missing static method comments in Swift. --- examples/generated-src/swift/SortItems+Private.swift | 1 + src/source/CppGenerator.scala | 9 +++++---- src/source/SwiftGenerator.scala | 3 +++ test-suite/generated-src/cpp/constant_enum.hpp | 1 + test-suite/generated-src/swift/ConstantEnum.swift | 1 + .../generated-src/swift/TestHelpers+Private.swift | 12 ++++++++++++ 6 files changed, 23 insertions(+), 4 deletions(-) diff --git a/examples/generated-src/swift/SortItems+Private.swift b/examples/generated-src/swift/SortItems+Private.swift index eed2207a..bd5bf80f 100644 --- a/examples/generated-src/swift/SortItems+Private.swift +++ b/examples/generated-src/swift/SortItems+Private.swift @@ -35,6 +35,7 @@ public class SortItems_statics { try handleCppErrors(&ret) return SortItemsMarshaller.fromCpp(ret) } + /** For the localhost / command-line demo */ public static func runSort(_ items: TextSort.ItemList) throws -> TextSort.ItemList { var _params = djinni.swift.ParameterList() _params.addValue(ItemListMarshaller.toCpp(items)) diff --git a/src/source/CppGenerator.scala b/src/source/CppGenerator.scala index 77c2a41d..9ae2b19c 100644 --- a/src/source/CppGenerator.scala +++ b/src/source/CppGenerator.scala @@ -12,7 +12,7 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * + * * This file has been modified by Snap, Inc. */ @@ -72,6 +72,7 @@ class CppGenerator(spec: Spec) extends Generator(spec) { val underlyingType = if(e.flags) flagsType else enumType writeHppFile(ident, origin, refs.hpp, refs.hppFwds, w => { + writeDoc(w, doc) w.w(s"enum class $self : $underlyingType").bracedSemi { writeEnumOptionNone(w, e, idCpp.enum) writeEnumOptions(w, e, idCpp.enum) @@ -264,9 +265,9 @@ class CppGenerator(spec: Spec) extends Generator(spec) { writeAlignedCall(w, ": " + actualSelf + "(", r.fields, ")", f => { var param = "std::move(" + idCpp.local(f.ident) + "_)" if (isOptional(f.ty.resolved)) - if (isInterface(f.ty.resolved.args.head)) - param = s"nullptr" - else + if (isInterface(f.ty.resolved.args.head)) + param = s"nullptr" + else param = s"${spec.cppNulloptValue}" param diff --git a/src/source/SwiftGenerator.scala b/src/source/SwiftGenerator.scala index 5c25c22b..8acae1a8 100644 --- a/src/source/SwiftGenerator.scala +++ b/src/source/SwiftGenerator.scala @@ -87,6 +87,7 @@ class SwiftGenerator(spec: Spec) extends Generator(spec) { writeSwiftFile(ident, origin, List[String](), w => { val t = marshal.typename(ident, e) if (e.flags) { + writeDoc(w, doc) w.w(s"public struct $t: OptionSet").braced { w.wl("public let rawValue: Int32") w.wl("public init(rawValue: Int32) { self.rawValue = rawValue }") @@ -95,6 +96,7 @@ class SwiftGenerator(spec: Spec) extends Generator(spec) { writeFlagAll(w, e, idSwift.enum, t) } } else { + writeDoc(w, doc) w.w(s"public enum ${marshal.typename(ident, e)}: Int32").braced { writeEnumOptions(w, e, idSwift.enum, "=", "case ", "") } @@ -326,6 +328,7 @@ class SwiftGenerator(spec: Spec) extends Generator(spec) { if (!staticMethods.isEmpty) { w.w(s"public class ${marshal.typename(ident, i)}_statics").braced { for (m <- staticMethods) { + writeMethodDoc(w, m, idSwift.local) w.w(s"public static func ${swiftMethodName(m.ident)}(") if (m.params.nonEmpty) { w.w("_ ") } w.w(m.params.map(p => s"${idSwift.local(p.ident)}: ${marshal.fqParamType(p.ty)}").mkString(", ")) diff --git a/test-suite/generated-src/cpp/constant_enum.hpp b/test-suite/generated-src/cpp/constant_enum.hpp index 69f8dc65..02d060ce 100644 --- a/test-suite/generated-src/cpp/constant_enum.hpp +++ b/test-suite/generated-src/cpp/constant_enum.hpp @@ -7,6 +7,7 @@ namespace testsuite { +/** enum for use in constants */ enum class constant_enum : int { SOME_VALUE = 0, SOME_OTHER_VALUE = 1, diff --git a/test-suite/generated-src/swift/ConstantEnum.swift b/test-suite/generated-src/swift/ConstantEnum.swift index 5be61eca..116b75da 100644 --- a/test-suite/generated-src/swift/ConstantEnum.swift +++ b/test-suite/generated-src/swift/ConstantEnum.swift @@ -1,6 +1,7 @@ // AUTOGENERATED FILE - DO NOT MODIFY! // This file was generated by Djinni from constant_enum.djinni +/** enum for use in constants */ public enum ConstantEnum: Int32 { case someValue = 0 case someOtherValue = 1 diff --git a/test-suite/generated-src/swift/TestHelpers+Private.swift b/test-suite/generated-src/swift/TestHelpers+Private.swift index 64e4605d..7b3d056a 100644 --- a/test-suite/generated-src/swift/TestHelpers+Private.swift +++ b/test-suite/generated-src/swift/TestHelpers+Private.swift @@ -25,12 +25,18 @@ public enum TestHelpersMarshaller: DjinniSupport.Marshaller { } } public class TestHelpers_statics { + /** Method with documentation */ public static func getSetRecord() throws -> TestSuite.SetRecord { var _params = djinni.swift.ParameterList() var ret = djinni_generated.TestHelpers_getSetRecord(&_params) try handleCppErrors(&ret) return SetRecordMarshaller.fromCpp(ret) } + /** + * Method with long documentation + * (Second line of multi-line documentation. + * Indented third line of multi-line documentation.) + */ public static func checkSetRecord(_ rec: TestSuite.SetRecord) throws -> Bool { var _params = djinni.swift.ParameterList() _params.addValue(SetRecordMarshaller.toCpp(rec)) @@ -172,6 +178,7 @@ public class TestHelpers_statics { try handleCppErrors(&ret) return OptionalMarshaller.fromCpp(ret) } + /** Ensures that we generate integer translation code */ public static func assortedPrimitivesId(_ i: TestSuite.AssortedPrimitives) throws -> TestSuite.AssortedPrimitives { var _params = djinni.swift.ParameterList() _params.addValue(AssortedPrimitivesMarshaller.toCpp(i)) @@ -212,6 +219,10 @@ public class TestHelpers_statics { try handleCppErrors(&ret) return DjinniSupport.FutureMarshaller.fromCpp(ret) } + /** + * If the input is empty, returns back an empty future. + * If the input is non-empty, returns back the value plus one. + */ public static func addOneIfPresent(_ f: DjinniSupport.DJFuture>) throws -> DjinniSupport.DJFuture> { var _params = djinni.swift.ParameterList() _params.addValue(DjinniSupport.FutureMarshaller>.toCpp(f)) @@ -219,6 +230,7 @@ public class TestHelpers_statics { try handleCppErrors(&ret) return DjinniSupport.FutureMarshaller>.fromCpp(ret) } + /** try-catches the future f, and accesses the error message, and returns as a string. */ public static func returnExceptionString(_ f: DjinniSupport.DJFuture) throws -> DjinniSupport.DJFuture { var _params = djinni.swift.ParameterList() _params.addValue(DjinniSupport.FutureMarshaller.toCpp(f))