1

Can clang extract all comments in C language code?

I'm a user VeriFast verifier https://github.com/verifast/verifast. And it has own parser, which is not C99 compatible. I would like to replace the parser with clang's to get C99 compatibility.

The verifier should maintain a special annotation such like following:

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

It means that the verifier should have precise source locations, including column numbers, for all comments.

First I tried following but can't extract all of them:

$ 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!"

Above testing can only find pre-comment of C language function.

Are there some methods to pick up all of comments in C language code using clang?

For more detail, please see following discussion:

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

Best regards,

masterq
  • 81
  • 5
  • 1
    What is the *actual* problem you want to solve? *Why* do you want to do this? Please read about [the XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) and think about how it relates to your question. – Some programmer dude Oct 05 '17 at 07:59
  • Possibly relevant, although not directly related to the debugging AST produced by `-ast-dump`: https://stackoverflow.com/q/25275212/1566221, https://stackoverflow.com/q/25275212/1566221 – rici Oct 05 '17 at 16:13

0 Answers0