views:

317

answers:

4

Whenever I do a commit cycle in svn, I examine the diff when writing my comments. I thought it would be really nice to show the actual function that I made the modifications in when showing the diff.

I checked out this page, which mentioned that the -p option will show the C function that the change is in. When I tried using the -p option with some C++ code, however, it usually returns the access specifier (private, public, protected, etc), which isn't terribly handy.

I did notice that there is a -F option for diff that does the same as -p, but takes a user-specified regex. I was wondering: is there a simple regex to match a C++ function? It seems like that would be all that is necessary to get this to work.

I'd spend some time looking at this myself, but work is in crunch-mode and this seemed like something that a lot of people would find useful, so I figured I'd post it here.

EDIT: I'm not looking for something that's a slam-dunk catch-all regex, but something that would simply find the nearest function definition above the area diff would show. The fact that it would be nowhere near perfect, and somewhat buggy is okay with me. Just as long as it works right maybe like 60% of the time would be a significant productivity improvement IMHO.

+1  A: 

TortoiseSVN uses the following regexes for autocompletion support in its commit dialog for C++ files:

.h, .hpp, .hxx = ^\s*(?:class|struct)\s+([\w_]+)|\W([\w_]+)\(
.cpp, .c, .cxx = \W(([\w_]+)::([\w_]+))|\W([\w_]+)\(

I don't know how accurate they are, though.

Joey
They are s**t: e.g. `void hallo () {` isn't recognized as function signature
jitter
Looks like they need the namespace declaration, seeing a :: in there.
Joey
But I don't provide any :) using namespace std;
jitter
+1  A: 

I don't know of an option in SVN that will do this, and a regex-based solution will likely be one or more of the following:

  • a nightmare to code and maintain, with lots of special cases
  • incorrect, and missing several valid C++ functions

You need some sort of parser for this. It's technically possible to enumerate all of the possible regex cases, but writing a parser is the correct way to solve this. If you have time to roll your own solution I'd check out ANTLR, they have several C/C++ grammars available on this page:

ANTLR Grammar Lists

James Thompson
+2  A: 

Is there a simple regex to match a C++ function? No.

Is there a (complex) regex to match a C++. Maybe or could be possible to write one.

But I would say regular expressions neither are easily up to such a task (given you want some kind of excat match) nor are they the right tool for such a task.

Just think about case like this one. How would you handle this stuff.

void (*function(int, void (*)(int)))(int);

func1(int), func2(double); double func3(int);

The only real solution is to use a parser using yacc/lex. Which for your use case of course does nothing.

So either hack together some incomplete regex which fits most functions signatures in your code

jitter
Hmmm... I guess the solution to this is way more complex than I thought.
sheepsimulator
+2  A: 

If you're going to be applying this only to your commits I would recommend making a habit of adding a commit comment to the function, e.g:

void something () 
{
    ...
    some thing = 1;
    ...
}

to

void something () 
// last change by me: a better value for thing
{
    ...
    some thing = 2;
    ...
}

will display for you the function and your comment with the edits. As a bonus, other people will be able to understand what you're doing.

ilya n.
+1, good out-of-the-box answer; it's probably something I should be doing anyway.
sheepsimulator
Yes, I think in general overestimate the importance of global commit comments and underestimate the importance of inline comments.
ilya n.
I was kinda looking for, and still am looking for, a simple regex that would catch function calls of the form: <type> <class>::<function_name>(<parameters>). Can you come up with something that will match that?
sheepsimulator