2015-09-04 12:15:17 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
|
** Contact: http://www.qt.io/licensing
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms and
|
|
|
|
|
** conditions see http://www.qt.io/terms-conditions. For further information
|
|
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "clangdiagnosticfilter.h"
|
|
|
|
|
#include "clangdiagnosticmanager.h"
|
|
|
|
|
|
2015-12-02 12:25:20 +01:00
|
|
|
#include <texteditor/fontsettings.h>
|
2015-09-04 12:15:17 +02:00
|
|
|
#include <texteditor/textdocument.h>
|
2015-12-02 12:25:20 +01:00
|
|
|
#include <texteditor/texteditorsettings.h>
|
2015-09-04 12:15:17 +02:00
|
|
|
|
|
|
|
|
#include <utils/fileutils.h>
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2015-12-02 20:07:29 +01:00
|
|
|
#include <QTextBlock>
|
|
|
|
|
|
2015-09-04 12:15:17 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-02 20:07:29 +01:00
|
|
|
int positionInText(QTextDocument *textDocument,
|
|
|
|
|
const ClangBackEnd::SourceLocationContainer &sourceLocationContainer)
|
|
|
|
|
{
|
|
|
|
|
auto textBlock = textDocument->findBlockByNumber(int(sourceLocationContainer.line()) - 1);
|
|
|
|
|
|
|
|
|
|
return textBlock.position() + int(sourceLocationContainer.column()) - 1;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-04 12:15:17 +02:00
|
|
|
void addRangeSelections(const ClangBackEnd::DiagnosticContainer &diagnostic,
|
|
|
|
|
QTextDocument *textDocument,
|
2015-12-02 12:25:20 +01:00
|
|
|
const QTextCharFormat &contextFormat,
|
2015-09-04 12:15:17 +02:00
|
|
|
const QString &diagnosticText,
|
|
|
|
|
QList<QTextEdit::ExtraSelection> &extraSelections)
|
|
|
|
|
{
|
|
|
|
|
for (auto &&range : diagnostic.ranges()) {
|
|
|
|
|
QTextCursor cursor(textDocument);
|
2015-12-02 20:07:29 +01:00
|
|
|
cursor.setPosition(positionInText(textDocument, range.start()));
|
|
|
|
|
cursor.setPosition(positionInText(textDocument, range.end()), QTextCursor::KeepAnchor);
|
2015-09-04 12:15:17 +02:00
|
|
|
|
2015-12-02 12:25:20 +01:00
|
|
|
auto extraSelection = createExtraSelections(contextFormat, cursor, diagnosticText);
|
2015-09-04 12:15:17 +02:00
|
|
|
|
|
|
|
|
extraSelections.push_back(std::move(extraSelection));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-02 20:07:29 +01:00
|
|
|
QTextCursor createSelectionCursor(QTextDocument *textDocument,
|
|
|
|
|
const ClangBackEnd::SourceLocationContainer &sourceLocationContainer)
|
2015-09-04 12:15:17 +02:00
|
|
|
{
|
|
|
|
|
QTextCursor cursor(textDocument);
|
2015-12-02 20:07:29 +01:00
|
|
|
cursor.setPosition(positionInText(textDocument, sourceLocationContainer));
|
2015-09-04 12:15:17 +02:00
|
|
|
cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
|
|
|
|
|
|
|
|
|
|
if (!cursor.hasSelection()) {
|
2015-12-02 20:07:29 +01:00
|
|
|
cursor.setPosition(positionInText(textDocument, sourceLocationContainer) - 1);
|
2015-09-04 12:15:17 +02:00
|
|
|
cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cursor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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()
|
2015-10-13 18:46:55 +02:00
|
|
|
+ QStringLiteral("\n\n")
|
2015-09-04 12:15:17 +02:00
|
|
|
+ diagnostic.text().toString();
|
2015-10-13 18:46:55 +02:00
|
|
|
|
|
|
|
|
#ifdef QT_DEBUG
|
|
|
|
|
if (!diagnostic.disableOption().isEmpty()) {
|
|
|
|
|
text += QStringLiteral(" (disable with ")
|
2015-09-04 12:15:17 +02:00
|
|
|
+ diagnostic.disableOption().toString()
|
|
|
|
|
+ QStringLiteral(")");
|
|
|
|
|
}
|
2015-10-13 18:46:55 +02:00
|
|
|
#endif
|
2015-09-04 12:15:17 +02:00
|
|
|
|
|
|
|
|
for (auto &&childDiagnostic : diagnostic.children()) {
|
|
|
|
|
if (isHelpfulChildDiagnostic(diagnostic, childDiagnostic))
|
|
|
|
|
text += QStringLiteral("\n ") + childDiagnostic.text().toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void addSelections(const QVector<ClangBackEnd::DiagnosticContainer> &diagnostics,
|
|
|
|
|
QTextDocument *textDocument,
|
|
|
|
|
const QTextCharFormat &mainFormat,
|
2015-12-02 12:25:20 +01:00
|
|
|
const QTextCharFormat &contextFormat,
|
2015-09-04 12:15:17 +02:00
|
|
|
QList<QTextEdit::ExtraSelection> &extraSelections)
|
|
|
|
|
{
|
|
|
|
|
for (auto &&diagnostic : diagnostics) {
|
2015-12-02 20:07:29 +01:00
|
|
|
auto cursor = createSelectionCursor(textDocument, diagnostic.location());
|
2015-09-04 12:15:17 +02:00
|
|
|
|
|
|
|
|
auto text = diagnosticText(diagnostic);
|
|
|
|
|
auto extraSelection = createExtraSelections(mainFormat, cursor, text);
|
|
|
|
|
|
2015-12-02 12:25:20 +01:00
|
|
|
addRangeSelections(diagnostic, textDocument, contextFormat, text, extraSelections);
|
2015-09-04 12:15:17 +02:00
|
|
|
|
|
|
|
|
extraSelections.push_back(std::move(extraSelection));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void addWarningSelections(const QVector<ClangBackEnd::DiagnosticContainer> &diagnostics,
|
|
|
|
|
QTextDocument *textDocument,
|
|
|
|
|
QList<QTextEdit::ExtraSelection> &extraSelections)
|
|
|
|
|
{
|
2015-12-02 12:25:20 +01:00
|
|
|
const auto fontSettings = TextEditor::TextEditorSettings::instance()->fontSettings();
|
2015-09-04 12:15:17 +02:00
|
|
|
|
2015-12-02 12:25:20 +01:00
|
|
|
QTextCharFormat warningFormat = fontSettings.toTextCharFormat(TextEditor::C_WARNING);
|
2015-09-04 12:15:17 +02:00
|
|
|
|
2015-12-02 12:25:20 +01:00
|
|
|
QTextCharFormat warningContextFormat = fontSettings.toTextCharFormat(TextEditor::C_WARNING_CONTEXT);
|
|
|
|
|
|
|
|
|
|
addSelections(diagnostics, textDocument, warningFormat, warningContextFormat, extraSelections);
|
2015-09-04 12:15:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void addErrorSelections(const QVector<ClangBackEnd::DiagnosticContainer> &diagnostics,
|
|
|
|
|
QTextDocument *textDocument,
|
|
|
|
|
QList<QTextEdit::ExtraSelection> &extraSelections)
|
|
|
|
|
{
|
2015-12-02 12:25:20 +01:00
|
|
|
const auto fontSettings = TextEditor::TextEditorSettings::instance()->fontSettings();
|
2015-09-04 12:15:17 +02:00
|
|
|
|
2015-12-02 12:25:20 +01:00
|
|
|
QTextCharFormat errorFormat = fontSettings.toTextCharFormat(TextEditor::C_ERROR);
|
|
|
|
|
QTextCharFormat errorContextFormat = fontSettings.toTextCharFormat(TextEditor::C_ERROR_CONTEXT);
|
2015-09-04 12:15:17 +02:00
|
|
|
|
2015-12-02 12:25:20 +01:00
|
|
|
addSelections(diagnostics, textDocument, errorFormat, errorContextFormat, extraSelections);
|
2015-09-04 12:15:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // anonymous
|
|
|
|
|
|
|
|
|
|
namespace ClangCodeModel {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
ClangDiagnosticManager::ClangDiagnosticManager(TextEditor::TextDocument *textDocument)
|
|
|
|
|
: m_textDocument(textDocument)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangDiagnosticManager::generateTextMarks()
|
|
|
|
|
{
|
|
|
|
|
m_clangTextMarks.clear();
|
|
|
|
|
m_clangTextMarks.reserve(m_warningDiagnostics.size() + m_errorDiagnostics.size());
|
|
|
|
|
|
|
|
|
|
addClangTextMarks(m_warningDiagnostics);
|
|
|
|
|
addClangTextMarks(m_errorDiagnostics);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<QTextEdit::ExtraSelection> ClangDiagnosticManager::takeExtraSelections()
|
|
|
|
|
{
|
|
|
|
|
auto extraSelections = m_extraSelections;
|
|
|
|
|
|
|
|
|
|
m_extraSelections.clear();
|
|
|
|
|
|
|
|
|
|
return extraSelections;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-25 15:11:23 +01:00
|
|
|
void ClangDiagnosticManager::clearDiagnosticsWithFixIts()
|
|
|
|
|
{
|
|
|
|
|
m_fixItdiagnostics.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-04 12:15:17 +02:00
|
|
|
void ClangDiagnosticManager::generateEditorSelections()
|
|
|
|
|
{
|
|
|
|
|
m_extraSelections.clear();
|
|
|
|
|
m_extraSelections.reserve(int(m_warningDiagnostics.size() + m_errorDiagnostics.size()));
|
|
|
|
|
|
|
|
|
|
addWarningSelections(m_warningDiagnostics, m_textDocument->document(), m_extraSelections);
|
|
|
|
|
addErrorSelections(m_errorDiagnostics, m_textDocument->document(), m_extraSelections);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangDiagnosticManager::processNewDiagnostics(
|
|
|
|
|
const QVector<ClangBackEnd::DiagnosticContainer> &allDiagnostics)
|
|
|
|
|
{
|
|
|
|
|
filterDiagnostics(allDiagnostics);
|
|
|
|
|
|
|
|
|
|
generateTextMarks();
|
|
|
|
|
generateEditorSelections();
|
|
|
|
|
|
|
|
|
|
clearWarningsAndErrors();
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-24 18:26:09 +02:00
|
|
|
const QVector<ClangBackEnd::DiagnosticContainer> &
|
|
|
|
|
ClangDiagnosticManager::diagnosticsWithFixIts() const
|
|
|
|
|
{
|
|
|
|
|
return m_fixItdiagnostics;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-04 12:15:17 +02:00
|
|
|
void ClangDiagnosticManager::addClangTextMarks(
|
|
|
|
|
const QVector<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(m_textDocument);
|
|
|
|
|
|
|
|
|
|
m_textDocument->addMark(&textMark);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangDiagnosticManager::clearWarningsAndErrors()
|
|
|
|
|
{
|
|
|
|
|
m_warningDiagnostics.clear();
|
|
|
|
|
m_errorDiagnostics.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString ClangDiagnosticManager::filePath() const
|
|
|
|
|
{
|
|
|
|
|
return m_textDocument->filePath().toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangDiagnosticManager::filterDiagnostics(
|
|
|
|
|
const QVector<ClangBackEnd::DiagnosticContainer> &diagnostics)
|
|
|
|
|
{
|
|
|
|
|
ClangDiagnosticFilter filter(filePath());
|
|
|
|
|
filter.filter(diagnostics);
|
|
|
|
|
|
|
|
|
|
m_warningDiagnostics = filter.takeWarnings();
|
|
|
|
|
m_errorDiagnostics = filter.takeErrors();
|
2015-08-24 18:26:09 +02:00
|
|
|
m_fixItdiagnostics = filter.takeFixIts();
|
2015-09-04 12:15:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace ClangCodeModel
|