bug-indent
[Top][All Lists]
Advanced

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

[PATCH 09/11] Fix handling of block comments after braces


From: Tim Hentenaar
Subject: [PATCH 09/11] Fix handling of block comments after braces
Date: Mon, 15 Jun 2015 21:53:00 +0200

Since the search_braces() function in indent.c loads _ALL_ comments
immediately surrounding braces into the save_com buffer, without
copying the indentation of the first line of the comment.

The code which copies the comment prepends it with a newline and a
space. The code in current_column() in code_io.c will only get the
newline and the space, returning a position of 2.

The code in print_comment() subtracts two from this number, printing the
whole thing verbatim as though it started in column 1.

This commit alters the print_comment() code in comments.c to check
whether or not the save buffer is being used, and if so, to use the
starting column set within the save buffer instead of calling
current_column() and subtracting 2, thus resulting in a properly
positioned comment.
---
 ChangeLog                            |  1 +
 regression/TEST                      |  2 +-
 regression/input/block-comments.c    | 16 ++++++++++++++++
 regression/standard/block-comments.c | 17 +++++++++++++++++
 src/comments.c                       | 11 ++++++++++-
 5 files changed, 45 insertions(+), 2 deletions(-)
 create mode 100644 regression/input/block-comments.c
 create mode 100644 regression/standard/block-comments.c

diff --git a/ChangeLog b/ChangeLog
index 2e9f639..015445a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -4,6 +4,7 @@
          with spces up till the target_column.
        * If -npcs is set, don't add a space between a function pointer's
          name and its arglist.
+       * Fix indentation of block comments following braces.
 
 2015-06-13 Tim Hentenaar <address@hidden>
        * lexi.c: Fix a compilation error related to the presence of
diff --git a/regression/TEST b/regression/TEST
index 7d6af44..53eb374 100755
--- a/regression/TEST
+++ b/regression/TEST
@@ -36,7 +36,7 @@ EXAMPLES="do.c else.c for.c func-def.c lshift.c ncs.c \
 BUGS="case-label.c one-line-1.c one-line-2.c one-line-3.c \
         one-line-4.c struct-decl.c sizeof-in-while.c line-break-comment.c \
         macro.c enum.c elif.c nested.c wrapped-string.c minus_predecrement.c \
-        bug-gnu-33364.c"
+        bug-gnu-33364.c block-comments.c"
 
 INDENTSRC="args.c backup.h backup.c dirent_def.h globs.c indent.h \
         indent.c indent_globs.h io.c lexi.c memcpy.c parse.c pr_comment.c \
diff --git a/regression/input/block-comments.c 
b/regression/input/block-comments.c
new file mode 100644
index 0000000..ec112b7
--- /dev/null
+++ b/regression/input/block-comments.c
@@ -0,0 +1,16 @@
+/**
+ * Test comment
+ */
+int
+a(int b)
+{
+  if (a == b) {
+    /* XXX */
+  }
+
+  /**
+   * This is a block comment
+   * spanning multiple lines
+   */
+  return b;
+}
diff --git a/regression/standard/block-comments.c 
b/regression/standard/block-comments.c
new file mode 100644
index 0000000..b597643
--- /dev/null
+++ b/regression/standard/block-comments.c
@@ -0,0 +1,17 @@
+/**
+ * Test comment
+ */
+int
+a (int b)
+{
+  if (a == b)
+    {
+      /* XXX */
+    }
+
+  /**
+   * This is a block comment
+   * spanning multiple lines
+   */
+  return b;
+}
diff --git a/src/comments.c b/src/comments.c
index 1b1fb57..a5d6dd5 100644
--- a/src/comments.c
+++ b/src/comments.c
@@ -164,7 +164,16 @@ extern void print_comment(
       line_preamble_length = 0;
       visible_preamble = 0;
 
-      start_column = current_column () - 2;
+      /* If this comment is in the save buffer, trust it's start_column. */
+      if ((buf_ptr >= save_com.ptr) && (buf_ptr <= save_com.ptr + 
save_com.len))
+      {
+          start_column = save_com.start_column;
+      }
+      else
+      {
+          start_column = current_column () - 2;
+      }
+
       found_column = start_column;
       parser_state_tos->box_com = 1;
       parser_state_tos->com_col = found_column;
-- 
2.3.6




reply via email to

[Prev in Thread] Current Thread [Next in Thread]