Reputation: 3123
Does the ClangFormat BasedOnStyle
style option have a default value that's applied if one's not selected by the user, and if so, what is that default value?
According to the BasedOnStyle option section of the CLANG-FORMAT STYLE OPTIONS documentation for Clang 12, BasedOnStyle
is a configurable format style option that can be one of LLVM
, Google
, Chromium
, Mozilla
, WebKit
, Microsoft
, or GNU
. There's no mention in that section however of whether BasedOnStyle
even has a default value.
I searched online for documentation to answer this but didn't find any. Scanning through all of the clang-format
tagged questions on SO for uses of BasedOnStyle
also revealed no answers. Given that ClangFormat is from the LLVM project it'd be unsurprising that this option defaults to LLVM
but that assumes that this option has a default which seems unclear to begin with.
Upvotes: 8
Views: 12638
Reputation:
I was also curious to figure out how it works.
Here are 2 lines of code that, in my opinion, explain everything:
const char *DefaultFormatStyle = "file";
const char *DefaultFallbackStyle = "LLVM";
Source: https://github.com/llvm/llvm-project/blob/main/clang/lib/Format/Format.cpp#L2942-L2944
Those values are used in ClangFormat.cpp
file when Style
and FallbackStyle
variables are initialized.
Source: https://github.com/llvm/llvm-project/blob/main/clang/tools/clang-format/ClangFormat.cpp#L62-L66
Even though the documentation states that style configuration needs to be passed explicitly via the --style
option, the clang-format
works when invoked directly.
# File is formatted with default configuration
# or with options from the ".clang-format` file, if present.
clang-format -i <file>
Upvotes: 1
Reputation: 3123
Seeing as there didn't seem to be Q&A already on SO for this and guessing others have also wondered about this or will wonder about this, I thought I'd post the question, and the answer I eventually found (regrettably only after checking the source code)...
In the comments in the clang/include/clang/Format/Format.h
file, and also in the RawStringFormats
section of the clang/docs/ClangFormatStyleOptions.rst
file, is the following sentence:
If 'BasedOnStyle' is not found, the formatting is based on llvm style.
That led me back to the CLANG-FORMAT STYLE OPTIONS documentation for Clang 12 that I'd mentioned in my question, where I realized this information was all along. It just isn't in the section on BasedOnStyle
but confusingly is in the section on RawStringFormats
.
Additionally, testing on clang-format-configurator, seems to corroborate that the default BasedOnStyle
value is indeed LLVM
and running clang-format --dump-config | grep BasedOnStyle
shows:
# BasedOnStyle: LLVM
Upvotes: 5