Vim errorformat:在消息字符串中包含部分表达式

2024-05-13

使用vim的errorformat语法,有没有办法使用部分消息来过滤结果?

例如,除了错误本身之外,某些链接器错误没有任何明确的内容将它们区分为线路上的错误:

/path/to/foo.cpp:42: undefined reference to 'UnimplementedFunction'

or

/path/to/foo.cpp:43: multiple definition of 'MultiplyDefinedFunction'

使用以下错误格式:

set efm=%f:%l:\ %m

会正确捕获并显示这两种情况,但会错误地匹配许多其他情况(任何以“[string]:[number]:”开头的行)。

或者,明确指定它们:

set efm=
set efm+=%f:%l:\ undefined\ reference\ to\ %m
set efm+=%f:%l:\ multiple\ definition\ of\ %m

消除了误报,但“消息”变得不再那么有用——不再包含实际错误(只是它后面的内容)。


我在处理这种情况时缺少语法中的任何内容吗?

理想情况下,我希望能够说出以下内容:

set efm+=%f:%l:\ %{StartMessage}undefined\ reference\ to\ %*\\S%{EndMessage}
set efm+=%f:%l:\ %{StartMessage}multiple\ definition\ of\ %*\\S%{EndMessage}

...其中 StartMessage 和 EndMessage 之间匹配的所有内容都用作错误消息。


errorformat 还可以使用 vim 的正则表达式语法(尽管以一种相当尴尬的方式),这为我们提供了问题的解决方案。我们可以使用一个非捕获组和零宽度断言来要求这些信号短语的存在而不消耗它们。这然后允许%m去接他们。作为普通的正则表达式语法,这个零宽度断言看起来像:

\%(undefined reference\|multiple definition\)\@=

但为了将其用于efm我们需要更换\ by %\ and % by %%并用于:set我们需要转义反斜杠、空格和竖线,所以我们最终得到:

:set efm=%f:%l:\ %\\%%(undefined\ reference%\\\|multiple\ definitions%\\)%\\@=%m

有了这个错误文件

/path/to/foo.cpp:42: undefined reference to 'UnimplementedFunction'
/path/to/foo.cpp:43: multiple definition of 'MultiplyDefinedFunction'
notafile:123: just some other text

如下所示:copen:

/path/to/foo.cpp|42| undefined reference to 'UnimplementedFunction'
/path/to/foo.cpp|43| multiple definition of 'MultiplyDefinedFunction'
|| notafile:123: just some other text
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Vim errorformat:在消息字符串中包含部分表达式 的相关文章

随机推荐