[cfe-dev] Help solve bug 17216 for clang-format on Windows

Johan Engelen jbc.engelen at swissonline.ch
Fri Nov 1 14:12:13 PDT 2013


Hello all,
   I've been looking into why clang-format cannot write in-place for 
large files (> 16 kB) on Windows [1].
I've found the culprit, but need help actually fixing it; the file 
handling code is somewhat complex for a new-comer like me. :)

The difference between small and large files is the returned value by 
shouldUseMmap in /llvm/lib/Support/MemoryBuffer.cpp. For small files, 
shouldUseMmap returns false and the already created normal MemoryBuffer 
is kept and all is fine. But for larger files, shouldUseMmap returns 
true and the already created normal MemoryBuffer is destroyed and a new 
memory mapped one is created. This becomes the OwningPtr<MemoryBuffer> 
Code memorybuffer in ClangFormat.cpp.
Now when the time comes to write the reformatted text in-place to the 
input file, llvm::raw_fd_ostream FileStream cannot be opened if the 
MemoryBuffer is a memory mapped one. Apparently Windows does not allow 
that. For small files, no Mmap buffer was created and all is fine.

I do not know nearly enough about the file handling internals to propose 
a good fix.
If shouldUseMmap is modified to always return false, clang-format works 
fine on large files. But that would be a very course "fix". I wanted to 
try releasing the Mmap interface to the file, right before writing back 
to it. But I was not able to figure out how to do that.

Thanks for the help,
     Johan

[1]  http://llvm.org/bugs/show_bug.cgi?id=17216



More information about the cfe-dev mailing list