diff options
author | Eric Liu <ioeric@google.com> | 2018-06-25 16:29:19 +0000 |
---|---|---|
committer | Eric Liu <ioeric@google.com> | 2018-06-25 16:29:19 +0000 |
commit | b4adc9110da2b22f15288cc79c245d1b4b32c12b (patch) | |
tree | 43c27326bc4f4f7a6b38fa832a3ff207f9d4eda2 | |
parent | e4788178f5628d2e1117ba7bdf425aa000164ca6 (diff) | |
download | bcm5719-llvm-b4adc9110da2b22f15288cc79c245d1b4b32c12b.tar.gz bcm5719-llvm-b4adc9110da2b22f15288cc79c245d1b4b32c12b.zip |
[clang-format] Add a default format style that can be used by users of `getStyle`
Summary:
Tools that reformat code often call `getStyle` to decide the format style
to use on a certain source file. In practice, "file" style is widely used. As a
result, many tools hardcode "file" when calling `getStyle`, which makes it hard
to control the default style in tools across a codebase when needed. This change
introduces a `DefaultFormatStyle` constant (default to "file" in upstream), which
can be modified downstream if wanted, so that all users/tools built from the same
source tree can have a consistent default format style.
This also adds an DefaultFallbackStyle that is recommended to be used by tools and can be modified downstream.
Reviewers: sammccall, djasper
Reviewed By: sammccall
Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D48492
llvm-svn: 335492
-rw-r--r-- | clang/include/clang/Format/Format.h | 9 | ||||
-rw-r--r-- | clang/lib/Format/Format.cpp | 4 | ||||
-rw-r--r-- | clang/tools/clang-format/ClangFormat.cpp | 21 |
3 files changed, 24 insertions, 10 deletions
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 1d135fe1bd2..36616e556a9 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -1963,6 +1963,15 @@ LangOptions getFormattingLangOpts(const FormatStyle &Style = getLLVMStyle()); /// of ``getStyle()``. extern const char *StyleOptionHelpDescription; +/// The suggested format style to use by default. This allows tools using +/// `getStyle` to have a consistent default style. +/// Different builds can modify the value to the preferred styles. +extern const char *DefaultFormatStyle; + +/// The suggested predefined style to use as the fallback style in `getStyle`. +/// Different builds can modify the value to the preferred styles. +extern const char *DefaultFallbackStyle; + /// Construct a FormatStyle based on ``StyleName``. /// /// ``StyleName`` can take several forms: diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 76dfc819507..374c25064a9 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -2144,6 +2144,10 @@ FormatStyle::LanguageKind guessLanguage(StringRef FileName, StringRef Code) { return GuessedLanguage; } +const char *DefaultFormatStyle = "file"; + +const char *DefaultFallbackStyle = "LLVM"; + llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName, StringRef FallbackStyleName, StringRef Code, vfs::FileSystem *FS) { diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp index fdd0c41d08e..de2f9d7fe76 100644 --- a/clang/tools/clang-format/ClangFormat.cpp +++ b/clang/tools/clang-format/ClangFormat.cpp @@ -60,17 +60,18 @@ LineRanges("lines", cl::desc("<start line>:<end line> - format a range of\n" "Can only be used with one input file."), cl::cat(ClangFormatCategory)); static cl::opt<std::string> - Style("style", - cl::desc(clang::format::StyleOptionHelpDescription), - cl::init("file"), cl::cat(ClangFormatCategory)); + Style("style", cl::desc(clang::format::StyleOptionHelpDescription), + cl::init(clang::format::DefaultFormatStyle), + cl::cat(ClangFormatCategory)); static cl::opt<std::string> -FallbackStyle("fallback-style", - cl::desc("The name of the predefined style used as a\n" - "fallback in case clang-format is invoked with\n" - "-style=file, but can not find the .clang-format\n" - "file to use.\n" - "Use -fallback-style=none to skip formatting."), - cl::init("LLVM"), cl::cat(ClangFormatCategory)); + FallbackStyle("fallback-style", + cl::desc("The name of the predefined style used as a\n" + "fallback in case clang-format is invoked with\n" + "-style=file, but can not find the .clang-format\n" + "file to use.\n" + "Use -fallback-style=none to skip formatting."), + cl::init(clang::format::DefaultFallbackStyle), + cl::cat(ClangFormatCategory)); static cl::opt<std::string> AssumeFileName("assume-filename", |