diff --git a/src/plugins/clangformat/clangformatbaseindenter.cpp b/src/plugins/clangformat/clangformatbaseindenter.cpp index d413b3d49af..c1f137f10c4 100644 --- a/src/plugins/clangformat/clangformatbaseindenter.cpp +++ b/src/plugins/clangformat/clangformatbaseindenter.cpp @@ -6,6 +6,7 @@ #include "llvmfilesystem.h" #include +#include #include #include @@ -944,6 +945,37 @@ const clang::format::FormatStyle &ClangFormatBaseIndenter::styleForFile() const return d->styleForFile(); } +const llvm::Expected getStyleFromProjectFolder( + const Utils::FilePath *fileName) +{ +#if LLVM_VERSION_MAJOR >= 19 + static QString s_cachedError; + llvm::SourceMgr::DiagHandlerTy diagHandler = [](const llvm::SMDiagnostic &diag, void *) { + QString errorMessage = QString::fromStdString(diag.getMessage().str()) + " " + + QString::number(diag.getLineNo()) + ":" + + QString::number(diag.getColumnNo()); + + if (s_cachedError == errorMessage) + return; + + s_cachedError = errorMessage; + Core::MessageManager::writeSilently("ClangFormat file error: " + errorMessage); + }; + + return clang::format::getStyle( + "file", + fileName->toFSPathString().toStdString(), + "none", + "", + &llvmFileSystemAdapter, + true, + diagHandler); +#else + return clang::format::getStyle( + "file", fileName->toFSPathString().toStdString(), "none", "", &llvmFileSystemAdapter, true); +#endif +} + const clang::format::FormatStyle &ClangFormatBaseIndenterPrivate::styleForFile() const { static const milliseconds cacheTimeout = getCacheTimeout(); @@ -962,8 +994,8 @@ const clang::format::FormatStyle &ClangFormatBaseIndenterPrivate::styleForFile() return m_cachedStyle.style; } - llvm::Expected styleFromProjectFolder = clang::format::getStyle( - "file", m_fileName->toFSPathString().toStdString(), "none", "", &llvmFileSystemAdapter, true); + llvm::Expected styleFromProjectFolder = getStyleFromProjectFolder( + m_fileName); if (styleFromProjectFolder && !(*styleFromProjectFolder == clang::format::getNoStyle())) { addQtcStatementMacros(*styleFromProjectFolder);