]> git.ipfire.org Git - thirdparty/git.git/commitdiff
userdiff: have 'cpp' hunk header pattern catch more C++ anchor points
authorJohannes Sixt <j6t@kdbg.org>
Fri, 21 Mar 2014 21:07:22 +0000 (22:07 +0100)
committerJunio C Hamano <gitster@pobox.com>
Fri, 21 Mar 2014 22:03:32 +0000 (15:03 -0700)
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
13 files changed:
t/t4018/cpp-class-constructor
t/t4018/cpp-class-constructor-mem-init
t/t4018/cpp-class-destructor
t/t4018/cpp-function-returning-global-type
t/t4018/cpp-function-returning-nested
t/t4018/cpp-function-returning-reference
t/t4018/cpp-gnu-style-function
t/t4018/cpp-namespace-definition
t/t4018/cpp-operator-definition
t/t4018/cpp-struct-single-line
t/t4018/cpp-template-function-definition
t/t4018/cpp-union-definition
userdiff.c

index 4c4925c2376b2c03fbb0d7e2a8f98c02cba38d85..ec4f115c250f34f44c727c2abcb54b663bc2ddff 100644 (file)
@@ -1,5 +1,4 @@
 Item::Item(int RIGHT)
 {
        ChangeMe;
-       broken;
 }
index eec1d7cbf390d1c018378af147d95472cb45c1bd..49a69f37e1616e683a9d80531faddf2d11c7e0b6 100644 (file)
@@ -2,5 +2,4 @@ Item::Item(int RIGHT) :
        member(0)
 {
        ChangeMe;
-       broken;
 }
index 03aa51ca5b51056e66712fee320127448ba46e34..5487665096513a8645eb0726533ab0db65a1987b 100644 (file)
@@ -1,5 +1,4 @@
 RIGHT::~RIGHT()
 {
        ChangeMe;
-       broken;
 }
index bff3e5f21c5153bcd327db8f922c703e4d4c2fb5..1084d5990efac0729d8bd6642d81f450a2c6b724 100644 (file)
@@ -1,5 +1,4 @@
 ::Item get::it::RIGHT()
 {
        ChangeMe;
-       broken;
 }
index 41700f2c0929973edcd4bb5a2573a05aea6a29e3..d9750aa61a5009e8a6fa54012688b027f0290d3f 100644 (file)
@@ -1,6 +1,5 @@
 get::Item get::it::RIGHT()
 {
        ChangeMe;
-       broken;
 }
 
index 29e2bd4632511af17d82cc2a0057f4a775c49cb8..01b051df7015e6cf3d4621f02dc6b9eb6011514f 100644 (file)
@@ -1,5 +1,4 @@
 string& get::it::RIGHT(char *ptr)
 {
        ChangeMe;
-       broken;
 }
index d65fc7489c63429f67859e74feda779dea689525..08c7c7565ae2e5d1fed66ee633009f4e714b39f6 100644 (file)
@@ -2,5 +2,4 @@ const char *
 RIGHT(int arg)
 {
        ChangeMe;
-       broken;
 }
index 6b88dd9c3b7e281c276bee1cefe2baab3fcafec6..6749980241ccf2625eb841dfc5660af921df5139 100644 (file)
@@ -1,5 +1,4 @@
 namespace RIGHT
 {
        ChangeMe;
-       broken;
 }
index f2bd1678f9d68808806c6f5207e41cbdee301839..1acd827159212f57cd6267c4eba7a51594173ba2 100644 (file)
@@ -1,5 +1,4 @@
 Value operator+(Value LEFT, Value RIGHT)
 {
        ChangeMe;
-       broken;
 }
index ad6fa8bbe9457e60bf239902800f0aacf801b3b0..a0de5fb800fec1455deadd9aa2b5ad964a65ecd4 100644 (file)
@@ -5,4 +5,3 @@ void wrong()
 struct RIGHT_iterator_tag {};
 
 int ChangeMe;
-// broken
index a410298b0b2ed5084910f1c906def7cfebc428a8..0cdf5ba5bd439156063d686d5faa31184cc697e6 100644 (file)
@@ -1,5 +1,4 @@
 template<class T> int RIGHT(T arg)
 {
        ChangeMe;
-       broken;
 }
index 133b6622589417a68d25606894ebea9c38d5a50f..7ec94df697349061ec20b221eddb9c27835965da 100644 (file)
@@ -1,5 +1,4 @@
 union RIGHT {
        double v;
        int ChangeMe;
-       broken;
 };
index 8830417e3be09ea47453bfc1a7567d3028f274f9..fad52d6392fab8aadc8f6555db5aafcde224a120 100644 (file)
@@ -125,11 +125,9 @@ PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
         "\\\\[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]*"