From 730c0cadde2302efa1487d672a1e2f53680ce2ea Mon Sep 17 00:00:00 2001 From: Gabriel Martinez Date: Wed, 24 Sep 2014 11:46:32 -0700 Subject: [PATCH] Output multiline doc comments over multiple lines Tested: on Linux Bug: 15779934 Change-Id: I6f822f1705e443d8721ea208dcb021aad3c8715c --- include/flatbuffers/idl.h | 10 ++++++---- src/idl_gen_general.cpp | 8 +++++--- src/idl_gen_go.cpp | 18 ++++-------------- src/idl_parser.cpp | 11 +++++------ tests/MyGame/Example/Monster.cs | 3 ++- tests/MyGame/Example/Monster.go | 3 ++- tests/MyGame/Example/Monster.java | 3 ++- tests/monster_test_generated.h | 3 ++- 8 files changed, 28 insertions(+), 31 deletions(-) diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index 76587c17b..4cbe6c537 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -178,7 +178,7 @@ struct Definition { Definition() : generated(false), defined_namespace(nullptr) {} std::string name; - std::string doc_comment; + std::vector doc_comment; SymbolTable attributes; bool generated; // did we already output code for this definition? Namespace *defined_namespace; // Where it was defined. @@ -234,7 +234,7 @@ struct EnumVal { : name(_name), value(_val), struct_def(nullptr) {} std::string name; - std::string doc_comment; + std::vector doc_comment; int64_t value; StructDef *struct_def; // only set if this is a union }; @@ -333,7 +333,8 @@ class Parser { const char *source_, *cursor_; int line_; // the current line being parsed int token_; - std::string attribute_, doc_comment_; + std::string attribute_; + std::vector doc_comment_; std::vector> field_stack_; std::vector struct_stack_; @@ -342,7 +343,8 @@ class Parser { // Utility functions for multiple generators: extern std::string MakeCamel(const std::string &in, bool first = true); -extern void GenComment(const std::string &dc, std::string *code_ptr, +extern void GenComment(const std::vector &dc, + std::string *code_ptr, const char *prefix = ""); // Container of options that may apply to any of the source/text generators. diff --git a/src/idl_gen_general.cpp b/src/idl_gen_general.cpp index 615db9240..51270ad2d 100644 --- a/src/idl_gen_general.cpp +++ b/src/idl_gen_general.cpp @@ -38,11 +38,13 @@ std::string MakeCamel(const std::string &in, bool first) { } // Generate a documentation comment, if available. -void GenComment(const std::string &dc, std::string *code_ptr, +void GenComment(const std::vector &dc, std::string *code_ptr, const char *prefix) { std::string &code = *code_ptr; - if (dc.length()) { - code += std::string(prefix) + "///" + dc + "\n"; + for (auto it = dc.begin(); + it != dc.end(); + ++it) { + code += std::string(prefix) + "///" + *it + "\n"; } } diff --git a/src/idl_gen_go.cpp b/src/idl_gen_go.cpp index 8b7afde4d..1dae653b4 100644 --- a/src/idl_gen_go.cpp +++ b/src/idl_gen_go.cpp @@ -44,16 +44,6 @@ static std::string GenTypeGet(const Type &type); static std::string TypeName(const FieldDef &field); -// Write a comment. -static void Comment(const std::string &dc, - std::string *code_ptr, - const char *prefix = "") { - std::string &code = *code_ptr; - if (dc.length()) { - code += std::string(prefix) + "///" + dc + "\n"; - } -} - // Most field accessors need to retrieve and test the field offset first, // this is the prefix code for that. std::string OffsetPrefix(const FieldDef &field) { @@ -453,7 +443,7 @@ static void GenReceiver(const StructDef &struct_def, std::string *code_ptr) { static void GenStructAccessor(const StructDef &struct_def, const FieldDef &field, std::string *code_ptr) { - Comment(field.doc_comment, code_ptr, ""); + GenComment(field.doc_comment, code_ptr, ""); if (IsScalar(field.value.type.base_type)) { if (struct_def.fixed) { GetScalarFieldOfStruct(struct_def, field, code_ptr); @@ -520,7 +510,7 @@ static void GenStruct(const StructDef &struct_def, StructDef *root_struct_def) { if (struct_def.generated) return; - Comment(struct_def.doc_comment, code_ptr); + GenComment(struct_def.doc_comment, code_ptr); BeginClass(struct_def, code_ptr); if (&struct_def == root_struct_def) { // Generate a special accessor for the table that has been declared as @@ -552,13 +542,13 @@ static void GenStruct(const StructDef &struct_def, static void GenEnum(const EnumDef &enum_def, std::string *code_ptr) { if (enum_def.generated) return; - Comment(enum_def.doc_comment, code_ptr); + GenComment(enum_def.doc_comment, code_ptr); BeginEnum(code_ptr); for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); ++it) { auto &ev = **it; - Comment(ev.doc_comment, code_ptr, " "); + GenComment(ev.doc_comment, code_ptr, "\t"); EnumMember(enum_def, ev, code_ptr); } EndEnum(code_ptr); diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index a0b05a6d1..97908c74c 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -186,8 +186,7 @@ void Parser::Next() { if (*start == '/') { // documentation comment if (!seen_newline) Error("a documentation comment should be on a line on its own"); - // todo: do we want to support multiline comments instead? - doc_comment_ += std::string(start + 1, cursor_); + doc_comment_.push_back(std::string(start + 1, cursor_)); } break; } @@ -345,7 +344,7 @@ FieldDef &Parser::AddField(StructDef &struct_def, void Parser::ParseField(StructDef &struct_def) { std::string name = attribute_; - std::string dc = doc_comment_; + std::vector dc = doc_comment_; Expect(kTokenIdentifier); Expect(':'); Type type; @@ -704,7 +703,7 @@ StructDef *Parser::LookupCreateStruct(const std::string &name) { } void Parser::ParseEnum(bool is_union) { - std::string dc = doc_comment_; + std::vector dc = doc_comment_; Next(); std::string name = attribute_; Expect(kTokenIdentifier); @@ -734,7 +733,7 @@ void Parser::ParseEnum(bool is_union) { if (is_union) enum_def.vals.Add("NONE", new EnumVal("NONE", 0)); do { std::string name = attribute_; - std::string dc = doc_comment_; + std::vector dc = doc_comment_; Expect(kTokenIdentifier); auto prevsize = enum_def.vals.vec.size(); auto value = enum_def.vals.vec.size() @@ -767,7 +766,7 @@ void Parser::ParseEnum(bool is_union) { } void Parser::ParseDecl() { - std::string dc = doc_comment_; + std::vector dc = doc_comment_; bool fixed = IsNext(kTokenStruct); if (!fixed) Expect(kTokenTable); std::string name = attribute_; diff --git a/tests/MyGame/Example/Monster.cs b/tests/MyGame/Example/Monster.cs index 61b966872..a389cb823 100644 --- a/tests/MyGame/Example/Monster.cs +++ b/tests/MyGame/Example/Monster.cs @@ -25,7 +25,8 @@ public class Monster : Table { public int Test4Length() { int o = __offset(22); return o != 0 ? __vector_len(o) : 0; } public string Testarrayofstring(int j) { int o = __offset(24); return o != 0 ? __string(__vector(o) + j * 4) : null; } public int TestarrayofstringLength() { int o = __offset(24); return o != 0 ? __vector_len(o) : 0; } - /// an example documentation comment: this will end up in the generated code multiline too + /// an example documentation comment: this will end up in the generated code + /// multiline too public Monster Testarrayoftables(int j) { return Testarrayoftables(new Monster(), j); } public Monster Testarrayoftables(Monster obj, int j) { int o = __offset(26); return o != 0 ? obj.__init(__indirect(__vector(o) + j * 4), bb) : null; } public int TestarrayoftablesLength() { int o = __offset(26); return o != 0 ? __vector_len(o) : 0; } diff --git a/tests/MyGame/Example/Monster.go b/tests/MyGame/Example/Monster.go index 688553c5a..74bf7b5a1 100644 --- a/tests/MyGame/Example/Monster.go +++ b/tests/MyGame/Example/Monster.go @@ -139,7 +139,8 @@ func (rcv *Monster) TestarrayofstringLength() int { return 0 } -/// an example documentation comment: this will end up in the generated code multiline too +/// an example documentation comment: this will end up in the generated code +/// multiline too func (rcv *Monster) Testarrayoftables(obj *Monster, j int) bool { o := flatbuffers.UOffsetT(rcv._tab.Offset(26)) if o != 0 { diff --git a/tests/MyGame/Example/Monster.java b/tests/MyGame/Example/Monster.java index 77805a27d..5f8da24b0 100644 --- a/tests/MyGame/Example/Monster.java +++ b/tests/MyGame/Example/Monster.java @@ -31,7 +31,8 @@ public class Monster extends Table { public String testarrayofstring(int j) { int o = __offset(24); return o != 0 ? __string(__vector(o) + j * 4) : null; } public int testarrayofstringLength() { int o = __offset(24); return o != 0 ? __vector_len(o) : 0; } public ByteBuffer testarrayofstringAsByteBuffer() { return __vector_as_bytebuffer(24, 4); } - /// an example documentation comment: this will end up in the generated code multiline too + /// an example documentation comment: this will end up in the generated code + /// multiline too public Monster testarrayoftables(int j) { return testarrayoftables(new Monster(), j); } public Monster testarrayoftables(Monster obj, int j) { int o = __offset(26); return o != 0 ? obj.__init(__indirect(__vector(o) + j * 4), bb) : null; } public int testarrayoftablesLength() { int o = __offset(26); return o != 0 ? __vector_len(o) : 0; } diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index 6345a764b..7b23c2175 100755 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -96,7 +96,8 @@ struct Monster : private flatbuffers::Table { const void *test() const { return GetPointer(20); } const flatbuffers::Vector *test4() const { return GetPointer *>(22); } const flatbuffers::Vector> *testarrayofstring() const { return GetPointer> *>(24); } - /// an example documentation comment: this will end up in the generated code multiline too + /// an example documentation comment: this will end up in the generated code + /// multiline too const flatbuffers::Vector> *testarrayoftables() const { return GetPointer> *>(26); } const Monster *enemy() const { return GetPointer(28); } const flatbuffers::Vector *testnestedflatbuffer() const { return GetPointer *>(30); }