Issue 53146
Summary libclang not working as expected
Labels new issue
Assignees
Reporter BaoshanPang
    I am trying to use libclang to handle a c header file, but run into this issue:
This is the header file:
```
typedef union {
    int i;
    int j;
    int k;
} bad_union;
union good_union {
    int m;
    int n;
};
```
This is the code to demo the issue:

```
#include "clang-c/CXString.h"
#include <clang-c/Index.h>
#include <iostream>
using namespace std;

int main() {                                                                                                                 Use of undeclared identifier 'CXIndex' 
  CXIndex index = clang_createIndex(0, 0);
  CXTranslationUnit unit = clang_parseTranslationUnit(
      index, "t.h", nullptr, 0, nullptr, 0, CXTranslationUnit_None);
  if (unit == nullptr) {
    cerr << "Unable to parse translation unit. Quitting." << endl;
    exit(-1);
  }

  CXCursor cursor = clang_getTranslationUnitCursor(unit);
  clang_visitChildren(
      cursor,
      [](CXCursor c, CXCursor parent, CXClientData client_data) {
          if (clang_getCursorKind(c) == CXCursor_FieldDecl){
              cout << clang_getCString(clang_getCursorSpelling(c)) << endl;
          }
         return CXChildVisit_Recurse;
      },
      nullptr);

  clang_disposeTranslationUnit(unit);
  clang_disposeIndex(index);
}
```

After compiling, linking and  running I get such result:
```
$ ./h2c
i
j
k
i
j
k
m
n
```

i.e, for the 'bad_union' every field has been handled twice while in the 'good_union' every field is only handled once which is expected by me.

What's wrong in my code? and How to avoid the twice traversal for the case like 'bad_union'?

Thanks,
Baoshan
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to