forked from qt-creator/qt-creator
Clang: Refactor handling of diagnostics
...by introducing ClangDiagnosticManager and the helper class ClangDiagnosticFilter. ClangDiagnosticManager will get more state in a follow-up change. Change-Id: Id2c312bc897ea41ed67292b56b24dcfb7975ff4a Reviewed-by: Marco Bubke <marco.bubke@theqtcompany.com>
This commit is contained in:
@@ -77,6 +77,7 @@ ClangEditorDocumentProcessor::ClangEditorDocumentProcessor(
|
||||
ModelManagerSupportClang *modelManagerSupport,
|
||||
TextEditor::TextDocument *document)
|
||||
: BaseEditorDocumentProcessor(document)
|
||||
, m_diagnosticManager(document)
|
||||
, m_modelManagerSupport(modelManagerSupport)
|
||||
, m_parser(new ClangEditorDocumentParser(document->filePath().toString()))
|
||||
, m_parserRevision(0)
|
||||
@@ -179,7 +180,8 @@ void ClangEditorDocumentProcessor::updateCodeWarnings(const QVector<ClangBackEnd
|
||||
uint documentRevision)
|
||||
{
|
||||
if (documentRevision == revision()) {
|
||||
const auto codeWarnings = generateDiagnosticHints(diagnostics);
|
||||
m_diagnosticManager.processNewDiagnostics(diagnostics);
|
||||
const auto codeWarnings = m_diagnosticManager.takeExtraSelections();
|
||||
emit codeWarningsUpdated(revision(), codeWarnings);
|
||||
}
|
||||
}
|
||||
@@ -246,219 +248,6 @@ void ClangEditorDocumentProcessor::updateTranslationUnitForEditor(CppTools::Proj
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
bool isWarningOrNote(ClangBackEnd::DiagnosticSeverity severity)
|
||||
{
|
||||
using ClangBackEnd::DiagnosticSeverity;
|
||||
switch (severity) {
|
||||
case DiagnosticSeverity::Ignored:
|
||||
case DiagnosticSeverity::Note:
|
||||
case DiagnosticSeverity::Warning: return true;
|
||||
case DiagnosticSeverity::Error:
|
||||
case DiagnosticSeverity::Fatal: return false;
|
||||
}
|
||||
|
||||
Q_UNREACHABLE();
|
||||
}
|
||||
|
||||
bool isHelpfulChildDiagnostic(const ClangBackEnd::DiagnosticContainer &parentDiagnostic,
|
||||
const ClangBackEnd::DiagnosticContainer &childDiagnostic)
|
||||
{
|
||||
auto parentLocation = parentDiagnostic.location();
|
||||
auto childLocation = childDiagnostic.location();
|
||||
|
||||
return parentLocation == childLocation;
|
||||
}
|
||||
|
||||
QString diagnosticText(const ClangBackEnd::DiagnosticContainer &diagnostic)
|
||||
{
|
||||
QString text = diagnostic.category().toString()
|
||||
+ QStringLiteral(" ")
|
||||
+ diagnostic.text().toString();
|
||||
if (!diagnostic.enableOption().isEmpty()) {
|
||||
text += QStringLiteral(" (clang option: ")
|
||||
+ diagnostic.enableOption().toString()
|
||||
+ QStringLiteral(" disable with: ")
|
||||
+ diagnostic.disableOption().toString()
|
||||
+ QStringLiteral(")");
|
||||
}
|
||||
|
||||
for (auto &&childDiagnostic : diagnostic.children()) {
|
||||
if (isHelpfulChildDiagnostic(diagnostic, childDiagnostic))
|
||||
text += QStringLiteral("\n ") + childDiagnostic.text().toString();
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
template <class Condition>
|
||||
std::vector<ClangBackEnd::DiagnosticContainer>
|
||||
filterDiagnostics(const QVector<ClangBackEnd::DiagnosticContainer> &diagnostics,
|
||||
const Condition &condition)
|
||||
{
|
||||
std::vector<ClangBackEnd::DiagnosticContainer> filteredDiagnostics;
|
||||
|
||||
std::copy_if(diagnostics.cbegin(),
|
||||
diagnostics.cend(),
|
||||
std::back_inserter(filteredDiagnostics),
|
||||
condition);
|
||||
|
||||
return filteredDiagnostics;
|
||||
}
|
||||
|
||||
std::vector<ClangBackEnd::DiagnosticContainer>
|
||||
filterInterestingWarningDiagnostics(const QVector<ClangBackEnd::DiagnosticContainer> &diagnostics,
|
||||
QString &&documentFilePath)
|
||||
{
|
||||
auto isLocalWarning = [documentFilePath] (const ClangBackEnd::DiagnosticContainer &diagnostic) {
|
||||
return isWarningOrNote(diagnostic.severity())
|
||||
&& diagnostic.location().filePath() == documentFilePath;
|
||||
};
|
||||
|
||||
return filterDiagnostics(diagnostics, isLocalWarning);
|
||||
}
|
||||
|
||||
std::vector<ClangBackEnd::DiagnosticContainer>
|
||||
filterInterestingErrorsDiagnostics(const QVector<ClangBackEnd::DiagnosticContainer> &diagnostics,
|
||||
QString &&documentFilePath)
|
||||
{
|
||||
auto isLocalWarning = [documentFilePath] (const ClangBackEnd::DiagnosticContainer &diagnostic) {
|
||||
return !isWarningOrNote(diagnostic.severity())
|
||||
&& diagnostic.location().filePath() == documentFilePath;
|
||||
};
|
||||
|
||||
return filterDiagnostics(diagnostics, isLocalWarning);
|
||||
}
|
||||
|
||||
QTextEdit::ExtraSelection createExtraSelections(const QTextCharFormat &mainformat,
|
||||
const QTextCursor &cursor,
|
||||
const QString &diagnosticText)
|
||||
{
|
||||
QTextEdit::ExtraSelection extraSelection;
|
||||
|
||||
extraSelection.format = mainformat;
|
||||
extraSelection.cursor = cursor;
|
||||
extraSelection.format.setToolTip(diagnosticText);
|
||||
|
||||
return extraSelection;
|
||||
}
|
||||
|
||||
void addRangeSelections(const ClangBackEnd::DiagnosticContainer &diagnostic,
|
||||
QTextDocument *textDocument,
|
||||
const QTextCharFormat &rangeFormat,
|
||||
const QString &diagnosticText,
|
||||
QList<QTextEdit::ExtraSelection> &extraSelections)
|
||||
{
|
||||
for (auto &&range : diagnostic.ranges()) {
|
||||
QTextCursor cursor(textDocument);
|
||||
cursor.setPosition(int(range.start().offset()));
|
||||
cursor.setPosition(int(range.end().offset()), QTextCursor::KeepAnchor);
|
||||
|
||||
auto extraSelection = createExtraSelections(rangeFormat, cursor, diagnosticText);
|
||||
|
||||
extraSelections.push_back(std::move(extraSelection));
|
||||
}
|
||||
}
|
||||
|
||||
QTextCursor createSelectionCursor(QTextDocument *textDocument, uint position)
|
||||
{
|
||||
QTextCursor cursor(textDocument);
|
||||
cursor.setPosition(int(position));
|
||||
cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
|
||||
|
||||
if (!cursor.hasSelection()) {
|
||||
cursor.setPosition(int(position) - 1);
|
||||
cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 2);
|
||||
}
|
||||
|
||||
return cursor;
|
||||
}
|
||||
|
||||
void addSelections(const std::vector<ClangBackEnd::DiagnosticContainer> &diagnostics,
|
||||
QTextDocument *textDocument,
|
||||
const QTextCharFormat &mainFormat,
|
||||
const QTextCharFormat &rangeFormat,
|
||||
QList<QTextEdit::ExtraSelection> &extraSelections)
|
||||
{
|
||||
for (auto &&diagnostic : diagnostics) {
|
||||
auto cursor = createSelectionCursor(textDocument, diagnostic.location().offset());
|
||||
|
||||
auto text = diagnosticText(diagnostic);
|
||||
auto extraSelection = createExtraSelections(mainFormat, cursor, text);
|
||||
|
||||
addRangeSelections(diagnostic, textDocument, rangeFormat, text, extraSelections);
|
||||
|
||||
extraSelections.push_back(std::move(extraSelection));
|
||||
}
|
||||
}
|
||||
|
||||
QTextCharFormat fontsettingsTextFormat(TextEditor::TextStyle textStyle)
|
||||
{
|
||||
return TextEditor::TextEditorSettings::fontSettings().toTextCharFormat(textStyle);
|
||||
}
|
||||
|
||||
void addWarningSelections(const std::vector<ClangBackEnd::DiagnosticContainer> &diagnostics,
|
||||
QTextDocument *textDocument,
|
||||
QList<QTextEdit::ExtraSelection> &extraSelections)
|
||||
{
|
||||
addSelections(diagnostics,
|
||||
textDocument,
|
||||
fontsettingsTextFormat(TextEditor::C_WARNING),
|
||||
fontsettingsTextFormat(TextEditor::C_WARNING_CONTEXT),
|
||||
extraSelections);
|
||||
}
|
||||
|
||||
void addErrorSelections(const std::vector<ClangBackEnd::DiagnosticContainer> &diagnostics,
|
||||
QTextDocument *textDocument,
|
||||
QList<QTextEdit::ExtraSelection> &extraSelections)
|
||||
{
|
||||
addSelections(diagnostics,
|
||||
textDocument,
|
||||
fontsettingsTextFormat(TextEditor::C_ERROR),
|
||||
fontsettingsTextFormat(TextEditor::C_ERROR_CONTEXT),
|
||||
extraSelections);
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
QList<QTextEdit::ExtraSelection>
|
||||
ClangEditorDocumentProcessor::generateDiagnosticHints(const QVector<ClangBackEnd::DiagnosticContainer> &allDiagnostics)
|
||||
{
|
||||
const auto warningDiagnostic = filterInterestingWarningDiagnostics(allDiagnostics, filePath());
|
||||
const auto errorDiagnostic = filterInterestingErrorsDiagnostics(allDiagnostics, filePath());
|
||||
|
||||
m_clangTextMarks.clear();
|
||||
m_clangTextMarks.reserve(warningDiagnostic.size() + errorDiagnostic.size());
|
||||
|
||||
addClangTextMarks(warningDiagnostic);
|
||||
addClangTextMarks(errorDiagnostic);
|
||||
|
||||
QList<QTextEdit::ExtraSelection> extraSelections;
|
||||
extraSelections.reserve(int(warningDiagnostic.size() + errorDiagnostic.size()));
|
||||
|
||||
addWarningSelections(warningDiagnostic, textDocument(), extraSelections);
|
||||
addErrorSelections(errorDiagnostic, textDocument(), extraSelections);
|
||||
|
||||
return extraSelections;
|
||||
}
|
||||
|
||||
void ClangEditorDocumentProcessor::addClangTextMarks(const std::vector<ClangBackEnd::DiagnosticContainer> &diagnostics)
|
||||
{
|
||||
QTC_ASSERT(m_clangTextMarks.size() + diagnostics.size() <= m_clangTextMarks.capacity(), return);
|
||||
|
||||
for (auto &&diagnostic : diagnostics) {
|
||||
m_clangTextMarks.emplace_back(filePath(),
|
||||
diagnostic.location().line(),
|
||||
diagnostic.severity());
|
||||
|
||||
ClangTextMark &textMark = m_clangTextMarks.back();
|
||||
|
||||
textMark.setBaseTextDocument(baseTextDocument());
|
||||
|
||||
baseTextDocument()->addMark(&textMark);
|
||||
}
|
||||
}
|
||||
|
||||
void ClangEditorDocumentProcessor::requestDiagnostics(CppTools::ProjectPart &projectPart)
|
||||
{
|
||||
if (!m_projectPart || projectPart.id() != m_projectPart->id()) {
|
||||
|
||||
Reference in New Issue
Block a user