All of lore.kernel.org
 help / color / mirror / Atom feed
From: Akira Yokosawa <akiyks@gmail.com>
To: "Paul E. McKenney" <paulmck@linux.ibm.com>
Cc: perfbook@vger.kernel.org, Akira Yokosawa <akiyks@gmail.com>
Subject: [PATCH 01/11] fcvextract.pl: Enhance comment block handling of C source
Date: Mon, 24 Dec 2018 23:53:55 +0900	[thread overview]
Message-ID: <42308834-adf6-46d9-c40d-9e65da22b6c1@gmail.com> (raw)
In-Reply-To: <0f522d14-373b-fdee-6779-eeaa04ee5fa4@gmail.com>

From b7193f419457aff8b34c4d293f6b747770d1a911 Mon Sep 17 00:00:00 2001
From: Akira Yokosawa <akiyks@gmail.com>
Date: Sun, 23 Dec 2018 09:12:48 +0900
Subject: [PATCH 01/11] fcvextract.pl: Enhance comment block handling of C source

Add an option "keepcomment" to \begin{snippet} meta command.
As a first step, default is "keepcomment=yes".
"keepcomment=no" will suppress comment blocks of the form
/* ... */ from appearing in the extracted snippet.

The default will be changed to "keepcomment=no" once the
explicit "keepcomment=yes" options are added where necessary.

Also add code to support "#ifndef FCV_SNIPPET -- #else -- #endif"
conditional to allow alternative code for snippet,
which is embedded using a comment block in a couple of code
samples as follows at the moment:

        <actual code>    //\fcvexclude
    /* --- begin alternative code for snippet \fcvexclude
        <alternative code>
       --- end alternative code for snippet \fcvexclude */

This won't work with "keepcomment=no". Instead, it can be embedded
in the following way:

    #ifndef FCV_SNIPPET
        <actual code>
    #else
        <alternative code>
    #endif

NOTE 1: "#else" is optional.
NOTE 2: FCV_SNIPPET should never be defined.

Signed-off-by: Akira Yokosawa <akiyks@gmail.com>
---
 utilities/fcvextract.pl | 82 ++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 77 insertions(+), 5 deletions(-)

diff --git a/utilities/fcvextract.pl b/utilities/fcvextract.pl
index ce549d3..2ad201f 100755
--- a/utilities/fcvextract.pl
+++ b/utilities/fcvextract.pl
@@ -89,6 +89,22 @@
 # To omit a line in extracted snippet, put "\fcvexclude" in comment
 # on the line.
 #
+# By default, comment blocks of the form "/* ... */" in C language
+# code will be kept in the extracted snippet. To omit those blocks,
+# put an option "keepcomment=no" to \begin{snippet} meta command.
+#
+# Also, this script recognizes #ifndef -- #else -- #endif conditional
+# of the following form to allow alternative code for snippet:
+#
+#	#ifndef FCV_SNIPPET
+#		<actual code>
+#	#else
+#		<alternative code for snippet>
+#	#endif
+#
+# NOTE: "#ifdef FCV_SNIPPET" is not recognized.
+#	"#else" can be omitted.
+#
 # Copyright (C) Akira Yokosawa, 2018
 #
 # Authors: Akira Yokosawa <akiyks@gmail.com>
@@ -97,6 +113,7 @@ use strict;
 use warnings;
 
 my $src_file;
+my $c_src = 0;
 my $lnlbl_re;
 my $line;
 my $edit_line;
@@ -113,6 +130,9 @@ my $file_name;
 my $func_name;
 my $label;
 my $env_name = "VerbatimL" ;
+my $keepcomment = 1;
+my $incomment = 0;
+my $ifndef = 0;
 my $other_opts;
 
 $src_file = $ARGV[0];
