clangeditordocumentprocessor.cpp 10.1 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 "clangmodelmanagersupport.h"
#include "clangutils.h"
35 36 37 38
#include "cppcreatemarkers.h"
#include "diagnostic.h"
#include "pchinfo.h"

Marco Bubke's avatar
Marco Bubke committed
39 40 41
#include <diagnosticcontainer.h>
#include <sourcelocationcontainer.h>

42 43 44
#include <cpptools/cpptoolsplugin.h>
#include <cpptools/cppworkingcopy.h>

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

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

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

Marco Bubke's avatar
Marco Bubke committed
55 56
#include <QTextBlock>

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
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 {
74
namespace Internal {
75

76 77 78
ClangEditorDocumentProcessor::ClangEditorDocumentProcessor(
        ModelManagerSupportClang *modelManagerSupport,
        TextEditor::TextDocument *document)
79
    : BaseEditorDocumentProcessor(document)
80
    , m_diagnosticManager(document)
81
    , m_modelManagerSupport(modelManagerSupport)
82
    , m_parser(new ClangEditorDocumentParser(document->filePath().toString()))
83 84 85 86 87 88 89 90 91 92 93
    , 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);

94 95 96
    connect(CppTools::CppModelManager::instance(), &CppTools::CppModelManager::projectPartsRemoved,
            this, &ClangEditorDocumentProcessor::onProjectPartsRemoved);

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

102
            CreateMarkers *createMarkers = CreateMarkers::create(m_parser->semanticMarker(),
103
                                                                 baseTextDocument()->filePath().toString(),
104 105 106 107 108 109 110 111 112
                                                                 firstLine, lastLine);
            return createMarkers->start();
        });
}

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

114 115
    if (m_projectPart) {
        QTC_ASSERT(m_modelManagerSupport, return);
116
        m_modelManagerSupport->ipcCommunicator().unregisterTranslationUnitsForEditor(
117 118
            {ClangBackEnd::FileContainer(filePath(), m_projectPart->id())});
    }
119 120 121 122
}

