[vala] Append doc comments to generated C files



commit f6b39547fac817ae7e6711f398eaa1ca87d98c90
Author: Jürg Billeter <j bitron ch>
Date:   Tue Jul 27 21:56:30 2010 +0200

    Append doc comments to generated C files

 ccode/valaccodewriter.vala         |   45 ++++++++++++++++++++---------------
 codegen/valaccodebasemodule.vala   |    4 +++
 codegen/valaccodemethodmodule.vala |    4 +++
 codegen/valagobjectmodule.vala     |    4 +++
 codegen/valagtypemodule.vala       |   18 ++++++++++++++
 vala/valaparser.vala               |    6 +++-
 vala/valapropertyaccessor.vala     |    4 +-
 vala/valascanner.vala              |    4 +++
 8 files changed, 66 insertions(+), 23 deletions(-)
---
diff --git a/ccode/valaccodewriter.vala b/ccode/valaccodewriter.vala
index 8f25754..93f4a42 100644
--- a/ccode/valaccodewriter.vala
+++ b/ccode/valaccodewriter.vala
@@ -217,30 +217,37 @@ public class Vala.CCodeWriter {
 	 * @param text the comment text
 	 */
 	public void write_comment (string text) {
-		write_indent ();
-		stream.puts ("/*");
-		bool first = true;
-		
-		/* separate declaration due to missing memory management in foreach statements */
-		var lines = text.split ("\n");
+		try {
+			write_indent ();
+			stream.puts ("/*");
+			bool first = true;
+
+			// discard tabs at beginning of line
+			var regex = new GLib.Regex ("^\t+");
+
+			/* separate declaration due to missing memory management in foreach statements */
+			var lines = text.split ("\n");
 		
-		foreach (string line in lines) {
-			if (!first) {
-				write_indent ();
-			} else {
-				first = false;
-			}
+			foreach (string line in lines) {
+				if (!first) {
+					write_indent ();
+				} else {
+					first = false;
+				}
 
-			var lineparts = line.split ("*/");
+				var lineparts = regex.replace_literal (line, -1, 0, "").split ("*/");
 
-			for (int i = 0; lineparts[i] != null; i++) {
-				stream.puts (lineparts[i]);
-				if (lineparts[i+1] != null) {
-					stream.puts ("* /");
+				for (int i = 0; lineparts[i] != null; i++) {
+					stream.puts (lineparts[i]);
+					if (lineparts[i+1] != null) {
+						stream.puts ("* /");
+					}
 				}
 			}
+			stream.puts ("*/");
+			write_newline ();
+		} catch (RegexError e) {
+			// ignore
 		}
-		stream.puts ("*/");
-		write_newline ();
 	}
 }
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 50127b6..8f78e15 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -1349,6 +1349,10 @@ public class Vala.CCodeBaseModule : CCodeModule {
 
 		var prop = (Property) acc.prop;
 
+		if (acc.comment != null) {
+			source_type_member_definition.append (new CCodeComment (acc.comment.content));
+		}
+
 		bool returns_real_struct = acc.readable && prop.property_type.is_real_non_null_struct_type ();
 
 		acc.accept_children (codegen);
diff --git a/codegen/valaccodemethodmodule.vala b/codegen/valaccodemethodmodule.vala
index a07ac05..0b35b99 100644
--- a/codegen/valaccodemethodmodule.vala
+++ b/codegen/valaccodemethodmodule.vala
@@ -420,6 +420,10 @@ public class Vala.CCodeMethodModule : CCodeStructModule {
 			}
 		}
 
+		if (m.comment != null) {
+			source_type_member_definition.append (new CCodeComment (m.comment.content));
+		}
+
 		function = new CCodeFunction (m.get_real_cname ());
 		m.ccodenode = function;
 
diff --git a/codegen/valagobjectmodule.vala b/codegen/valagobjectmodule.vala
index bd527fd..d1c2eea 100644
--- a/codegen/valagobjectmodule.vala
+++ b/codegen/valagobjectmodule.vala
@@ -137,6 +137,10 @@ public class Vala.GObjectModule : GTypeModule {
 				continue;
 			}
 
+			if (prop.comment != null) {
+				init_block.add_statement (new CCodeComment (prop.comment.content));
+			}
+
 			if (prop.overrides || prop.base_interface_property != null) {
 				var cinst = new CCodeFunctionCall (new CCodeIdentifier ("g_object_class_override_property"));
 				cinst.add_argument (ccall);
diff --git a/codegen/valagtypemodule.vala b/codegen/valagtypemodule.vala
index e2a001e..5781cd7 100644
--- a/codegen/valagtypemodule.vala
+++ b/codegen/valagtypemodule.vala
@@ -590,6 +590,10 @@ public class Vala.GTypeModule : GErrorModule {
 				add_finalize_function (cl);
 			}
 
+			if (cl.comment != null) {
+				source_type_member_definition.append (new CCodeComment (cl.comment.content));
+			}
+
 			var type_fun = new ClassRegisterFunction (cl, context);
 			type_fun.init_from_type (in_plugin);
 			source_declarations.add_type_member_declaration (type_fun.get_source_declaration ());
@@ -1295,6 +1299,9 @@ public class Vala.GTypeModule : GErrorModule {
 		if (!cl.is_compact) {
 			/* create signals */
 			foreach (Signal sig in cl.get_signals ()) {
+				if (sig.comment != null) {
+					init_block.add_statement (new CCodeComment (sig.comment.content));
+				}
 				init_block.add_statement (new CCodeExpressionStatement (head.get_signal_creation (sig, cl)));
 			}
 		}
@@ -1932,6 +1939,10 @@ public class Vala.GTypeModule : GErrorModule {
 
 		add_interface_base_init_function (iface);
 
+		if (iface.comment != null) {
+			source_type_member_definition.append (new CCodeComment (iface.comment.content));
+		}
+
 		var type_fun = create_interface_register_function (iface);
 		type_fun.init_from_type (in_plugin);
 		source_declarations.add_type_member_declaration (type_fun.get_source_declaration ());
@@ -1970,6 +1981,10 @@ public class Vala.GTypeModule : GErrorModule {
 						continue;
 					}
 
+					if (prop.comment != null) {
+						init_block.add_statement (new CCodeComment (prop.comment.content));
+					}
+
 					var cinst = new CCodeFunctionCall (new CCodeIdentifier ("g_object_interface_install_property"));
 					cinst.add_argument (new CCodeIdentifier ("iface"));
 					cinst.add_argument (head.get_param_spec (prop));
@@ -1981,6 +1996,9 @@ public class Vala.GTypeModule : GErrorModule {
 
 		/* create signals */
 		foreach (Signal sig in iface.get_signals ()) {
+			if (sig.comment != null) {
+				init_block.add_statement (new CCodeComment (sig.comment.content));
+			}
 			init_block.add_statement (new CCodeExpressionStatement (head.get_signal_creation (sig, iface)));
 		}
 
diff --git a/vala/valaparser.vala b/vala/valaparser.vala
index 9161da2..8d375cc 100644
--- a/vala/valaparser.vala
+++ b/vala/valaparser.vala
@@ -2746,6 +2746,8 @@ public class Vala.Parser : CodeVisitor {
 				prop.initializer = parse_expression ();
 				expect (TokenType.SEMICOLON);
 			} else {
+				comment = scanner.pop_comment ();
+
 				var accessor_begin = get_location ();
 				var accessor_attrs = parse_attributes ();
 				var accessor_access = parse_access_modifier (SymbolAccessibility.PUBLIC);
@@ -2767,7 +2769,7 @@ public class Vala.Parser : CodeVisitor {
 						block = parse_block ();
 						prop.external = false;
 					}
-					prop.get_accessor = new PropertyAccessor (true, false, false, value_type, block, get_src (accessor_begin));
+					prop.get_accessor = new PropertyAccessor (true, false, false, value_type, block, get_src (accessor_begin), comment);
 					set_attributes (prop.get_accessor, accessor_attrs);
 					prop.get_accessor.access = accessor_access;
 				} else {
@@ -2789,7 +2791,7 @@ public class Vala.Parser : CodeVisitor {
 						block = parse_block ();
 						prop.external = false;
 					}
-					prop.set_accessor = new PropertyAccessor (false, writable, _construct, value_type, block, get_src (accessor_begin));
+					prop.set_accessor = new PropertyAccessor (false, writable, _construct, value_type, block, get_src (accessor_begin), comment);
 					set_attributes (prop.set_accessor, accessor_attrs);
 					prop.set_accessor.access = accessor_access;
 				}
diff --git a/vala/valapropertyaccessor.vala b/vala/valapropertyaccessor.vala
index 9e3944e..ca0a1c5 100644
--- a/vala/valapropertyaccessor.vala
+++ b/vala/valapropertyaccessor.vala
@@ -128,8 +128,8 @@ public class Vala.PropertyAccessor : Symbol {
 	 * @param source       reference to source code
 	 * @return             newly created property accessor
 	 */
-	public PropertyAccessor (bool readable, bool writable, bool construction, DataType? value_type, Block? body, SourceReference? source_reference) {
-		base (null, source_reference);
+	public PropertyAccessor (bool readable, bool writable, bool construction, DataType? value_type, Block? body, SourceReference? source_reference, Comment? comment = null) {
+		base (null, source_reference, comment);
 		this.readable = readable;
 		this.writable = writable;
 		this.construction = construction;
diff --git a/vala/valascanner.vala b/vala/valascanner.vala
index 0f097e8..e5fd0db 100644
--- a/vala/valascanner.vala
+++ b/vala/valascanner.vala
@@ -1562,6 +1562,10 @@ public class Vala.Scanner {
 
 	void push_comment (string comment_item, SourceReference source_reference, bool file_comment) {
 		if (comment_item[0] == '*') {
+			if (_comment != null) {
+				// extra doc comment, add it to source file comments
+				source_file.add_comment (_comment);
+			}
 			_comment = new Comment (comment_item, source_reference);
 		}
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]