如何检查doxygen \ return语句的正确性?

时间:2013-08-27 14:10:40

标签: c perl sed awk doxygen

我在评论中继承了一些带有doxygen注释的C源代码。由于有点腐烂,\return注释的一些注释现在是谎言,即函数已被转换为返回void,但\return注释仍然存在并且另有说明。

示例:

/*!
 * \brief   The foo function
 * \return  OK if successful, ERROR otherwise
 */
void foo(void)
{
    ...
}

现在我想找到所有文件。我想过使用grep / sed / awk / perl查找\return,然后打印以void开头的下一行。一个简单的

 grep -E '(\\return|^void)' file ...

还会打印来自非void函数的所有\return个注释。如果看到\return行,我确信只有 打印上一个^void行的简单方法。

2 个答案:

答案 0 :(得分:2)

如果您的所有功能及其前面的评论都遵循您发布的样式,那么这可能就是您所需要的:

awk '/\\return/{cmt=$0} /^[[:alpha:]]/{ if (/^void/ && cmt) print cmt ORS $0; cmt=""}' file

e.g:

$ cat file
/*!
 * \brief   The foo function
 * \return  OK if successful, ERROR otherwise
 */
void foo(void)
{
    ...
}

/*!
 * \brief   The bar function
 * \return  OK if successful, ERROR otherwise
 */
int bar(void)
{
    ...
}

$ awk '/\\return/{cmt=$0} /^[[:alpha:]]/{ if (/^void/ && cmt) print cmt ORS $0; cmt=""}' file
 * \return  OK if successful, ERROR otherwise
void foo(void)

答案 1 :(得分:1)

#! /usr/bin/env perl

use strict;
use warnings;

my $prev_return;

while (<>) {
  # assume letter in first column introduces a function return type
  if (my($return_type) = /^([^\W\d]\w*)/) {
    if ($return_type eq "void" && defined $prev_return) {
      print $prev_return;
    }
    undef $prev_return;  # reset after each function definition
  }
  elsif (/\\return\b/) {
    $prev_return = "$ARGV:$.: $_";
  }
}
continue {
  close ARGV if eof;  # reset $. at the end of each named file
}

注意:模式[^\W\d]使用double-negative technique来匹配不是数字的“单词字符”,,字母和下划线。

示例输出:

$ cat file.c
/*!
 * \brief   The foo function
 * \return  OK if successful, ERROR otherwise
 */
void foo(void)
{
    ...
}

\return fdsklj
void bar(void)

void baz

$ ./doxygen-return-void file.c
file.c:3:  * \return  OK if successful, ERROR otherwise
file.c:10: \return fdsklj

Perl的<>,也称为菱形运算符,使编写Unix风格的过滤器变得容易。这意味着您可以根据需要命名多个路径。

$ ./doxygen-return-void file.c file.c
file.c:3:  * \return  OK if successful, ERROR otherwise
file.c:10: \return fdsklj
file.c:3:  * \return  OK if successful, ERROR otherwise
file.c:10: \return fdsklj

上面的程序也会消耗标准输入,但输出并不是非常有用。

$ cat file.c | ./doxygen-return-void
-:3:  * \return  OK if successful, ERROR otherwise
-:10: \return fdsklj
相关问题