clang_indexSourceFile and CXCursor_(ParmDecl|VarDecl|DeclRefExpr)

Hi Guys

First off I want to thank you for the efforts on clang and llvm. Awesome job.

We're a couple of people who are writing a tagger for C/C++ (and I
suppose we could use it for objective-c and c++ as well) using clang.

We started off using the clang_visitChildren API and friends but have
recently to using clang_indexSourceFile which is nicer to work with
and seemingly faster. We've noticed however that we no longer seem to
get declarations/references of parameters and local variables. Is this
intentional? We're able to get to it using clang_visitChildren to
handle CXCursor_ParmDecl, CXCursor_VarDecl and CXCursor_DeclRefExpr
but this comes at a pretty hefty price in turns of performance.

Please find a small test attached.

To compile do:
g++ main.cpp -lclang

The app essentially dumps out some info for each call to the
indexDeclaration and indexReference callbacks. It's running on this
file:

int foo(int param)
{
    const int local = param * param;
    return (local - param);
}

int main()
{
    return foo(12);
}

// When I run this I get:

1:5: foo Function
7:5: main Function
9:12: ref of Function foo

No mention of either the local variable inside foo(int) or the
parameter. Is this the way it's supposed to be? I've noticed a lot
work has gone into these new APIs.

If anyone has any interest in the project or want to see how we use
clang it can be found here:

The clang-usage is mostly in RBuild.cpp

regards

Anders Bakken

main.cpp (2.99 KB)

test.cpp (124 Bytes)

Hi All,

For the record once I got syntax colouring working (tokenise and attribute the resulting list) I moved on to flagging warnings / errors (a-la XCode). I too was surprised that only some declarations/references were raised to the callback functions - most seemed related to global and cross-translation unit. All the 'internal' stuff was stripped.
Additionally, try as I might I cannot get the 'diagnostic' callback to be triggered, whether I introduce warnings or errors in the parsed code.

At first I thought it was just me or my app doing something stupid :slight_smile:

I might try migrating to visitChildren and the explicit API calls to get and walk the diagnostic list of a TU.

Cheers,
DavidM

<snip>

Skipping function-local symbols was intentional, but it is a reasonable request to get callbacks for such symbols.
After r148160 & r148169 you can set CXIndexOpt_IndexFunctionLocalSymbols at indexing options to get callbacks for function-local symbols.

I don't see issue with the 'diagnostic' callback. Try using 'c-index-test' like this:

c-index-test -index-file <compiler args>

on your source file. You should see 'diagnostic' getting called for errors/warnings.

-Argyrios

Sweet. That sounds like just what we need. Will try out.

Thanks

Anders

Seems to work great except that I it gives me the references twice.

I get this output from the attached test:

test.cpp:2:7: CXXClass TemplateClass
test.cpp:5:5: CXXConstructor TemplateClass<T>
test.cpp:5:21: Variable t
test.cpp:5:30: Variable b
test.cpp:5:34: ref of Variable t test.cpp:5:21
test.cpp:5:34: ref of Variable t test.cpp:5:21
test.cpp:5:41: ref of Variable b test.cpp:5:30
test.cpp:5:45: ref of Variable t test.cpp:5:21
test.cpp:5:50: ref of Variable b test.cpp:5:30
test.cpp:8:7: CXXClass NonTemplateClass
test.cpp:11:5: CXXConstructor NonTemplateClass
test.cpp:14:5: Function main
test.cpp:16:24: Variable tt
test.cpp:16:24: ref of CXXConstructor TemplateClass test.cpp:5:5
test.cpp:16:24: ref of CXXConstructor TemplateClass test.cpp:5:5
test.cpp:17:22: Variable t
test.cpp:17:22: ref of CXXConstructor NonTemplateClass test.cpp:11:5
test.cpp:17:5: ref of CXXClass NonTemplateClass test.cpp:8:7
test.cpp:17:22: ref of CXXConstructor NonTemplateClass test.cpp:11:5

E.g.

$ ./clangtest | sort | uniq -c
      1 test.cpp:11:5: CXXConstructor NonTemplateClass
      1 test.cpp:14:5: Function main
      1 test.cpp:16:24: Variable tt
      2 test.cpp:16:24: ref of CXXConstructor TemplateClass test.cpp:5:5
      1 test.cpp:17:22: Variable t
      2 test.cpp:17:22: ref of CXXConstructor NonTemplateClass test.cpp:11:5
      1 test.cpp:17:5: ref of CXXClass NonTemplateClass test.cpp:8:7
      1 test.cpp:2:7: CXXClass TemplateClass
      1 test.cpp:5:21: Variable t
      1 test.cpp:5:30: Variable b
      2 test.cpp:5:34: ref of Variable t test.cpp:5:21
      1 test.cpp:5:41: ref of Variable b test.cpp:5:30
      1 test.cpp:5:45: ref of Variable t test.cpp:5:21
      1 test.cpp:5:50: ref of Variable b test.cpp:5:30
      1 test.cpp:5:5: CXXConstructor TemplateClass<T>
      1 test.cpp:8:7: CXXClass NonTemplateClass

On a not necessarily related topic. I was kinda expecting a reference
to TemplateClass at 16:5:

E.g. something like this:

test.cpp:16:5: ref of CXXClass NonTemplateClass test.cpp:2:7

Is this expected behavior? Thanks again.

Anders

main.cpp (3.65 KB)

test.cpp (258 Bytes)