[PATCH] clang-format: allow _clang-format as an alternative config file name

Hans Wennborg hans at chromium.org
Mon Sep 9 11:42:50 PDT 2013


Hi djasper,

As pointed out in this cfe-users thread [1], the '.clang-format' filename is impractical on Windows. This patch makes clang-format allow the alternative filename '_clang_format', which is more practical. This is similar to how VIM allows '_vimrc' on Windows.

[1]. http://lists.cs.uiuc.edu/pipermail/cfe-users/2013-September/000208.html

http://llvm-reviews.chandlerc.com/D1629

Files:
  docs/ClangFormat.rst
  docs/ClangFormatStyleOptions.rst
  test/Format/style-on-command-line.cpp
  tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
  tools/clang-format/ClangFormat.cpp
  tools/clang-format/clang-format-sublime.py
  tools/clang-format/clang-format.py

Index: docs/ClangFormat.rst
===================================================================
--- docs/ClangFormat.rst
+++ docs/ClangFormat.rst
@@ -72,8 +72,8 @@
 
 When the desired code formatting style is different from the available options,
 the style can be customized using the ``-style="{key: value, ...}"`` option or
-by putting your style configuration to the ``.clang-format`` file in your
-project's directory and using ``clang-format -style=file``.
+by putting your style configuration in the ``.clang-format`` or ``_clang-format``
+file in your project's directory and using ``clang-format -style=file``.
 
 An easy way to create the ``.clang-format`` file is:
 
Index: docs/ClangFormatStyleOptions.rst
===================================================================
--- docs/ClangFormatStyleOptions.rst
+++ docs/ClangFormatStyleOptions.rst
@@ -16,8 +16,8 @@
 
 :program:`clang-format` supports two ways to provide custom style options:
 directly specify style configuration in the ``-style=`` command line option or
-use ``-style=file`` and put style configuration in the ``.clang-format`` file
-in the project directory.
+use ``-style=file`` and put style configuration in the ``.clang-format`` or
+``_clang-format`` file in the project directory.
 
 When using ``-style=file``, :program:`clang-format` for each input file will
 try to find the ``.clang-format`` file located in the closest parent directory
Index: test/Format/style-on-command-line.cpp
===================================================================
--- test/Format/style-on-command-line.cpp
+++ test/Format/style-on-command-line.cpp
@@ -8,6 +8,10 @@
 // RUN: clang-format -style=file %t.cpp 2>&1 | FileCheck -strict-whitespace -check-prefix=CHECK5 %s
 // RUN: printf "\n" > %T/.clang-format
 // RUN: clang-format -style=file %t.cpp 2>&1 | FileCheck -strict-whitespace -check-prefix=CHECK6 %s
+// RUN: [ ! -e %T/.clang-format ] || rm %T/.clang-format
+// RUN: [ ! -e %T/_clang-format ] || rm %T/_clang-format
+// RUN: printf "BasedOnStyle: google\nIndentWidth: 6\n" > %T/_clang-format
+// RUN: clang-format -style=file %t.cpp 2>&1 | FileCheck -strict-whitespace -check-prefix=CHECK7 %s
 void f() {
 // CHECK1: {{^        int\* i;$}}
 // CHECK2: {{^       int \*i;$}}
@@ -19,6 +23,7 @@
 // CHECK5: {{^     int\* i;$}}
 // CHECK6: {{^Error reading .*\.clang-format: Invalid argument}}
 // CHECK6: {{^  int \*i;$}}
+// CHECK7: {{^      int\* i;$}}
 int*i;
 int j;
 }
Index: tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
===================================================================
--- tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
+++ tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
@@ -37,11 +37,12 @@
         [DisplayName("Style")]
         [Description("Coding style, currently supports:\n" +
                      "  - Predefined styles ('LLVM', 'Google', 'Chromium', 'Mozilla').\n" +
-                     "  - 'File' to search for a YAML .clang-format configuration.\n" +
+                     "  - 'File' to search for a YAML .clang-format or _clang-format\n" +
+                     "    configuration file.\n" +
                      "  - A YAML configuration snippet.\n\n" +
                      "'File':\n" +
-                     "  Searches for a .clang-format configuration in the source file's\n" +
-                     "  directory and its parents.\n\n" +
+                     "  Searches for a .clang-format or _clang_format configuration file\n" +
+                     "  in the source file's directory and its parents.\n\n" +
                      "YAML configuration snippet:\n" +
                      "  The content of a .clang-format configuration file, as string.\n" +
                      "  Example: '{BasedOnStyle: \"LLVM\", IndentWidth: 8}'\n\n" +
Index: tools/clang-format/ClangFormat.cpp
===================================================================
--- tools/clang-format/ClangFormat.cpp
+++ tools/clang-format/ClangFormat.cpp
@@ -132,12 +132,22 @@
        !Directory.empty();
        Directory = llvm::sys::path::parent_path(Directory)) {
     SmallString<128> ConfigFile(Directory);
+
     llvm::sys::path::append(ConfigFile, ".clang-format");
     DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
     bool IsFile = false;
     // Ignore errors from is_regular_file: we only need to know if we can read
     // the file or not.
     llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);
+
+    if (!IsFile) {
+      // Try _clang-format too, since dotfiles are not commonly used on Windows.
+      ConfigFile = Directory;
+      llvm::sys::path::append(ConfigFile, "_clang-format");
+      DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
+      llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);
+    }
+
     if (IsFile) {
       OwningPtr<MemoryBuffer> Text;
       if (error_code ec = MemoryBuffer::getFile(ConfigFile, Text)) {
Index: tools/clang-format/clang-format-sublime.py
===================================================================
--- tools/clang-format/clang-format-sublime.py
+++ tools/clang-format/clang-format-sublime.py
@@ -21,7 +21,8 @@
 
 # Change this to format according to other formatting styles. See the output of
 # 'clang-format --help' for a list of supported styles. The default looks for
-# a '.clang-format' file to indicate the style that should be used.
+# a '.clang-format' or '_clang-format' file to indicate the style that should be
+# used.
 style = 'file'
 
 class ClangFormatCommand(sublime_plugin.TextCommand):
Index: tools/clang-format/clang-format.py
===================================================================
--- tools/clang-format/clang-format.py
+++ tools/clang-format/clang-format.py
@@ -28,7 +28,8 @@
 
 # Change this to format according to other formatting styles. See the output of
 # 'clang-format --help' for a list of supported styles. The default looks for
-# a '.clang-format' file to indicate the style that should be used.
+# a '.clang-format' or '_clang-format' file to indicate the style that should be
+# used.
 style = 'file'
 
 # Get the current text.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1629.1.patch
Type: text/x-patch
Size: 6146 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130909/78ede29f/attachment.bin>


More information about the cfe-commits mailing list