2014-08-19 15:59:29 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
|
** Contact: http://www.qt.io/licensing
|
2014-08-19 15:59:29 +02:00
|
|
|
**
|
|
|
|
|
** 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
|
2015-01-14 18:07:15 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms and
|
|
|
|
|
** conditions see http://www.qt.io/terms-conditions. For further information
|
2014-10-16 12:00:23 +02:00
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2014-08-19 15:59:29 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-16 12:00:23 +02:00
|
|
|
** 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.
|
2014-08-19 15:59:29 +02:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
2014-08-19 15:59:29 +02:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "clangeditordocumentprocessor.h"
|
|
|
|
|
|
2015-05-08 15:48:17 +02:00
|
|
|
#include "clangmodelmanagersupport.h"
|
|
|
|
|
#include "clangutils.h"
|
2014-08-19 15:59:29 +02:00
|
|
|
#include "cppcreatemarkers.h"
|
|
|
|
|
#include "diagnostic.h"
|
|
|
|
|
#include "pchinfo.h"
|
|
|
|
|
|
2015-08-31 16:28:26 +02:00
|
|
|
#include <diagnosticcontainer.h>
|
|
|
|
|
#include <sourcelocationcontainer.h>
|
|
|
|
|
|
2014-08-19 15:59:29 +02:00
|
|
|
#include <cpptools/cpptoolsplugin.h>
|
|
|
|
|
#include <cpptools/cppworkingcopy.h>
|
|
|
|
|
|
2015-09-10 14:18:09 +02:00
|
|
|
#include <texteditor/fontsettings.h>
|
2015-08-31 16:28:26 +02:00
|
|
|
#include <texteditor/texteditor.h>
|
2015-09-10 14:18:09 +02:00
|
|
|
#include <texteditor/texteditorconstants.h>
|
|
|
|
|
#include <texteditor/texteditorsettings.h>
|
2015-08-31 16:28:26 +02:00
|
|
|
|
2014-08-19 15:59:29 +02:00
|
|
|
#include <cplusplus/CppDocument.h>
|
|
|
|
|
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
#include <utils/QtConcurrentTools>
|
|
|
|
|
|
2015-08-31 16:28:26 +02:00
|
|
|
#include <QTextBlock>
|
|
|
|
|
|
2014-08-19 15:59:29 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
typedef CPlusPlus::Document::DiagnosticMessage CppToolsDiagnostic;
|
|
|
|
|
|
|
|
|
|
QList<TextEditor::BlockRange> toTextEditorBlocks(
|
|
|
|
|
const QList<ClangCodeModel::SemanticMarker::Range> &ranges)
|
|
|
|
|
{
|
|
|
|
|
QList<TextEditor::BlockRange> result;
|
|
|
|
|
result.reserve(ranges.size());
|
|
|
|
|
foreach (const ClangCodeModel::SemanticMarker::Range &range, ranges)
|
|
|
|
|
result.append(TextEditor::BlockRange(range.first, range.last));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
|
|
namespace ClangCodeModel {
|
2015-05-08 15:48:17 +02:00
|
|
|
namespace Internal {
|
2014-08-19 15:59:29 +02:00
|
|
|
|
2015-05-08 15:48:17 +02:00
|
|
|
ClangEditorDocumentProcessor::ClangEditorDocumentProcessor(
|
|
|
|
|
ModelManagerSupportClang *modelManagerSupport,
|
|
|
|
|
TextEditor::TextDocument *document)
|
2014-08-19 15:59:29 +02:00
|
|
|
: BaseEditorDocumentProcessor(document)
|
2015-05-08 15:48:17 +02:00
|
|
|
, m_modelManagerSupport(modelManagerSupport)
|
2015-09-01 17:34:07 +02:00
|
|
|
, m_parser(new ClangEditorDocumentParser(document->filePath().toString()))
|
2014-08-19 15:59:29 +02:00
|
|
|
, m_parserRevision(0)
|
|
|
|
|
, m_semanticHighlighter(document)
|
|
|
|
|
, m_builtinProcessor(document, /*enableSemanticHighlighter=*/ false)
|
|
|
|
|
{
|
|
|
|
|
// Forwarding the semantic info from the builtin processor enables us to provide all
|
|
|
|
|
// editor (widget) related features that are not yet implemented by the clang plugin.
|
|
|
|
|
connect(&m_builtinProcessor, &CppTools::BuiltinEditorDocumentProcessor::cppDocumentUpdated,
|
|
|
|
|
this, &ClangEditorDocumentProcessor::cppDocumentUpdated);
|
|
|
|
|
connect(&m_builtinProcessor, &CppTools::BuiltinEditorDocumentProcessor::semanticInfoUpdated,
|
|
|
|
|
this, &ClangEditorDocumentProcessor::semanticInfoUpdated);
|
|
|
|
|
|
2015-07-14 19:01:32 +02:00
|
|
|
connect(CppTools::CppModelManager::instance(), &CppTools::CppModelManager::projectPartsRemoved,
|
|
|
|
|
this, &ClangEditorDocumentProcessor::onProjectPartsRemoved);
|
|
|
|
|
|
2014-08-19 15:59:29 +02:00
|
|
|
m_semanticHighlighter.setHighlightingRunner(
|
|
|
|
|
[this]() -> QFuture<TextEditor::HighlightingResult> {
|
|
|
|
|
const int firstLine = 1;
|
|
|
|
|
const int lastLine = baseTextDocument()->document()->blockCount();
|
|
|
|
|
|
2015-09-01 17:34:07 +02:00
|
|
|
CreateMarkers *createMarkers = CreateMarkers::create(m_parser->semanticMarker(),
|
2014-12-21 21:54:30 +02:00
|
|
|
baseTextDocument()->filePath().toString(),
|
2014-08-19 15:59:29 +02:00
|
|
|
firstLine, lastLine);
|
|
|
|
|
return createMarkers->start();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ClangEditorDocumentProcessor::~ClangEditorDocumentProcessor()
|
|
|
|
|
{
|
|
|
|
|
m_parserWatcher.cancel();
|
|
|
|
|
m_parserWatcher.waitForFinished();
|
2015-05-08 15:48:17 +02:00
|
|
|
|
2015-07-14 19:01:32 +02:00
|
|
|
if (m_projectPart) {
|
|
|
|
|
QTC_ASSERT(m_modelManagerSupport, return);
|
2015-08-31 16:10:36 +02:00
|
|
|
m_modelManagerSupport->ipcCommunicator().unregisterTranslationUnitsForEditor(
|
2015-07-14 19:01:32 +02:00
|
|
|
{ClangBackEnd::FileContainer(filePath(), m_projectPart->id())});
|
|
|
|
|
}
|
2014-08-19 15:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangEditorDocumentProcessor::run()
|
|
|
|
|
{
|
2015-08-31 16:28:26 +02:00
|
|
|
requestDiagnostics();
|
|
|
|
|
|
2014-08-19 15:59:29 +02:00
|
|
|
// Run clang parser
|
|
|
|
|
disconnect(&m_parserWatcher, &QFutureWatcher<void>::finished,
|
|
|
|
|
this, &ClangEditorDocumentProcessor::onParserFinished);
|
|
|
|
|
m_parserWatcher.cancel();
|
|
|
|
|
m_parserWatcher.setFuture(QFuture<void>());
|
|
|
|
|
|
|
|
|
|
m_parserRevision = revision();
|
|
|
|
|
connect(&m_parserWatcher, &QFutureWatcher<void>::finished,
|
|
|
|
|
this, &ClangEditorDocumentProcessor::onParserFinished);
|
2015-07-10 14:57:42 +02:00
|
|
|
const QFuture<void> future = QtConcurrent::run(&runParser,
|
|
|
|
|
parser(),
|
|
|
|
|
ClangEditorDocumentParser::InMemoryInfo(true));
|
2014-08-19 15:59:29 +02:00
|
|
|
m_parserWatcher.setFuture(future);
|
|
|
|
|
|
|
|
|
|
// Run builtin processor
|
|
|
|
|
m_builtinProcessor.run();
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-19 14:37:26 +02:00
|
|
|
void ClangEditorDocumentProcessor::recalculateSemanticInfoDetached(bool force)
|
2014-08-19 15:59:29 +02:00
|
|
|
{
|
2015-06-19 14:37:26 +02:00
|
|
|
m_builtinProcessor.recalculateSemanticInfoDetached(force);
|
2014-08-19 15:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
2015-06-19 14:43:41 +02:00
|
|
|
void ClangEditorDocumentProcessor::semanticRehighlight()
|
|
|
|
|
{
|
|
|
|
|
m_semanticHighlighter.updateFormatMapFromFontSettings();
|
|
|
|
|
m_semanticHighlighter.run();
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-19 15:59:29 +02:00
|
|
|
CppTools::SemanticInfo ClangEditorDocumentProcessor::recalculateSemanticInfo()
|
|
|
|
|
{
|
|
|
|
|
return m_builtinProcessor.recalculateSemanticInfo();
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-01 17:34:07 +02:00
|
|
|
CppTools::BaseEditorDocumentParser::Ptr ClangEditorDocumentProcessor::parser()
|
2014-08-19 15:59:29 +02:00
|
|
|
{
|
2015-09-01 17:34:07 +02:00
|
|
|
return m_parser;
|
2014-08-19 15:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
2014-11-28 12:03:58 +01:00
|
|
|
CPlusPlus::Snapshot ClangEditorDocumentProcessor::snapshot()
|
|
|
|
|
{
|
|
|
|
|
return m_builtinProcessor.snapshot();
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-19 15:59:29 +02:00
|
|
|
bool ClangEditorDocumentProcessor::isParserRunning() const
|
|
|
|
|
{
|
|
|
|
|
return m_parserWatcher.isRunning();
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-14 19:01:32 +02:00
|
|
|
CppTools::ProjectPart::Ptr ClangEditorDocumentProcessor::projectPart() const
|
|
|
|
|
{
|
|
|
|
|
return m_projectPart;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-31 16:28:26 +02:00
|
|
|
void ClangEditorDocumentProcessor::updateCodeWarnings(const QVector<ClangBackEnd::DiagnosticContainer> &diagnostics,
|
|
|
|
|
uint documentRevision)
|
|
|
|
|
{
|
|
|
|
|
if (documentRevision == revision()) {
|
|
|
|
|
const auto codeWarnings = generateDiagnosticHints(diagnostics);
|
|
|
|
|
emit codeWarningsUpdated(revision(), codeWarnings);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-14 19:01:32 +02:00
|
|
|
ClangEditorDocumentProcessor *ClangEditorDocumentProcessor::get(const QString &filePath)
|
|
|
|
|
{
|
|
|
|
|
return qobject_cast<ClangEditorDocumentProcessor *>(BaseEditorDocumentProcessor::get(filePath));
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-31 14:36:58 +02:00
|
|
|
void ClangEditorDocumentProcessor::updateProjectPartAndTranslationUnitForEditor()
|
2015-07-14 19:01:32 +02:00
|
|
|
{
|
2015-09-01 17:34:07 +02:00
|
|
|
const CppTools::ProjectPart::Ptr projectPart = m_parser->projectPart();
|
2015-07-14 19:01:32 +02:00
|
|
|
QTC_ASSERT(projectPart, return);
|
|
|
|
|
|
2015-08-31 14:36:58 +02:00
|
|
|
updateTranslationUnitForEditor(*projectPart.data());
|
2015-08-31 16:28:26 +02:00
|
|
|
requestDiagnostics(*projectPart.data());
|
|
|
|
|
|
2015-07-14 19:01:32 +02:00
|
|
|
m_projectPart = projectPart;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-19 15:59:29 +02:00
|
|
|
void ClangEditorDocumentProcessor::onParserFinished()
|
|
|
|
|
{
|
|
|
|
|
if (revision() != m_parserRevision)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Emit ifdefed out blocks
|
2015-09-01 17:34:07 +02:00
|
|
|
const auto ifdefoutBlocks = toTextEditorBlocks(m_parser->ifdefedOutBlocks());
|
2014-08-19 15:59:29 +02:00
|
|
|
emit ifdefedOutBlocksUpdated(revision(), ifdefoutBlocks);
|
|
|
|
|
|
|
|
|
|
// Run semantic highlighter
|
|
|
|
|
m_semanticHighlighter.run();
|
2015-07-14 19:01:32 +02:00
|
|
|
|
2015-08-31 14:36:58 +02:00
|
|
|
updateProjectPartAndTranslationUnitForEditor();
|
2015-07-14 19:01:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangEditorDocumentProcessor::onProjectPartsRemoved(const QStringList &projectPartIds)
|
|
|
|
|
{
|
|
|
|
|
if (m_projectPart && projectPartIds.contains(m_projectPart->id()))
|
|
|
|
|
m_projectPart.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-31 14:36:58 +02:00
|
|
|
void ClangEditorDocumentProcessor::updateTranslationUnitForEditor(CppTools::ProjectPart &projectPart)
|
2015-07-14 19:01:32 +02:00
|
|
|
{
|
|
|
|
|
QTC_ASSERT(m_modelManagerSupport, return);
|
|
|
|
|
IpcCommunicator &ipcCommunicator = m_modelManagerSupport->ipcCommunicator();
|
|
|
|
|
|
|
|
|
|
if (m_projectPart) {
|
|
|
|
|
if (projectPart.id() != m_projectPart->id()) {
|
2015-08-31 12:40:14 +02:00
|
|
|
auto container1 = ClangBackEnd::FileContainer(filePath(),
|
|
|
|
|
m_projectPart->id(),
|
|
|
|
|
revision());
|
2015-08-31 16:10:36 +02:00
|
|
|
ipcCommunicator.unregisterTranslationUnitsForEditor({container1});
|
2015-07-14 19:01:32 +02:00
|
|
|
|
2015-08-31 12:40:14 +02:00
|
|
|
auto container2 = ClangBackEnd::FileContainer(filePath(),
|
|
|
|
|
projectPart.id(),
|
|
|
|
|
revision());
|
2015-08-31 16:10:36 +02:00
|
|
|
ipcCommunicator.registerTranslationUnitsForEditor({container2});
|
2015-07-14 19:01:32 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
2015-08-31 12:40:14 +02:00
|
|
|
auto container = ClangBackEnd::FileContainer(filePath(),
|
|
|
|
|
projectPart.id(),
|
|
|
|
|
revision());
|
2015-08-31 16:10:36 +02:00
|
|
|
ipcCommunicator.registerTranslationUnitsForEditor({container});
|
2015-08-31 16:28:26 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-10 14:18:09 +02:00
|
|
|
QTextCharFormat fontsettingsTextFormat(TextEditor::TextStyle textStyle)
|
|
|
|
|
{
|
|
|
|
|
return TextEditor::TextEditorSettings::fontSettings().toTextCharFormat(textStyle);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-31 16:28:26 +02:00
|
|
|
void addWarningSelections(const std::vector<ClangBackEnd::DiagnosticContainer> &diagnostics,
|
|
|
|
|
QTextDocument *textDocument,
|
|
|
|
|
QList<QTextEdit::ExtraSelection> &extraSelections)
|
|
|
|
|
{
|
2015-09-10 14:18:09 +02:00
|
|
|
addSelections(diagnostics,
|
|
|
|
|
textDocument,
|
|
|
|
|
fontsettingsTextFormat(TextEditor::C_WARNING),
|
|
|
|
|
fontsettingsTextFormat(TextEditor::C_WARNING_CONTEXT),
|
|
|
|
|
extraSelections);
|
2015-08-31 16:28:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void addErrorSelections(const std::vector<ClangBackEnd::DiagnosticContainer> &diagnostics,
|
|
|
|
|
QTextDocument *textDocument,
|
|
|
|
|
QList<QTextEdit::ExtraSelection> &extraSelections)
|
|
|
|
|
{
|
2015-09-10 14:18:09 +02:00
|
|
|
addSelections(diagnostics,
|
|
|
|
|
textDocument,
|
|
|
|
|
fontsettingsTextFormat(TextEditor::C_ERROR),
|
|
|
|
|
fontsettingsTextFormat(TextEditor::C_ERROR_CONTEXT),
|
|
|
|
|
extraSelections);
|
2015-08-31 16:28:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // 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()) {
|
|
|
|
|
IpcCommunicator &ipcCommunicator = m_modelManagerSupport->ipcCommunicator();
|
|
|
|
|
|
2015-08-31 12:40:14 +02:00
|
|
|
ipcCommunicator.requestDiagnostics({filePath(),
|
|
|
|
|
projectPart.id(),
|
|
|
|
|
Utf8String(),
|
|
|
|
|
false,
|
|
|
|
|
revision()});
|
2015-08-31 16:28:26 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangEditorDocumentProcessor::requestDiagnostics()
|
|
|
|
|
{
|
|
|
|
|
// Get diagnostics
|
|
|
|
|
if (m_projectPart) {
|
|
|
|
|
auto &ipcCommunicator = m_modelManagerSupport->ipcCommunicator();
|
|
|
|
|
ipcCommunicator.requestDiagnostics({filePath(),
|
2015-08-31 12:40:14 +02:00
|
|
|
m_projectPart->id(),
|
|
|
|
|
baseTextDocument()->plainText(),
|
|
|
|
|
true,
|
|
|
|
|
revision()});
|
2015-07-14 19:01:32 +02:00
|
|
|
}
|
2014-08-19 15:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
2015-05-08 15:48:17 +02:00
|
|
|
} // namespace Internal
|
2014-08-19 15:59:29 +02:00
|
|
|
} // namespace ClangCodeModel
|