From 8a2e8da367f7175465118510b474ad365161d6b1 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Fri, 21 Mar 2014 22:07:22 +0100 Subject: 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 int func(T arg) . functions returning references: const string& get_message() . functions returning templated types: vector 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 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 Signed-off-by: Junio C Hamano --- 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 - 12 files changed, 12 deletions(-) (limited to 't') diff --git a/t/t4018/cpp-class-constructor b/t/t4018/cpp-class-constructor index 4c4925c237..ec4f115c25 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 eec1d7cbf3..49a69f37e1 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 03aa51ca5b..5487665096 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 bff3e5f21c..1084d5990e 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 41700f2c09..d9750aa61a 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 29e2bd4632..01b051df70 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 d65fc7489c..08c7c7565a 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 6b88dd9c3b..6749980241 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 f2bd1678f9..1acd827159 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 ad6fa8bbe9..a0de5fb800 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 a410298b0b..0cdf5ba5bd 100644 --- a/t/t4018/cpp-template-function-definition +++ b/t/t4018/cpp-template-function-definition @@ -1,5 +1,4 @@ template int RIGHT(T arg) { ChangeMe; - broken; } diff --git a/t/t4018/cpp-union-definition b/t/t4018/cpp-union-definition index 133b662258..7ec94df697 100644 --- a/t/t4018/cpp-union-definition +++ b/t/t4018/cpp-union-definition @@ -1,5 +1,4 @@ union RIGHT { double v; int ChangeMe; - broken; }; -- cgit v1.2.3