Unable to do even basic Clang tutorial

I’m trying to do the tutorials at:

https://github.com/loarabia/Clang-tu…i/TutorialOrig

and I can’t even do the first tutorial. I installed LLVM 3.1 and Clang 3.1 and when I do:

~/loarabia-Clang-tutorial-3d79443$ clang++ tutorial1.cpp In file included from tutorial1.cpp:5: In file included from ./llvm/Support/raw_ostream.h:17: ./llvm/Support/llvm/ADT/StringRef.h:13:10: fatal error: ‘llvm/Support/type_traits.h’ file not found

include “llvm/Support/type_traits.h”
^
1 error generated.

Do you know what I am doing wrong? I’m not sure what is the best way to handle this header file problem.

I’m running Ubuntu 12.04.

Thank you.

That tutorial is pretty old; I wouldn't be surprised if headers have
moved around or otherwise broken the tutorial. You also might need to
use `llvm-config` to get certain flags that are needed for compiling
with LLVM/Clang.

Lack of good tutorials is currently one of LLVM/Clang's biggest
documentation deficiencies (if not *the* biggest). If there's a
certain tutorial that you would like to see, please let us know. I
can't promise anything, but it's worth vocalizing these things so that
they have a sliver of a chance of getting done.

--Sean Silva

Is there an up-to-date tutorial that you can recommend?

I haven’t been able to successfully do anything at all on Clang except compile stuff.

The python bindings don’t work for me either.

http://clang.llvm.org/docs/Tooling.html

In particular, the LibTooling link is likely what you want.

  • Doug

BTW, the tutorial could use some updating as well. I tried it the other day on Mac OS X using Xcode and it didn't link properly. I had to first disable RTTI and then link libLLVMSupport.a and libLLVMMC.a as well.

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.

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?

Thanks.

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 :slight_smile:

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.

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?

FWIW, that's the intent of this page:

  http://clang.llvm.org/docs/Tooling.html

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.

If Clang finds the right system headers when invoked as a compiler, libclang/tooling/plugins will all find those same headers.

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?

libclang does much of this, although it only directly resolves within a single translation unit. It has some facilities (by way of "USRs"; see the documentation) to help clients provide cross-translation unit features.

(1) is handled by clang_getCursor() and clang_getCursorReferenced()/ clang_getCursorDefinition()

(2) is handled by walking the cursors of a particular class to find its base classes or (alternatively) using the indexing functionality and collecting all of the base classes of all types.

(3) is not supported by libclang, although it would make a wonderful addition to the library.

  - Doug

Hi Bill

For your information, compilation database is also available with libclang.
This has been added very recently.

Cheers,

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?

Read http://clang.llvm.org/docs/Tooling.html

and if you decide libclang is the best choice for you, I strongly
recommend you use the python bindings (rather than writing your indexing
tool in C). Use ipython (with its tab-completion) for discovering the
libclang API.

In particular, I'd like to write an indexing tool, like ctags but
better.

At the European LLVM conference in April 2012, someone gave a lightning
talk on a C++ class browser he was developing. I don't remember his name
though, and it isn't on the schedule or the slides/videos at
http://llvm.org/devmtg/2012-04-12/

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.

I have written "clang-ctags", a clang-based ctags implementation:
http://lists.cs.uiuc.edu/pipermail/cfe-dev/2012-July/023223.html

You might consider extending clang-ctags to produce a cscope database,
so you can use Emacs's cscope browser.

Cheers,
Dave.