r190413 - Allow _clang-format as alternative to .clang-format config filename

Hans Wennborg hans at hanshq.net
Tue Sep 10 08:41:12 PDT 2013


Author: hans
Date: Tue Sep 10 10:41:12 2013
New Revision: 190413

URL: http://llvm.org/viewvc/llvm-project?rev=190413&view=rev
Log:
Allow _clang-format as alternative to .clang-format config filename

Dotfiles are impractical on Windows. This makes clang-format search
for the style configuration file as '_clang-format' in addition to
the usual '.clang-format'. This is similar to how VIM searches for
'_vimrc' on Windows.

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

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

Modified: cfe/trunk/docs/ClangFormat.rst
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/docs/ClangFormat.rst?rev=190413&r1=190412&r2=190413&view=diff
==============================================================================
--- cfe/trunk/docs/ClangFormat.rst (original)
+++ cfe/trunk/docs/ClangFormat.rst Tue Sep 10 10:41:12 2013
@@ -72,8 +72,8 @@ to format C/C++/Obj-C code.
 
 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:
 

Modified: cfe/trunk/docs/ClangFormatStyleOptions.rst
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/docs/ClangFormatStyleOptions.rst?rev=190413&r1=190412&r2=190413&view=diff
==============================================================================
--- cfe/trunk/docs/ClangFormatStyleOptions.rst (original)
+++ cfe/trunk/docs/ClangFormatStyleOptions.rst Tue Sep 10 10:41:12 2013
@@ -16,8 +16,8 @@ Configuring Style with clang-format
 
 :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

Modified: cfe/trunk/test/Format/style-on-command-line.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Format/style-on-command-line.cpp?rev=190413&r1=190412&r2=190413&view=diff
==============================================================================
--- cfe/trunk/test/Format/style-on-command-line.cpp (original)
+++ cfe/trunk/test/Format/style-on-command-line.cpp Tue Sep 10 10:41:12 2013
@@ -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 @@ void f() {
 // CHECK5: {{^     int\* i;$}}
 // CHECK6: {{^Error reading .*\.clang-format: Invalid argument}}
 // CHECK6: {{^  int \*i;$}}
+// CHECK7: {{^      int\* i;$}}
 int*i;
 int j;
 }

Modified: cfe/trunk/tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs?rev=190413&r1=190412&r2=190413&view=diff
==============================================================================
--- cfe/trunk/tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs (original)
+++ cfe/trunk/tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs Tue Sep 10 10:41:12 2013
@@ -37,11 +37,12 @@ namespace LLVM.ClangFormat
         [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" +

Modified: cfe/trunk/tools/clang-format/ClangFormat.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/ClangFormat.cpp?rev=190413&r1=190412&r2=190413&view=diff
==============================================================================
--- cfe/trunk/tools/clang-format/ClangFormat.cpp (original)
+++ cfe/trunk/tools/clang-format/ClangFormat.cpp Tue Sep 10 10:41:12 2013
@@ -132,12 +132,22 @@ FormatStyle getStyle(StringRef StyleName
        !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)) {

Modified: cfe/trunk/tools/clang-format/clang-format-sublime.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/clang-format-sublime.py?rev=190413&r1=190412&r2=190413&view=diff
==============================================================================
--- cfe/trunk/tools/clang-format/clang-format-sublime.py (original)
+++ cfe/trunk/tools/clang-format/clang-format-sublime.py Tue Sep 10 10:41:12 2013
@@ -21,7 +21,8 @@ binary = 'clang-format'
 
 # 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):

Modified: cfe/trunk/tools/clang-format/clang-format.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/clang-format.py?rev=190413&r1=190412&r2=190413&view=diff
==============================================================================
--- cfe/trunk/tools/clang-format/clang-format.py (original)
+++ cfe/trunk/tools/clang-format/clang-format.py Tue Sep 10 10:41:12 2013
@@ -28,7 +28,8 @@ binary = 'clang-format'
 
 # 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.





More information about the cfe-commits mailing list