git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 10/10] userdiff: have 'cpp' hunk header pattern catch more C++ anchor points

From
Johannes Sixt <j6t@kdbg.org>
Date
Mar 21, 2014, 21:07 UTC
Message-ID
<49db8b0f66c725ef6ff0b8a4d536c43e6397879a.1395433874.git.j6t@kdbg.org>
In-Reply-To
<cover.1395433874.git.j6t@kdbg.org>

The hunk header pattern 'cpp' is intended for C and C++ source code, but it is actually not particularly useful for the latter, and even misses some use-cases for the former.

The parts of the pattern have the following flaws:
- The first part matches an identifier followed immediately by a colon
  and arbitrary text and is intended to reject goto labels and C++
  access specifiers (public, private, protected). But this pattern also
  rejects C++ constructs, which look like this:
    MyClass::MyClass()
    MyClass::~MyClass()
    MyClass::Item MyClass::Find(...
- The second part matches an identifier followed by a list of qualified
  names (i.e. identifiers separated by the C++ scope operator '::')
  separated by space or '*' followed by an opening parenthesis (with
  space between the tokens). It matches function declarations like
    struct item* get_head(...
    int Outer::Inner::Func(...
  Since the pattern requires at least two identifiers, GNU-style
  function definitions are ignored:
    void
    func(...
  Moreover, since the pattern does not allow punctuation other than '*',
  the following C++ constructs are not recognized:
  . template definitions:
      template<class T> int func(T arg)
  . functions returning references:
      const string& get_message()
  . functions returning templated types:
      vector<int> foo()
  . operator definitions:
      Value operator+(Value l, Value r)
- The third part of the pattern finally matches compound definitions.
  But it forgets about unions and namespaces, and also skips single-line
  definitions
    struct random_iterator_tag {};
  because no semicolon can occur on the line.

Change the first pattern to require a colon at the end of the line (except for trailing space and comments), so that it does not reject constructor or destructor definitions.

Notice that all interesting anchor points begin with an identifier or keyword. But since there is a large variety of syntactical constructs after the first "word", the simplest is to require only this word and accept everything else. Therefore, this boils down to a line that begins with a letter or underscore (optionally preceded by the C++ scope operator '::' to accept functions returning a type anchored at the global namespace). Replace the second and third part by a single pattern that picks such a line.

This has the following desirable consequence:
- All constructs mentioned above are recognized.
and the following likely desirable consequences:
- Definitions of global variables and typedefs are recognized:
    int num_entries = 0;
    extern const char* help_text;
    typedef basic_string<wchar_t> wstring;
- Commonly used marco-ized boilerplate code is recognized:
    BEGIN_MESSAGE_MAP(CCanvas,CWnd)
    Q_DECLARE_METATYPE(MyStruct)
    PATTERNS("tex",...)
  (The last one is from this very patch.)
but also the following possibly undesirable consequence:
- When a label is not on a line by itself (except for a comment) it is
  no longer rejected, but can appear as a hunk header if it occurs at
  the beginning of a line:
    next:;

IMO, the benefits of the change outweigh the (possible) regressions by a large margin.

Signed-off-by: Johannes Sixt <j6t@kdbg.org>
---
 t/t4018/cpp-class-constructor              | 1 -
 t/t4018/cpp-class-constructor-mem-init     | 1 -
 t/t4018/cpp-class-destructor               | 1 -
 t/t4018/cpp-function-returning-global-type | 1 -
 t/t4018/cpp-function-returning-nested      | 1 -
 t/t4018/cpp-function-returning-reference   | 1 -
 t/t4018/cpp-gnu-style-function             | 1 -
 t/t4018/cpp-namespace-definition           | 1 -
 t/t4018/cpp-operator-definition            | 1 -
 t/t4018/cpp-struct-single-line             | 1 -
 t/t4018/cpp-template-function-definition   | 1 -
 t/t4018/cpp-union-definition               | 1 -
 userdiff.c                                 | 8 +++-----
 13 files changed, 3 insertions(+), 17 deletions(-)
diff --git a/t/t4018/cpp-class-constructor b/t/t4018/cpp-class-constructor
index 4c4925c..ec4f115 100644
--- a/t/t4018/cpp-class-constructor
+++ b/t/t4018/cpp-class-constructor
@@ -1,5 +1,4 @@
 Item::Item(int RIGHT)
 {
 	ChangeMe;
-	broken;
 }
diff --git a/t/t4018/cpp-class-constructor-mem-init b/t/t4018/cpp-class-constructor-mem-init
index eec1d7c..49a69f3 100644
--- a/t/t4018/cpp-class-constructor-mem-init
+++ b/t/t4018/cpp-class-constructor-mem-init
@@ -2,5 +2,4 @@ Item::Item(int RIGHT) :
 	member(0)
 {
 	ChangeMe;
-	broken;
 }
diff --git a/t/t4018/cpp-class-destructor b/t/t4018/cpp-class-destructor
index 03aa51c..5487665 100644
--- a/t/t4018/cpp-class-destructor
+++ b/t/t4018/cpp-class-destructor
@@ -1,5 +1,4 @@
 RIGHT::~RIGHT()
 {
 	ChangeMe;
-	broken;
 }
diff --git a/t/t4018/cpp-function-returning-global-type b/t/t4018/cpp-function-returning-global-type
index bff3e5f..1084d59 100644
--- a/t/t4018/cpp-function-returning-global-type
+++ b/t/t4018/cpp-function-returning-global-type
@@ -1,5 +1,4 @@
 ::Item get::it::RIGHT()
 {
 	ChangeMe;
-	broken;
 }
diff --git a/t/t4018/cpp-function-returning-nested b/t/t4018/cpp-function-returning-nested
index 41700f2..d9750aa 100644
--- a/t/t4018/cpp-function-returning-nested
+++ b/t/t4018/cpp-function-returning-nested
@@ -1,6 +1,5 @@
 get::Item get::it::RIGHT()
 {
 	ChangeMe;
-	broken;
 }
 
diff --git a/t/t4018/cpp-function-returning-reference b/t/t4018/cpp-function-returning-reference
index 29e2bd4..01b051d 100644
--- a/t/t4018/cpp-function-returning-reference
+++ b/t/t4018/cpp-function-returning-reference
@@ -1,5 +1,4 @@
 string& get::it::RIGHT(char *ptr)
 {
 	ChangeMe;
-	broken;
 }
diff --git a/t/t4018/cpp-gnu-style-function b/t/t4018/cpp-gnu-style-function
index d65fc74..08c7c75 100644
--- a/t/t4018/cpp-gnu-style-function
+++ b/t/t4018/cpp-gnu-style-function
@@ -2,5 +2,4 @@ const char *
 RIGHT(int arg)
 {
 	ChangeMe;
-	broken;
 }
diff --git a/t/t4018/cpp-namespace-definition b/t/t4018/cpp-namespace-definition
index 6b88dd9..6749980 100644
--- a/t/t4018/cpp-namespace-definition
+++ b/t/t4018/cpp-namespace-definition
@@ -1,5 +1,4 @@
 namespace RIGHT
 {
 	ChangeMe;
-	broken;
 }
diff --git a/t/t4018/cpp-operator-definition b/t/t4018/cpp-operator-definition
index f2bd167..1acd827 100644
--- a/t/t4018/cpp-operator-definition
+++ b/t/t4018/cpp-operator-definition
@@ -1,5 +1,4 @@
 Value operator+(Value LEFT, Value RIGHT)
 {
 	ChangeMe;
-	broken;
 }
diff --git a/t/t4018/cpp-struct-single-line b/t/t4018/cpp-struct-single-line
index ad6fa8b..a0de5fb 100644
--- a/t/t4018/cpp-struct-single-line
+++ b/t/t4018/cpp-struct-single-line
@@ -5,4 +5,3 @@ void wrong()
 struct RIGHT_iterator_tag {};
 
 int ChangeMe;
-// broken
diff --git a/t/t4018/cpp-template-function-definition b/t/t4018/cpp-template-function-definition
index a410298..0cdf5ba 100644
--- a/t/t4018/cpp-template-function-definition
+++ b/t/t4018/cpp-template-function-definition
@@ -1,5 +1,4 @@
 template<class T> int RIGHT(T arg)
 {
 	ChangeMe;
-	broken;
 }
diff --git a/t/t4018/cpp-union-definition b/t/t4018/cpp-union-definition
index 133b662..7ec94df 100644
--- a/t/t4018/cpp-union-definition
+++ b/t/t4018/cpp-union-definition
@@ -1,5 +1,4 @@
 union RIGHT {
 	double v;
 	int ChangeMe;
-	broken;
 };
diff --git a/userdiff.c b/userdiff.c
index 8830417..fad52d6 100644
--- a/userdiff.c
+++ b/userdiff.c
@@ -125,11 +125,9 @@
 	 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
 PATTERNS("cpp",
 	 /* Jump targets or access declarations */
-	 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:.*$\n"
-	 /* C/++ functions/methods at top level */
-	 "^([A-Za-z_][A-Za-z_0-9]*([ \t*]+[A-Za-z_][A-Za-z_0-9]*([ \t]*::[ \t]*[^[:space:]]+)?){1,}[ \t]*\\([^;]*)$\n"
-	 /* compound type at top level */
-	 "^((struct|class|enum)[^;]*)$",
+	 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
+	 /* functions/methods, variables, and compounds at top level */
+	 "^((::[[:space:]]*)?[A-Za-z_].*)$",
 	 /* -- */
 	 "[a-zA-Z_][a-zA-Z0-9_]*"
 	 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lLuU]*"
-- 
1.8.5.2.244.g9fb3fb1
Previous: Johannes SixtNext: Junio C Hamano
Message 27 of 30 in “diff: simplify cpp funcname regex”
  1. diff: simplify cpp funcname regexJeff King, Mar 5, 2014
  2. Johannes SixtMar 5, 2014
  3. Jeff KingMar 6, 2014
  4. Johannes SixtMar 7, 2014
  5. Jeff KingMar 14, 2014
  6. Johannes SixtMar 14, 2014
  7. Jeff KingMar 18, 2014
  8. Johannes SixtMar 18, 2014
  9. René ScharfeMar 18, 2014
  10. 00/10 userdiff: cpp pattern simplification and test frameworkJohannes Sixt, Mar 21, 2014
  11. 01/10 userdiff: support C++ ->* and .* operators in the word regexpJohannes Sixt, Mar 21, 2014
  12. 02/10 userdiff: support unsigned and long long suffixes of integer constantsJohannes Sixt, Mar 21, 2014
  13. 03/10 t4018: an infrastructure to test hunk headersJohannes Sixt, Mar 21, 2014
  14. Junio C HamanoMar 21, 2014
  15. Johannes SixtMar 22, 2014
  16. Junio C HamanoMar 23, 2014
  17. Jeff KingMar 24, 2014
  18. Jeff KingMar 24, 2014
  19. Johannes SixtMar 25, 2014
  20. Jeff KingMar 25, 2014
  21. 04/10 t4018: convert perl pattern tests to the new infrastructureJohannes Sixt, Mar 21, 2014
  22. 05/10 t4018: convert java pattern test to the new infrastructureJohannes Sixt, Mar 21, 2014
  23. 06/10 t4018: convert custom pattern test to the new infrastructureJohannes Sixt, Mar 21, 2014
  24. 07/10 t4018: reduce test files for pattern compilation testsJohannes Sixt, Mar 21, 2014
  25. 08/10 t4018: test cases for the built-in cpp patternJohannes Sixt, Mar 21, 2014
  26. 09/10 t4018: test cases showing that the cpp pattern misses many anchor pointsJohannes Sixt, Mar 21, 2014
  27. 10/10 userdiff: have 'cpp' hunk header pattern catch more C++ anchor pointsJohannes Sixt, Mar 21, 2014
  28. Junio C HamanoMar 21, 2014
  29. Jeff KingMar 24, 2014
  30. Junio C HamanoMar 5, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.