<div dir="ltr">On Fri, Sep 13, 2013 at 6:40 AM, Daniel Jasper <span dir="ltr"><<a href="mailto:djasper@google.com" target="_blank">djasper@google.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Author: djasper<br>
Date: Fri Sep 13 08:40:24 2013<br>
New Revision: 190691<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=190691&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=190691&view=rev</a><br>
Log:<br>
clang-format: Add -assume-filename option for editor integrations.<br>
<br>
With -style=file, clang-format now starts to search for a .clang-format<br>
file starting at the file given with -assume-filename if it reads from<br>
stdin. Otherwise, it would start searching from the current directory,<br>
which is not helpful for editor integrations.<br>
<br>
Also changed vim, emacs and sublime integrations to actually make use of<br>
this flag.<br>
<br>
This fixes <a href="http://llvm.org/PR17072" target="_blank">llvm.org/PR17072</a>.<br>
<br>
Modified:<br>
cfe/trunk/tools/clang-format/ClangFormat.cpp<br>
cfe/trunk/tools/clang-format/clang-format-sublime.py<br>
cfe/trunk/tools/clang-format/clang-format.el<br>
cfe/trunk/tools/clang-format/clang-format.py<br>
<br>
Modified: cfe/trunk/tools/clang-format/ClangFormat.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/ClangFormat.cpp?rev=190691&r1=190690&r2=190691&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/ClangFormat.cpp?rev=190691&r1=190690&r2=190691&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/tools/clang-format/ClangFormat.cpp (original)<br>
+++ cfe/trunk/tools/clang-format/ClangFormat.cpp Fri Sep 13 08:40:24 2013<br>
@@ -73,6 +73,14 @@ static cl::opt<std::string><br>
"parameters, e.g.:\n"<br>
" -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\""),<br>
cl::init("file"), cl::cat(ClangFormatCategory));<br>
+<br>
+static cl::opt<std::string><br>
+AssumeFilename("assume-filename",<br>
+ cl::desc("When reading from stdin, clang-format assumes this\n"<br>
+ "filename to look for a style config file (with\n"<br>
+ "-style=file)."),<br>
+ cl::cat(ClangFormatCategory));<br>
+<br>
static cl::opt<bool> Inplace("i",<br>
cl::desc("Inplace edit <file>s, if specified."),<br>
cl::cat(ClangFormatCategory));<br>
@@ -126,11 +134,15 @@ FormatStyle getStyle(StringRef StyleName<br>
return Style;<br>
}<br>
<br>
+ if (FileName == "-")<br>
+ FileName = AssumeFilename;<br>
SmallString<128> Path(FileName);<br>
llvm::sys::fs::make_absolute(Path);<br>
- for (StringRef Directory = llvm::sys::path::parent_path(Path);<br>
+ for (StringRef Directory = Path;<br>
!Directory.empty();<br>
Directory = llvm::sys::path::parent_path(Directory)) {<br>
+ if (!llvm::sys::fs::is_directory(Directory))<br>
+ continue;<br>
SmallString<128> ConfigFile(Directory);<br>
<br>
llvm::sys::path::append(ConfigFile, ".clang-format");<br>
<br>
Modified: cfe/trunk/tools/clang-format/clang-format-sublime.py<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/clang-format-sublime.py?rev=190691&r1=190690&r2=190691&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/clang-format-sublime.py?rev=190691&r1=190690&r2=190691&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/tools/clang-format/clang-format-sublime.py (original)<br>
+++ cfe/trunk/tools/clang-format/clang-format-sublime.py Fri Sep 13 08:40:24 2013<br>
@@ -37,21 +37,21 @@ class ClangFormatCommand(sublime_plugin.<br>
region_offset = min(region.a, region.b)<br>
region_length = abs(region.b - region.a)<br>
command.extend(['-offset', str(region_offset),<br>
- '-length', str(region_length)])<br>
+ '-length', str(region_length),<br>
+ '-assume-filename', str(self.view.file_name())])<br>
old_viewport_position = self.view.viewport_position()<br>
buf = self.view.substr(sublime.Region(0, self.view.size()))<br>
p = subprocess.Popen(command, stdout=subprocess.PIPE,<br>
stderr=subprocess.PIPE, stdin=subprocess.PIPE)<br>
output, error = p.communicate(buf.encode(encoding))<br>
- if not error:<br>
- self.view.replace(<br>
- edit, sublime.Region(0, self.view.size()),<br>
- output.decode(encoding))<br>
- self.view.sel().clear()<br>
- for region in regions:<br>
- self.view.sel().add(region)<br>
- # FIXME: Without the 10ms delay, the viewport sometimes jumps.<br>
- sublime.set_timeout(lambda: self.view.set_viewport_position(<br>
- old_viewport_position, False), 10)<br>
- else:<br>
+ if error:<br>
print error<br>
+ self.view.replace(<br>
+ edit, sublime.Region(0, self.view.size()),<br>
+ output.decode(encoding))<br>
+ self.view.sel().clear()<br>
+ for region in regions:<br>
+ self.view.sel().add(region)<br>
+ # FIXME: Without the 10ms delay, the viewport sometimes jumps.<br>
+ sublime.set_timeout(lambda: self.view.set_viewport_position(<br>
+ old_viewport_position, False), 10)<br>
<br>
Modified: cfe/trunk/tools/clang-format/clang-format.el<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/clang-format.el?rev=190691&r1=190690&r2=190691&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/clang-format.el?rev=190691&r1=190690&r2=190691&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/tools/clang-format/clang-format.el (original)<br>
+++ cfe/trunk/tools/clang-format/clang-format.el Fri Sep 13 08:40:24 2013<br>
@@ -38,10 +38,12 @@<br>
(orig-point (point))<br>
(style "file"))<br>
(unwind-protect<br>
- (call-process-region (point-min) (point-max) clang-format-binary t t nil<br>
+ (call-process-region (point-min) (point-max) clang-format-binary<br>
+ t (list t nil) nil<br>
"-offset" (number-to-string (1- begin))<br>
"-length" (number-to-string (- end begin))<br>
"-cursor" (number-to-string (1- (point)))<br>
+ "-assume-filename" (buffer-file-name)<br>
"-style" style)<br>
(goto-char (point-min))<br>
(let ((json-output (json-read-from-string<br>
<br>
Modified: cfe/trunk/tools/clang-format/clang-format.py<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/clang-format.py?rev=190691&r1=190690&r2=190691&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/clang-format.py?rev=190691&r1=190690&r2=190691&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/tools/clang-format/clang-format.py (original)<br>
+++ cfe/trunk/tools/clang-format/clang-format.py Fri Sep 13 08:40:24 2013<br>
@@ -49,7 +49,8 @@ if sys.platform.startswith('win32'):<br>
<br>
# Call formatter.<br>
p = subprocess.Popen([binary, '-lines', lines, '-style', style,<br>
- '-cursor', str(cursor)],<br>
+ '-cursor', str(cursor),<br>
+ '-assume-filename', <a href="http://vim.current.buffer.name" target="_blank">vim.current.buffer.name</a>],<br></blockquote><div><br></div><div>This broke formatting unnamed buffers in vim. I sometimes open a new tab, paste in some code, and format that to see what clang-format does. This used to work, now I get something about "execv() arg 2 must contain only strings", probably due to <a href="http://vim.current.buffer.name">vim.current.buffer.name</a> being None. This patch fixes it for me, is it ok to land this?</div>
<div><br></div><div><div>Index: tools/clang-format/clang-format.py</div><div>===================================================================</div><div>--- tools/clang-format/clang-format.py<span class="" style="white-space:pre"> </span>(revision 193427)</div>
<div>+++ tools/clang-format/clang-format.py<span class="" style="white-space:pre"> </span>(working copy)</div><div>@@ -48,9 +48,10 @@</div><div> startupinfo.wShowWindow = subprocess.SW_HIDE</div><div> </div><div> # Call formatter.</div>
<div>-p = subprocess.Popen([binary, '-lines', lines, '-style', style,</div><div>- '-cursor', str(cursor),</div><div>- '-assume-filename', <a href="http://vim.current.buffer.name">vim.current.buffer.name</a>],</div>
<div>+command = [binary, '-lines', lines, '-style', style, '-cursor', str(cursor)]</div><div>+if <a href="http://vim.current.buffer.name">vim.current.buffer.name</a>:</div><div>+ command.append(['-assume-filename', <a href="http://vim.current.buffer.name">vim.current.buffer.name</a>])</div>
<div>+p = subprocess.Popen(command,</div><div> stdout=subprocess.PIPE, stderr=subprocess.PIPE,</div><div> stdin=subprocess.PIPE, startupinfo=startupinfo)</div><div> stdout, stderr = p.communicate(input=text)</div>
</div><div><br></div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
stdout=subprocess.PIPE, stderr=subprocess.PIPE,<br>
stdin=subprocess.PIPE, startupinfo=startupinfo)<br>
stdout, stderr = p.communicate(input=text)<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br></div></div>