Skip to content

Commit 439fc85

Browse files
committed
Switch the default mode for clang-format to '-file'. Make 'LLVM' the
fallback syntax used when we fail to find a '.clang-format' file. Adjust variable names appropriately. Update the editor integration pieces that specify a '-style' option to specify it as '-style=file'. I left the functionality in place because even if the preferred method is to use '.clang-format' files, this way if someone needs to clobber the style in their editor we show how to do so in these examples. Also check in a '.clang-format' file for Clang to ensure that separate checkouts and builds of Clang from LLVM can still get the nice formatting. =] This unfortunately required nuking the test for the absence of a '.clang-format' file as now the directory happening to be under your clang source tree will cause there to always be a file. ;] git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@189741 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 0180688 commit 439fc85

File tree

6 files changed

+19
-19
lines changed

6 files changed

+19
-19
lines changed

.clang-format

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BasedOnStyle: LLVM

test/Format/style-on-command-line.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
// RUN: clang-format -style="{BasedOnStyle: invalid, IndentWidth: 7}" %t.cpp 2>&1 | FileCheck -strict-whitespace -check-prefix=CHECK3 %s
55
// RUN: clang-format -style="{lsjd}" %t.cpp 2>&1 | FileCheck -strict-whitespace -check-prefix=CHECK4 %s
66
// RUN: [ ! -e %T/.clang-format ] || rm %T/.clang-format
7-
// RUN: clang-format -style=file %t.cpp 2>&1 | FileCheck -strict-whitespace -check-prefix=CHECK5 %s
87
// RUN: printf "BasedOnStyle: google\nIndentWidth: 5\n" > %T/.clang-format
9-
// RUN: clang-format -style=file %t.cpp 2>&1 | FileCheck -strict-whitespace -check-prefix=CHECK6 %s
8+
// RUN: clang-format -style=file %t.cpp 2>&1 | FileCheck -strict-whitespace -check-prefix=CHECK5 %s
109
void f() {
1110
// CHECK1: {{^ int\* i;$}}
1211
// CHECK2: {{^ int \*i;$}}
@@ -15,9 +14,7 @@ void f() {
1514
// CHECK3: {{^ int \*i;$}}
1615
// CHECK4: Error parsing -style: Invalid argument, using LLVM style
1716
// CHECK4: {{^ int \*i;$}}
18-
// CHECK5: Can't find usable .clang-format, using LLVM style
19-
// CHECK5: {{^ int \*i;$}}
20-
// CHECK6: {{^ int\* i;$}}
17+
// CHECK5: {{^ int\* i;$}}
2118
int*i;
2219
int j;
2320
}

tools/clang-format/ClangFormat.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727

2828
using namespace llvm;
2929

30-
// Default style to use when no style specified or specified style not found.
31-
static const char *DefaultStyle = "LLVM";
30+
// Fallback style when no style specified or found in a .clang-format file.
31+
static const char FallbackStyle[] = "LLVM";
3232

3333
static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
3434

@@ -72,7 +72,7 @@ static cl::opt<std::string>
7272
"Use -style=\"{key: value, ...}\" to set specific\n"
7373
"parameters, e.g.:\n"
7474
" -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\""),
75-
cl::init(DefaultStyle), cl::cat(ClangFormatCategory));
75+
cl::init("file"), cl::cat(ClangFormatCategory));
7676
static cl::opt<bool> Inplace("i",
7777
cl::desc("Inplace edit <file>s, if specified."),
7878
cl::cat(ClangFormatCategory));
@@ -108,20 +108,20 @@ static FileID createInMemoryFile(StringRef FileName, const MemoryBuffer *Source,
108108

109109
FormatStyle getStyle(StringRef StyleName, StringRef FileName) {
110110
FormatStyle Style;
111-
getPredefinedStyle(DefaultStyle, &Style);
111+
getPredefinedStyle(FallbackStyle, &Style);
112112

113113
if (StyleName.startswith("{")) {
114114
// Parse YAML/JSON style from the command line.
115115
if (error_code ec = parseConfiguration(StyleName, &Style)) {
116116
llvm::errs() << "Error parsing -style: " << ec.message()
117-
<< ", using " << DefaultStyle << " style\n";
117+
<< ", using " << FallbackStyle << " style\n";
118118
}
119119
return Style;
120120
}
121121

122122
if (!StyleName.equals_lower("file")) {
123123
if (!getPredefinedStyle(StyleName, &Style))
124-
llvm::errs() << "Invalid value for -style, using " << DefaultStyle
124+
llvm::errs() << "Invalid value for -style, using " << FallbackStyle
125125
<< " style\n";
126126
return Style;
127127
}
@@ -153,7 +153,7 @@ FormatStyle getStyle(StringRef StyleName, StringRef FileName) {
153153
return Style;
154154
}
155155
}
156-
llvm::errs() << "Can't find usable .clang-format, using " << DefaultStyle
156+
llvm::errs() << "Can't find usable .clang-format, using " << FallbackStyle
157157
<< " style\n";
158158
return Style;
159159
}

tools/clang-format/clang-format-sublime.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
# Change this to the full path if clang-format is not on the path.
2020
binary = 'clang-format'
2121

22-
# Change this to format according to other formatting styles
23-
# (see clang-format -help).
24-
style = 'LLVM'
22+
# Change this to format according to other formatting styles. See the output of
23+
# 'clang-format --help' for a list of supported styles. The default looks for
24+
# a '.clang-format' file to indicate the style that should be used.
25+
style = 'file'
2526

2627
class ClangFormatCommand(sublime_plugin.TextCommand):
2728
def run(self, edit):

tools/clang-format/clang-format.el

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
(let* ((orig-windows (get-buffer-window-list (current-buffer)))
3737
(orig-window-starts (mapcar #'window-start orig-windows))
3838
(orig-point (point))
39-
(style "LLVM"))
39+
(style "file"))
4040
(unwind-protect
4141
(call-process-region (point-min) (point-max) clang-format-binary t t nil
4242
"-offset" (number-to-string (1- begin))

tools/clang-format/clang-format.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
# Change this to the full path if clang-format is not on the path.
2727
binary = 'clang-format'
2828

29-
# Change this to format according to other formatting styles (see
30-
# clang-format -help)
31-
style = 'LLVM'
29+
# Change this to format according to other formatting styles. See the output of
30+
# 'clang-format --help' for a list of supported styles. The default looks for
31+
# a '.clang-format' file to indicate the style that should be used.
32+
style = 'file'
3233

3334
# Get the current text.
3435
buf = vim.current.buffer

0 commit comments

Comments
 (0)