diff options
author | Alexander Kornienko <alexfh@google.com> | 2013-05-19 00:53:30 +0000 |
---|---|---|
committer | Alexander Kornienko <alexfh@google.com> | 2013-05-19 00:53:30 +0000 |
commit | 006b5c89cec1ae5de19bcbef71c04c74754594fb (patch) | |
tree | 3330dbe770316ce2fe22e9666044acc88ef5707e /clang/tools/clang-format/ClangFormat.cpp | |
parent | 8cfbaa6988282d52f17b2ddb2944582804bbc247 (diff) | |
download | bcm5719-llvm-006b5c89cec1ae5de19bcbef71c04c74754594fb.tar.gz bcm5719-llvm-006b5c89cec1ae5de19bcbef71c04c74754594fb.zip |
Clang-format: allow -style="{yaml/json}" on command line
Summary: + improved handling of default style and predefined styles.
Reviewers: djasper, klimek
Reviewed By: klimek
CC: cfe-commits
Differential Revision: http://llvm-reviews.chandlerc.com/D813
llvm-svn: 182205
Diffstat (limited to 'clang/tools/clang-format/ClangFormat.cpp')
-rw-r--r-- | clang/tools/clang-format/ClangFormat.cpp | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp index f779348f0fc..fccd6bf2571 100644 --- a/clang/tools/clang-format/ClangFormat.cpp +++ b/clang/tools/clang-format/ClangFormat.cpp @@ -27,6 +27,9 @@ using namespace llvm; +// Default style to use when no style specified or specified style not found. +static const char *DefaultStyle = "LLVM"; + static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden); // Mark all our options with this category, everything else (except for -version @@ -54,11 +57,14 @@ static cl::opt<std::string> Style("style", cl::desc("Coding style, currently supports:\n" " LLVM, Google, Chromium, Mozilla.\n" - "Use '-style file' to load style configuration from\n" + "Use -style=file to load style configuration from\n" ".clang-format file located in one of the parent\n" "directories of the source file (or current\n" - "directory for stdin)."), - cl::init("LLVM"), cl::cat(ClangFormatCategory)); + "directory for stdin).\n" + "Use -style=\"{key: value, ...}\" to set specific\n" + "parameters, e.g.:\n" + " -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\""), + cl::init(DefaultStyle), cl::cat(ClangFormatCategory)); static cl::opt<bool> Inplace("i", cl::desc("Inplace edit <file>s, if specified."), cl::cat(ClangFormatCategory)); @@ -88,8 +94,24 @@ static FileID createInMemoryFile(StringRef FileName, const MemoryBuffer *Source, } FormatStyle getStyle(StringRef StyleName, StringRef FileName) { - if (!StyleName.equals_lower("file")) - return getPredefinedStyle(StyleName); + FormatStyle Style; + getPredefinedStyle(DefaultStyle, &Style); + + if (StyleName.startswith("{")) { + // Parse YAML/JSON style from the command line. + if (error_code ec = parseConfiguration(StyleName, &Style)) { + llvm::errs() << "Error parsing -style: " << ec.message() + << ", using " << DefaultStyle << " style\n"; + } + return Style; + } + + if (!StyleName.equals_lower("file")) { + if (!getPredefinedStyle(StyleName, &Style)) + llvm::errs() << "Invalid value for -style, using " << DefaultStyle + << " style\n"; + return Style; + } SmallString<128> Path(FileName); llvm::sys::fs::make_absolute(Path); @@ -109,7 +131,6 @@ FormatStyle getStyle(StringRef StyleName, StringRef FileName) { llvm::errs() << ec.message() << "\n"; continue; } - FormatStyle Style; if (error_code ec = parseConfiguration(Text->getBuffer(), &Style)) { llvm::errs() << "Error reading " << ConfigFile << ": " << ec.message() << "\n"; @@ -119,8 +140,9 @@ FormatStyle getStyle(StringRef StyleName, StringRef FileName) { return Style; } } - llvm::errs() << "Can't find usable .clang-format, using LLVM style\n"; - return getLLVMStyle(); + llvm::errs() << "Can't find usable .clang-format, using " << DefaultStyle + << " style\n"; + return Style; } // Returns true on error. |