Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

c++11 files not parsed correctly? #2

Open
ikirill opened this issue Jan 6, 2015 · 13 comments
Open

c++11 files not parsed correctly? #2

ikirill opened this issue Jan 6, 2015 · 13 comments

Comments

@ikirill
Copy link

ikirill commented Jan 6, 2015

In this file (with flycheck-irony installed from melpa in a sandboxed emacs version 25.0.50)

#include <functional>

int main() {
  std::function<int(int)> f;
  return f(0);
}

// Local Variables:
// irony-additional-clang-options: ("-std=c++11")
// eval: (progn (irony-mode) (flycheck-mode) (flycheck-select-checker 'irony))
// End:

flycheck-irony says no member named 'function'..., which is wrong, because if I understand the documentation correctly it should be using -std=c++11. (On the command line clang -x c++ does not even need -std=c++11.)

Clang version Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)

@ikirill ikirill changed the title c++11 files not be parsed correctly? c++11 files not parsed correctly? Jan 6, 2015
@Sarcasm
Copy link
Owner

Sarcasm commented Jan 6, 2015

Doesn't happen for me. :/

Can you check that the proper checker is used even if it looks like your code set it properly: M-x flycheck-list-errors RET and see if the checker is really irony.

I do not really recommend using irony-additional-clang-options but for now I guess there is no better alternative. Ideally a fallback compilation database could exists that specifies these flags, that would be better but that's another subject.

You are on Mac OS X, I'm wondering if there is not some kind of issues with the standard library picked. What if you provide additional arguments such as -stdlib=libc++?

@ikirill
Copy link
Author

ikirill commented Jan 9, 2015

My libclang came with xcode, and the problem was that it could not find c++ headers. Cmake found libclang headers in /usr/local/include (they don't come with xcode, so I installed them separately), but libclang needs also to be told where xcode clang headers live

-I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1

because cmake does not seem to pick up that path with find_package(LibClang).

This is irritating, but not flycheck-irony's fault. If I invoke clang with -### to see what flags libclang should use, clang is invoked with

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang

This is a related bug, I think: Sarcasm/irony-mode#138

What's interesting is that the completion code doesn't check for diagnostics, and just fails silently on errors like 'vector' file not found or undeclared identifier 'std', failing to suggest any c++ completions.

@Sarcasm
Copy link
Owner

Sarcasm commented Jan 9, 2015

My libclang came with xcode, and the problem was that it could not find c++ headers. Cmake found libclang headers in /usr/local/include (they don't come with xcode, so I installed them separately), but libclang needs also to be told where xcode clang headers live

I think it would be better to copy the whole clang-related stuff from the /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/ prefix and put it somewhere else. Then tell irony-mode -DCMAKE_PREFIX_PATH=<libclang-prefix>. This could avoid some of the issues but I'm not sure that it will solves everything.

Or you sure you wanted this compile options?

-I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1

Wouldn't the following be better?

-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/ [-stdlib=libc++]

Maybe you would be better of using and "portable release": http://llvm.org/releases/download.html
And then maybe install libc++ separately if you don't have a proper libc++ installed? Re-using the XCode

What's interesting is that the completion code doesn't check for diagnostics, and just fails silently on errors like 'vector' file not found or undeclared identifier 'std', failing to suggest any c++ completions.

This may be fixed in the future. I implemented completion before diagnostics, at this time I wasn't sure what to do with the completion diagnostics. Now that I have implemented diagnostics I will be able to use the one provided by code completion. This will require some changes in the way the requests are made to the irony-server, this is something I'm thinking about but I haven't get around yet.

@silgon
Copy link

silgon commented Oct 27, 2015

Little question regarding this question. How can I activate C++11? besides using the irony-additional-clang-options.

@Sarcasm
Copy link
Owner

Sarcasm commented Oct 27, 2015

It depends, what do you want, just to have -std=c++11 if no compilation database is setup, or do you want to overlay the compile options by adding -std=c++11 automatically?

@silgon
Copy link

silgon commented Oct 27, 2015

I would like overlay the compilation options. But it would be nice to know both anyway ;)

@Sarcasm
Copy link
Owner

Sarcasm commented Oct 27, 2015

For overlay, irony-additional-clang-options is the way to go.
For fallback options, I provide it in irony-mode but user can already do it in their config, see discussion here: Sarcasm/irony-mode#245 (comment)

@silgon
Copy link

silgon commented Oct 27, 2015

Thanks for the fast answer. Look, I'm actually put an example with one function std::stod which is supposed to be working with c++11 according to the stod documentation. I set, for example with M-: irony-additional-clang-options "-std=c++11", I reload the options just in case with irony-cdb-autosetup-compile-options and this is what I get:
turu
Still the same error. One (maybe foolish and naive) thing I did was to add set(CMAKE_CXX_COMPILER_ARG1 -std=c++11) to the CMakeLists.txt of irony-mode, and then recompile it, it didn't work either.

@Sarcasm
Copy link
Owner

Sarcasm commented Oct 27, 2015

Try: M-: (setq irony-additional-clang-options '("-std=c++11"))

@silgon
Copy link

silgon commented Oct 27, 2015

In the end it was apparently working but I did have some problem. Anyway, so, if I want to load it automatically for all my c files I add (setq irony-additional-clang-options '("-std=c++11")) to my .emacs file and that should be it, correct?

@Sarcasm
Copy link
Owner

Sarcasm commented Oct 27, 2015

Yes it should be.

@silgon
Copy link

silgon commented Oct 27, 2015

thanks again ;)

@deniskokarev
Copy link

The problem is with Xcode-specific includes. Even Apple-own libclang.dylib doesn't know of them.
I could compile irony-server with standard osx libclang.dylib by grabbing headers from llvm 3.8, yet it did not work properly until the following includes were added into .clang_complete file.

Peek at what Apple compiler uses

echo | clang -x c++ -v -E - 2>&1 | sed -n '/^#include </,/^End/s|^[^/]*\([^ ]*/include[^ ]*\).*$|-I\1|p'

Add them to your .clang_complete:

$ cat .clang_complete
-I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
-I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.2/include
-I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
-I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants