如何使用clang提取C语言代码中的所有注释?

时间:2017-10-05 07:56:52

标签: c parsing clang comments

clang能用C语言代码提取所有注释吗?

我是用户VeriFast验证程序https://github.com/verifast/verifast。它有自己的解析器,不兼容C99。我想用clang&替换解析器以获得C99兼容性。

验证者应该保留一个特殊的注释,如下所示:

https://github.com/verifast/verifast/blob/master/tutorial_solutions/stack.c

这意味着验证者应该拥有所有注释的精确源位置,包括列号。

首先我尝试了以下但无法提取所有这些内容:

$ vi hello.c
/* Comment: can't be found 1 */

// Comment: find me!
int main() {
  /* Comment: can't be found 2 */
  return 1+2;
}
$ clang --version |grep version
clang version 3.8.1-24 (tags/RELEASE_381/final)
$ clang -Xclang -ast-dump -fsyntax-only -fparse-all-comments hello.c
TranslationUnitDecl 0x564f03dc0d80 <<invalid sloc>> <invalid sloc>
|-TypedefDecl 0x564f03dc1278 <<invalid sloc>> <invalid sloc> implicit __int128_t '__int128'
| `-BuiltinType 0x564f03dc0fd0 '__int128'
|-TypedefDecl 0x564f03dc12d8 <<invalid sloc>> <invalid sloc> implicit __uint128_t 'unsigned __int128'
| `-BuiltinType 0x564f03dc0ff0 'unsigned __int128'
|-TypedefDecl 0x564f03dc1368 <<invalid sloc>> <invalid sloc> implicit __builtin_ms_va_list 'char *'
| `-PointerType 0x564f03dc1330 'char *'
|   `-BuiltinType 0x564f03dc0e10 'char'
|-TypedefDecl 0x564f03dc1618 <<invalid sloc>> <invalid sloc> implicit __builtin_va_list 'struct __va_list_tag [1]'
| `-ConstantArrayType 0x564f03dc15c0 'struct __va_list_tag [1]' 1 
|   `-RecordType 0x564f03dc1440 'struct __va_list_tag'
|     `-Record 0x564f03dc13b8 '__va_list_tag'
`-FunctionDecl 0x564f03dc1718 <hello.c:4:1, line:7:1> line:4:5 main 'int ()'
  |-CompoundStmt 0x564f03dc18a8 <col:12, line:7:1>
  | `-ReturnStmt 0x564f03dc1890 <line:6:3, col:12>
  |   `-BinaryOperator 0x564f03dc1868 <col:10, col:12> 'int' '+'
  |     |-IntegerLiteral 0x564f03dc1828 <col:10> 'int' 1
  |     `-IntegerLiteral 0x564f03dc1848 <col:12> 'int' 2
  `-FullComment 0x564f03dc1970 <line:3:3, col:20>
    `-ParagraphComment 0x564f03dc1940 <col:3, col:20>
      `-TextComment 0x564f03dc1910 <col:3, col:20> Text=" Comment: find me!"

以上测试只能找到C语言功能的预注释。

是否有一些方法可以使用clang来获取C语言代码中的所有注释?

有关详细信息,请参阅以下讨论:

https://github.com/verifast/verifast/issues/82

致以最诚挚的问候,

0 个答案:

没有答案