dense-analysis / ale

Check syntax in Vim/Neovim asynchronously and fix files, with Language Server Protocol (LSP) support
BSD 2-Clause "Simplified" License
13.49k stars 1.43k forks source link

clang-tidy treats .h header files as c headers #1608

Closed ZhiruiLi closed 4 years ago

ZhiruiLi commented 6 years ago

clang-tidy not working properly for c++ header files

VIM version

NVIM v0.2.2 Build type: Release

Operating System: macOS 10.13.4

:ALEInfo

 Current Filetype: cpp
Available Linters: ['clang', 'clangcheck', 'clangtidy', 'cppcheck', 'cpplint', 'flawfinder', 'gcc']
   Linter Aliases:
'gcc' -> ['g++']
  Enabled Linters: ['clangtidy']
 Linter Variables:

let g:ale_cpp_clangtidy_checks = ['*']
let g:ale_cpp_clangtidy_executable = 'clang-tidy'
let g:ale_cpp_clangtidy_options = ''
......

What went wrong

When current file is a C++ header file with extension ‘.h', ALE can recognize it is a C++ header file. But the clang-tidy seems to treat it as a C header file. As it find error like "'string' file not found" and "unknown type name 'namespace'".

If I rename the header file with extension '.hpp', then the clang-tidy can work properly. Do anyone has ideas to solve this problem?

Reproducing the bug

  1. Install clang-tidy
  2. Open C++ header file with extension '.h'
w0rp commented 6 years ago

This sounds like an issue with clang-tidy, but maybe there's an option ALE can pass to make clang-tidy understand that .h can be treated as C++ headers. There's a variable for controlling if headers are C or C++ files in Vim, which ALE could use.

yeshengm commented 6 years ago

Seems that we can use clangtidy's -x option to handle this.

let g:ale_cpp_clangtidy_options = '-x c++'
yeshengm commented 5 years ago

Also, it seems that CMake's compile_commands.json can not be used for C++ header files.

ghost commented 5 years ago

I did a PR for this recently, but I have closed for the following reason.

cpp is the default filetype for .h files in Vim. This means when you have a .h header file open under a C project (not C++) then ALE will think this file is -x c++ which is clearly wrong.

Instead I think the fix should introduce a new variable that switches on this behaviour for c++ header files.

I suggest: g:cpp_clangtidy_h_is_hpp

Type: Number Default: 0

what do you think?

peter-moran commented 4 years ago

I am also having an issue where I have C-compliant .h files that I do not want checked as .cpp files. Are there any workarounds at this time?

ghost commented 4 years ago

Hi Peter,

The issue I was describing above was not merged into master, so I'm not sure what issues you are experiencing. I think its best if you open a new issue so we can understand your situation better.

Thanks, Rich.

w0rp commented 4 years ago

Almost all C/C++ issues with flags should now be discussed in #3276, so a comprehensive solution can be found for most people, for automatically detecting flags.

w0rp commented 4 years ago

Now ALE will tell clang-tidy that .h files are C++ files if the filetype is set to C++ in Vim.

Aneurin commented 3 years ago

This change actually broke things for me because without -- -x c++ clang-tidy will happily use my compile_commands.json, whereas when you pass compiler options, they appears to replace compile_commands.json entirely.

It would be good if there were an option controlling this, so it can be disabled. Or even better, if it's only enabled when you have no compile_commands.json - but that would mean having to check which may not be reasonable.

(For the moment I've just deleted those lines from clangtidy.vim locally.)

aaron-michaux commented 2 years ago

Seems that we can use clangtidy's -x option to handle this.

let g:ale_cpp_clangtidy_options = '-x c++'

Which file do you add this to?