From c49ec17d01fd5448fdf59a13b83e3543ddd0a96f Mon Sep 17 00:00:00 2001 From: Artem Sokolovskii Date: Mon, 3 Jun 2024 15:55:29 +0200 Subject: [PATCH] ClangFormat: Allow unknown options in .clang-format file This commit disables syntax checks for the .clang-format file, enabling its use even if it contains errors. Removed pop-up shown when save .clang-format file with errors but left warning when editing it in Code Style Tab. Fixes: QTCREATORBUG-30087 Change-Id: I37a0b1e9d602fcbe4fbbc27f7ab190bcd5c1a1fd Reviewed-by: Christian Kandeler --- .../clangformat/clangformatbaseindenter.cpp | 8 ++----- .../clangformat/clangformatconfigwidget.cpp | 24 ++----------------- .../clangformatglobalconfigwidget.cpp | 4 ++-- src/plugins/clangformat/clangformatutils.cpp | 16 +++++++------ src/plugins/clangformat/clangformatutils.h | 3 ++- 5 files changed, 17 insertions(+), 38 deletions(-) diff --git a/src/plugins/clangformat/clangformatbaseindenter.cpp b/src/plugins/clangformat/clangformatbaseindenter.cpp index 295c86fcb6f..ee89619225c 100644 --- a/src/plugins/clangformat/clangformatbaseindenter.cpp +++ b/src/plugins/clangformat/clangformatbaseindenter.cpp @@ -961,12 +961,8 @@ const clang::format::FormatStyle &ClangFormatBaseIndenterPrivate::styleForFile() return m_cachedStyle.style; } - llvm::Expected styleFromProjectFolder - = clang::format::getStyle("file", - m_fileName->toFSPathString().toStdString(), - "none", - "", - &llvmFileSystemAdapter); + llvm::Expected styleFromProjectFolder = clang::format::getStyle( + "file", m_fileName->toFSPathString().toStdString(), "none", "", &llvmFileSystemAdapter, true); if (styleFromProjectFolder && !(*styleFromProjectFolder == clang::format::getNoStyle())) { addQtcStatementMacros(*styleFromProjectFolder); diff --git a/src/plugins/clangformat/clangformatconfigwidget.cpp b/src/plugins/clangformat/clangformatconfigwidget.cpp index ec1afdcfc1f..418769cbf12 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.cpp +++ b/src/plugins/clangformat/clangformatconfigwidget.cpp @@ -326,28 +326,8 @@ void ClangFormatConfigWidget::apply() if (!m_editorWidget->isEnabled()) return; - clang::format::FormatStyle currentSettingsStyle; - const Utils::expected_str success - = parseConfigurationContent(m_editor->document()->contents().toStdString(), - currentSettingsStyle); - - auto saveSettings = [this] { - QString errorString; - m_editor->document()->save(&errorString, m_config->filePath()); - }; - - if (success) { - saveSettings(); - return; - } - - QMessageBox mBox; - mBox.setText(Tr::tr("The current ClangFormat (C++ > Code Style > ClangFormat) settings are not " - "valid. Are you sure you want to apply them?")); - mBox.setStandardButtons(QMessageBox::No | QMessageBox::Yes); - mBox.setDefaultButton(QMessageBox::No); - if (mBox.exec() == QMessageBox::Yes) - saveSettings(); + QString errorString; + m_editor->document()->save(&errorString, m_config->filePath()); } TextEditor::CodeStyleEditorWidget *createClangFormatConfigWidget( diff --git a/src/plugins/clangformat/clangformatglobalconfigwidget.cpp b/src/plugins/clangformat/clangformatglobalconfigwidget.cpp index 27629823426..fdcceea62b4 100644 --- a/src/plugins/clangformat/clangformatglobalconfigwidget.cpp +++ b/src/plugins/clangformat/clangformatglobalconfigwidget.cpp @@ -256,8 +256,8 @@ void ClangFormatGlobalConfigWidget::initCurrentProjectLabel() bool ClangFormatGlobalConfigWidget::projectClangFormatFileExists() { - llvm::Expected styleFromProjectFolder - = clang::format::getStyle("file", m_project->projectFilePath().path().toStdString(), "none"); + llvm::Expected styleFromProjectFolder = clang::format::getStyle( + "file", m_project->projectFilePath().path().toStdString(), "none", "", nullptr, true); return styleFromProjectFolder && !(*styleFromProjectFolder == clang::format::getNoStyle()); } diff --git a/src/plugins/clangformat/clangformatutils.cpp b/src/plugins/clangformat/clangformatutils.cpp index 6732524f41e..7beb062a45e 100644 --- a/src/plugins/clangformat/clangformatutils.cpp +++ b/src/plugins/clangformat/clangformatutils.cpp @@ -394,7 +394,8 @@ Utils::FilePath filePathToCurrentSettings(const TextEditor::ICodeStylePreference static QString s_errorMessage; Utils::expected_str parseConfigurationContent(const std::string &fileContent, - clang::format::FormatStyle &style) + clang::format::FormatStyle &style, + bool allowUnknownOptions) { auto diagHandler = [](const llvm::SMDiagnostic &diag, void * /*context*/) { s_errorMessage = QString::fromStdString(diag.getMessage().str()) + " " @@ -403,11 +404,12 @@ Utils::expected_str parseConfigurationContent(const std::string &fileConte }; style.Language = clang::format::FormatStyle::LK_Cpp; - const std::error_code error = parseConfiguration(llvm::MemoryBufferRef(fileContent, "YAML"), - &style, - false, - diagHandler, - nullptr); + const std::error_code error = parseConfiguration( + llvm::MemoryBufferRef(fileContent, "YAML"), + &style, + allowUnknownOptions, + diagHandler, + nullptr); if (error) return make_unexpected(s_errorMessage); @@ -418,7 +420,7 @@ Utils::expected_str parseConfigurationFile(const Utils::FilePath &filePath clang::format::FormatStyle &style) { return parseConfigurationContent(filePath.fileContents().value_or(QByteArray()).toStdString(), - style); + style, true); } } // namespace ClangFormat diff --git a/src/plugins/clangformat/clangformatutils.h b/src/plugins/clangformat/clangformatutils.h index 019414b11f5..bea4cdb8cb8 100644 --- a/src/plugins/clangformat/clangformatutils.h +++ b/src/plugins/clangformat/clangformatutils.h @@ -49,7 +49,8 @@ clang::format::FormatStyle currentQtStyle(const TextEditor::ICodeStylePreference Utils::FilePath filePathToCurrentSettings(const TextEditor::ICodeStylePreferences *codeStyle); Utils::expected_str parseConfigurationContent(const std::string &fileContent, - clang::format::FormatStyle &style); + clang::format::FormatStyle &style, + bool allowUnknownOptions = false); Utils::expected_str parseConfigurationFile(const Utils::FilePath &filePath, clang::format::FormatStyle &style);