From d0136707a97b379680210f29e5caa2092fe80668 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Thu, 12 Jun 2014 02:50:04 +0000 Subject: [PATCH] Give clang-format its own error category. The posix errno values are probably to the best thing to use for describing parse errors. This should also fix the mingw build. llvm-svn: 210739 --- clang/include/clang/Format/Format.h | 14 +++++++++ clang/lib/Format/Format.cpp | 35 ++++++++++++++++++---- clang/test/Format/style-on-command-line.cpp | 6 ++-- clang/unittests/Format/FormatTest.cpp | 46 +++++++++++++---------------- 4 files changed, 67 insertions(+), 34 deletions(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 244a9c6..7656ec2 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -27,6 +27,15 @@ class DiagnosticConsumer; namespace format { +enum class ParseError { Success = 0, Error, Unsuitable }; +class ParseErrorCategory final : public std::error_category { +public: + const char *name() const LLVM_NOEXCEPT override; + std::string message(int EV) const override; +}; +const std::error_category &getParestCategory(); +std::error_code make_error_code(ParseError e); + /// \brief The \c FormatStyle is used to configure the formatting to follow /// specific guidelines. struct FormatStyle { @@ -506,4 +515,9 @@ FormatStyle getStyle(StringRef StyleName, StringRef FileName, } // end namespace format } // end namespace clang +namespace std { +template <> +struct is_error_code_enum : std::true_type {}; +} + #endif // LLVM_CLANG_FORMAT_FORMAT_H diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index e3378d14..d8633d8 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -258,6 +258,29 @@ template <> struct DocumentListTraits > { namespace clang { namespace format { +const std::error_category &getParestCategory() { + static ParseErrorCategory C; + return C; +} +std::error_code make_error_code(ParseError e) { + return std::error_code(static_cast(e), getParestCategory()); +} + +const char *ParseErrorCategory::name() const LLVM_NOEXCEPT { + return "clang-format.parse_error"; +} + +std::string ParseErrorCategory::message(int EV) const { + switch (static_cast(EV)) { + case ParseError::Success: + return "Success"; + case ParseError::Error: + return "Invalid argument"; + case ParseError::Unsuitable: + return "Unsuitable"; + } +} + FormatStyle getLLVMStyle() { FormatStyle LLVMStyle; LLVMStyle.Language = FormatStyle::LK_Cpp; @@ -447,7 +470,7 @@ llvm::error_code parseConfiguration(StringRef Text, FormatStyle *Style) { FormatStyle::LanguageKind Language = Style->Language; assert(Language != FormatStyle::LK_None); if (Text.trim().empty()) - return llvm::make_error_code(std::errc::invalid_argument); + return make_error_code(ParseError::Error); std::vector Styles; llvm::yaml::Input Input(Text); @@ -463,14 +486,14 @@ llvm::error_code parseConfiguration(StringRef Text, FormatStyle *Style) { for (unsigned i = 0; i < Styles.size(); ++i) { // Ensures that only the first configuration can skip the Language option. if (Styles[i].Language == FormatStyle::LK_None && i != 0) - return llvm::make_error_code(std::errc::invalid_argument); + return make_error_code(ParseError::Error); // Ensure that each language is configured at most once. for (unsigned j = 0; j < i; ++j) { if (Styles[i].Language == Styles[j].Language) { DEBUG(llvm::dbgs() << "Duplicate languages in the config file on positions " << j << " and " << i << "\n"); - return llvm::make_error_code(std::errc::invalid_argument); + return make_error_code(ParseError::Error); } } } @@ -482,10 +505,10 @@ llvm::error_code parseConfiguration(StringRef Text, FormatStyle *Style) { Styles[i].Language == FormatStyle::LK_None) { *Style = Styles[i]; Style->Language = Language; - return llvm::error_code(); + return make_error_code(ParseError::Success); } } - return llvm::make_error_code(std::errc::not_supported); + return make_error_code(ParseError::Unsuitable); } std::string configurationAsText(const FormatStyle &Style) { @@ -2049,7 +2072,7 @@ FormatStyle getStyle(StringRef StyleName, StringRef FileName, break; } if (llvm::error_code ec = parseConfiguration(Text->getBuffer(), &Style)) { - if (ec == std::errc::not_supported) { + if (ec == ParseError::Unsuitable) { if (!UnsuitableConfigFiles.empty()) UnsuitableConfigFiles.append(", "); UnsuitableConfigFiles.append(ConfigFile); diff --git a/clang/test/Format/style-on-command-line.cpp b/clang/test/Format/style-on-command-line.cpp index a0a39cb..19add89 100644 --- a/clang/test/Format/style-on-command-line.cpp +++ b/clang/test/Format/style-on-command-line.cpp @@ -16,12 +16,12 @@ void f() { // CHECK1: {{^ int\* i;$}} // CHECK2: {{^ int \*i;$}} // CHECK3: Unknown value for BasedOnStyle: invalid -// CHECK3: Error parsing -style: {{I|i}}nvalid argument, using LLVM style +// CHECK3: Error parsing -style: Invalid argument, using LLVM style // CHECK3: {{^ int \*i;$}} -// CHECK4: Error parsing -style: {{I|i}}nvalid argument, using LLVM style +// CHECK4: Error parsing -style: Invalid argument, using LLVM style // CHECK4: {{^ int \*i;$}} // CHECK5: {{^ int\* i;$}} -// CHECK6: {{^Error reading .*\.clang-format: (I|i)nvalid argument}} +// CHECK6: {{^Error reading .*\.clang-format: Invalid argument}} // CHECK6: {{^Can't find usable .clang-format, using webkit style$}} // CHECK6: {{^ int\* i;$}} // CHECK7: {{^ int\* i;$}} diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index f8bf85b..d7aeb30 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -8181,10 +8181,9 @@ TEST_F(FormatTest, ParsesConfigurationWithLanguages) { CHECK_PARSE("Language: Cpp\n" "IndentWidth: 12", IndentWidth, 12u); - EXPECT_EQ(std::errc::not_supported, - parseConfiguration("Language: JavaScript\n" - "IndentWidth: 34", - &Style)); + EXPECT_EQ(ParseError::Unsuitable, parseConfiguration("Language: JavaScript\n" + "IndentWidth: 34", + &Style)); EXPECT_EQ(12u, Style.IndentWidth); CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); @@ -8194,9 +8193,9 @@ TEST_F(FormatTest, ParsesConfigurationWithLanguages) { "IndentWidth: 12", IndentWidth, 12u); CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); - EXPECT_EQ(std::errc::not_supported, parseConfiguration("Language: Cpp\n" - "IndentWidth: 34", - &Style)); + EXPECT_EQ(ParseError::Unsuitable, parseConfiguration("Language: Cpp\n" + "IndentWidth: 34", + &Style)); EXPECT_EQ(23u, Style.IndentWidth); CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); @@ -8253,24 +8252,21 @@ TEST_F(FormatTest, ParsesConfigurationWithLanguages) { EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); EXPECT_EQ(789u, Style.TabWidth); - - EXPECT_EQ(std::errc::invalid_argument, - parseConfiguration("---\n" - "Language: JavaScript\n" - "IndentWidth: 56\n" - "---\n" - "IndentWidth: 78\n" - "...\n", - &Style)); - EXPECT_EQ(std::errc::invalid_argument, - parseConfiguration("---\n" - "Language: JavaScript\n" - "IndentWidth: 56\n" - "---\n" - "Language: JavaScript\n" - "IndentWidth: 78\n" - "...\n", - &Style)); + EXPECT_EQ(ParseError::Error, parseConfiguration("---\n" + "Language: JavaScript\n" + "IndentWidth: 56\n" + "---\n" + "IndentWidth: 78\n" + "...\n", + &Style)); + EXPECT_EQ(ParseError::Error, parseConfiguration("---\n" + "Language: JavaScript\n" + "IndentWidth: 56\n" + "---\n" + "Language: JavaScript\n" + "IndentWidth: 78\n" + "...\n", + &Style)); EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); } -- 2.7.4