clangeditordocumentprocessor.cpp 11.7 KB
Newer Older
1 2
/****************************************************************************
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://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
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Tobias Hunger's avatar
Tobias Hunger committed
14
** use the contact form at http://www.qt.io/contact-us.
15 16 17
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
Tobias Hunger's avatar
Tobias Hunger committed
18 19 20 21 22 23
** 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.
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
27 28 29 30 31 32
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "clangeditordocumentprocessor.h"

33 34
#include "clangfixitoperation.h"
#include "clangfixitoperationsextractor.h"
35 36
#include "clangmodelmanagersupport.h"
#include "clangutils.h"
37 38 39 40
#include "cppcreatemarkers.h"
#include "diagnostic.h"
#include "pchinfo.h"

Marco Bubke's avatar
Marco Bubke committed
41 42 43
#include <diagnosticcontainer.h>
#include <sourcelocationcontainer.h>

44
#include <cpptools/cppprojects.h>
45 46 47
#include <cpptools/cpptoolsplugin.h>
#include <cpptools/cppworkingcopy.h>

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

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

#include <utils/qtcassert.h>
#include <utils/QtConcurrentTools>

Marco Bubke's avatar
Marco Bubke committed
59 60
#include <QTextBlock>

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
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 {
78
namespace Internal {
79

80 81 82
ClangEditorDocumentProcessor::ClangEditorDocumentProcessor(
        ModelManagerSupportClang *modelManagerSupport,
        TextEditor::TextDocument *document)
83
    : BaseEditorDocumentProcessor(document)
84
    , m_diagnosticManager(document)
85
    , m_modelManagerSupport(modelManagerSupport)
86
    , m_parser(new ClangEditorDocumentParser(document->filePath().toString()))
87 88 89 90
    , m_parserRevision(0)
    , m_semanticHighlighter(document)
    , m_builtinProcessor(document, /*enableSemanticHighlighter=*/ false)
{
91 92 93
    connect(m_parser.data(), &ClangEditorDocumentParser::projectPartDetermined,
            this, &ClangEditorDocumentProcessor::onParserDeterminedProjectPart);

94 95 96 97 98 99 100 101 102 103 104 105
    // 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);

    m_semanticHighlighter.setHighlightingRunner(
        [this]() -> QFuture<TextEditor::HighlightingResult> {
            const int firstLine = 1;
            const int lastLine = baseTextDocument()->document()->blockCount();

106
            CreateMarkers *createMarkers = CreateMarkers::create(m_parser->semanticMarker(),
107
                                                                 baseTextDocument()->filePath().toString(),
108 109 110 111 112 113 114 115 116
                                                                 firstLine, lastLine);
            return createMarkers->start();
        });
}

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

118 119
    if (m_projectPart) {
        QTC_ASSERT(m_modelManagerSupport, return);
120
        m_modelManagerSupport->ipcCommunicator().unregisterTranslationUnitsForEditor(
121 122
            {ClangBackEnd::FileContainer(filePath(), m_projectPart->id())});
    }
123 124 125 126
}

void ClangEditorDocumentProcessor::run()
{
Marco Bubke's avatar
Marco Bubke committed
127 128
    requestDiagnostics();

129 130 131 132 133 134 135 136 137
    // 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);
138 139 140
    const QFuture<void> future = QtConcurrent::run(&runParser,
                                                   parser(),
                                                   ClangEditorDocumentParser::InMemoryInfo(true));
141 142 143 144 145 146
    m_parserWatcher.setFuture(future);

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

147
void ClangEditorDocumentProcessor::recalculateSemanticInfoDetached(bool force)
148
{
149
    m_builtinProcessor.recalculateSemanticInfoDetached(force);
150 151
}

152 153 154 155 156 157
void ClangEditorDocumentProcessor::semanticRehighlight()
{
    m_semanticHighlighter.updateFormatMapFromFontSettings();
    m_semanticHighlighter.run();
}

158 159 160 161 162
CppTools::SemanticInfo ClangEditorDocumentProcessor::recalculateSemanticInfo()
{
    return m_builtinProcessor.recalculateSemanticInfo();
}

163
CppTools::BaseEditorDocumentParser::Ptr ClangEditorDocumentProcessor::parser()
164
{
165
    return m_parser;
166 167
}

168 169 170 171 172
CPlusPlus::Snapshot ClangEditorDocumentProcessor::snapshot()
{
   return m_builtinProcessor.snapshot();
}

173 174 175 176 177
bool ClangEditorDocumentProcessor::isParserRunning() const
{
    return m_parserWatcher.isRunning();
}

178 179 180 181 182
bool ClangEditorDocumentProcessor::hasProjectPart() const
{
    return m_projectPart;
}

183 184 185 186 187
CppTools::ProjectPart::Ptr ClangEditorDocumentProcessor::projectPart() const
{
    return m_projectPart;
}

188 189 190 191 192
void ClangEditorDocumentProcessor::clearProjectPart()
{
    m_projectPart.clear();
}

Marco Bubke's avatar
Marco Bubke committed
193 194 195 196
void ClangEditorDocumentProcessor::updateCodeWarnings(const QVector<ClangBackEnd::DiagnosticContainer> &diagnostics,
                                                      uint documentRevision)
{
    if (documentRevision == revision()) {
197 198
        m_diagnosticManager.processNewDiagnostics(diagnostics);
        const auto codeWarnings = m_diagnosticManager.takeExtraSelections();
Marco Bubke's avatar
Marco Bubke committed
199 200 201 202
        emit codeWarningsUpdated(revision(), codeWarnings);
    }
}

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
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));
}

221 222 223 224 225
ClangBackEnd::FileContainer ClangEditorDocumentProcessor::fileContainer() const
{
    return fileContainer(m_projectPart.data());
}

226 227 228 229 230
ClangEditorDocumentProcessor *ClangEditorDocumentProcessor::get(const QString &filePath)
{
    return qobject_cast<ClangEditorDocumentProcessor *>(BaseEditorDocumentProcessor::get(filePath));
}

231 232 233
static bool isProjectPartLoadedOrIsFallback(CppTools::ProjectPart::Ptr projectPart)
{
    return projectPart
234
        && (projectPart->id().isEmpty() || ClangCodeModel::Utils::isProjectPartLoaded(projectPart));
235 236
}

237 238
void ClangEditorDocumentProcessor::updateProjectPartAndTranslationUnitForEditor(
        CppTools::ProjectPart::Ptr projectPart)
239
{
240
    QTC_ASSERT(projectPart, return);
241

242
    if (isProjectPartLoadedOrIsFallback(projectPart)) {
243 244
        updateTranslationUnitForEditor(projectPart.data());
        requestDiagnostics(projectPart.data());
Marco Bubke's avatar
Marco Bubke committed
245

246 247
        m_projectPart = projectPart;
    }
248 249
}

250 251 252 253 254 255
void ClangEditorDocumentProcessor::onParserDeterminedProjectPart(
        CppTools::ProjectPart::Ptr projectPart)
{
    updateProjectPartAndTranslationUnitForEditor(projectPart);
}

256 257 258 259 260 261
void ClangEditorDocumentProcessor::onParserFinished()
{
    if (revision() != m_parserRevision)
        return;

    // Emit ifdefed out blocks
262
    const auto ifdefoutBlocks = toTextEditorBlocks(m_parser->ifdefedOutBlocks());
263 264 265 266
    emit ifdefedOutBlocksUpdated(revision(), ifdefoutBlocks);

    // Run semantic highlighter
    m_semanticHighlighter.run();
267 268
}

269
void ClangEditorDocumentProcessor::updateTranslationUnitForEditor(CppTools::ProjectPart *projectPart)
270 271 272 273 274
{
    QTC_ASSERT(m_modelManagerSupport, return);
    IpcCommunicator &ipcCommunicator = m_modelManagerSupport->ipcCommunicator();

    if (m_projectPart) {
275 276 277
        if (projectPart->id() != m_projectPart->id()) {
            ipcCommunicator.unregisterTranslationUnitsForEditor({fileContainer()});
            ipcCommunicator.registerTranslationUnitsForEditor({fileContainer(projectPart)});
278 279
        }
    } else {
280
        ipcCommunicator.registerTranslationUnitsForEditor({{fileContainer(projectPart)}});
Marco Bubke's avatar
Marco Bubke committed
281 282 283
    }
}

284
void ClangEditorDocumentProcessor::requestDiagnostics(CppTools::ProjectPart *projectPart)
Marco Bubke's avatar
Marco Bubke committed
285
{
286
    if (!m_projectPart || projectPart->id() != m_projectPart->id()) {
Marco Bubke's avatar
Marco Bubke committed
287 288
        IpcCommunicator &ipcCommunicator = m_modelManagerSupport->ipcCommunicator();

289
        ipcCommunicator.requestDiagnostics({fileContainer(projectPart)});
Marco Bubke's avatar
Marco Bubke committed
290 291 292 293 294 295 296 297 298
    }
}

void ClangEditorDocumentProcessor::requestDiagnostics()
{
    // Get diagnostics
    if (m_projectPart) {
        auto  &ipcCommunicator = m_modelManagerSupport->ipcCommunicator();
        ipcCommunicator.requestDiagnostics({filePath(),
299 300 301 302
                                            m_projectPart->id(),
                                            baseTextDocument()->plainText(),
                                            true,
                                            revision()});
303
    }
304 305
}

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
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)
{
    const auto theProjectPart = CppTools::ProjectPart::Ptr(
                new CppTools::ProjectPart(projectPartForLanguageOption(projectPart)));
    CppTools::CompilerOptionsBuilder builder(theProjectPart);
    builder.addLanguageOption(CppTools::ProjectFile::classify(filePath));

    return builder.options();
}

323 324 325
ClangBackEnd::FileContainer
ClangEditorDocumentProcessor::fileContainer(CppTools::ProjectPart *projectPart) const
{
326 327 328 329
    const auto projectPartId = projectPart
            ? Utf8String::fromString(projectPart->id())
            : Utf8String();
    const auto theLanguageOption = languageOptions(filePath(), projectPart);
330

331
    return {filePath(), projectPartId, Utf8StringVector(theLanguageOption), revision()};
332 333
}

334
} // namespace Internal
335
} // namespace ClangCodeModel