git: 8f79bd9b8571 - main - diff: Detect Objective-C methods

From: Tom Jones <thj_at_FreeBSD.org>
Date: Fri, 18 Feb 2022 15:11:01 UTC
The branch main has been updated by thj:

URL: https://cgit.FreeBSD.org/src/commit/?id=8f79bd9b85716c495c2741ac25db37e8d71f22f7

commit 8f79bd9b85716c495c2741ac25db37e8d71f22f7
Author:     Tom Jones <thj@FreeBSD.org>
AuthorDate: 2022-02-18 15:07:52 +0000
Commit:     Tom Jones <thj@FreeBSD.org>
CommitDate: 2022-02-18 15:09:57 +0000

    diff: Detect Objective-C methods
    
    When searching back for function definitions, consider lines starting
    with '+' and '-', this allows us to pick up Objective-C methods as well
    as C style function definitions.
    
    Reviewed by:    bapt
    Sponsored by:   Klara Inc.
    Differential Revision:  https://reviews.freebsd.org/D34202
---
 usr.bin/diff/diff.1                            |  4 ++--
 usr.bin/diff/diffreg.c                         |  3 ++-
 usr.bin/diff/tests/Makefile                    |  9 +++++++-
 usr.bin/diff/tests/diff_test.sh                | 19 ++++++++++++++++
 usr.bin/diff/tests/functionname.in             | 29 ++++++++++++++++++++++++
 usr.bin/diff/tests/functionname_c.in           | 29 ++++++++++++++++++++++++
 usr.bin/diff/tests/functionname_c.out          | 11 +++++++++
 usr.bin/diff/tests/functionname_objcclassm.in  | 31 ++++++++++++++++++++++++++
 usr.bin/diff/tests/functionname_objcclassm.out | 11 +++++++++
 usr.bin/diff/tests/functionname_objcm.in       | 29 ++++++++++++++++++++++++
 usr.bin/diff/tests/functionname_objcm.out      | 11 +++++++++
 11 files changed, 182 insertions(+), 4 deletions(-)

diff --git a/usr.bin/diff/diff.1 b/usr.bin/diff/diff.1
index dea01bf918f4..b17ddb123fd5 100644
--- a/usr.bin/diff/diff.1
+++ b/usr.bin/diff/diff.1
@@ -396,8 +396,8 @@ file name and time in the context or unified diff header.
 With unified and context diffs, show with each change
 the first 40 characters of the last line before the context beginning
 with a letter, an underscore or a dollar sign.
-For C source code following standard layout conventions, this will
-show the prototype of the function the change applies to.
+For C and Objective-C source code following standard layout conventions, this
+will show the prototype of the function the change applies to.
 .It Fl T -initial-tab
 Print a tab rather than a space before the rest of the line for the
 normal, context or unified output formats.
diff --git a/usr.bin/diff/diffreg.c b/usr.bin/diff/diffreg.c
index 995843f9e539..ae815aa4e01f 100644
--- a/usr.bin/diff/diffreg.c
+++ b/usr.bin/diff/diffreg.c
@@ -1419,7 +1419,8 @@ match_function(const long *f, int pos, FILE *fp)
 			strlcpy(lastbuf, buf, sizeof(lastbuf));
 			lastmatchline = pos;
 			return (lastbuf);
