简体   繁体   English

获取 svn diff 以在提交期间显示 C++ function

[英]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.每当我在 svn 中执行提交周期时,我都会在编写评论时检查差异。 I thought it would be really nice to show the actual function that I made the modifications in when showing the diff.我认为在显示差异时显示我在其中进行修改的实际 function 会非常好。

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.我查看了此页面,其中提到 -p 选项将显示更改所在的 C function。当我尝试将 -p 选项与一些 C++ 代码一起使用时,它通常返回访问说明符(私有、公共、 protected 等),这不是很方便。

I did notice that there is a -F option for diff that does the same as -p, but takes a user-specified regex.我确实注意到 diff 有一个 -F 选项,它的作用与 -p 相同,但采用用户指定的正则表达式。 I was wondering: is there a simple regex to match a C++ function?我想知道:是否有一个简单的正则表达式来匹配 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.编辑:我不是在寻找灌篮高手正则表达式的东西,而是会显示区域差异上方最接近的 function 定义的东西。 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.恕我直言,只要它能正常工作,大约 60% 的时间就可以显着提高生产力。

Is there a simple regex to match a C++ function?是否有一个简单的正则表达式来匹配 C++ function? No.不。

Is there a (complex) regex to match a C++. Maybe or could be possible to write one.是否有一个(复杂的)正则表达式来匹配 C++。也许或者可以写一个。

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.但我会说正则表达式既不容易完成这样的任务(假设你想要某种 excat 匹配)也不是完成这样任务的正确工具。

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.唯一真正的解决方案是使用使用 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:如果您打算仅将此应用于您的提交,我建议您养成向 function 添加提交评论的习惯,例如:

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.将为您显示 function您对编辑的评论。 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: TortoiseSVN 在其 C++ 文件的提交对话框中使用以下正则表达式来支持自动完成:

.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:我不知道 SVN 中的选项会执行此操作,基于正则表达式的解决方案可能是以下一项或多项:

  • a nightmare to code and maintain, with lots of special cases编码和维护的噩梦,有很多特殊情况
  • incorrect, and missing several valid C++ functions不正确,并且缺少几个有效的 C++ 函数

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,他们在此页面上提供了几种 C/C++ 语法:

ANTLR Grammar Lists ANTLR 语法列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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