vim-clang-format icon indicating copy to clipboard operation
vim-clang-format copied to clipboard

Error when formatting empty file

Open bgrabow opened this issue 8 years ago • 6 comments
trafficstars

I am getting an error when executing ClangFormat in Vim on an empty file.

Error detected while processing function clang_format#replace..clang_format#format..<SNR>30_system: 
line    8: 
E677: Error writing temp file

I get this error when the target buffer has exactly one empty line, and when the buffer has exactly two empty lines. I do not get the error when the buffer has three or more empty lines, nor when the buffer has at least one non-empty line.

Here's an example file that causes the error:

foo.hpp



This file does not cause the error:

bar.hpp




Also, when I run ClangFormat on a file with multiple blank lines, each format will remove one of those lines. So 4 blank lines goes to 3 blank lines, 3 blank lines goes to 2 blank lines, 2 blank lines results in an error.

clang-format on the target file from the shell works correctly, and preserves the correct number of blank lines.

$ clang-format --version
LLVM (http://llvm.org/):
  LLVM version 3.4.2
  Optimized build.
  Built Dec  7 2015 (09:37:36).
  Default target: x86_64-redhat-linux-gnu
  Host CPU: corei7-avx


$ clang-format -dump-config
---
AccessModifierOffset: -2
ConstructorInitializerIndentWidth: 4
AlignEscapedNewlinesLeft: false
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: false
AlwaysBreakTemplateDeclarations: false
AlwaysBreakBeforeMultilineStrings: false
BreakBeforeBinaryOperators: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BinPackParameters: true
ColumnLimit:     0
ConstructorInitializerAllOnOneLineOrOnePerLine: false
DerivePointerBinding: false
ExperimentalAutoDetectBinPacking: false
IndentCaseLabels: false
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 60
PenaltyBreakString: 1000
PenaltyBreakFirstLessLess: 120
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerBindsToType: true
SpacesBeforeTrailingComments: 1
Cpp11BracedListStyle: false
Standard:        Cpp03
IndentWidth:     4
TabWidth:        8
UseTab:          Never
BreakBeforeBraces: Linux
IndentFunctionDeclarationAfterType: false
SpacesInParentheses: false
SpacesInAngles:  false
SpaceInEmptyParentheses: false
SpacesInCStyleCastParentheses: false
SpaceAfterControlStatementKeyword: true
SpaceBeforeAssignmentOperators: true
ContinuationIndentWidth: 4
...


$ vim --version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Dec 21 2016 17:00:20)
Included patches: 1-160
Modified by <[email protected]>
Compiled by <[email protected]>
Huge version without GUI.  Features included (+) or not (-):
+acl             +farsi           +mouse_netterm   +syntax
+arabic          +file_in_path    +mouse_sgr       +tag_binary
+autocmd         +find_in_path    -mouse_sysmouse  +tag_old_static
-balloon_eval    +float           +mouse_urxvt     -tag_any_white
-browse          +folding         +mouse_xterm     -tcl
++builtin_terms  -footer          +multi_byte      +terminfo
+byte_offset     +fork()          +multi_lang      +termresponse
+cindent         +gettext         -mzscheme        +textobjects
-clientserver    -hangul_input    +netbeans_intg   +title
-clipboard       +iconv           +path_extra      -toolbar
+cmdline_compl   +insert_expand   +perl            +user_commands
+cmdline_hist    +jumplist        +persistent_undo +vertsplit
+cmdline_info    +keymap          +postscript      +virtualedit
+comments        +langmap         +printer         +visual
+conceal         +libcall         +profile         +visualextra
+cryptv          +linebreak       +python/dyn      +viminfo
+cscope          +lispindent      -python3         +vreplace
+cursorbind      +listcmds        +quickfix        +wildignore
+cursorshape     +localmap        +reltime         +wildmenu
+dialog_con      -lua             +rightleft       +windows
+diff            +menu            +ruby/dyn        +writebackup
+digraphs        +mksession       +scrollbind      -X11
-dnd             +modify_fname    +signs           -xfontset
-ebcdic          +mouse           +smartindent     -xim
+emacs_tags      -mouseshape      -sniff           -xsmp
+eval            +mouse_dec       +startuptime     -xterm_clipboard
+ex_extra        +mouse_gpm       +statusline      -xterm_save
+extra_search    -mouse_jsbterm   -sun_workshop    -xpm
   system vimrc file: "/etc/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
  fall-back for $VIM: "/etc"
 f-b for $VIMRUNTIME: "/usr/share/vim/vim74"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H     -O2 -g -pipe -Wall -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1      
Linking: gcc   -L. -Wl,-z,relro -fstack-protector -rdynamic -Wl,-export-dynamic -Wl,--enable-new-dtags -Wl,-rpath,/usr/lib64/perl5/CORE  -Wl,-z,relro  -L/usr/local/lib -Wl,--as-needed -o vim        -lm -lnsl  -lselinux  -lncurses -lacl -lattr -lgpm -ldl   -Wl,--enable-new-dtags -Wl,-rpath,/usr/lib64/perl5/CORE  -fstack-protector  -L/usr/lib64/perl5/CORE -lperl -lresolv -lnsl -ldl -lm -lcrypt -lutil -lpthread -lc    

bgrabow avatar Apr 07 '17 16:04 bgrabow

This is the cause of the error.

If I change it to this I fix the case for 2 blank lines, but 1 blank line still causes an error.

                \ && a:result !~# '^$'

Elsewhere, if I add a trailing newline to the text on its way to the clang-format system call, I fix the case for 1 blank line (and fix the problem where blank lines were being erroneously deleted).

Old:

    return s:system(clang_format, join(getline(1, '$'), "\n"))

New:

    return s:system(clang_format, join(getline(1, '$'), "\n") . "\n")

Everything seems to be working fine for me with these changes in place.

bgrabow avatar Apr 07 '17 20:04 bgrabow

@bgrabow

I could reproduce and addressed this issue. Could you confirm the latest?

rhysd avatar Apr 09 '17 03:04 rhysd

Your changes fix the case for 2 or more empty lines, but do not fix the case for 1 empty line. For that case I get an error here.

Also, with 2 or more empty lines the script is still incorrectly removing one EOL each time the script is run. You should add this change:

Old:

    return s:system(clang_format, join(getline(1, '$'), "\n"))

New:

    return s:system(clang_format, join(getline(1, '$'), "\n") . "\n")

The join() call only puts the delimiter between the array elements, but the clang-format system call expects newline characters at the end of each line. We're missing the trailing newline character.

bgrabow avatar Apr 10 '17 14:04 bgrabow

I also recommend adding this test case. I think it will fail with your current code:

    it 'can format a buffer containing a single empty line'
        call setline('.', [''])
        ClangFormat
    end

bgrabow avatar Apr 10 '17 14:04 bgrabow

I couldn't reproduce the issue. It works with a buffer containing two newlines only. What version clang-format are you using? My clang-format is 4.0.0.

rhysd avatar Apr 13 '17 10:04 rhysd

The issue is with a buffer containing exactly 1 newline. I agree it works now for a buffer with 2 newlines.

For example, try opening a new file and immediately running ClangFormat. It will fail with an error. This is especially an issue when using ClangFormatAutoEnable since I cannot write an empty file to disk due to the error.

bgrabow avatar Apr 13 '17 18:04 bgrabow