-		} else if (isalpha(buf[0]) || buf[0] == '_' || buf[0] == '$') {
+		} else if (isalpha(buf[0]) || buf[0] == '_' || buf[0] == '$'
+			|| buf[0] == '-' || buf[0] == '+') {
 			if (begins_with(buf, "private:")) {
 				if (!state)
 					state = " (private)";
diff --git a/usr.bin/diff/tests/Makefile b/usr.bin/diff/tests/Makefile
index 303a37911fbd..fbfac0c8372d 100644
--- a/usr.bin/diff/tests/Makefile
+++ b/usr.bin/diff/tests/Makefile
@@ -27,7 +27,14 @@ ${PACKAGE}FILES+=	\
 	header_ns.out \
 	ifdef.out \
 	group-format.out \
-	strip_o.out
+	strip_o.out \
+	functionname.in \
+	functionname_c.in \
+	functionname_c.out \
+	functionname_objcclassm.in \
+	functionname_objcclassm.out \
+	functionname_objcm.in \
+	functionname_objcm.out
 
 NETBSD_ATF_TESTS_SH+=	netbsd_diff_test
 
diff --git a/usr.bin/diff/tests/diff_test.sh b/usr.bin/diff/tests/diff_test.sh
index 4f73b23d686a..8c0219712db7 100755
--- a/usr.bin/diff/tests/diff_test.sh
+++ b/usr.bin/diff/tests/diff_test.sh
@@ -19,6 +19,7 @@ atf_test_case label
 atf_test_case report_identical
 atf_test_case non_regular_file
 atf_test_case binary
+atf_test_case functionname
 
 simple_body()
 {
@@ -278,6 +279,23 @@ binary_body()
 	atf_check -o inline:"176c\nx\n.\n" -s exit:1 diff -ae A B
 }
 
+functionname_body()
+{
+	atf_check -o empty -x "which diff"
+
+	atf_check -o file:$(atf_get_srcdir)/functionname_c.out -s exit:1 \
+		diff -u -p -L functionname.in -L functionname_c.in \
+		"$(atf_get_srcdir)/functionname.in" "$(atf_get_srcdir)/functionname_c.in"
+
+	atf_check -o file:$(atf_get_srcdir)/functionname_objcm.out -s exit:1 \
+		diff -u -p -L functionname.in -L functionname_objcm.in \
+		"$(atf_get_srcdir)/functionname.in" "$(atf_get_srcdir)/functionname_objcm.in"
+
+	atf_check -o file:$(atf_get_srcdir)/functionname_objcclassm.out -s exit:1 \
+		diff -u -p -L functionname.in -L functionname_objcclassm.in \
+		"$(atf_get_srcdir)/functionname.in" "$(atf_get_srcdir)/functionname_objcclassm.in"
+}
+
 atf_init_test_cases()
 {
 	atf_add_test_case simple
@@ -299,4 +317,5 @@ atf_init_test_cases()
 	atf_add_test_case report_identical
 	atf_add_test_case non_regular_file
 	atf_add_test_case binary
+	atf_add_test_case functionname
 }
diff --git a/usr.bin/diff/tests/functionname.in b/usr.bin/diff/tests/functionname.in
new file mode 100644
index 000000000000..7b4c50c86cd9
--- /dev/null
+++ b/usr.bin/diff/tests/functionname.in
@@ -0,0 +1,29 @@
+static void
+doSomethingThenPrintHello(int test)
+{
+	test = test << 4;
+	if (test % 8 == 6) {
+		return;
+	}
+
+	print("goodbye\n");
+}
+
+
+- (long) readOffset:(FILE*)file
+{
+	if( version >= 11){
+		long offset;
+		fread(&offset, sizeof(long), 1, file);
+		return offset;
+	} else {
+		int offset;
+		fread(&offset, sizeof(int), 1, file);
+		return offset;
+	}
+}
+
++ (BOOL) isEdible:(NSString *)mushroom
+{
+	return TRUE;
+}
diff --git a/usr.bin/diff/tests/functionname_c.in b/usr.bin/diff/tests/functionname_c.in
new file mode 100644
index 000000000000..84f6846783ca
--- /dev/null
+++ b/usr.bin/diff/tests/functionname_c.in
@@ -0,0 +1,29 @@
+static void
+doSomethingThenPrintHello(int test)
+{
+	test = test << 4;
+	if (test % 8 == 6) {
+		return;
+	}
+
+	print("hello\n");
+}
+
+
+- (long) readOffset:(FILE*)file
+{
+	if( version >= 11){
+		long offset;
+		fread(&offset, sizeof(long), 1, file);
+		return offset;
+	} else {
+		int offset;
+		fread(&offset, sizeof(int), 1, file);
+		return offset;
+	}
+}
+
++ (BOOL) isEdible:(NSString *)mushroom
+{
+	return TRUE;
+}
diff --git a/usr.bin/diff/tests/functionname_c.out b/usr.bin/diff/tests/functionname_c.out
new file mode 100644
index 000000000000..b17ce05d04ca
--- /dev/null
+++ b/usr.bin/diff/tests/functionname_c.out
@@ -0,0 +1,11 @@
+--- functionname.in
++++ functionname_c.in
+@@ -6,7 +6,7 @@ doSomethingThenPrintHello(int test)
+ 		return;
+ 	}
+ 
+-	print("goodbye\n");
++	print("hello\n");
+ }
+ 
+ 
diff --git a/usr.bin/diff/tests/functionname_objcclassm.in b/usr.bin/diff/tests/functionname_objcclassm.in
new file mode 100644
index 000000000000..37a9a76c6e6a
--- /dev/null
+++ b/usr.bin/diff/tests/functionname_objcclassm.in
@@ -0,0 +1,31 @@
+static void
+doSomethingThenPrintHello(int test)
+{
+	test = test << 4;
+	if (test % 8 == 6) {
+		return;
+	}
+
+	print("goodbye\n");
+}
+
+
+- (long) readOffset:(FILE*)file
+{
+	if( version >= 11){
+		long offset;
+		fread(&offset, sizeof(long), 1, file);
+		return offset;
+	} else {
+		int offset;
+		fread(&offset, sizeof(int), 1, file);
+		return offset;
+	}
+}
+
++ (BOOL) isEdible:(NSString *)mushroom
+{
+	/* With a solid guide book (such as Phillips 2006) assume we can't eat
+	 * the fungus */
+	return FALSE;
+}
diff --git a/usr.bin/diff/tests/functionname_objcclassm.out b/usr.bin/diff/tests/functionname_objcclassm.out
new file mode 100644
index 000000000000..b68b732fb7c3
--- /dev/null
+++ b/usr.bin/diff/tests/functionname_objcclassm.out
@@ -0,0 +1,11 @@
+--- functionname.in
++++ functionname_objcclassm.in
+@@ -25,5 +25,7 @@ + (BOOL) isEdible:(NSString *)mushroom
+ 
+ + (BOOL) isEdible:(NSString *)mushroom
+ {
+-	return TRUE;
++	/* With a solid guide book (such as Phillips 2006) assume we can't eat
++	 * the fungus */
++	return FALSE;
+ }
diff --git a/usr.bin/diff/tests/functionname_objcm.in b/usr.bin/diff/tests/functionname_objcm.in
new file mode 100644
index 000000000000..06c3e9b2722d
--- /dev/null
+++ b/usr.bin/diff/tests/functionname_objcm.in
@@ -0,0 +1,29 @@
+static void
+doSomethingThenPrintHello(int test)
+{
+	test = test << 4;
+	if (test % 8 == 6) {
+		return;
+	}
+
+	print("goodbye\n");
+}
+
+
+- (long) readOffset:(FILE*)file
+{
+	if( version >= 11){
+		long offset;
+		fread(&offset, sizeof(long), 1, file);
+		return offset;
+	} else {
+		int offset;
+		fread(&offset-1, sizeof(int), 1, file);
+		return offset;
+	}
+}
+
++ (BOOL) isEdible:(NSString *)mushroom
+{
+	return TRUE;
+}
diff --git a/usr.bin/diff/tests/functionname_objcm.out b/usr.bin/diff/tests/functionname_objcm.out
new file mode 100644
index 000000000000..cb29b0892115
--- /dev/null
+++ b/usr.bin/diff/tests/functionname_objcm.out
@@ -0,0 +1,11 @@
+--- functionname.in
++++ functionname_objcm.in
+@@ -18,7 +18,7 @@ - (long) readOffset:(FILE*)file
+ 		return offset;
+ 	} else {
+ 		int offset;
+-		fread(&offset, sizeof(int), 1, file);
++		fread(&offset-1, sizeof(int), 1, file);
+ 		return offset;
+ 	}
+ }