clangeditordocumentprocessor.cpp 12.9 KB
Newer Older
1 2
/****************************************************************************
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
5 6 7 8 9 10 11
**
** 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
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
15
**
16 17 18 19 20 21 22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 24 25 26 27
**
****************************************************************************/

#include "clangeditordocumentprocessor.h"

28
#include "clangbackendipcintegration.h"
29
#include "clangdiagnostictooltipwidget.h"
30 31
#include "clangfixitoperation.h"
#include "clangfixitoperationsextractor.h"
32
#include "clanghighlightingmarksreporter.h"
33
#include "clangutils.h"
34

Marco Bubke's avatar
Marco Bubke committed
35 36 37
#include <diagnosticcontainer.h>
#include <sourcelocationcontainer.h>

38
#include <cpptools/compileroptionsbuilder.h>
39
#include <cpptools/cppcodemodelsettings.h>
40
#include <cpptools/cppmodelmanager.h>
Marco Bubke's avatar
Marco Bubke committed
41
#include <cpptools/cpptoolsbridge.h>
42
#include <cpptools/cpptoolsreuse.h>
43 44
#include <cpptools/cppworkingcopy.h>

45
#include <texteditor/convenience.h>
46
#include <texteditor/fontsettings.h>
Marco Bubke's avatar
Marco Bubke committed
47
#include <texteditor/texteditor.h>
48 49
#include <texteditor/texteditorconstants.h>
#include <texteditor/texteditorsettings.h>
Marco Bubke's avatar
Marco Bubke committed
50

51 52 53
#include <cplusplus/CppDocument.h>

#include <utils/qtcassert.h>
54
#include <utils/tooltip/tooltip.h>
55 56
#include <utils/QtConcurrentTools>

Marco Bubke's avatar
Marco Bubke committed
57 58
#include <QTextBlock>

59
namespace ClangCodeModel {
60
namespace Internal {
61

62
ClangEditorDocumentProcessor::ClangEditorDocumentProcessor(
63
        IpcCommunicator &ipcCommunicator,
64
        TextEditor::TextDocument *document)
65
    : BaseEditorDocumentProcessor(document->document(), document->filePath().toString())
66
    , m_diagnosticManager(document)
67
    , m_ipcCommunicator(ipcCommunicator)
68
    , m_parser(new ClangEditorDocumentParser(document->filePath().toString()))
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    , 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);
}

ClangEditorDocumentProcessor::~ClangEditorDocumentProcessor()
{
    m_parserWatcher.cancel();
    m_parserWatcher.waitForFinished();
85

86
    if (m_projectPart) {
87
        m_ipcCommunicator.unregisterTranslationUnitsForEditor(
88 89
            {ClangBackEnd::FileContainer(filePath(), m_projectPart->id())});
    }
90 91 92 93
}

void ClangEditorDocumentProcessor::run()
{
94
    updateTranslationUnitIfProjectPartExists();
Marco Bubke's avatar
Marco Bubke committed
95

96 97 98 99 100 101 102 103 104
    // 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);
105 106
    const CppTools::WorkingCopy workingCopy = CppTools::CppModelManager::instance()->workingCopy();
    const QFuture<void> future = QtConcurrent::run(&runParser, parser(), workingCopy);
107 108 109 110 111 112
    m_parserWatcher.setFuture(future);

    // Run builtin processor
    m_builtinProcessor.run();
}

113
void ClangEditorDocumentProcessor::recalculateSemanticInfoDetached(bool force)
114
{
115
    m_builtinProcessor.recalculateSemanticInfoDetached(force);
116 117
}

118 119 120
void ClangEditorDocumentProcessor::semanticRehighlight()
{
    m_semanticHighlighter.updateFormatMapFromFontSettings();
121 122 123

    if (m_projectPart)
        requestDocumentAnnotations(m_projectPart->id());
124 125
}

126 127 128 129 130
CppTools::SemanticInfo ClangEditorDocumentProcessor::recalculateSemanticInfo()
{
    return m_builtinProcessor.recalculateSemanticInfo();
}

131
CppTools::BaseEditorDocumentParser::Ptr ClangEditorDocumentProcessor::parser()
132
{
133
    return m_parser;
134 135
}

136 137 138 139 140
CPlusPlus::Snapshot ClangEditorDocumentProcessor::snapshot()
{
   return m_builtinProcessor.snapshot();
}

141 142 143 144 145
bool ClangEditorDocumentProcessor::isParserRunning() const
{
    return m_parserWatcher.isRunning();
}

146 147 148 149 150
bool ClangEditorDocumentProcessor::hasProjectPart() const
{
    return m_projectPart;
}

151 152 153 154 155
CppTools::ProjectPart::Ptr ClangEditorDocumentProcessor::projectPart() const
{
    return m_projectPart;
}

156 157 158 159 160
void ClangEditorDocumentProcessor::clearProjectPart()
{
    m_projectPart.clear();
}

