]> granicus.if.org Git - clang/commitdiff
clang-format sort include use the source file name to determine the
authorDaniel Jasper <djasper@google.com>
Wed, 10 Feb 2016 12:42:58 +0000 (12:42 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 10 Feb 2016 12:42:58 +0000 (12:42 +0000)
"main include" that will be the 1st include (category 0).

Because the clang-format visual studio extension does not pass the file
name and use the standard input, sort include cannot find a "main
include":

Testing fix on llvm\tools\clang\lib\Format\Format.cpp:
Original file:
  #include "clang/Format/Format.h"
  ...
  #include "clang/Basic/SourceManager.h"
  #include "clang/Lex/Lexer.h"

Without fix, selecting the includes and running visual studio
clang-format:
  ...
  #include "clang/Basic/SourceManager.h"
  #include "clang/Format/Format.h"
  #include "clang/Lex/Lexer.h"

With fix, selecting the includes and running visual studio clang-format:
  #include "clang/Format/Format.h"
  ...
  #include "clang/Basic/SourceManager.h"
  #include "clang/Lex/Lexer.h"

Test 2 with main header not at the start:
Original file:
  ...
  #include "clang/Format/Format.h"
  #include "clang/Basic/SourceManager.h"
  #include "clang/Lex/Lexer.h"

Without fix, selecting the includes and running visual studio
clang-format:
  ...
  #include "clang/Basic/SourceManager.h"
  #include "clang/Format/Format.h"
  #include "clang/Lex/Lexer.h"

With fix, selecting the includes and running visual studio clang-format:
  #include "clang/Format/Format.h"
  ...
  #include "clang/Basic/SourceManager.h"
  #include "clang/Lex/Lexer.h"

Patch by Jean-Philippe Dufraigne, thank you.
Review: http://reviews.llvm.org/D16524

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@260378 91177308-0d34-0410-b5e6-96231b3b80d8

tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs

index df872b2e2198235fd507047e2c5afbac45cf426a..6af2fd177f0fec361b1b6c2e4883e7f50f7167df 100644 (file)
@@ -202,9 +202,10 @@ namespace LLVM.ClangFormat
             if (start >= text.Length && text.Length > 0)\r
                 start = text.Length - 1;\r
             string path = GetDocumentParent(view);\r
+            string filePath = GetDocumentPath(view);\r
             try\r
             {\r
-                var root = XElement.Parse(RunClangFormat(text, start, length, path));\r
+                var root = XElement.Parse(RunClangFormat(text, start, length, path, filePath));\r
                 var edit = view.TextBuffer.CreateEdit();\r
                 foreach (XElement replacement in root.Descendants("replacement"))\r
                 {\r
@@ -237,7 +238,7 @@ namespace LLVM.ClangFormat
         /// \r
         /// Formats the text range starting at offset of the given length.\r
         /// </summary>\r
-        private string RunClangFormat(string text, int offset, int length, string path)\r
+        private string RunClangFormat(string text, int offset, int length, string path, string filePath)\r
         {\r
             string vsixPath = Path.GetDirectoryName(\r
                 typeof(ClangFormatPackage).Assembly.Location);\r
@@ -257,6 +258,8 @@ namespace LLVM.ClangFormat
             if (GetSortIncludes())\r
               process.StartInfo.Arguments += " -sort-includes ";\r
             string assumeFilename = GetAssumeFilename();\r
+            if (string.IsNullOrEmpty(assumeFilename))\r
+                assumeFilename = filePath;\r
             if (!string.IsNullOrEmpty(assumeFilename))\r
               process.StartInfo.Arguments += " -assume-filename \"" + assumeFilename + "\"";\r
             process.StartInfo.CreateNoWindow = true;\r
@@ -355,5 +358,15 @@ namespace LLVM.ClangFormat
             }\r
             return null;\r
         }\r
+\r
+        private string GetDocumentPath(IWpfTextView view)\r
+        {\r
+            ITextDocument document;\r
+            if (view.TextBuffer.Properties.TryGetProperty(typeof(ITextDocument), out document))\r
+            {\r
+                return document.FilePath;\r
+            }\r
+            return null;\r
+        }\r
     }\r
 }\r