@@ -121,8 +141,9 @@ $extract_labelbase = $ARGV[1];
 $begin_re = qr/\\begin\{snippet\}.*labelbase=[^,\]]*$extract_labelbase[,\]]/ ;
 $end_re = qr/\\end\{snippet\}/;
 
-if ($src_file =~ /.*\.h$/ ) {
+if ($src_file =~ /.*\.[ch]$/ ) {
     $lnlbl_re = qr!(.*?)(\s*//\s*)\\lnlbl\{(.*)}\s*$!;
+    $c_src = 1;
 } elsif ($src_file =~ /.*\.c$/ ) {
     $lnlbl_re = qr!(.*?)(\s*//\s*)\\lnlbl\{(.*)}\s*$!;
 } elsif ($src_file =~ /.*\.spin$/ ) {
@@ -143,14 +164,57 @@ while($line = <>) {
 	last;
     }
     if ($extracting == 2) {
-	if (($line =~ /$end_re/) && ($extracting == 2)) {
+	if ($line =~ /$end_re/) {
 	    last;
 	}
 	if ($line =~ /\\fcvexclude/) {
-	    # skip this line
-	} elsif ($line =~ m!$lnlbl_re!) {
+	    next; # skip this line
+	}
+	if ($c_src && !$keepcomment) {
+	    if ($incomment) {
+		if ($line =~ /\*\/(.*$)/) {
+		    $line = $1;
+		    $incomment = 0;
+		} else {
+		    $line = "";
+		}
+	    } else {
+		if ($line =~ /(.*)\/\*.*\*\/(.*)/) {
+		    $line = $1 . $2;
+		    if ($line =~ /\S/) {
+			$line = $line . "\n";
+		    } else {
+			next;
+		    }
+		} elsif ($line =~ /(.*)\/\*.*/) {
+		    $line = $1;
+		    $incomment = 1;
+		}
+	    }
+	}
+	if ($ifndef == 1) {
+	    if ($line =~ /\#else/) {
+		$ifndef = 2;
+	    } elsif ($line =~ /\#endif/) {
+		$ifndef = 0;
+	    }
+	    next;
+	}
+	if ($ifndef == 2) {
+	    if ($line =~ /\#endif/ ) {
+		$ifndef = 0;
+		next;
+	    }
+	}
+	if ($c_src && $line =~ /\#ifndef\s+FCV_SNIPPET/) {
+	    $ifndef = 1;
+	    next;
+	}
+	if ($line =~ m!$lnlbl_re!) {
 	    $edit_line = $1 . $esc_bsl . "lnlbl" . $esc_open . $3 . $esc_close ;
 	    print $edit_line . "\n" ;
+	} elsif ($line eq "") {
+	    next;
 	} else {
 	    print $line ;
         }
@@ -158,7 +222,7 @@ while($line = <>) {
     if ($extracting == 1) {
 	print "% Do not edit!\n" ;
 	print "% Generated by utilities/fcvextract.pl.\n" ;
-	if ($line =~ /labelbase=([^,]+)/) {
+	if ($line =~ /labelbase=([^,\]]+)/) {
 	    print "\\begin\{linelabel}\[$1\]\n" ;
 	    $_ = $line ;
 	    s/labelbase=[^,\]]+,?// ;
@@ -191,6 +255,14 @@ while($line = <>) {
 	    $esc_open = "\{" ;
 	    $esc_close = "\}" ;
 	}
+	if ($line =~ /keepcomment=([^,\]]+).\]/) {
+	    if ($1 eq "no") {
+		$keepcomment = 0;
+	    }
+	    $_ = $line;
+	    s/keepcomment=[^,\]]+,?// ;
+	    $line = $_ ;
+	}
 	if ($line =~ /\[(.*)\]$/) {
 	    $_ = $1 ;
 	    s/,$// ;
-- 
2.7.4



  reply	other threads:[~2018-12-24 14:53 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-24 14:46 [PATCH 00/11] datastruct: Employ new scheme for code snippet Akira Yokosawa
2018-12-24 14:53 ` Akira Yokosawa [this message]
2018-12-24 14:55 ` [PATCH 02/11] CodeSamples: Add explicit 'keepcomment=yes' options Akira Yokosawa
2018-12-24 14:56 ` [PATCH 03/11] fcvextract.pl: Make 'keepcomment=no' as default Akira Yokosawa
2018-12-24 14:57 ` [PATCH 04/11] CodeSamples: Remove redundant \fcvexclude Akira Yokosawa
2018-12-24 14:59 ` [PATCH 05/11] fcvextract.pl: Support '/* \lnlbl{...} */' style label in C source Akira Yokosawa
2018-12-24 15:00 ` [PATCH 06/11] datastruct: Employ new scheme for snippets of hash_bkt.c Akira Yokosawa
2018-12-24 15:01 ` [PATCH 07/11] datastruct: Update hashdiagram figure Akira Yokosawa
2018-12-24 15:02 ` [PATCH 08/11] datastruct: Employ new scheme for snippets of hash_bkt_rcu and hash_resize Akira Yokosawa
2018-12-24 15:03 ` [PATCH 09/11] Make sure lmtt font is used in 'VerbatimL' and 'Verbatim' env Akira Yokosawa
2018-12-24 15:04 ` [PATCH 10/11] Use wider tabsize for snippet in 'listing*' Akira Yokosawa
2018-12-24 15:05 ` [PATCH 11/11] datastruct: Tweak hyphenation Akira Yokosawa
2018-12-24 23:58 ` [PATCH 00/11] datastruct: Employ new scheme for code snippet Paul E. McKenney
2018-12-25  0:53   ` Paul E. McKenney
2018-12-25 14:30     ` Akira Yokosawa
2018-12-26 14:17       ` Paul E. McKenney
2018-12-26 14:31       ` [PATCH] gen_snippet_d.pl: Add rules to ignore editor's backup files Akira Yokosawa
2018-12-26 15:00         ` Paul E. McKenney
2018-12-31  4:37           ` Sporadic SIGSEGV in hash_bkt_rcu and hash_resize (was Re: [PATCH] gen_snippet_d.pl: Add rules to ignore editor's backup files) Akira Yokosawa
2018-12-31 15:15             ` [PATCH] EXP hashtorture.h: Avoid sporadic SIGSEGV in hash_bkt_rcu Akira Yokosawa
2018-12-31 21:03               ` Paul E. McKenney
2019-01-01  0:27                 ` Akira Yokosawa
2019-01-01 18:00                   ` Paul E. McKenney
2019-01-02 15:02                     ` Akira Yokosawa
2019-01-02 17:18                       ` Paul E. McKenney
2019-01-02 19:18                         ` Paul E. McKenney
2019-01-03 15:57                           ` [PATCH] datastruct/hash: Tweak appearance of updated code in snippet Akira Yokosawa
2019-01-03 17:21                             ` Paul E. McKenney
2019-01-03 23:35                               ` Akira Yokosawa
2019-01-04  0:52                                 ` Paul E. McKenney
2019-01-04  1:56                                   ` Akira Yokosawa
2019-01-04  3:56                                     ` Paul E. McKenney
2019-01-04 15:38                                 ` Akira Yokosawa
2019-01-04 15:39                                   ` [PATCH 1/2] datastruct/hash: Tweak indent of folded line " Akira Yokosawa
2019-01-04 22:40                                     ` Paul E. McKenney
2019-01-04 15:41                                   ` [PATCH 2/2] datastruct/hash: Annotate racy accesses with READ_ONCE/WRITE_ONCE Akira Yokosawa
2019-01-05  0:10                                     ` Paul E. McKenney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=42308834-adf6-46d9-c40d-9e65da22b6c1@gmail.com \
    --to=akiyks@gmail.com \
    --cc=paulmck@linux.ibm.com \
    --cc=perfbook@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.