void ClangEditorDocumentProcessor::run()
{
Marco Bubke's avatar
Marco Bubke committed
123 124
    requestDiagnostics();

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

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

143
void ClangEditorDocumentProcessor::recalculateSemanticInfoDetached(bool force)
144
{
145
    m_builtinProcessor.recalculateSemanticInfoDetached(force);
146 147
}

148 149 150 151 152 153
void ClangEditorDocumentProcessor::semanticRehighlight()
{
    m_semanticHighlighter.updateFormatMapFromFontSettings();
    m_semanticHighlighter.run();
}

154 155 156 157 158
CppTools::SemanticInfo ClangEditorDocumentProcessor::recalculateSemanticInfo()
{
    return m_builtinProcessor.recalculateSemanticInfo();
}

159
CppTools::BaseEditorDocumentParser::Ptr ClangEditorDocumentProcessor::parser()
160
{
161
    return m_parser;
162 163
}

164 165 166 167 168
CPlusPlus::Snapshot ClangEditorDocumentProcessor::snapshot()
{
   return m_builtinProcessor.snapshot();
}

169 170 171 172 173
bool ClangEditorDocumentProcessor::isParserRunning() const
{
    return m_parserWatcher.isRunning();
}

174 175 176 177 178
CppTools::ProjectPart::Ptr ClangEditorDocumentProcessor::projectPart() const
{
    return m_projectPart;
}

Marco Bubke's avatar
Marco Bubke committed
179 180 181 182
void ClangEditorDocumentProcessor::updateCodeWarnings(const QVector<ClangBackEnd::DiagnosticContainer> &diagnostics,
                                                      uint documentRevision)
{
    if (documentRevision == revision()) {
183 184
        m_diagnosticManager.processNewDiagnostics(diagnostics);
        const auto codeWarnings = m_diagnosticManager.takeExtraSelections();
Marco Bubke's avatar
Marco Bubke committed
185 186 187 188
        emit codeWarningsUpdated(revision(), codeWarnings);
    }
}

189 190 191 192 193
ClangEditorDocumentProcessor *ClangEditorDocumentProcessor::get(const QString &filePath)
{
    return qobject_cast<ClangEditorDocumentProcessor *>(BaseEditorDocumentProcessor::get(filePath));
}

194
void ClangEditorDocumentProcessor::updateProjectPartAndTranslationUnitForEditor()
195
{
196
    const CppTools::ProjectPart::Ptr projectPart = m_parser->projectPart();
197 198
    QTC_ASSERT(projectPart, return);

199
    updateTranslationUnitForEditor(*projectPart.data());
Marco Bubke's avatar
Marco Bubke committed
200 201
    requestDiagnostics(*projectPart.data());

202 203 204
    m_projectPart = projectPart;
}

205 206 207 208 209 210
void ClangEditorDocumentProcessor::onParserFinished()
{
    if (revision() != m_parserRevision)
        return;

    // Emit ifdefed out blocks
211
    const auto ifdefoutBlocks = toTextEditorBlocks(m_parser->ifdefedOutBlocks());
212 213 214 215
    emit ifdefedOutBlocksUpdated(revision(), ifdefoutBlocks);

    // Run semantic highlighter
    m_semanticHighlighter.run();
216

217
    updateProjectPartAndTranslationUnitForEditor();
218 219 220 221 222 223 224 225
}

void ClangEditorDocumentProcessor::onProjectPartsRemoved(const QStringList &projectPartIds)
{
    if (m_projectPart && projectPartIds.contains(m_projectPart->id()))
        m_projectPart.clear();
}

226
void ClangEditorDocumentProcessor::updateTranslationUnitForEditor(CppTools::ProjectPart &projectPart)
227 228 229 230 231 232
{
    QTC_ASSERT(m_modelManagerSupport, return);
    IpcCommunicator &ipcCommunicator = m_modelManagerSupport->ipcCommunicator();

    if (m_projectPart) {
        if (projectPart.id() != m_projectPart->id()) {
233 234 235
            auto container1 = ClangBackEnd::FileContainer(filePath(),
                                                          m_projectPart->id(),
                                                          revision());
236
            ipcCommunicator.unregisterTranslationUnitsForEditor({container1});
237

238 239 240
            auto container2 = ClangBackEnd::FileContainer(filePath(),
                                                          projectPart.id(),
                                                          revision());
241
            ipcCommunicator.registerTranslationUnitsForEditor({container2});
242 243
        }
    } else {
244 245 246
        auto container = ClangBackEnd::FileContainer(filePath(),
                                                     projectPart.id(),
                                                     revision());
247
        ipcCommunicator.registerTranslationUnitsForEditor({container});
Marco Bubke's avatar
Marco Bubke committed
248 249 250 251 252 253 254 255
    }
}

void ClangEditorDocumentProcessor::requestDiagnostics(CppTools::ProjectPart &projectPart)
{
    if (!m_projectPart || projectPart.id() != m_projectPart->id()) {
        IpcCommunicator &ipcCommunicator = m_modelManagerSupport->ipcCommunicator();

256 257 258 259 260
        ipcCommunicator.requestDiagnostics({filePath(),
                                            projectPart.id(),
                                            Utf8String(),
                                            false,
                                            revision()});
Marco Bubke's avatar
Marco Bubke committed
261 262 263 264 265 266 267 268 269
    }
}

void ClangEditorDocumentProcessor::requestDiagnostics()
{
    // Get diagnostics
    if (m_projectPart) {
        auto  &ipcCommunicator = m_modelManagerSupport->ipcCommunicator();
        ipcCommunicator.requestDiagnostics({filePath(),
270 271 272 273
                                            m_projectPart->id(),
                                            baseTextDocument()->plainText(),
                                            true,
                                            revision()});
274
    }
275 276
}

277
} // namespace Internal
278
} // namespace ClangCodeModel