Marco Bubke's avatar
Marco Bubke committed
161 162 163 164
void ClangEditorDocumentProcessor::updateCodeWarnings(const QVector<ClangBackEnd::DiagnosticContainer> &diagnostics,
                                                      uint documentRevision)
{
    if (documentRevision == revision()) {
165 166
        m_diagnosticManager.processNewDiagnostics(diagnostics);
        const auto codeWarnings = m_diagnosticManager.takeExtraSelections();
Marco Bubke's avatar
Marco Bubke committed
167 168 169
        emit codeWarningsUpdated(revision(), codeWarnings);
    }
}
170
namespace {
Marco Bubke's avatar
Marco Bubke committed
171

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
int positionInText(QTextDocument *textDocument,
                   const ClangBackEnd::SourceLocationContainer &sourceLocationContainer)
{
    auto textBlock = textDocument->findBlockByNumber(int(sourceLocationContainer.line()) - 1);

    return textBlock.position() + int(sourceLocationContainer.column()) - 1;
}

TextEditor::BlockRange
toTextEditorBlock(QTextDocument *textDocument,
                  const ClangBackEnd::SourceRangeContainer &sourceRangeContainer)
{
    return TextEditor::BlockRange(positionInText(textDocument, sourceRangeContainer.start()),
                                  positionInText(textDocument, sourceRangeContainer.end()));
}

QList<TextEditor::BlockRange>
toTextEditorBlocks(QTextDocument *textDocument,
                   const QVector<ClangBackEnd::SourceRangeContainer> &ifdefedOutRanges)
191 192 193 194 195
{
    QList<TextEditor::BlockRange> blockRanges;
    blockRanges.reserve(ifdefedOutRanges.size());

    for (const auto &range : ifdefedOutRanges)
196
        blockRanges.append(toTextEditorBlock(textDocument, range));
197 198 199

    return blockRanges;
}
200
}
201 202 203 204 205 206 207

void ClangEditorDocumentProcessor::updateHighlighting(
        const QVector<ClangBackEnd::HighlightingMarkContainer> &highlightingMarks,
        const QVector<ClangBackEnd::SourceRangeContainer> &skippedPreprocessorRanges,
        uint documentRevision)
{
    if (documentRevision == revision()) {
208
        const auto skippedPreprocessorBlocks = toTextEditorBlocks(textDocument(), skippedPreprocessorRanges);
209 210 211 212 213 214 215 216 217 218 219
        emit ifdefedOutBlocksUpdated(documentRevision, skippedPreprocessorBlocks);

        m_semanticHighlighter.setHighlightingRunner(
            [highlightingMarks]() {
                auto *reporter = new HighlightingMarksReporter(highlightingMarks);
                return reporter->start();
            });
        m_semanticHighlighter.run();
    }
}

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
static int currentLine(const TextEditor::AssistInterface &assistInterface)
{
    int line, column;
    TextEditor::Convenience::convertPosition(assistInterface.textDocument(),
                                             assistInterface.position(),
                                             &line,
                                             &column);
    return line;
}

TextEditor::QuickFixOperations ClangEditorDocumentProcessor::extraRefactoringOperations(
        const TextEditor::AssistInterface &assistInterface)
{
    ClangFixItOperationsExtractor extractor(m_diagnosticManager.diagnosticsWithFixIts());

    return extractor.extract(assistInterface.fileName(), currentLine(assistInterface));
}

238 239 240 241 242 243 244 245 246 247 248 249
bool ClangEditorDocumentProcessor::hasDiagnosticsAt(uint line, uint column) const
{
    return m_diagnosticManager.hasDiagnosticsAt(line, column);
}

void ClangEditorDocumentProcessor::showDiagnosticTooltip(const QPoint &point,
                                                         QWidget *parent,
                                                         uint line,
                                                         uint column) const
{
    const QVector<ClangBackEnd::DiagnosticContainer> diagnostics
            = m_diagnosticManager.diagnosticsAt(line, column);
250
    auto *tooltipWidget = new ClangDiagnosticToolTipWidget(diagnostics, parent);
251

252
    ::Utils::ToolTip::show(point, tooltipWidget, parent);
253 254
}

255
ClangBackEnd::FileContainer ClangEditorDocumentProcessor::fileContainerWithArguments() const
Marco Bubke's avatar
Marco Bubke committed
256
{
257
    return fileContainerWithArguments(m_projectPart.data());
Marco Bubke's avatar
Marco Bubke committed
258 259
}

260 261 262 263 264
void ClangEditorDocumentProcessor::clearDiagnosticsWithFixIts()
{
    m_diagnosticManager.clearDiagnosticsWithFixIts();
}

265 266
ClangEditorDocumentProcessor *ClangEditorDocumentProcessor::get(const QString &filePath)
{
Marco Bubke's avatar
Marco Bubke committed
267 268 269
    auto *processor = CppTools::CppToolsBridge::baseEditorDocumentProcessor(filePath);

    return qobject_cast<ClangEditorDocumentProcessor*>(processor);
270 271
}

