Commit 90f94363 authored by Nikolai Kosjar's avatar Nikolai Kosjar

Clang: Extract TranslationUnitCore and (re)parse explicitly

Extract TranslationUnitCore, which is the API for the high-level
operations we need. TranslationUnit does not call any clang_* functions
anymore, except the one needed for disposing the CXTranslationUnit - for
now, we keep TranslationUnit the owner of TranslationUnitCore.

TranslationUnitCore will be passed on to the worker threads.

With this, the current "TranslationUnit" looses its meaning. We will
rename it to "Document" in a follow-up change.

                            ***

TranslationUnit::cxTranslationUnit does not implicitly
creates/parses/reparses anymore. We use more verbose update operations
now.

The test ClangIpcServer.GetCodeCompletionDependingOnArgumets fails now
because of this - CodeCompleter::completeHelper() does not recreate the
translation unit anymore, thus working on the old data.

This will be addressed in a follow-up change.

Change-Id: I6213d6f1609cd3c9a54666c84cb8b623b2fefe1c
Reviewed-by: Tim Jenssen's avatarTim Jenssen <tim.jenssen@qt.io>
parent dc734f45
......@@ -37,7 +37,8 @@ HEADERS += $$PWD/clangcodemodelserver.h \
$$PWD/highlightingmarksiterator.h \
$$PWD/utf8positionfromlinecolumn.h \
$$PWD/clangfilepath.h \
$$PWD/clangunsavedfilesshallowarguments.h
$$PWD/clangunsavedfilesshallowarguments.h \
$$PWD/clangtranslationunitcore.h
SOURCES += $$PWD/clangcodemodelserver.cpp \
$$PWD/codecompleter.cpp \
......@@ -74,4 +75,5 @@ SOURCES += $$PWD/clangcodemodelserver.cpp \
$$PWD/highlightingmarks.cpp \
$$PWD/utf8positionfromlinecolumn.cpp \
$$PWD/clangfilepath.cpp \
$$PWD/clangunsavedfilesshallowarguments.cpp
$$PWD/clangunsavedfilesshallowarguments.cpp \
$$PWD/clangtranslationunitcore.cpp
......@@ -240,8 +240,10 @@ void ClangCodeModelServer::completeCode(const ClangBackEnd::CompleteCodeMessage
TIME_SCOPE_DURATION("ClangCodeModelServer::completeCode");
try {
CodeCompleter codeCompleter(translationUnits.translationUnit(message.filePath(), message.projectPartId()),
unsavedFiles);
auto translationUnit = translationUnits.translationUnit(message.filePath(), message.projectPartId());
auto translationUnitCore = translationUnit.translationUnitCore();
CodeCompleter codeCompleter(translationUnitCore, unsavedFiles);
const auto codeCompletions = codeCompleter.complete(message.line(), message.column());
......@@ -264,11 +266,13 @@ void ClangCodeModelServer::requestDocumentAnnotations(const RequestDocumentAnnot
try {
auto translationUnit = translationUnits.translationUnit(message.fileContainer().filePath(),
message.fileContainer().projectPartId());
auto translationUnitCore = translationUnit.translationUnitCore();
client()->documentAnnotationsChanged(DocumentAnnotationsChangedMessage(translationUnit.fileContainer(),
translationUnit.mainFileDiagnostics(),
translationUnit.highlightingMarks().toHighlightingMarksContainers(),
translationUnit.skippedSourceRanges().toSourceRangeContainers()));
client()->documentAnnotationsChanged(DocumentAnnotationsChangedMessage(
translationUnit.fileContainer(),
translationUnitCore.mainFileDiagnostics(),
translationUnitCore.highlightingMarks().toHighlightingMarksContainers(),
translationUnitCore.skippedSourceRanges().toSourceRangeContainers()));
} catch (const TranslationUnitDoesNotExistException &exception) {
client()->translationUnitDoesNotExist(TranslationUnitDoesNotExistMessage(exception.fileContainer()));
} catch (const ProjectPartDoNotExistException &exception) {
......
......@@ -42,6 +42,7 @@
#include "translationunitisnullexception.h"
#include "translationunitparseerrorexception.h"
#include "translationunitreparseerrorexception.h"
#include "clangtranslationunitcore.h"
#include "clangtranslationunitupdater.h"
#include "translationunits.h"
#include "unsavedfiles.h"
......@@ -148,11 +149,24 @@ void TranslationUnit::reset()
d.reset();
}
void TranslationUnit::parse() const
{
checkIfNull();
const TranslationUnitUpdateInput updateInput = createUpdateInput();
TranslationUnitUpdateResult result = translationUnitCore().parse(updateInput);
incorporateUpdaterResult(result);
}
void TranslationUnit::reparse() const
{
cxTranslationUnit();
parse(); // TODO: Remove
updateSynchronously(TranslationUnitUpdater::UpdateMode::ForceReparse);
const TranslationUnitUpdateInput updateInput = createUpdateInput();
TranslationUnitUpdateResult result = translationUnitCore().reparse(updateInput);
incorporateUpdaterResult(result);
}
bool TranslationUnit::parseWasSuccessful() const
......@@ -172,23 +186,11 @@ CXIndex &TranslationUnit::index() const
return d->index;
}
CXTranslationUnit TranslationUnit::cxTranslationUnit() const
{
checkIfNull();
checkIfFileExists();
updateSynchronously(TranslationUnitUpdater::UpdateMode::AsNeeded);
return d->translationUnit;
}
CXTranslationUnit TranslationUnit::cxTranslationUnitWithoutReparsing() const
CXTranslationUnit &TranslationUnit::cxTranslationUnit() const
{
checkIfNull();
checkIfFileExists();
updateSynchronously(TranslationUnitUpdater::UpdateMode::ParseIfNeeded);
return d->translationUnit;
}
......@@ -270,17 +272,14 @@ DiagnosticSet TranslationUnit::diagnostics() const
{
d->hasNewDiagnostics = false;
return DiagnosticSet(clang_getDiagnosticSetFromTU(cxTranslationUnit()));
return translationUnitCore().diagnostics();
}
QVector<ClangBackEnd::DiagnosticContainer> TranslationUnit::mainFileDiagnostics() const
{
const auto mainFilePath = filePath();
const auto isMainFileDiagnostic = [mainFilePath](const Diagnostic &diagnostic) {
return diagnostic.location().filePath() == mainFilePath;
};
d->hasNewDiagnostics = false;
return diagnostics().toDiagnosticContainers(isMainFileDiagnostic);
return translationUnitCore().mainFileDiagnostics();
}
const QSet<Utf8String> &TranslationUnit::dependedFilePaths() const
......@@ -302,58 +301,11 @@ void TranslationUnit::setDirtyIfDependencyIsMet(const Utf8String &filePath)
setDirty();
}
SourceLocation TranslationUnit::sourceLocationAt(uint line, uint column) const
{
return SourceLocation(cxTranslationUnit(), filePath(), line, column);
}
SourceLocation TranslationUnit::sourceLocationAt(const Utf8String &filePath, uint line, uint column) const
{
return SourceLocation(cxTranslationUnit(), filePath, line, column);
}
SourceRange TranslationUnit::sourceRange(uint fromLine, uint fromColumn, uint toLine, uint toColumn) const
{
return SourceRange(sourceLocationAt(fromLine, fromColumn),
sourceLocationAt(toLine, toColumn));
}
Cursor TranslationUnit::cursorAt(uint line, uint column) const
{
return clang_getCursor(cxTranslationUnit(), sourceLocationAt(line, column));
}
Cursor TranslationUnit::cursorAt(const Utf8String &filePath, uint line, uint column) const
{
return clang_getCursor(cxTranslationUnit(), sourceLocationAt(filePath, line, column));
}
Cursor TranslationUnit::cursor() const
{
return clang_getTranslationUnitCursor(cxTranslationUnit());
}
HighlightingMarks TranslationUnit::highlightingMarks() const
{
d->hasNewHighlightingMarks = false;
return highlightingMarksInRange(cursor().sourceRange());
}
HighlightingMarks TranslationUnit::highlightingMarksInRange(const SourceRange &range) const
{
CXToken *cxTokens = 0;
uint cxTokensCount = 0;
auto translationUnit = cxTranslationUnit();
clang_tokenize(translationUnit, range, &cxTokens, &cxTokensCount);
return HighlightingMarks(translationUnit, cxTokens, cxTokensCount);
}
SkippedSourceRanges TranslationUnit::skippedSourceRanges() const
{
return SkippedSourceRanges(cxTranslationUnit(), d->filePath.constData());
return translationUnitCore().highlightingMarks();
}
void TranslationUnit::checkIfNull() const
......@@ -402,15 +354,7 @@ bool TranslationUnit::fileExists() const
return QFileInfo::exists(d->filePath.toString());
}
void TranslationUnit::updateSynchronously(TranslationUnitUpdater::UpdateMode updateMode) const
{
TranslationUnitUpdater updater = createUpdater();
const TranslationUnitUpdateResult updateResult = updater.update(updateMode);
incorporateUpdaterResult(updateResult);
}
TranslationUnitUpdater TranslationUnit::createUpdater() const
TranslationUnitUpdateInput TranslationUnit::createUpdateInput() const
{
TranslationUnitUpdateInput updateInput;
updateInput.reparseNeeded = isNeedingReparse();
......@@ -421,6 +365,12 @@ TranslationUnitUpdater TranslationUnit::createUpdater() const
updateInput.projectId = projectPart().projectPartId();
updateInput.projectArguments = projectPart().arguments();
return updateInput;
}
TranslationUnitUpdater TranslationUnit::createUpdater() const
{
const TranslationUnitUpdateInput updateInput = createUpdateInput();
TranslationUnitUpdater updater(index(), d->translationUnit, updateInput);
return updater;
......@@ -431,8 +381,7 @@ void TranslationUnit::incorporateUpdaterResult(const TranslationUnitUpdateResult
if (result.parseTimePointIsSet)
d->lastProjectPartChangeTimePoint = result.parseTimePoint;
if (!result.dependedOnFilePaths.isEmpty()) // TODO: Remove me
d->dependedFilePaths = result.dependedOnFilePaths;
d->dependedFilePaths = result.dependedOnFilePaths;
d->translationUnits.addWatchedFiles(d->dependedFilePaths);
if (result.reparsed)
......@@ -452,14 +401,9 @@ CommandLineArguments TranslationUnit::commandLineArguments() const
return createUpdater().commandLineArguments();
}
SourceLocation TranslationUnit::sourceLocationAtWithoutReparsing(uint line, uint column) const
{
return SourceLocation(cxTranslationUnitWithoutReparsing(), filePath(), line, column);
}
uint TranslationUnit::defaultParseOptions()
TranslationUnitCore TranslationUnit::translationUnitCore() const
{
return TranslationUnitUpdater::defaultParseOptions();
return TranslationUnitCore(d->filePath, d->index, d->translationUnit);
}
uint TranslationUnit::unsavedFilesCount() const
......
......@@ -27,6 +27,8 @@
#include "clangtranslationunitupdater.h"
#include "clangtranslationunitcore.h"
#include <utf8stringvector.h>
#include <clang-c/Index.h>
......@@ -41,6 +43,7 @@ class Utf8String;
namespace ClangBackEnd {
class TranslationUnitCore;
class TranslationUnitData;
class TranslationUnitUpdateResult;
class CodeCompleter;
......@@ -91,14 +94,14 @@ public:
bool isVisibleInEditor() const;
void reset();
void parse() const;
void reparse() const;
bool isIntact() const;
CXIndex &index() const;
CXTranslationUnit cxTranslationUnit() const;
CXTranslationUnit cxTranslationUnitWithoutReparsing() const;
CXTranslationUnit &cxTranslationUnit() const;
UnsavedFile unsavedFile() const;
UnsavedFiles unsavedFiles() const;
......@@ -116,9 +119,12 @@ public:
const time_point &lastProjectPartChangeTimePoint() const;
bool isNeedingReparse() const;
// TODO: Remove the following two
bool hasNewDiagnostics() const;
bool hasNewHighlightingMarks() const;
// TODO: Remove the following two
DiagnosticSet diagnostics() const;
QVector<DiagnosticContainer> mainFileDiagnostics() const;
......@@ -129,23 +135,12 @@ public:
CommandLineArguments commandLineArguments() const;
SourceLocation sourceLocationAtWithoutReparsing(uint line, uint column) const;
SourceLocation sourceLocationAt(uint line, uint column) const;
SourceLocation sourceLocationAt(const Utf8String &filePath, uint line, uint column) const;
SourceRange sourceRange(uint fromLine, uint fromColumn, uint toLine, uint toColumn) const;
Cursor cursorAt(uint line, uint column) const;
Cursor cursorAt(const Utf8String &filePath, uint line, uint column) const;
Cursor cursor() const;
// TODO: Remove
HighlightingMarks highlightingMarks() const;
HighlightingMarks highlightingMarksInRange(const SourceRange &range) const;
SkippedSourceRanges skippedSourceRanges() const;
TranslationUnitCore translationUnitCore() const;
bool projectPartIsOutdated() const;
static uint defaultParseOptions();
private:
void setDirty();
......@@ -157,7 +152,7 @@ private:
bool reparseWasSuccessful() const;
bool fileExists() const;
void updateSynchronously(TranslationUnitUpdater::UpdateMode updateMode) const;
TranslationUnitUpdateInput createUpdateInput() const;
TranslationUnitUpdater createUpdater() const;
void incorporateUpdaterResult(const TranslationUnitUpdateResult &result) const;
......
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** 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 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.
**
** 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.
**
****************************************************************************/
#include "clangtranslationunitcore.h"
#include "clangtranslationunitupdater.h"
#include <codecompleter.h>
#include <cursor.h>
#include <diagnosticcontainer.h>
#include <diagnosticset.h>
#include <highlightingmark.h>
#include <highlightingmarks.h>
#include <skippedsourceranges.h>
#include <sourcelocation.h>
#include <sourcerange.h>
namespace ClangBackEnd {
TranslationUnitCore::TranslationUnitCore(const Utf8String &filepath,
CXIndex &cxIndex,
CXTranslationUnit &cxTranslationUnit)
: m_filePath(filepath)
, m_cxIndex(cxIndex)
, m_cxTranslationUnit(cxTranslationUnit)
{
}
bool TranslationUnitCore::isNull() const
{
return !m_cxTranslationUnit || !m_cxIndex || m_filePath.isEmpty();
}
Utf8String TranslationUnitCore::filePath() const
{
return m_filePath;
}
CXIndex &TranslationUnitCore::cxIndex() const
{
return m_cxIndex;
}
CXTranslationUnit &TranslationUnitCore::cxTranslationUnit() const
{
return m_cxTranslationUnit;
}
TranslationUnitUpdateResult TranslationUnitCore::update(
const TranslationUnitUpdateInput &parseInput) const
{
TranslationUnitUpdater updater(cxIndex(), cxTranslationUnit(), parseInput);
return updater.update(TranslationUnitUpdater::UpdateMode::AsNeeded);
}
TranslationUnitUpdateResult TranslationUnitCore::parse(
const TranslationUnitUpdateInput &parseInput) const
{
TranslationUnitUpdater updater(cxIndex(), cxTranslationUnit(), parseInput);
return updater.update(TranslationUnitUpdater::UpdateMode::ParseIfNeeded);
}
TranslationUnitUpdateResult TranslationUnitCore::reparse(
const TranslationUnitUpdateInput &parseInput) const
{
TranslationUnitUpdater updater(cxIndex(), cxTranslationUnit(), parseInput);
return updater.update(TranslationUnitUpdater::UpdateMode::ForceReparse);
}
TranslationUnitCore::CodeCompletionResult TranslationUnitCore::complete(
UnsavedFiles &unsavedFiles,
uint line,
uint column) const
{
CodeCompleter codeCompleter(*this, unsavedFiles);
const CodeCompletions completions = codeCompleter.complete(line, column);
const CompletionCorrection correction = codeCompleter.neededCorrection();
return CodeCompletionResult{completions, correction};
}
void TranslationUnitCore::extractDocumentAnnotations(
QVector<DiagnosticContainer> &diagnostics,
QVector<HighlightingMarkContainer> &highlightingMarks,
QVector<SourceRangeContainer> &skippedSourceRanges) const
{
diagnostics = mainFileDiagnostics();
highlightingMarks = this->highlightingMarks().toHighlightingMarksContainers();
skippedSourceRanges = this->skippedSourceRanges().toSourceRangeContainers();
}
DiagnosticSet TranslationUnitCore::diagnostics() const
{
return DiagnosticSet(clang_getDiagnosticSetFromTU(m_cxTranslationUnit));
}
QVector<DiagnosticContainer> TranslationUnitCore::mainFileDiagnostics() const
{
const auto isMainFileDiagnostic = [this](const Diagnostic &diagnostic) {
return diagnostic.location().filePath() == m_filePath;
};
return diagnostics().toDiagnosticContainers(isMainFileDiagnostic);
}
SourceLocation TranslationUnitCore::sourceLocationAt(uint line,uint column) const
{
return SourceLocation(m_cxTranslationUnit, m_filePath, line, column);
}
SourceLocation TranslationUnitCore::sourceLocationAt(const Utf8String &filePath,
uint line,
uint column) const
{
return SourceLocation(m_cxTranslationUnit, filePath, line, column);
}
SourceRange TranslationUnitCore::sourceRange(uint fromLine,
uint fromColumn,
uint toLine,
uint toColumn) const
{
return SourceRange(sourceLocationAt(fromLine, fromColumn),
sourceLocationAt(toLine, toColumn));
}
Cursor TranslationUnitCore::cursorAt(uint line, uint column) const
{
return clang_getCursor(m_cxTranslationUnit, sourceLocationAt(line, column));
}
Cursor TranslationUnitCore::cursorAt(const Utf8String &filePath,
uint line,
uint column) const
{
return clang_getCursor(m_cxTranslationUnit, sourceLocationAt(filePath, line, column));
}
Cursor TranslationUnitCore::cursor() const
{
return clang_getTranslationUnitCursor(m_cxTranslationUnit);
}
HighlightingMarks TranslationUnitCore::highlightingMarks() const
{
return highlightingMarksInRange(cursor().sourceRange());
}
HighlightingMarks TranslationUnitCore::highlightingMarksInRange(const SourceRange &range) const
{
CXToken *cxTokens = 0;
uint cxTokensCount = 0;
clang_tokenize(m_cxTranslationUnit, range, &cxTokens, &cxTokensCount);
return HighlightingMarks(m_cxTranslationUnit, cxTokens, cxTokensCount);
}
SkippedSourceRanges TranslationUnitCore::skippedSourceRanges() const
{
return SkippedSourceRanges(m_cxTranslationUnit, m_filePath.constData());
}
} // namespace ClangBackEnd
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** 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 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.
**
** 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.
**
****************************************************************************/
#pragma once
#include <clangbackendipc/codecompletion.h>
#include <utf8string.h>
#include <clang-c/Index.h>
class Utf8String;
namespace ClangBackEnd {
class Cursor;
class DiagnosticContainer;
class DiagnosticSet;
class HighlightingMarkContainer;
class HighlightingMarks;
class SkippedSourceRanges;
class SourceLocation;
class SourceRange;
class SourceRangeContainer;
class TranslationUnitUpdateInput;
class TranslationUnitUpdateResult;
class UnsavedFiles;
class TranslationUnitCore
{
public:
struct CodeCompletionResult {
CodeCompletions completions;
CompletionCorrection correction;
};
public:
TranslationUnitCore(const Utf8String &filePath,
CXIndex &cxIndex,
CXTranslationUnit &cxTranslationUnit);
bool isNull() const;
Utf8String filePath() const;
CXIndex &cxIndex() const;
CXTranslationUnit &cxTranslationUnit() const;
TranslationUnitUpdateResult update(const TranslationUnitUpdateInput &parseInput) const;
TranslationUnitUpdateResult parse(const TranslationUnitUpdateInput &parseInput) const;
TranslationUnitUpdateResult reparse(const TranslationUnitUpdateInput &parseInput) const;
CodeCompletionResult complete(UnsavedFiles &unsavedFiles, uint line, uint column) const;
void extractDocumentAnnotations(QVector<DiagnosticContainer> &diagnostics,
QVector<HighlightingMarkContainer> &highlightingMarks,
QVector<SourceRangeContainer> &skippedSourceRanges) const;
DiagnosticSet diagnostics() const;
QVector<DiagnosticContainer> mainFileDiagnostics() const;
SourceLocation sourceLocationAt(uint line, uint column) const;
SourceLocation sourceLocationAt(const Utf8String &filePath, uint line, uint column) const;
SourceRange sourceRange(uint fromLine, uint fromColumn, uint toLine, uint toColumn) const;
Cursor cursorAt(uint line, uint column) const;
Cursor cursorAt(const Utf8String &filePath, uint line, uint column) const;
Cursor cursor() const;
HighlightingMarks highlightingMarks() const;
HighlightingMarks highlightingMarksInRange(const SourceRange &range) const;
SkippedSourceRanges skippedSourceRanges() const;
private:
const Utf8String m_filePath;
CXIndex &m_cxIndex;
CXTranslationUnit &m_cxTranslationUnit;
};