diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -2879,7 +2879,8 @@ private: FormatStyleSet StyleSet; - friend std::error_code parseConfiguration(StringRef Text, FormatStyle *Style, + friend std::error_code parseConfiguration(llvm::MemoryBufferRef Config, + FormatStyle *Style, bool AllowUnknownOptions); }; @@ -2938,9 +2939,17 @@ /// /// If AllowUnknownOptions is true, no errors are emitted if unknown /// format options are occured. -std::error_code parseConfiguration(StringRef Text, FormatStyle *Style, +std::error_code parseConfiguration(llvm::MemoryBufferRef Config, + FormatStyle *Style, bool AllowUnknownOptions = false); +/// Like above but accepts an unnamed buffer. +inline std::error_code parseConfiguration(StringRef Config, FormatStyle *Style, + bool AllowUnknownOptions = false) { + return parseConfiguration(llvm::MemoryBufferRef(Config, "YAML"), Style, + AllowUnknownOptions); +} + /// Gets configuration in a YAML string. std::string configurationAsText(const FormatStyle &Style); diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1327,16 +1327,17 @@ return true; } -std::error_code parseConfiguration(StringRef Text, FormatStyle *Style, +std::error_code parseConfiguration(llvm::MemoryBufferRef Config, + FormatStyle *Style, bool AllowUnknownOptions) { assert(Style); FormatStyle::LanguageKind Language = Style->Language; assert(Language != FormatStyle::LK_None); - if (Text.trim().empty()) + if (Config.getBuffer().trim().empty()) return make_error_code(ParseError::Error); Style->StyleSet.Clear(); std::vector Styles; - llvm::yaml::Input Input(Text); + llvm::yaml::Input Input(Config); // DocumentListTraits> uses the context to get default // values for the fields, keys for which are missing from the configuration. // Mapping also uses the context to get the language to find the correct @@ -2864,8 +2865,9 @@ if (StyleName.startswith("{")) { // Parse YAML/JSON style from the command line. - if (std::error_code ec = - parseConfiguration(StyleName, &Style, AllowUnknownOptions)) + if (std::error_code ec = parseConfiguration( + llvm::MemoryBufferRef(StyleName, ""), &Style, + AllowUnknownOptions)) return make_string_error("Error parsing -style: " + ec.message()); return Style; } @@ -2909,8 +2911,8 @@ FS->getBufferForFile(ConfigFile.str()); if (std::error_code EC = Text.getError()) return make_string_error(EC.message()); - if (std::error_code ec = parseConfiguration( - Text.get()->getBuffer(), &Style, AllowUnknownOptions)) { + if (std::error_code ec = + parseConfiguration(*Text.get(), &Style, AllowUnknownOptions)) { if (ec == ParseError::Unsuitable) { if (!UnsuitableConfigFiles.empty()) UnsuitableConfigFiles.append(", "); diff --git a/clang/test/Format/error-config.cpp b/clang/test/Format/error-config.cpp --- a/clang/test/Format/error-config.cpp +++ b/clang/test/Format/error-config.cpp @@ -1,10 +1,10 @@ // RUN: clang-format %s --Wno-error=unknown --style="{UnknownKey: true}" 2>&1 | FileCheck %s -check-prefix=CHECK // RUN: not clang-format %s --style="{UnknownKey: true}" 2>&1 | FileCheck %s -check-prefix=CHECK-FAIL -// CHECK: YAML:1:2: warning: unknown key 'UnknownKey' +// CHECK: :1:2: warning: unknown key 'UnknownKey' // CHECK-NEXT: {UnknownKey: true} // CHECK-NEXT: ^~~~~~~~~~ -// CHECK-FAIL: YAML:1:2: error: unknown key 'UnknownKey' +// CHECK-FAIL: :1:2: error: unknown key 'UnknownKey' // CHECK-FAIL-NEXT: {UnknownKey: true} // CHECK-FAIL-NEXT: ^~~~~~~~~~