简体   繁体   中英

Getting svn diff to show C++ function during commit

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.

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

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, eg:

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM