Sarcasm / compdb

The compilation database Swiss army knife
MIT License
292 stars 23 forks source link

Error: expected identifier or '(' #7

Closed phcerdan closed 6 years ago

phcerdan commented 6 years ago

Using clangd (LSP) with the compile_commands.json generated by compdb after:

compdb list > headerdb.json
cp headerdb.json ../compile_commands.json

I am hitting Error: expected identifier or '(', I am guessing... is that because there is no main to compile the header? Any workaround for this?

The .cpp files from the original compile_commands.json generated by cmake are working ok with clangd.

A simplification of my header to reproduce the error.

#ifndef FOO_H
#define FOO_H

template < class T >
void foo(const T & input);

#endif

Also note, maybe relevant that foo.h is included in a test.cpp using catch framework.

Sarcasm commented 6 years ago

What happens if you the compile command to compile the file?

$ compdb list foo.h
<the compile command to copy>
$ clang++ <options...> foo.h
phcerdan commented 6 years ago

I had to clang++ "<options....>" foo.h because parsing error for bash with a compiler declaration with parenthesis -DvtkAutoInit=3(a,b,c). Beyond that unrelated problem, I just got a warning with clang++

clang-5.0: warning: treating 'c-header' input as 'c++-header' when in C++ mode, this behavior is deprecated [-Wdeprecated]

And zero warnings with c++.

I have noticed that the only differences between test and foo entries in compile_commands.json are: the file key, and in command, foo.h lacks: -o CMakeFiles/test.dir/test.cpp.o -c /path/src/test.cpp

Also, if I add a std header to foo.h I receive file not found from the LSP. #include <iostream>

phcerdan commented 6 years ago

By the way, pinging this link here about proposing to cmake to generate info for headers: https://gitlab.kitware.com/cmake/cmake/issues/16285

Sarcasm commented 6 years ago

Can you edit the command key to add -x c++-header, and see if that helps? Maybe libclang think it is in C-mode and not C++, so it does not find C++-only headers like iostream.

What mode is your Emacs when the file ends with .h? By default it is c-mode, not c++-mode. You can try to turn M-x c++-mode RET manually and see if that changes anything.

phcerdan commented 6 years ago

I will try soon. I am on neovim though, with the plugin LanguageClient-neovim calling clangd, I will check if the filetype of the buffer has any effect here, but I doubt it.

phcerdan commented 6 years ago

No difference with -x c++-header

Sarcasm commented 6 years ago

I believe the issue is with your plugin/clangd, if clang works from the command line. If you look at the compile command, do you see anything wrong?

phcerdan commented 6 years ago

You are right, still receiving the warning from clang++ from the header, but clangd/LSP plugin is not doing it right. Closing, but I shall return in the future!