"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Dag-Erling Smørgrav <des@FreeBSD.org>
Subject:
diff -p redux
To:
gameoftrees@openbsd.org
Cc:
thj@freebsd.org
Date:
Thu, 07 Mar 2024 17:22:55 +0100

Download raw body.

Thread
  • Dag-Erling Smørgrav:

    diff -p redux

Hi,

I believe the attached patch solves the issue of function prototype
detection, cf. dc306c6bd88271ab911e205539974da98be82d17.  It simply
moves the calculation of chunk_start_line up and passes that to
diff_output_match_function_prototype(), which can then start the search
at the line preceding chunk_start_line.

The patch also includes a change to is_function_prototype() which causes
it to recognize Objective C methods, feel free to leave it out if you
don't care about Objective C.

DES
-- 
Dag-Erling Smørgrav - des@FreeBSD.org

diff --git a/lib/diff_internal.h b/lib/diff_internal.h
index 46bbadf..16ee677 100644
--- a/lib/diff_internal.h
+++ b/lib/diff_internal.h
@@ -148,7 +148,7 @@ int diff_output_trailing_newline_msg(struct diff_output_info *outinfo,
 int diff_output_match_function_prototype(char *prototype, size_t prototype_size,
 					 int *last_prototype_idx,
 					 const struct diff_result *result,
-					 const struct diff_chunk_context *cc);
+					 int chunk_start_line);
 
 struct diff_output_info *diff_output_info_alloc(void);
 
diff --git a/lib/diff_output.c b/lib/diff_output.c
index 7ac63bb..78d9b89 100644
--- a/lib/diff_output.c
+++ b/lib/diff_output.c
@@ -255,7 +255,8 @@ diff_output_trailing_newline_msg(struct diff_output_info *outinfo, FILE *dest,
 static bool
 is_function_prototype(unsigned char ch)
 {
-	return (isalpha((unsigned char)ch) || ch == '_' || ch == '$');
+	return (isalpha((unsigned char)ch) || ch == '_' || ch == '$' ||
+	    ch == '-' || ch == '+');
 }
 
 #define begins_with(s, pre) (strncmp(s, pre, sizeof(pre)-1) == 0)
@@ -263,7 +264,7 @@ is_function_prototype(unsigned char ch)
 int
 diff_output_match_function_prototype(char *prototype, size_t prototype_size,
     int *last_prototype_idx, const struct diff_result *result,
-    const struct diff_chunk_context *cc)
+    int chunk_start_line)
 {
 	struct diff_atom *start_atom, *atom;
 	const struct diff_data *data;
@@ -271,9 +272,9 @@ diff_output_match_function_prototype(char *prototype, size_t prototype_size,
 	const char *state = NULL;
 	int rc, i, ch;
 
-	if (result->left->atoms.len > 0 && cc->left.start > 0) {
+	if (result->left->atoms.len > 0 && chunk_start_line > 0) {
 		data = result->left;
-		start_atom = &data->atoms.head[cc->left.start - 1];
+		start_atom = &data->atoms.head[chunk_start_line - 1];
 	} else
 		return DIFF_RC_OK;
 
diff --git a/lib/diff_output_unidiff.c b/lib/diff_output_unidiff.c
index d480a02..88c98c6 100644
--- a/lib/diff_output_unidiff.c
+++ b/lib/diff_output_unidiff.c
@@ -301,10 +301,21 @@ output_unidiff_chunk(struct diff_output_info *outinfo, FILE *dest,
 	else
 		right_start = cc->right.start + 1;
 
+	/* Got the absolute line numbers where to start printing, and the index
+	 * of the interesting (non-context) chunk.
+	 * To print context lines above the interesting chunk, nipping on the
+	 * previous chunk index may be necessary.
+	 * It is guaranteed to be only context lines where left == right, so it
+	 * suffices to look on the left. */
+	const struct diff_chunk *first_chunk;
+	int chunk_start_line;
+	first_chunk = &result->chunks.head[cc->chunk.start];
+	chunk_start_line = diff_atom_root_idx(result->left,
+					      first_chunk->left_start);
 	if (show_function_prototypes) {
 		rc = diff_output_match_function_prototype(state->prototype,
 		    sizeof(state->prototype), &state->last_prototype_idx,
-		    result, cc);
+		    result, chunk_start_line);
 		if (rc)
 			return rc;
 	}
@@ -344,17 +355,6 @@ output_unidiff_chunk(struct diff_output_info *outinfo, FILE *dest,
 		*typep = DIFF_LINE_HUNK;
 	}
 
-	/* Got the absolute line numbers where to start printing, and the index
-	 * of the interesting (non-context) chunk.
-	 * To print context lines above the interesting chunk, nipping on the
-	 * previous chunk index may be necessary.
-	 * It is guaranteed to be only context lines where left == right, so it
-	 * suffices to look on the left. */
-	const struct diff_chunk *first_chunk;
-	int chunk_start_line;
-	first_chunk = &result->chunks.head[cc->chunk.start];
-	chunk_start_line = diff_atom_root_idx(result->left,
-					      first_chunk->left_start);
 	if (cc->left.start < chunk_start_line) {
 		rc = diff_output_lines(outinfo, dest, " ",
 				  &result->left->atoms.head[cc->left.start],
diff --git a/test/expect124.diff b/test/expect124.diff
index 82713c2..2ae051a 100644
--- a/test/expect124.diff
+++ b/test/expect124.diff
@@ -1,6 +1,6 @@
 --- test124.left-p.txt
 +++ test124.right-p.txt
-@@ -11,5 +11,5 @@ doSomethingThenPrintHello(int test)
+@@ -11,5 +11,5 @@ return_test(int test) {
  
  struct testfile *
  return_test(int test) {