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
This commit is contained in:
Dag-Erling Smørgrav 2024-03-27 11:03:29 +01:00
parent d72a37b535
commit 5fbe8912d6
3 changed files with 18 additions and 17 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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],