clangeditordocumentprocessor.cpp 6.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** 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
** a written agreement between you and Digia.  For licensing terms and
Tobias Hunger's avatar
Tobias Hunger committed
13
14
** conditions see http://www.qt.io/licensing.  For further information
** 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "clangeditordocumentprocessor.h"

#include "cppcreatemarkers.h"
#include "diagnostic.h"
#include "pchinfo.h"

#include <cpptools/cpptoolsplugin.h>
#include <cpptools/cppworkingcopy.h>

#include <cplusplus/CppDocument.h>

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

45
46
47
#include <QLoggingCategory>

static Q_LOGGING_CATEGORY(log, "qtc.clangcodemodel.clangeditordocumentprocessor")
48
49
50
51
52
53
54
55
56
57
58
59

namespace {

typedef CPlusPlus::Document::DiagnosticMessage CppToolsDiagnostic;
QList<CppToolsDiagnostic> toCppToolsDiagnostics(
        const QString &filePath,
        const QList<ClangCodeModel::Diagnostic> &diagnostics)
{
    using namespace ClangCodeModel;

    QList<CppToolsDiagnostic> converted;
    foreach (const ClangCodeModel::Diagnostic &d, diagnostics) {
60
        qCDebug(log) << "diagnostic" << d.severityAsString() << d.location() << d.spelling();
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

        if (d.location().fileName() != filePath)
            continue;

        // TODO: retrieve fix-its for this diagnostic

        int level;
        switch (d.severity()) {
        case Diagnostic::Fatal: level = CppToolsDiagnostic::Fatal; break;
        case Diagnostic::Error: level = CppToolsDiagnostic::Error; break;
        case Diagnostic::Warning: level = CppToolsDiagnostic::Warning; break;
        default: continue;
        }
        converted.append(CppToolsDiagnostic(level, d.location().fileName(), d.location().line(),
                                           d.location().column(), d.spelling(), d.length()));
    }

    return converted;
}

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 {

95
ClangEditorDocumentProcessor::ClangEditorDocumentProcessor(TextEditor::TextDocument *document)
96
    : BaseEditorDocumentProcessor(document)
97
    , m_parser(document->filePath())
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
    , 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);

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

114
            CreateMarkers *createMarkers = CreateMarkers::create(m_parser.semanticMarker(),
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
                                                                 baseTextDocument()->filePath(),
                                                                 firstLine, lastLine);
            return createMarkers->start();
        });
}

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

void ClangEditorDocumentProcessor::run()
{
    // Run clang parser
    const CppTools::WorkingCopy workingCopy
131
        = CppTools::CppModelManager::instance()->workingCopy();
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159

    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);
    const QFuture<void> future = QtConcurrent::run(&runParser, parser(), workingCopy);
    m_parserWatcher.setFuture(future);

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

void ClangEditorDocumentProcessor::semanticRehighlight(bool force)
{
    m_builtinProcessor.semanticRehighlight(force);
}

CppTools::SemanticInfo ClangEditorDocumentProcessor::recalculateSemanticInfo()
{
    return m_builtinProcessor.recalculateSemanticInfo();
}

CppTools::BaseEditorDocumentParser *ClangEditorDocumentProcessor::parser()
{
160
    return &m_parser;
161
162
}

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

168
169
170
171
172
173
174
175
176
177
178
bool ClangEditorDocumentProcessor::isParserRunning() const
{
    return m_parserWatcher.isRunning();
}

void ClangEditorDocumentProcessor::onParserFinished()
{
    if (revision() != m_parserRevision)
        return;

    // Emit ifdefed out blocks
179
    const auto ifdefoutBlocks = toTextEditorBlocks(m_parser.ifdefedOutBlocks());
180
181
182
    emit ifdefedOutBlocksUpdated(revision(), ifdefoutBlocks);

    // Emit code warnings
183
    const auto diagnostics = toCppToolsDiagnostics(filePath(), m_parser.diagnostics());
184
185
186
187
188
189
190
191
    const auto codeWarnings = toTextEditorSelections(diagnostics, textDocument());
    emit codeWarningsUpdated(revision(), codeWarnings);

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

} // namespace ClangCodeModel