r210739 - Give clang-format its own error category.

Rafael Espindola rafael.espindola at gmail.com
Wed Jun 11 19:50:04 PDT 2014


Author: rafael
Date: Wed Jun 11 21:50:04 2014
New Revision: 210739

URL: http://llvm.org/viewvc/llvm-project?rev=210739&view=rev
Log:
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.

Modified:
    cfe/trunk/include/clang/Format/Format.h
    cfe/trunk/lib/Format/Format.cpp
    cfe/trunk/test/Format/style-on-command-line.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/include/clang/Format/Format.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Format/Format.h?rev=210739&r1=210738&r2=210739&view=diff
==============================================================================
--- cfe/trunk/include/clang/Format/Format.h (original)
+++ cfe/trunk/include/clang/Format/Format.h Wed Jun 11 21:50:04 2014
@@ -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
 } // end namespace format
 } // end namespace clang
 
+namespace std {
+template <>
+struct is_error_code_enum<clang::format::ParseError> : std::true_type {};
+}
+
 #endif // LLVM_CLANG_FORMAT_FORMAT_H

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=210739&r1=210738&r2=210739&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Wed Jun 11 21:50:04 2014
@@ -258,6 +258,29 @@ template <> struct DocumentListTraits<st
 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<int>(e), getParestCategory());
+}
+
+const char *ParseErrorCategory::name() const LLVM_NOEXCEPT {
+  return "clang-format.parse_error";
+}
+
+std::string ParseErrorCategory::message(int EV) const {
+  switch (static_cast<ParseError>(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(Stri
   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<FormatStyle> Styles;
   llvm::yaml::Input Input(Text);
@@ -463,14 +486,14 @@ llvm::error_code parseConfiguration(Stri
   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(Stri
         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
         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);

Modified: cfe/trunk/test/Format/style-on-command-line.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Format/style-on-command-line.cpp?rev=210739&r1=210738&r2=210739&view=diff
==============================================================================
--- cfe/trunk/test/Format/style-on-command-line.cpp (original)
+++ cfe/trunk/test/Format/style-on-command-line.cpp Wed Jun 11 21:50:04 2014
@@ -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;$}}

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=210739&r1=210738&r2=210739&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Jun 11 21:50:04 2014
@@ -8181,10 +8181,9 @@ TEST_F(FormatTest, ParsesConfigurationWi
   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, ParsesConfigurationWi
               "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, ParsesConfigurationWi
   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);
 }





More information about the cfe-commits mailing list