git: 5fbe8912d6a2 - main - libdiff: Improve function prototype detection.

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Wed, 27 Mar 2024 11:26:15 UTC
The branch main has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=5fbe8912d6a2d1178468d1994c301d4c7cd4f975

commit 5fbe8912d6a2d1178468d1994c301d4c7cd4f975
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2024-03-27 10:03:29 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2024-03-27 10:03:29 +0000

    libdiff: Improve function prototype detection.
    
    - Recognize ObjC methods.
    - Start searching within the leading context.
    
    Sponsored by:   Klara, Inc.
    Reviewed by:    thj
    Differential Revision:  https://reviews.freebsd.org/D44301
---
 contrib/libdiff/lib/diff_internal.h       |  2 +-
 contrib/libdiff/lib/diff_output.c         |  9 +++++----
 contrib/libdiff/lib/diff_output_unidiff.c | 24 ++++++++++++------------
 3 files changed, 18 insertions(+), 17 deletions(-)

diff --git a/contrib/libdiff/lib/diff_internal.h b/contrib/libdiff/lib/diff_internal.h
index 46bbadf3cb64..16ee6776a71f 100644
--- a/contrib/libdiff/lib/diff_internal.h
+++ b/contrib/libdiff/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/contrib/libdiff/lib/diff_output.c b/contrib/libdiff/lib/diff_output.c
index 7ac63bb6c433..78d9b8942077 100644
--- a/contrib/libdiff/lib/diff_output.c
+++ b/contrib/libdiff/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/contrib/libdiff/lib/diff_output_unidiff.c b/contrib/libdiff/lib/diff_output_unidiff.c
index d480a022a9a7..88c98c663c00 100644
--- a/contrib/libdiff/lib/diff_output_unidiff.c
+++ b/contrib/libdiff/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],