https://github.com/pointhex created https://github.com/llvm/llvm-project/pull/91317
It allows to control of error output for the function. >From 74a0053509564a10faf335c32211cf3dddef4e98 Mon Sep 17 00:00:00 2001 From: Artem Sokolovskii <artem.sokolovs...@qt.io> Date: Tue, 7 May 2024 12:27:29 +0200 Subject: [PATCH] [ClangFormat] Add DiagHandler for getStyle function It allows to control of error output for the function. --- clang/include/clang/Format/Format.h | 3 ++- clang/lib/Format/Format.cpp | 19 +++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 74893f23210cd0..aa6a2a16fa8ecc 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -5385,7 +5385,8 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName, StringRef FallbackStyle, StringRef Code = "", llvm::vfs::FileSystem *FS = nullptr, - bool AllowUnknownOptions = false); + bool AllowUnknownOptions = false, + llvm::SourceMgr::DiagHandlerTy DiagHandler = nullptr); // Guesses the language from the ``FileName`` and ``Code`` to be formatted. // Defaults to FormatStyle::LK_Cpp. diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index c4eac1c99a663f..cf8a2a050a83c5 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -3947,12 +3947,13 @@ const char *DefaultFallbackStyle = "LLVM"; llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> loadAndParseConfigFile(StringRef ConfigFile, llvm::vfs::FileSystem *FS, - FormatStyle *Style, bool AllowUnknownOptions) { + FormatStyle *Style, bool AllowUnknownOptions, + llvm::SourceMgr::DiagHandlerTy DiagHandler = nullptr) { llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text = FS->getBufferForFile(ConfigFile.str()); if (auto EC = Text.getError()) return EC; - if (auto EC = parseConfiguration(*Text.get(), Style, AllowUnknownOptions)) + if (auto EC = parseConfiguration(*Text.get(), Style, AllowUnknownOptions, DiagHandler)) return EC; return Text; } @@ -3960,7 +3961,8 @@ loadAndParseConfigFile(StringRef ConfigFile, llvm::vfs::FileSystem *FS, llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName, StringRef FallbackStyleName, StringRef Code, llvm::vfs::FileSystem *FS, - bool AllowUnknownOptions) { + bool AllowUnknownOptions, + llvm::SourceMgr::DiagHandlerTy DiagHandler) { FormatStyle Style = getLLVMStyle(guessLanguage(FileName, Code)); FormatStyle FallbackStyle = getNoStyle(); if (!getPredefinedStyle(FallbackStyleName, Style.Language, &FallbackStyle)) @@ -3974,7 +3976,7 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName, StringRef Source = "<command-line>"; if (std::error_code ec = parseConfiguration(llvm::MemoryBufferRef(StyleName, Source), &Style, - AllowUnknownOptions)) { + AllowUnknownOptions, DiagHandler)) { return make_string_error("Error parsing -style: " + ec.message()); } @@ -3994,7 +3996,7 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName, StyleName.starts_with_insensitive("file:")) { auto ConfigFile = StyleName.substr(5); llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text = - loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions); + loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions, DiagHandler); if (auto EC = Text.getError()) { return make_string_error("Error reading " + ConfigFile + ": " + EC.message()); @@ -4029,12 +4031,13 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName, // Reset possible inheritance Style.InheritsParentConfig = false; - auto dropDiagnosticHandler = [](const llvm::SMDiagnostic &, void *) {}; + auto diagHandlerOrDropHandling = + DiagHandler ? DiagHandler : [](llvm::SMDiagnostic const &, void *) {}; auto applyChildFormatTexts = [&](FormatStyle *Style) { for (const auto &MemBuf : llvm::reverse(ChildFormatTextToApply)) { auto EC = parseConfiguration(*MemBuf, Style, AllowUnknownOptions, - dropDiagnosticHandler); + diagHandlerOrDropHandling); // It was already correctly parsed. assert(!EC); static_cast<void>(EC); @@ -4068,7 +4071,7 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName, } llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text = - loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions); + loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions, DiagHandler); if (auto EC = Text.getError()) { if (EC != ParseError::Unsuitable) { return make_string_error("Error reading " + ConfigFile + ": " + _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits