clangeditordocumentprocessor.cpp 20.2 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 "clangprojectsettings.h"
34
#include "clangutils.h"
35

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

39 40
#include <cpptools/clangdiagnosticconfigsmodel.h>
#include <cpptools/clangdiagnosticconfigsmodel.h>
41
#include <cpptools/compileroptionsbuilder.h>
42
#include <cpptools/cppcodemodelsettings.h>
43
#include <cpptools/cppmodelmanager.h>
Marco Bubke's avatar
Marco Bubke committed
44
#include <cpptools/cpptoolsbridge.h>
45
#include <cpptools/cpptoolsreuse.h>
46
#include <cpptools/cppworkingcopy.h>
47
#include <cpptools/editordocumenthandle.h>
48

49
#include <texteditor/convenience.h>
50
#include <texteditor/fontsettings.h>
Marco Bubke's avatar
Marco Bubke committed
51
#include <texteditor/texteditor.h>
52 53
#include <texteditor/texteditorconstants.h>
#include <texteditor/texteditorsettings.h>
Marco Bubke's avatar
Marco Bubke committed
54

55 56 57
#include <cplusplus/CppDocument.h>

#include <utils/qtcassert.h>
58
#include <utils/runextensions.h>
59

Marco Bubke's avatar
Marco Bubke committed
60
#include <QTextBlock>
61 62
#include <QVBoxLayout>
#include <QWidget>
Marco Bubke's avatar
Marco Bubke committed
63

64
namespace ClangCodeModel {
65
namespace Internal {
66

67
ClangEditorDocumentProcessor::ClangEditorDocumentProcessor(
68
        IpcCommunicator &ipcCommunicator,
69
        TextEditor::TextDocument *document)
70
    : BaseEditorDocumentProcessor(document->document(), document->filePath().toString())
71
    , m_document(*document)
72
    , m_diagnosticManager(document)
73
    , m_ipcCommunicator(ipcCommunicator)
74
    , m_parser(new ClangEditorDocumentParser(document->filePath().toString()))
75 76 77 78
    , m_parserRevision(0)
    , m_semanticHighlighter(document)
    , m_builtinProcessor(document, /*enableSemanticHighlighter=*/ false)
{
79 80 81 82 83
    m_updateTranslationUnitTimer.setSingleShot(true);
    m_updateTranslationUnitTimer.setInterval(350);
    connect(&m_updateTranslationUnitTimer, &QTimer::timeout,
            this, &ClangEditorDocumentProcessor::updateTranslationUnitIfProjectPartExists);

84 85 86
    connect(m_parser.data(), &ClangEditorDocumentParser::projectPartInfoUpdated,
            this, &BaseEditorDocumentProcessor::projectPartInfoUpdated);

87 88 89 90 91 92 93 94 95 96
    // 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()
{
97 98
    m_updateTranslationUnitTimer.stop();

99 100
    m_parserWatcher.cancel();
    m_parserWatcher.waitForFinished();
101

102
    if (m_projectPart) {
103
        m_ipcCommunicator.unregisterTranslationUnitsForEditor(
104 105
            {ClangBackEnd::FileContainer(filePath(), m_projectPart->id())});
    }
106 107
}

108 109
void ClangEditorDocumentProcessor::runImpl(
        const CppTools::BaseEditorDocumentParser::UpdateParams &updateParams)
110
{
111
    m_updateTranslationUnitTimer.start();
Marco Bubke's avatar
Marco Bubke committed
112

113 114 115 116 117 118 119 120 121
    // 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);
122
    const QFuture<void> future = ::Utils::runAsync(&runParser, parser(), updateParams);
123 124 125
    m_parserWatcher.setFuture(future);

    // Run builtin processor
126
    m_builtinProcessor.runImpl(updateParams);
127 128
}

129
void ClangEditorDocumentProcessor::recalculateSemanticInfoDetached(bool force)
130
{
131
    m_builtinProcessor.recalculateSemanticInfoDetached(force);
132 133
}

134 135 136
void ClangEditorDocumentProcessor::semanticRehighlight()
{
    m_semanticHighlighter.updateFormatMapFromFontSettings();
137 138 139

    if (m_projectPart)
        requestDocumentAnnotations(m_projectPart->id());
140 141
}

142 143 144 145 146
CppTools::SemanticInfo ClangEditorDocumentProcessor::recalculateSemanticInfo()
{
    return m_builtinProcessor.recalculateSemanticInfo();
}

147
CppTools::BaseEditorDocumentParser::Ptr ClangEditorDocumentProcessor::parser()
148
{
149
    return m_parser;
150 151
}

152 153 154 155 156
CPlusPlus::Snapshot ClangEditorDocumentProcessor::snapshot()
{
   return m_builtinProcessor.snapshot();
}

157 158 159 160 161
bool ClangEditorDocumentProcessor::isParserRunning() const
{
    return m_parserWatcher.isRunning();
}

162 163 164 165 166
bool ClangEditorDocumentProcessor::hasProjectPart() const
{
    return m_projectPart;
}

167 168 169 170 171
CppTools::ProjectPart::Ptr ClangEditorDocumentProcessor::projectPart() const
{
    return m_projectPart;
}

172 173 174 175 176
void ClangEditorDocumentProcessor::clearProjectPart()
{
    m_projectPart.clear();
}

177 178 179 180
void ClangEditorDocumentProcessor::updateCodeWarnings(
        const QVector<ClangBackEnd::DiagnosticContainer> &diagnostics,
        const ClangBackEnd::DiagnosticContainer &firstHeaderErrorDiagnostic,
        uint documentRevision)
Marco Bubke's avatar
Marco Bubke committed
181 182
{
    if (documentRevision == revision()) {
183
        m_diagnosticManager.processNewDiagnostics(diagnostics, m_isProjectFile);
184
        const auto codeWarnings = m_diagnosticManager.takeExtraSelections();
185
        const auto fixitAvailableMarkers = m_diagnosticManager.takeFixItAvailableMarkers();
186 187 188 189 190 191
        const auto creator = creatorForHeaderErrorDiagnosticWidget(firstHeaderErrorDiagnostic);

        emit codeWarningsUpdated(revision(),
                                 codeWarnings,
                                 creator,
                                 fixitAvailableMarkers);
Marco Bubke's avatar
Marco Bubke committed
192 193
    }
}
194
namespace {
Marco Bubke's avatar
Marco Bubke committed
195

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
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)
215 216 217 218 219
{
    QList<TextEditor::BlockRange> blockRanges;
    blockRanges.reserve(ifdefedOutRanges.size());

    for (const auto &range : ifdefedOutRanges)
220
        blockRanges.append(toTextEditorBlock(textDocument, range));
221 222 223

    return blockRanges;
}
224
}
225 226 227 228 229 230 231

void ClangEditorDocumentProcessor::updateHighlighting(
        const QVector<ClangBackEnd::HighlightingMarkContainer> &highlightingMarks,
        const QVector<ClangBackEnd::SourceRangeContainer> &skippedPreprocessorRanges,
        uint documentRevision)
{
    if (documentRevision == revision()) {
232
        const auto skippedPreprocessorBlocks = toTextEditorBlocks(textDocument(), skippedPreprocessorRanges);
233 234 235 236 237 238 239 240 241 242 243
        emit ifdefedOutBlocksUpdated(documentRevision, skippedPreprocessorBlocks);

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

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
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));
}

262 263 264 265 266
bool ClangEditorDocumentProcessor::hasDiagnosticsAt(uint line, uint column) const
{
    return m_diagnosticManager.hasDiagnosticsAt(line, column);
}

267 268 269
void ClangEditorDocumentProcessor::addDiagnosticToolTipToLayout(uint line,
                                                                uint column,
                                                                QLayout *target) const
270
{
271 272 273 274 275 276
    using Internal::ClangDiagnosticWidget;

    const QVector<ClangBackEnd::DiagnosticContainer> diagnostics
        = m_diagnosticManager.diagnosticsAt(line, column);

    target->addWidget(ClangDiagnosticWidget::create(diagnostics, ClangDiagnosticWidget::ToolTip));
277 278
}

279 280 281 282 283
void ClangEditorDocumentProcessor::editorDocumentTimerRestarted()
{
    m_updateTranslationUnitTimer.stop(); // Wait for the next call to run().
}

284 285 286 287 288
void ClangEditorDocumentProcessor::invalidateDiagnostics()
{
    m_diagnosticManager.invalidateDiagnostics();
}

289 290 291 292 293 294 295
void ClangEditorDocumentProcessor::setParserConfig(
        const CppTools::BaseEditorDocumentParser::Configuration config)
{
    m_parser->setConfiguration(config);
    m_builtinProcessor.parser()->setConfiguration(config);
}

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
static bool isCursorOnIdentifier(const QTextCursor &textCursor)
{
    QTextDocument *document = textCursor.document();
    return CppTools::isValidIdentifierChar(document->characterAt(textCursor.position()));
}

static QFuture<CppTools::CursorInfo> defaultCursorInfoFuture()
{
    QFutureInterface<CppTools::CursorInfo> futureInterface;
    futureInterface.reportResult(CppTools::CursorInfo());
    futureInterface.reportFinished();

    return futureInterface.future();
}

static bool convertPosition(const QTextCursor &textCursor, int *line, int *column)
{
    const bool converted = TextEditor::Convenience::convertPosition(textCursor.document(),
                                                                    textCursor.position(),
                                                                    line,
                                                                    column);
    QTC_CHECK(converted);
    return converted;
}

321 322 323
QFuture<CppTools::CursorInfo>
ClangEditorDocumentProcessor::cursorInfo(const CppTools::CursorInfoParams &params)
{
324 325 326 327 328 329 330 331 332 333 334 335 336 337
    int line, column;
    convertPosition(params.textCursor, &line, &column);
    ++column; // for 1-based columns

    if (!isCursorOnIdentifier(params.textCursor))
        return defaultCursorInfoFuture();

    const QTextBlock block = params.textCursor.document()->findBlockByNumber(line - 1);
    column += ClangCodeModel::Utils::extraUtf8CharsShift(block.text(), column);

    return m_ipcCommunicator.requestReferences(simpleFileContainer(),
                                               static_cast<quint32>(line),
                                               static_cast<quint32>(column),
                                               textDocument());
338 339
}

340
ClangBackEnd::FileContainer ClangEditorDocumentProcessor::fileContainerWithArguments() const
Marco Bubke's avatar
Marco Bubke committed
341
{
342
    return fileContainerWithArguments(m_projectPart.data());
Marco Bubke's avatar
Marco Bubke committed
343 344
}

345 346 347 348 349
void ClangEditorDocumentProcessor::clearDiagnosticsWithFixIts()
{
    m_diagnosticManager.clearDiagnosticsWithFixIts();
}

350 351
ClangEditorDocumentProcessor *ClangEditorDocumentProcessor::get(const QString &filePath)
{
Marco Bubke's avatar
Marco Bubke committed
352 353 354
    auto *processor = CppTools::CppToolsBridge::baseEditorDocumentProcessor(filePath);

    return qobject_cast<ClangEditorDocumentProcessor*>(processor);
355 356
}

357 358 359
static bool isProjectPartLoadedOrIsFallback(CppTools::ProjectPart::Ptr projectPart)
{
    return projectPart
360
        && (projectPart->id().isEmpty() || ClangCodeModel::Utils::isProjectPartLoaded(projectPart));
361 362
}

363
void ClangEditorDocumentProcessor::updateProjectPartAndTranslationUnitForEditor()
364
{
365
    const CppTools::ProjectPart::Ptr projectPart = m_parser->projectPartInfo().projectPart;
366

367
    if (isProjectPartLoadedOrIsFallback(projectPart)) {
368
        registerTranslationUnitForEditor(projectPart.data());
Marco Bubke's avatar
Marco Bubke committed
369

370
        m_projectPart = projectPart;
371 372
        m_isProjectFile = m_parser->projectPartInfo().hints
                & CppTools::ProjectPartInfo::IsFromProjectMatch;
373
    }
374 375
}

376 377 378 379 380
void ClangEditorDocumentProcessor::onParserFinished()
{
    if (revision() != m_parserRevision)
        return;

381
    updateProjectPartAndTranslationUnitForEditor();
382 383
}

384
void ClangEditorDocumentProcessor::registerTranslationUnitForEditor(CppTools::ProjectPart *projectPart)
385
{
386 387 388 389 390 391 392 393 394
    // On registration we send the document content immediately as an unsaved
    // file, because
    //   (1) a refactoring action might have opened and already modified
    //       this document.
    //   (2) it prevents an extra preamble generation on first user
    //       modification of the document in case the line endings on disk
    //       differ from the ones returned by textDocument()->toPlainText(),
    //       like on Windows.

395
    if (m_projectPart) {
396 397 398
        if (projectPart->id() == m_projectPart->id())
            return;
        m_ipcCommunicator.unregisterTranslationUnitsForEditor({fileContainerWithArguments()});
Marco Bubke's avatar
Marco Bubke committed
399
    }
400 401 402 403

    m_ipcCommunicator.registerTranslationUnitsForEditor(
        {fileContainerWithArgumentsAndDocumentContent(projectPart)});
    ClangCodeModel::Utils::setLastSentDocumentRevision(filePath(), revision());
Marco Bubke's avatar
Marco Bubke committed
404 405
}

406
void ClangEditorDocumentProcessor::updateTranslationUnitIfProjectPartExists()
Marco Bubke's avatar
Marco Bubke committed
407
{
408 409
    if (m_projectPart) {
        const ClangBackEnd::FileContainer fileContainer = fileContainerWithDocumentContent(m_projectPart->id());
Marco Bubke's avatar
Marco Bubke committed
410

411
        m_ipcCommunicator.updateTranslationUnitWithRevisionCheck(fileContainer);
Marco Bubke's avatar
Marco Bubke committed
412 413 414
    }
}

415
void ClangEditorDocumentProcessor::requestDocumentAnnotations(const QString &projectpartId)
416
{
417
    const auto fileContainer = fileContainerWithDocumentContent(projectpartId);
418

419
    m_ipcCommunicator.requestDocumentAnnotations(fileContainer);
420 421
}

422 423 424 425 426 427 428 429 430 431 432 433 434
CppTools::BaseEditorDocumentProcessor::HeaderErrorDiagnosticWidgetCreator
ClangEditorDocumentProcessor::creatorForHeaderErrorDiagnosticWidget(
        const ClangBackEnd::DiagnosticContainer &firstHeaderErrorDiagnostic)
{
    if (firstHeaderErrorDiagnostic.text().isEmpty())
        return CppTools::BaseEditorDocumentProcessor::HeaderErrorDiagnosticWidgetCreator();

    return [firstHeaderErrorDiagnostic]() {
        auto vbox = new QVBoxLayout;
        vbox->setMargin(0);
        vbox->setContentsMargins(10, 0, 0, 2);
        vbox->setSpacing(2);

435 436
        vbox->addWidget(ClangDiagnosticWidget::create({firstHeaderErrorDiagnostic},
                                                      ClangDiagnosticWidget::InfoBar));
437 438 439 440 441 442 443 444

        auto widget = new QWidget;
        widget->setLayout(vbox);

        return widget;
    };
}

445 446 447 448 449 450 451 452 453
ClangBackEnd::FileContainer ClangEditorDocumentProcessor::simpleFileContainer() const
{
    Utf8String projectPartId;
    if (m_projectPart)
        projectPartId = m_projectPart->id();

    return ClangBackEnd::FileContainer(filePath(), projectPartId, Utf8String(), false, revision());
}

454 455 456 457 458 459 460 461 462
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)
{
463
    const auto theProjectPart = projectPartForLanguageOption(projectPart);
464 465 466 467 468 469 470 471 472

    // Determine file kind with respect to ambiguous headers.
    CppTools::ProjectFile::Kind fileKind = CppTools::ProjectFile::classify(filePath);
    if (fileKind == CppTools::ProjectFile::AmbiguousHeader) {
        fileKind = theProjectPart.languageVersion <= CppTools::ProjectPart::LatestCVersion
             ? CppTools::ProjectFile::CHeader
             : CppTools::ProjectFile::CXXHeader;
    }

473
    CppTools::CompilerOptionsBuilder builder(theProjectPart);
474
    builder.addLanguageOption(fileKind);
475 476 477 478

    return builder.options();
}

479 480 481 482
static QStringList warningOptions(CppTools::ProjectPart *projectPart)
{
    if (projectPart && projectPart->project) {
        ClangProjectSettings projectSettings(projectPart->project);
483
        if (!projectSettings.useGlobalConfig()) {
484 485 486 487
            const Core::Id warningConfigId = projectSettings.warningConfigId();
            const CppTools::ClangDiagnosticConfigsModel configsModel(
                        CppTools::codeModelSettings()->clangCustomDiagnosticConfigs());
            if (configsModel.hasConfigWithId(warningConfigId))
488
                return configsModel.configWithId(warningConfigId).commandLineWarnings();
489 490 491
        }
    }

492 493 494 495 496 497 498 499 500
    return CppTools::codeModelSettings()->clangDiagnosticConfig().commandLineWarnings();
}

static QStringList commandLineOptions(CppTools::ProjectPart *projectPart)
{
    if (!projectPart || !projectPart->project)
        return ClangProjectSettings::globalCommandLineOptions();

    return ClangProjectSettings{projectPart->project}.commandLineOptions();
501 502
}

503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
static QStringList precompiledHeaderOptions(
        const QString& filePath,
        CppTools::ProjectPart *projectPart)
{
    using namespace CppTools;

    if (CppTools::getPchUsage() == CompilerOptionsBuilder::PchUsage::None)
        return QStringList();

    if (projectPart->precompiledHeaders.contains(filePath))
        return QStringList();

    const CppTools::ProjectPart theProjectPart = projectPartForLanguageOption(projectPart);
    CppTools::CompilerOptionsBuilder builder(theProjectPart);
    builder.addPrecompiledHeaderOptions(CompilerOptionsBuilder::PchUsage::Use);

    return builder.options();
}

522 523
static QStringList fileArguments(const QString &filePath, CppTools::ProjectPart *projectPart)
{
524 525
    return languageOptions(filePath, projectPart)
            + warningOptions(projectPart)
526
            + commandLineOptions(projectPart)
527
            + precompiledHeaderOptions(filePath, projectPart);
528 529
}

Marco Bubke's avatar
Marco Bubke committed
530
ClangBackEnd::FileContainer
531
ClangEditorDocumentProcessor::fileContainerWithArguments(CppTools::ProjectPart *projectPart) const
Marco Bubke's avatar
Marco Bubke committed
532
{
533 534 535
    const auto projectPartId = projectPart
            ? Utf8String::fromString(projectPart->id())
            : Utf8String();
536
    const QStringList theFileArguments = fileArguments(filePath(), projectPart);
Marco Bubke's avatar
Marco Bubke committed
537

538
    return {filePath(), projectPartId, Utf8StringVector(theFileArguments), revision()};
Marco Bubke's avatar
Marco Bubke committed
539 540
}

541 542 543 544 545 546 547 548 549 550 551 552 553 554
ClangBackEnd::FileContainer
ClangEditorDocumentProcessor::fileContainerWithArgumentsAndDocumentContent(
        CppTools::ProjectPart *projectPart) const
{
    const QStringList theFileArguments = fileArguments(filePath(), projectPart);

    return ClangBackEnd::FileContainer(filePath(),
                                       projectPart->id(),
                                       Utf8StringVector(theFileArguments),
                                       textDocument()->toPlainText(),
                                       true,
                                       revision());
}

555 556 557 558 559
ClangBackEnd::FileContainer
ClangEditorDocumentProcessor::fileContainerWithDocumentContent(const QString &projectpartId) const
{
    return ClangBackEnd::FileContainer(filePath(),
                                       projectpartId,
560
                                       textDocument()->toPlainText(),
561 562 563 564
                                       true,
                                       revision());
}

565
} // namespace Internal
566
} // namespace ClangCodeModel