272 273 274
static bool isProjectPartLoadedOrIsFallback(CppTools::ProjectPart::Ptr projectPart)
{
    return projectPart
275
        && (projectPart->id().isEmpty() || ClangCodeModel::Utils::isProjectPartLoaded(projectPart));
276 277
}

278
void ClangEditorDocumentProcessor::updateProjectPartAndTranslationUnitForEditor()
279
{
280
    const CppTools::ProjectPart::Ptr projectPart = m_parser->projectPart();
281

282
    if (isProjectPartLoadedOrIsFallback(projectPart)) {
283
        registerTranslationUnitForEditor(projectPart.data());
Marco Bubke's avatar
Marco Bubke committed
284

285 286
        m_projectPart = projectPart;
    }
287 288
}

289 290 291 292 293
void ClangEditorDocumentProcessor::onParserFinished()
{
    if (revision() != m_parserRevision)
        return;

294
    updateProjectPartAndTranslationUnitForEditor();
295 296
}

297
void ClangEditorDocumentProcessor::registerTranslationUnitForEditor(CppTools::ProjectPart *projectPart)
298 299
{
    if (m_projectPart) {
Marco Bubke's avatar
Marco Bubke committed
300
        if (projectPart->id() != m_projectPart->id()) {
301 302
            m_ipcCommunicator.unregisterTranslationUnitsForEditor({fileContainerWithArguments()});
            m_ipcCommunicator.registerTranslationUnitsForEditor({fileContainerWithArguments(projectPart)});
303 304
        }
    } else {
305
        m_ipcCommunicator.registerTranslationUnitsForEditor({{fileContainerWithArguments(projectPart)}});
Marco Bubke's avatar
Marco Bubke committed
306 307 308
    }
}

309
void ClangEditorDocumentProcessor::updateTranslationUnitIfProjectPartExists()
Marco Bubke's avatar
Marco Bubke committed
310
{
311 312
    if (m_projectPart) {
        const ClangBackEnd::FileContainer fileContainer = fileContainerWithDocumentContent(m_projectPart->id());
Marco Bubke's avatar
Marco Bubke committed
313

314
        m_ipcCommunicator.updateTranslationUnitWithRevisionCheck(fileContainer);
Marco Bubke's avatar
Marco Bubke committed
315 316 317
    }
}

318
void ClangEditorDocumentProcessor::requestDocumentAnnotations(const QString &projectpartId)
319
{
320
    const auto fileContainer = fileContainerWithDocumentContent(projectpartId);
321

322 323
    m_ipcCommunicator.requestDiagnostics(fileContainer);
    m_ipcCommunicator.requestHighlighting(fileContainer);
324 325
}

326 327 328 329 330 331 332 333 334
static CppTools::ProjectPart projectPartForLanguageOption(CppTools::ProjectPart *projectPart)
{
    if (projectPart)
        return *projectPart;
    return *CppTools::CppModelManager::instance()->fallbackProjectPart().data();
}

static QStringList languageOptions(const QString &filePath, CppTools::ProjectPart *projectPart)
{
335
    const auto theProjectPart = projectPartForLanguageOption(projectPart);
336 337 338 339 340 341
    CppTools::CompilerOptionsBuilder builder(theProjectPart);
    builder.addLanguageOption(CppTools::ProjectFile::classify(filePath));

    return builder.options();
}

342 343 344 345 346 347
static QStringList fileArguments(const QString &filePath, CppTools::ProjectPart *projectPart)
{
    return QStringList(languageOptions(filePath, projectPart))
         + CppTools::codeModelSettings()->extraClangOptions();
}

Marco Bubke's avatar
Marco Bubke committed
348
ClangBackEnd::FileContainer
349
ClangEditorDocumentProcessor::fileContainerWithArguments(CppTools::ProjectPart *projectPart) const
Marco Bubke's avatar
Marco Bubke committed
350
{
351 352 353
    const auto projectPartId = projectPart
            ? Utf8String::fromString(projectPart->id())
            : Utf8String();
354
    const QStringList theFileArguments = fileArguments(filePath(), projectPart);
Marco Bubke's avatar
Marco Bubke committed
355

356
    return {filePath(), projectPartId, Utf8StringVector(theFileArguments), revision()};
Marco Bubke's avatar
Marco Bubke committed
357 358
}

359 360 361 362 363
ClangBackEnd::FileContainer
ClangEditorDocumentProcessor::fileContainerWithDocumentContent(const QString &projectpartId) const
{
    return ClangBackEnd::FileContainer(filePath(),
                                       projectpartId,
364
                                       textDocument()->toPlainText(),
365 366 367 368
                                       true,
                                       revision());
}

369
} // namespace Internal
370
} // namespace ClangCodeModel