git/t/t4018
Johannes Sixt 8a2e8da367 userdiff: have 'cpp' hunk header pattern catch more C++ anchor points
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>
2014-03-21 15:03:32 -07:00
..
cpp-c++-function t4018: test cases for the built-in cpp pattern 2014-03-21 15:03:29 -07:00
cpp-class-constructor userdiff: have 'cpp' hunk header pattern catch more C++ anchor points 2014-03-21 15:03:32 -07:00
cpp-class-constructor-mem-init userdiff: have 'cpp' hunk header pattern catch more C++ anchor points 2014-03-21 15:03:32 -07:00
cpp-class-definition t4018: test cases for the built-in cpp pattern 2014-03-21 15:03:29 -07:00
cpp-class-definition-derived t4018: test cases for the built-in cpp pattern 2014-03-21 15:03:29 -07:00
cpp-class-destructor userdiff: have 'cpp' hunk header pattern catch more C++ anchor points 2014-03-21 15:03:32 -07:00
cpp-function-returning-global-type userdiff: have 'cpp' hunk header pattern catch more C++ anchor points 2014-03-21 15:03:32 -07:00
cpp-function-returning-nested userdiff: have 'cpp' hunk header pattern catch more C++ anchor points 2014-03-21 15:03:32 -07:00
cpp-function-returning-pointer t4018: test cases for the built-in cpp pattern 2014-03-21 15:03:29 -07:00
cpp-function-returning-reference userdiff: have 'cpp' hunk header pattern catch more C++ anchor points 2014-03-21 15:03:32 -07:00
cpp-gnu-style-function userdiff: have 'cpp' hunk header pattern catch more C++ anchor points 2014-03-21 15:03:32 -07:00
cpp-namespace-definition userdiff: have 'cpp' hunk header pattern catch more C++ anchor points 2014-03-21 15:03:32 -07:00
cpp-operator-definition userdiff: have 'cpp' hunk header pattern catch more C++ anchor points 2014-03-21 15:03:32 -07:00
cpp-skip-access-specifiers t4018: test cases for the built-in cpp pattern 2014-03-21 15:03:29 -07:00
cpp-skip-comment-block t4018: test cases for the built-in cpp pattern 2014-03-21 15:03:29 -07:00
cpp-skip-labels t4018: test cases for the built-in cpp pattern 2014-03-21 15:03:29 -07:00
cpp-struct-definition t4018: test cases for the built-in cpp pattern 2014-03-21 15:03:29 -07:00
cpp-struct-single-line userdiff: have 'cpp' hunk header pattern catch more C++ anchor points 2014-03-21 15:03:32 -07:00
cpp-template-function-definition userdiff: have 'cpp' hunk header pattern catch more C++ anchor points 2014-03-21 15:03:32 -07:00
cpp-union-definition userdiff: have 'cpp' hunk header pattern catch more C++ anchor points 2014-03-21 15:03:32 -07:00
cpp-void-c-function t4018: test cases for the built-in cpp pattern 2014-03-21 15:03:29 -07:00
custom1-pattern t4018: convert custom pattern test to the new infrastructure 2014-03-21 15:02:57 -07:00
custom2-match-to-end-of-line t4018: convert custom pattern test to the new infrastructure 2014-03-21 15:02:57 -07:00
custom3-alternation-in-pattern t4018: convert custom pattern test to the new infrastructure 2014-03-21 15:02:57 -07:00
java-class-member-function t4018: convert java pattern test to the new infrastructure 2014-03-21 15:02:36 -07:00
perl-skip-end-of-heredoc
perl-skip-forward-decl
perl-skip-sub-in-pod
perl-sub-definition
perl-sub-definition-kr-brace
README

How to write RIGHT test cases
=============================

Insert the word "ChangeMe" (exactly this form) at a distance of
at least two lines from the line that must appear in the hunk header.

The text that must appear in the hunk header must contain the word
"right", but in all upper-case, like in the title above.

To mark a test case that highlights a malfunction, insert the word
BROKEN in all lower-case somewhere in the file.

This text is a bit twisted and out of order, but it is itself a
test case for the default hunk header pattern. Know what you are doing
if you change it.

BTW, this tests that the head line goes to the hunk header, not the line
of equal signs.