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 <christian.kandeler@qt.io>
This commit is contained in:
Artem Sokolovskii
2024-06-03 15:55:29 +02:00
parent 09fdd9ad9e
commit c49ec17d01
5 changed files with 17 additions and 38 deletions

View File

@@ -961,12 +961,8 @@ const clang::format::FormatStyle &ClangFormatBaseIndenterPrivate::styleForFile()
return m_cachedStyle.style; return m_cachedStyle.style;
} }
llvm::Expected<clang::format::FormatStyle> styleFromProjectFolder llvm::Expected<clang::format::FormatStyle> styleFromProjectFolder = clang::format::getStyle(
= clang::format::getStyle("file", "file", m_fileName->toFSPathString().toStdString(), "none", "", &llvmFileSystemAdapter, true);
m_fileName->toFSPathString().toStdString(),
"none",
"",
&llvmFileSystemAdapter);
if (styleFromProjectFolder && !(*styleFromProjectFolder == clang::format::getNoStyle())) { if (styleFromProjectFolder && !(*styleFromProjectFolder == clang::format::getNoStyle())) {
addQtcStatementMacros(*styleFromProjectFolder); addQtcStatementMacros(*styleFromProjectFolder);

View File

@@ -326,28 +326,8 @@ void ClangFormatConfigWidget::apply()
if (!m_editorWidget->isEnabled()) if (!m_editorWidget->isEnabled())
return; return;
clang::format::FormatStyle currentSettingsStyle; QString errorString;
const Utils::expected_str<void> success m_editor->document()->save(&errorString, m_config->filePath());
= 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();
} }
TextEditor::CodeStyleEditorWidget *createClangFormatConfigWidget( TextEditor::CodeStyleEditorWidget *createClangFormatConfigWidget(

View File

@@ -256,8 +256,8 @@ void ClangFormatGlobalConfigWidget::initCurrentProjectLabel()
bool ClangFormatGlobalConfigWidget::projectClangFormatFileExists() bool ClangFormatGlobalConfigWidget::projectClangFormatFileExists()
{ {
llvm::Expected<clang::format::FormatStyle> styleFromProjectFolder llvm::Expected<clang::format::FormatStyle> styleFromProjectFolder = clang::format::getStyle(
= clang::format::getStyle("file", m_project->projectFilePath().path().toStdString(), "none"); "file", m_project->projectFilePath().path().toStdString(), "none", "", nullptr, true);
return styleFromProjectFolder && !(*styleFromProjectFolder == clang::format::getNoStyle()); return styleFromProjectFolder && !(*styleFromProjectFolder == clang::format::getNoStyle());
} }

View File

@@ -394,7 +394,8 @@ Utils::FilePath filePathToCurrentSettings(const TextEditor::ICodeStylePreference
static QString s_errorMessage; static QString s_errorMessage;
Utils::expected_str<void> parseConfigurationContent(const std::string &fileContent, Utils::expected_str<void> parseConfigurationContent(const std::string &fileContent,
clang::format::FormatStyle &style) clang::format::FormatStyle &style,
bool allowUnknownOptions)
{ {
auto diagHandler = [](const llvm::SMDiagnostic &diag, void * /*context*/) { auto diagHandler = [](const llvm::SMDiagnostic &diag, void * /*context*/) {
s_errorMessage = QString::fromStdString(diag.getMessage().str()) + " " s_errorMessage = QString::fromStdString(diag.getMessage().str()) + " "
@@ -403,11 +404,12 @@ Utils::expected_str<void> parseConfigurationContent(const std::string &fileConte
}; };
style.Language = clang::format::FormatStyle::LK_Cpp; style.Language = clang::format::FormatStyle::LK_Cpp;
const std::error_code error = parseConfiguration(llvm::MemoryBufferRef(fileContent, "YAML"), const std::error_code error = parseConfiguration(
&style, llvm::MemoryBufferRef(fileContent, "YAML"),
false, &style,
diagHandler, allowUnknownOptions,
nullptr); diagHandler,
nullptr);
if (error) if (error)
return make_unexpected(s_errorMessage); return make_unexpected(s_errorMessage);
@@ -418,7 +420,7 @@ Utils::expected_str<void> parseConfigurationFile(const Utils::FilePath &filePath
clang::format::FormatStyle &style) clang::format::FormatStyle &style)
{ {
return parseConfigurationContent(filePath.fileContents().value_or(QByteArray()).toStdString(), return parseConfigurationContent(filePath.fileContents().value_or(QByteArray()).toStdString(),
style); style, true);
} }
} // namespace ClangFormat } // namespace ClangFormat

View File

@@ -49,7 +49,8 @@ clang::format::FormatStyle currentQtStyle(const TextEditor::ICodeStylePreference
Utils::FilePath filePathToCurrentSettings(const TextEditor::ICodeStylePreferences *codeStyle); Utils::FilePath filePathToCurrentSettings(const TextEditor::ICodeStylePreferences *codeStyle);
Utils::expected_str<void> parseConfigurationContent(const std::string &fileContent, Utils::expected_str<void> parseConfigurationContent(const std::string &fileContent,
clang::format::FormatStyle &style); clang::format::FormatStyle &style,
bool allowUnknownOptions = false);
Utils::expected_str<void> parseConfigurationFile(const Utils::FilePath &filePath, Utils::expected_str<void> parseConfigurationFile(const Utils::FilePath &filePath,
clang::format::FormatStyle &style); clang::format::FormatStyle &style);