[cfe-dev] Unable to do even basic Clang tutorial

Jacob Carlborg doob at me.com
Tue Jul 10 14:40:16 PDT 2012


On 2012-07-10 23:04, Bill White wrote:
> So, here's a followon question.  If one was starting a new project,
> would one use libclang,
> write a plug-in, or use the libtooling library?  For my application,
> at least initially, libclang
> seems like a good starting point.  But the tooling library would give
> me access to the
> compilation database, which would let me tell the clang parser what -I
> and -D flags to use.  I'm
> not sure how to do that with the libclang interface today.  The
> documentation is not
> clear.  Also, I'm not sure how system headers and definitions are to
> be supplied.  But in
> my application I would have to specify these separately, as we are
> very sensitive to the
> system headers, or at least to the version of the gcc headers which
> are included.

You basically use "clang_parseTranslationUnit" and can pass the same 
arguments as you would pass to Clang.

http://clang.llvm.org/doxygen/group__CINDEX__TRANSLATION__UNIT.html#ga2baf83f8c3299788234c8bce55e4472e

You can have a look at my tool, which uses libclang:

https://github.com/jacob-carlborg/dstep

In particular these two files:

https://github.com/jacob-carlborg/dstep/blob/master/dstep/driver/Application.d

https://github.com/jacob-carlborg/dstep/blob/master/dstep/translator/Translator.d

Note, this is written in D, but that should be any problem :)

Here's the most basic example using C :

http://pastebin.com/eYxsEQ01

> In particular, I'd like to write an indexing tool,
> like ctags but better.  There seems to be a lot of interest and
> discussion of it, but nothing
> seems to be usable.  I particularly want a tool which can be usable by
> emacs, which is my
> main editor, but with the kind of facilities that Eclipse CDT has.  These are:
>    1.) The ability to dive on an identifier and tell (a) where the identifier is
>        declared, and (b) the static type of the identifier.
>    2.) The ability to dive on a class name and display the entire class
> hierarchy,
>        both base classes and classes derived from the class.  I've used
> this is Java
>        under Eclipse, and found it enormously useful.
>    3.) The ability to show, in some sort of a pop-up window, the result
> of expanding
>        a macro invocation, or some kind of way of telling that a
> template is being
>        expanded.  These seem secondary to (1) and (2), though.
>
> If I'm missing some progress in this area, I apologize.  It seems as
> if there has been a lot
> of discussion, but I haven't seen anything usable, and I'm coming into
> some time to
> work on it.  Does libclang seem usable?

You can easily do 1 and 2 with libclang. 3 would be harder. It currently 
doesn't have a proper API for the preprocessor, something I would like 
to do as well.

-- 
/Jacob Carlborg






More information about the cfe-dev mailing list