Automate creation of C comments from autogen headers
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/eaf2c94b Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/eaf2c94b Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/eaf2c94b Branch: refs/heads/master Commit: eaf2c94b9e97feac4431bda135a6498c030b7767 Parents: c8e8fe0 Author: Nick Wellnhofer <[email protected]> Authored: Sat Aug 23 17:17:36 2014 +0200 Committer: Nick Wellnhofer <[email protected]> Committed: Tue Nov 11 16:17:23 2014 +0100 ---------------------------------------------------------------------- compiler/perl/lib/Clownfish/CFC/Perl/Build.pm | 9 ++-- compiler/src/CFCBindCore.c | 32 +++++++------- compiler/src/CFCC.c | 18 ++++---- compiler/src/CFCPerl.c | 24 ++++++---- compiler/src/CFCRuby.c | 26 +++++------ compiler/src/CFCUtil.c | 51 ++++++++++++++++++++++ compiler/src/CFCUtil.h | 13 ++++++ runtime/c/cfc_header | 37 ++++++++-------- runtime/perl/buildlib/Clownfish/Build.pm | 44 +++++++++---------- runtime/ruby/Rakefile.common | 8 ++-- 10 files changed, 164 insertions(+), 98 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/eaf2c94b/compiler/perl/lib/Clownfish/CFC/Perl/Build.pm ---------------------------------------------------------------------- diff --git a/compiler/perl/lib/Clownfish/CFC/Perl/Build.pm b/compiler/perl/lib/Clownfish/CFC/Perl/Build.pm index b50c976..fc4ba86 100644 --- a/compiler/perl/lib/Clownfish/CFC/Perl/Build.pm +++ b/compiler/perl/lib/Clownfish/CFC/Perl/Build.pm @@ -77,14 +77,13 @@ sub new { my $autogen_header = $self->clownfish_params('autogen_header'); if ( !defined($autogen_header) ) { $self->clownfish_params( autogen_header => <<'END_AUTOGEN' ); -/*********************************************** +*********************************************** - !!!! DO NOT EDIT !!!! +!!!! DO NOT EDIT !!!! - This file was auto-generated by Build.PL. - - ***********************************************/ +This file was auto-generated by Build.PL. +*********************************************** END_AUTOGEN } http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/eaf2c94b/compiler/src/CFCBindCore.c ---------------------------------------------------------------------- diff --git a/compiler/src/CFCBindCore.c b/compiler/src/CFCBindCore.c index 774b1a3..679da04 100644 --- a/compiler/src/CFCBindCore.c +++ b/compiler/src/CFCBindCore.c @@ -36,8 +36,8 @@ struct CFCBindCore { CFCBase base; CFCHierarchy *hierarchy; - char *header; - char *footer; + char *c_header; + char *c_footer; }; /* Write the "parcel.h" header file, which contains common symbols needed by @@ -92,24 +92,24 @@ CFCBindCore_init(CFCBindCore *self, CFCHierarchy *hierarchy, CFCUTIL_NULL_CHECK(header); CFCUTIL_NULL_CHECK(footer); self->hierarchy = (CFCHierarchy*)CFCBase_incref((CFCBase*)hierarchy); - self->header = CFCUtil_strdup(header); - self->footer = CFCUtil_strdup(footer); + self->c_header = CFCUtil_make_c_comment(header); + self->c_footer = CFCUtil_make_c_comment(footer); return self; } void CFCBindCore_destroy(CFCBindCore *self) { CFCBase_decref((CFCBase*)self->hierarchy); - FREEMEM(self->header); - FREEMEM(self->footer); + FREEMEM(self->c_header); + FREEMEM(self->c_footer); CFCBase_destroy((CFCBase*)self); } int CFCBindCore_write_all_modified(CFCBindCore *self, int modified) { CFCHierarchy *hierarchy = self->hierarchy; - const char *header = self->header; - const char *footer = self->footer; + const char *header = self->c_header; + const char *footer = self->c_footer; // Discover whether files need to be regenerated. modified = CFCHierarchy_propagate_modified(hierarchy, modified); @@ -330,10 +330,10 @@ S_write_parcel_h(CFCBindCore *self, CFCParcel *parcel) { "%s\n" "\n"; char *file_content - = CFCUtil_sprintf(pattern, self->header, PREFIX, PREFIX, + = CFCUtil_sprintf(pattern, self->c_header, PREFIX, PREFIX, extra_includes, privacy_sym, PREFIX, PREFIX, typedefs, extra_defs, PREFIX, prefix, PREFIX, prefix, - prefix, PREFIX, self->footer); + prefix, PREFIX, self->c_footer); // Unlink then write file. const char *inc_dest = CFCHierarchy_get_include_dest(hierarchy); @@ -467,10 +467,10 @@ S_write_parcel_c(CFCBindCore *self, CFCParcel *parcel) { "\n" "%s\n"; char *file_content - = CFCUtil_sprintf(pattern, self->header, privacy_syms, prefix, + = CFCUtil_sprintf(pattern, self->c_header, privacy_syms, prefix, includes, c_data, class_specs, prefix, inh_bootstrap, num_specs, prefix, prefix, prereq_bootstrap, prefix, - self->footer); + self->c_footer); // Unlink then open file. const char *src_dest = CFCHierarchy_get_source_dest(hierarchy); @@ -539,8 +539,8 @@ CFCBindCore_write_callbacks_h(CFCBindCore *self) { "%s\n" "\n"; char *file_content - = CFCUtil_sprintf(pattern, self->header, includes, all_cb_decs, - self->footer); + = CFCUtil_sprintf(pattern, self->c_header, includes, all_cb_decs, + self->c_footer); // Unlink then write file. const char *inc_dest = CFCHierarchy_get_include_dest(hierarchy); @@ -592,9 +592,9 @@ S_write_platform_h(CFCBindCore *self) { "%s" "\n"; char *file_content - = CFCUtil_sprintf(pattern, self->header, feature_defs, string_defs, + = CFCUtil_sprintf(pattern, self->c_header, feature_defs, string_defs, stdbool_defs, stdint_defs, alloca_defs, - self->footer); + self->c_footer); // Unlink then write file. const char *inc_dest = CFCHierarchy_get_include_dest(self->hierarchy); http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/eaf2c94b/compiler/src/CFCC.c ---------------------------------------------------------------------- diff --git a/compiler/src/CFCC.c b/compiler/src/CFCC.c index 3f21a0d..e0a355d 100644 --- a/compiler/src/CFCC.c +++ b/compiler/src/CFCC.c @@ -30,8 +30,8 @@ struct CFCC { CFCBase base; CFCHierarchy *hierarchy; - char *header; - char *footer; + char *c_header; + char *c_footer; }; static const CFCMeta CFCC_META = { @@ -53,16 +53,16 @@ CFCC_init(CFCC *self, CFCHierarchy *hierarchy, const char *header, CFCUTIL_NULL_CHECK(header); CFCUTIL_NULL_CHECK(footer); self->hierarchy = (CFCHierarchy*)CFCBase_incref((CFCBase*)hierarchy); - self->header = CFCUtil_strdup(header); - self->footer = CFCUtil_strdup(footer); + self->c_header = CFCUtil_make_c_comment(header); + self->c_footer = CFCUtil_make_c_comment(footer); return self; } void CFCC_destroy(CFCC *self) { CFCBase_decref((CFCBase*)self->hierarchy); - FREEMEM(self->header); - FREEMEM(self->footer); + FREEMEM(self->c_header); + FREEMEM(self->c_footer); CFCBase_destroy((CFCBase*)self); } @@ -99,8 +99,8 @@ CFCC_write_callbacks(CFCC *self) { "\n" "%s\n" "\n"; - char *file_content = CFCUtil_sprintf(pattern, self->header, all_cb_decs, - self->footer); + char *file_content = CFCUtil_sprintf(pattern, self->c_header, all_cb_decs, + self->c_footer); // Unlink then write file. const char *inc_dest = CFCHierarchy_get_include_dest(hierarchy); @@ -182,7 +182,7 @@ CFCC_write_hostdefs(CFCC *self) { "\n" "%s\n"; char *content - = CFCUtil_sprintf(pattern, self->header, self->footer); + = CFCUtil_sprintf(pattern, self->c_header, self->c_footer); // Unlink then write file. const char *inc_dest = CFCHierarchy_get_include_dest(self->hierarchy); http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/eaf2c94b/compiler/src/CFCPerl.c ---------------------------------------------------------------------- diff --git a/compiler/src/CFCPerl.c b/compiler/src/CFCPerl.c index 41fce09..c78fcbf 100644 --- a/compiler/src/CFCPerl.c +++ b/compiler/src/CFCPerl.c @@ -41,6 +41,8 @@ struct CFCPerl { char *boot_class; char *header; char *footer; + char *c_header; + char *c_footer; char *xs_path; char *boot_func; }; @@ -78,6 +80,8 @@ CFCPerl_init(CFCPerl *self, CFCHierarchy *hierarchy, const char *lib_dir, self->boot_class = CFCUtil_strdup(boot_class); self->header = CFCUtil_strdup(header); self->footer = CFCUtil_strdup(footer); + self->c_header = CFCUtil_make_c_comment(header); + self->c_footer = CFCUtil_make_c_comment(footer); // Derive path to generated .xs file. self->xs_path = CFCUtil_sprintf("%s" CHY_DIR_SEP "%s.xs", lib_dir, @@ -102,6 +106,8 @@ CFCPerl_destroy(CFCPerl *self) { FREEMEM(self->boot_class); FREEMEM(self->header); FREEMEM(self->footer); + FREEMEM(self->c_header); + FREEMEM(self->c_footer); FREEMEM(self->xs_path); FREEMEM(self->boot_func); CFCBase_destroy((CFCBase*)self); @@ -196,8 +202,8 @@ S_write_boot_h(CFCPerl *self) { "\n" "%s\n"; char *content - = CFCUtil_sprintf(pattern, self->header, guard, guard, self->boot_func, - guard, self->footer); + = CFCUtil_sprintf(pattern, self->c_header, guard, guard, + self->boot_func, guard, self->c_footer); const char *inc_dest = CFCHierarchy_get_include_dest(self->hierarchy); char *boot_h_path = CFCUtil_sprintf("%s" CHY_DIR_SEP "boot.h", inc_dest); @@ -303,9 +309,9 @@ S_write_boot_c(CFCPerl *self) { "%s\n" "\n"; char *content - = CFCUtil_sprintf(pattern, self->header, pound_includes, + = CFCUtil_sprintf(pattern, self->c_header, pound_includes, self->boot_func, bootstrap_code, alias_adds, - isa_pushes, self->footer); + isa_pushes, self->c_footer); const char *src_dest = CFCHierarchy_get_source_dest(self->hierarchy); char *boot_c_path = CFCUtil_sprintf("%s" CHY_DIR_SEP "boot.c", src_dest); @@ -341,7 +347,7 @@ CFCPerl_write_hostdefs(CFCPerl *self) { "\n" "%s\n"; char *content - = CFCUtil_sprintf(pattern, self->header, self->footer); + = CFCUtil_sprintf(pattern, self->c_header, self->c_footer); // Unlink then write file. const char *inc_dest = CFCHierarchy_get_include_dest(self->hierarchy); @@ -381,9 +387,9 @@ S_xs_file_contents(CFCPerl *self, const char *generated_xs, "\n" "%s"; char *contents - = CFCUtil_sprintf(pattern, self->header, generated_xs, + = CFCUtil_sprintf(pattern, self->c_header, generated_xs, self->boot_class, self->boot_class, self->boot_func, - xs_init, hand_rolled_xs, self->footer); + xs_init, hand_rolled_xs, self->c_footer); return contents; } @@ -577,7 +583,7 @@ S_write_callbacks_c(CFCPerl *self) { " return retval;\n" "}\n" "\n"; - char *content = CFCUtil_sprintf(pattern, self->header); + char *content = CFCUtil_sprintf(pattern, self->c_header); for (size_t i = 0; ordered[i] != NULL; i++) { CFCClass *klass = ordered[i]; @@ -597,7 +603,7 @@ S_write_callbacks_c(CFCPerl *self) { FREEMEM(fresh_methods); } - content = CFCUtil_cat(content, self->footer, NULL); + content = CFCUtil_cat(content, self->c_footer, NULL); // Write if changed. const char *src_dest = CFCHierarchy_get_source_dest(self->hierarchy); http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/eaf2c94b/compiler/src/CFCRuby.c ---------------------------------------------------------------------- diff --git a/compiler/src/CFCRuby.c b/compiler/src/CFCRuby.c index c185218..ec3dd9a 100644 --- a/compiler/src/CFCRuby.c +++ b/compiler/src/CFCRuby.c @@ -33,8 +33,8 @@ struct CFCRuby { CFCHierarchy *hierarchy; char *lib_dir; char *boot_class; - char *header; - char *footer; + char *c_header; + char *c_footer; char *boot_h_file; char *boot_c_file; char *boot_h_path; @@ -74,8 +74,8 @@ CFCRuby_init(CFCRuby *self, CFCParcel *parcel, CFCHierarchy *hierarchy, self->hierarchy = (CFCHierarchy*)CFCBase_incref((CFCBase*)hierarchy); self->lib_dir = CFCUtil_strdup(lib_dir); self->boot_class = CFCUtil_strdup(boot_class); - self->header = CFCUtil_strdup(header); - self->footer = CFCUtil_strdup(footer); + self->c_header = CFCUtil_make_c_comment(header); + self->c_footer = CFCUtil_make_c_comment(footer); const char *prefix = CFCParcel_get_prefix(parcel); const char *inc_dest = CFCHierarchy_get_include_dest(hierarchy); @@ -103,8 +103,8 @@ CFCRuby_destroy(CFCRuby *self) { CFCBase_decref((CFCBase*)self->hierarchy); FREEMEM(self->lib_dir); FREEMEM(self->boot_class); - FREEMEM(self->header); - FREEMEM(self->footer); + FREEMEM(self->c_header); + FREEMEM(self->c_footer); FREEMEM(self->boot_h_file); FREEMEM(self->boot_c_file); FREEMEM(self->boot_h_path); @@ -153,16 +153,16 @@ S_write_boot_h(CFCRuby *self) { "%s\n"; size_t size = sizeof(pattern) - + strlen(self->header) + + strlen(self->c_header) + strlen(guard) + strlen(guard) + strlen(self->boot_func) + strlen(guard) - + strlen(self->footer) + + strlen(self->c_footer) + 20; char *content = (char*)MALLOCATE(size); - sprintf(content, pattern, self->header, guard, guard, self->boot_func, - guard, self->footer); + sprintf(content, pattern, self->c_header, guard, guard, self->boot_func, + guard, self->c_footer); CFCUtil_write_file(self->boot_h_path, content, strlen(content)); FREEMEM(content); @@ -212,9 +212,9 @@ S_write_boot_c(CFCRuby *self) { "\n"; char *content - = CFCUtil_sprintf(pattern, self->header, self->boot_h_file, prefix, + = CFCUtil_sprintf(pattern, self->c_header, self->boot_h_file, prefix, pound_includes, self->boot_func, prefix, - self->footer); + self->c_footer); CFCUtil_write_file(self->boot_c_path, content, strlen(content)); FREEMEM(content); @@ -249,7 +249,7 @@ CFCRuby_write_hostdefs(CFCRuby *self) { "\n" "%s\n"; char *content - = CFCUtil_sprintf(pattern, self->header, self->footer); + = CFCUtil_sprintf(pattern, self->c_header, self->c_footer); // Unlink then write file. const char *inc_dest = CFCHierarchy_get_include_dest(self->hierarchy); http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/eaf2c94b/compiler/src/CFCUtil.c ---------------------------------------------------------------------- diff --git a/compiler/src/CFCUtil.c b/compiler/src/CFCUtil.c index fdcfe7a..c3b47d7 100644 --- a/compiler/src/CFCUtil.c +++ b/compiler/src/CFCUtil.c @@ -144,6 +144,57 @@ CFCUtil_trim_whitespace(char *text) { *text = '\0'; } +char* +CFCUtil_enclose_lines(const char *text, const char *line_prefix, + const char *line_postfix, const char *prefix, + const char *postfix) { + if (!text) { return NULL; } + + if (!line_prefix) { line_prefix = ""; } + if (!line_postfix) { line_postfix = ""; } + if (!prefix) { prefix = ""; } + if (!postfix) { postfix = ""; } + + char *result = CFCUtil_strdup(prefix); + + const char *line_start = text; + const char *text_end = text + strlen(text); + + while (line_start < text_end) { + const char *line_end = strchr(line_start, '\n'); + const char *next_start; + size_t line_len; + + if (line_end == NULL) { + line_len = text_end - line_start; + next_start = text_end; + } + else { + line_len = line_end - line_start; + next_start = line_end + 1; + } + + char *line = (char*)MALLOCATE(line_len + 1); + memcpy(line, line_start, line_len); + line[line_len] = '\0'; + result = CFCUtil_cat(result, line_prefix, line, line_postfix, "\n", + NULL); + FREEMEM(line); + + line_start = next_start; + } + + result = CFCUtil_cat(result, postfix, NULL); + + return result; +} + +char* +CFCUtil_make_c_comment(const char *text) { + if (text && text[0] == '\0') { return CFCUtil_strdup(text); } + return CFCUtil_enclose_lines(text, " * ", "", "/*\n", " */\n"); +} + void* CFCUtil_wrapped_malloc(size_t count, const char *file, int line) { void *pointer = malloc(count); http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/eaf2c94b/compiler/src/CFCUtil.h ---------------------------------------------------------------------- diff --git a/compiler/src/CFCUtil.h b/compiler/src/CFCUtil.h index 961bccf..6012b3d 100644 --- a/compiler/src/CFCUtil.h +++ b/compiler/src/CFCUtil.h @@ -69,6 +69,19 @@ CFCUtil_cat(char *string, ...); void CFCUtil_trim_whitespace(char *text); +/** Enclose every line in text with line_prefix and line_postfix and the + * whole text with prefix and postfix. + */ +char* +CFCUtil_enclose_lines(const char *text, const char *line_prefix, + const char *line_postfix, const char *prefix, + const char *postfix); + +/** Create a C comment. + */ +char* +CFCUtil_make_c_comment(const char *text); + /** Attempt to allocate memory with malloc, but print an error and exit if the * call fails. */ http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/eaf2c94b/runtime/c/cfc_header ---------------------------------------------------------------------- diff --git a/runtime/c/cfc_header b/runtime/c/cfc_header index d982cf9..f2fff7c 100644 --- a/runtime/c/cfc_header +++ b/runtime/c/cfc_header @@ -1,23 +1,22 @@ -/*********************************************** +*********************************************** - !!!! DO NOT EDIT !!!! +!!!! DO NOT EDIT !!!! - This file was auto-generated by cfc. +This file was auto-generated by cfc. - ***********************************************/ +*********************************************** -/* Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * 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. - */ +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +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. http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/eaf2c94b/runtime/perl/buildlib/Clownfish/Build.pm ---------------------------------------------------------------------- diff --git a/runtime/perl/buildlib/Clownfish/Build.pm b/runtime/perl/buildlib/Clownfish/Build.pm index 0b7da01..b0743e2 100644 --- a/runtime/perl/buildlib/Clownfish/Build.pm +++ b/runtime/perl/buildlib/Clownfish/Build.pm @@ -246,30 +246,28 @@ sub ACTION_test_valgrind { sub _autogen_header { return <<"END_AUTOGEN"; -/*********************************************** - - !!!! DO NOT EDIT !!!! - - This file was auto-generated by Build.PL. - - ***********************************************/ - -/* Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * 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. - */ +*********************************************** +!!!! DO NOT EDIT !!!! + +This file was auto-generated by Build.PL. + +*********************************************** + +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +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. END_AUTOGEN } http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/eaf2c94b/runtime/ruby/Rakefile.common ---------------------------------------------------------------------- diff --git a/runtime/ruby/Rakefile.common b/runtime/ruby/Rakefile.common index cca6c09..3e61582 100644 --- a/runtime/ruby/Rakefile.common +++ b/runtime/ruby/Rakefile.common @@ -136,13 +136,13 @@ end def autogen_header " -/*********************************************** +*********************************************** - !!!! DO NOT EDIT !!!! +!!!! DO NOT EDIT !!!! - This file was auto-generated by Rakefile. +This file was auto-generated by Rakefile. - ***********************************************/ +*********************************************** " end
