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 {
} // 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
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;
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);
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);
}
}
}
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) {
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);
// 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;$}}
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);
"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);
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);
}