rhysd/vim-clang-format

Failed to format cpp

372046933 opened this issue ยท 16 comments

.vimrc

let g:clang_format#style_options = {
            \ "AccessModifierOffset" : -4,
            \ "Standard" : "C++14"}

ClangFormat returns

Error detected while processing function clang_format#replace[6]..<SNR>39_error_message:                             
line    1:
clang-format has failed to format.

:12verbose ClangFormat returns

...
continuing in clang_format#format
calling <SNR>39_shellescape
<SNR>39_shellescape returning '''clang-format'''
continuing in clang_format#format
calling <SNR>39_system
<SNR>39_system returning 'YAML:1:91: error: unknown enumerated ...ror parsing -style: Invalid argument^@'
continuing in clang_format#format
clang_format#format returning 'YAML:1:91: error: unknown enumerated ...ror parsing -style: Invalid argument^@'
continuing in clang_format#replace
calling <SNR>39_success
calling <SNR>39_has_vimproc
<SNR>39_has_vimproc returning #0
continuing in <SNR>39_success
<SNR>39_success returning #0
continuing in clang_format#replace
calling <SNR>39_error_message

Seems no activity in this repo. I have reverted to clang-format.py solution. So far so good.
map <C-K> :py3file /usr/share/clang/clang-format.py<cr>

Seems to work for me, if you just switch to lower-case c++14 instead:

let g:clang_format#style_options = {
            \ "AccessModifierOffset" : -4,
            \ "Standard" : "c++14"}

(Looks like upper-case C was regarded as legacy after C++11: https://github.com/llvm-mirror/clang/blob/65acf43270ea2894dffa0d0b292b92402f80c8cb/lib/Format/Format.cpp#L77)

Can you confirm?

@Kypert Confirmed that only when g:clang_format#style_options is removed, ClangFormat will work. Lower case does not work for me either. Sad

Perhaps you can obtain more clear information by running: :ClangFormatEchoFormattedCode

@Kypert Cool. It's clear now. But I do not know how to fix. Could you throw some light upon this?

YAML:1:91: error: unknown enumerated scalar
{BasedOnStyle: google, IndentWidth: 4, UseTab: true, AccessModifierOffset: '-4',Standard: 'c++14'}
                                                                                          ^~~~~~~
Error parsing -style: Invalid argument

Thank you for the output. Could you please share the version of the clang-format that you are trying to invoke? Did you point to a certain version via g:clang_format#command?

Could it be that you are using a version of clang-format that does not include llvm/llvm-project@e503256?

You can also try to run the command manually with your style, to isolate the problem:

> echo test > testfile
> clang-format --style="{BasedOnStyle: google, IndentWidth: 4, UseTab: true, AccessModifierOffset: '-4',Standard: 'c++14'}" -- testfile
test

Tried your test case, but it failed to format.
I am using Arch Linux. clang-format is provided by package clang. llvm
is also installed.

โžœ  ~ cat testfile
test
โžœ  ~ clang-format --style="{BasedOnStyle: google, IndentWidth: 4, UseTab: true, AccessModifierOffset: '-4',Standard: 'c++14'}" -- testfile
YAML:1:91: error: unknown enumerated scalar
{BasedOnStyle: google, IndentWidth: 4, UseTab: true, AccessModifierOffset: '-4',Standard: 'c++14'}
                                                                                          ^~~~~~~
Error parsing -style: Invalid argument
โžœ  ~ 

To me it looks like the version of clang-format you use do not include the commit I mentioned. Are you using 9.0.1? If I am reading it correctly, you need 10.0.0-rc1 to get the c++14 attribute.

Even if I changed Standard to c++11, the same error occurred. But upper case C++11 works!

clang-format --style="{BasedOnStyle: google, IndentWidth: 4, UseTab: true, AccessModifierOffset: '-4',Standard: 'c++11'}" -- testfile
YAML:1:91: error: unknown enumerated scalar
{BasedOnStyle: google, IndentWidth: 4, UseTab: true, AccessModifierOffset: '-4',Standard: 'c++11'}
                                                                                          ^~~~~~~
Error parsing -style: Invalid argument

But upper case C++11 works!

Makes sense, since the lower-case versions were added in the commit I mentioned. I think we have a case-closed situations :) Please update your clang-format binary or adapt the config.

There is another problem that I didn't mention earlier. Invoking :ClangFormat from vim doesn't format the code correctly. While clang-format command line works. The incorrect format result is as follows. You can see that the indentation of line 107 is wrong. I didn't set g:clang_format#style_options in .vimrc

int main() {
 99     std::vector<float> nums(10);
100     std::copy(nums.begin(), nums.end(),
101           std::ostream_iterator<float>(cout, " "));
102     cout << '\n';
103     return 0;
104     Solution solution;
105     std::string digits;
106     while (std::getline(cin, digits)) {
107     cout << solution.digitsToChinese(digits) << endl;
108     }

Sorry, not sure how to tackle that one. Like you said, it is another problem indeed, maybe check :ClangFormatEchoFormattedCode? Can you see if other parts of the code get formatted, like if you introduce extra spaces on other lines?

Surprised. :ClangFormatEchoFormattedCode shows rightly formatted code. Quite confused now :P

Could it be that another plugin is interfering, would it be possible to scale down and see if the issue goes away at some point? Maybe you already solved it?

@Kypert , I just followed your suggestion to scale down. Found that set tabstop=4 and set shiftwidth=4 vim .vimrc is the root cause. Thanks a million.

Upgraded to clang 10.0.0-2, now Standard" : "c++17" is recognized.