diff options
author | Antonio Maiorano <amaiorano@gmail.com> | 2017-01-17 00:12:27 +0000 |
---|---|---|
committer | Antonio Maiorano <amaiorano@gmail.com> | 2017-01-17 00:12:27 +0000 |
commit | 9163a050930210e6d63abaed418fd89e3c53b827 (patch) | |
tree | 99f9d471798403d09f512c4dbfdabdf1704b4294 /tools/clang-format | |
parent | 115251d60ba426e6891d88f5c68e15f96e2e644e (diff) | |
download | clang-9163a050930210e6d63abaed418fd89e3c53b827.tar.gz |
clang-format: Make GetStyle return Expected<FormatStyle> instead of FormatStyle
Change the contract of GetStyle so that it returns an error when an error occurs
(i.e. when it writes to stderr), and only returns the fallback style when it
can't find a configuration file.
Differential Revision: https://reviews.llvm.org/D28081
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@292174 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/clang-format')
-rw-r--r-- | tools/clang-format/ClangFormat.cpp | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/tools/clang-format/ClangFormat.cpp b/tools/clang-format/ClangFormat.cpp index 6c50daf538..b08d4fa4c1 100644 --- a/tools/clang-format/ClangFormat.cpp +++ b/tools/clang-format/ClangFormat.cpp @@ -249,12 +249,17 @@ static bool format(StringRef FileName) { if (fillRanges(Code.get(), Ranges)) return true; StringRef AssumedFileName = (FileName == "-") ? AssumeFileName : FileName; - FormatStyle FormatStyle = + + llvm::Expected<FormatStyle> FormatStyle = getStyle(Style, AssumedFileName, FallbackStyle, Code->getBuffer()); + if (!FormatStyle) { + llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n"; + return true; + } if (SortIncludes.getNumOccurrences() != 0) - FormatStyle.SortIncludes = SortIncludes; + FormatStyle->SortIncludes = SortIncludes; unsigned CursorPosition = Cursor; - Replacements Replaces = sortIncludes(FormatStyle, Code->getBuffer(), Ranges, + Replacements Replaces = sortIncludes(*FormatStyle, Code->getBuffer(), Ranges, AssumedFileName, &CursorPosition); auto ChangedCode = tooling::applyAllReplacements(Code->getBuffer(), Replaces); if (!ChangedCode) { @@ -264,7 +269,7 @@ static bool format(StringRef FileName) { // Get new affected ranges after sorting `#includes`. Ranges = tooling::calculateRangesAfterReplacements(Replaces, Ranges); bool IncompleteFormat = false; - Replacements FormatChanges = reformat(FormatStyle, *ChangedCode, Ranges, + Replacements FormatChanges = reformat(*FormatStyle, *ChangedCode, Ranges, AssumedFileName, &IncompleteFormat); Replaces = Replaces.merge(FormatChanges); if (OutputXML) { @@ -334,10 +339,15 @@ int main(int argc, const char **argv) { cl::PrintHelpMessage(); if (DumpConfig) { - std::string Config = - clang::format::configurationAsText(clang::format::getStyle( + llvm::Expected<clang::format::FormatStyle> FormatStyle = + clang::format::getStyle( Style, FileNames.empty() ? AssumeFileName : FileNames[0], - FallbackStyle)); + FallbackStyle); + if (!FormatStyle) { + llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n"; + return 1; + } + std::string Config = clang::format::configurationAsText(*FormatStyle); outs() << Config << "\n"; return 0; } |