Problem parsing a simple C++ file.

I am trying to parse simple C++ class with CompilerInstance but get the following error;

E:/mycpp_vc/classtest.cpp:3:1: error: unknown type name ‘class’
class Test
^
E:/mycpp_vc/classtest.cpp:3:13: error: expected ‘;’ after top level declarator
class Test
^
;
My understanding is clang assumes the file is a C file and hence the error.
I’ve thought clang would deduce that the file is a C++ by its extension.
I’ve also tried setting ci.getLangOpts().CPlusPlus=1 before parsing to no avail.

Is there something am I overlooking?

Instead of using CompilerInstance yourself, you could try writing either a clang plugin or use libtooling (see http://clang.llvm.org/docs/Tooling.html for when to use what).

If you cannot do that for some reason, you can take a look at:
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Tooling.cpp?view=markup

for a working (rather minimal) example on how to set up a CompilerInstance.

Cheers,
/Manuel

http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Tooling.cpp?view=markup

Ugh. Why is this directly including system headers? That's a blatant
violation of <http://llvm.org/docs/SystemLibrary.html#don-t-include-system-headers>.

Manuel, what kind of review did this code get before being checked in?
I can't seem to find any review thread leading up to r154008.

-- Sean Silva

http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20120123/051828.html
(note that this review went over 4 months, so the mail archive can't follow
it through; use site:lists.cs.uiuc.edu "[PATCH] Implements support to run
standalone tools" in your favorite search engine to find all parts of the
review)

Note also that the lines you object to are not in the original patch. The
patch that made those includes necessary was reviewed here:
http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20120507/057330.html
(again, the mail archive doesn't tell the full story)

If you have a better solution, feel free to make suggestions.

Cheers,
/Manuel

Ah, I see, the initial review precedes my gmail log.

The natural thing for me seems like it would just be to add a
changeDirectory() function to libSystem. Was this not considered?

-- Sean Silva

Ah, I see, the initial review precedes my gmail log.

The natural thing for me seems like it would just be to add a
changeDirectory() function to libSystem. Was this not considered?

+Michael
There was definitely some talk about mid- / long-term strategies, but I
think we didn't find a good short-term solution beside adding the non-nice
system includes.

One thing we'll need to do anyway is make clang thread-safe regarding
chdir, which means that we'll need an interface that supports keeping state
between calls so that changes to the directory (like symlinks changing
during a compile) do not affect the compilation.

Cheers,
/Manuel

Ah, I see, the initial review precedes my gmail log.

The natural thing for me seems like it would just be to add a
changeDirectory() function to libSystem. Was this not considered?

This was considered, but chdir is evil due to its hatred of threads.
The alternative needs directory handles, which we have yet to do, but
Chandler and I were just discussing this in #llvm last night.

I'm fine with the current code as a short-term solution. I'm also fine
with making it work on Windows by using ChangeDirectory.

- Michael Spencer