2016-06-05 19:59:23 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2016-09-07 14:50:58 +02:00
|
|
|
#include "clangtranslationunit.h"
|
2017-06-09 12:19:09 +02:00
|
|
|
|
2017-07-28 15:15:46 +02:00
|
|
|
#include "clangbackend_global.h"
|
2017-06-09 12:19:09 +02:00
|
|
|
#include "clangreferencescollector.h"
|
2016-06-05 19:59:23 +02:00
|
|
|
#include "clangtranslationunitupdater.h"
|
2017-07-27 13:20:22 +02:00
|
|
|
#include "clangfollowsymbol.h"
|
|
|
|
|
#include "clangfollowsymboljob.h"
|
2016-06-05 19:59:23 +02:00
|
|
|
|
|
|
|
|
#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>
|
2017-07-27 13:20:22 +02:00
|
|
|
#include <commandlinearguments.h>
|
2016-06-05 19:59:23 +02:00
|
|
|
|
2017-07-28 15:15:46 +02:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2016-06-05 19:59:23 +02:00
|
|
|
namespace ClangBackEnd {
|
|
|
|
|
|
2016-09-13 10:41:22 +02:00
|
|
|
TranslationUnit::TranslationUnit(const Utf8String &id,
|
|
|
|
|
const Utf8String &filepath,
|
2016-09-07 14:50:58 +02:00
|
|
|
CXIndex &cxIndex,
|
|
|
|
|
CXTranslationUnit &cxTranslationUnit)
|
2016-09-13 10:41:22 +02:00
|
|
|
: m_id(id)
|
|
|
|
|
, m_filePath(filepath)
|
2016-06-05 19:59:23 +02:00
|
|
|
, m_cxIndex(cxIndex)
|
|
|
|
|
, m_cxTranslationUnit(cxTranslationUnit)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 14:50:58 +02:00
|
|
|
bool TranslationUnit::isNull() const
|
2016-06-05 19:59:23 +02:00
|
|
|
{
|
2016-09-13 10:41:22 +02:00
|
|
|
return !m_cxTranslationUnit || !m_cxIndex || m_filePath.isEmpty() || m_id.isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utf8String TranslationUnit::id() const
|
|
|
|
|
{
|
|
|
|
|
return m_id;
|
2016-06-05 19:59:23 +02:00
|
|
|
}
|
|
|
|
|
|
2016-09-07 14:50:58 +02:00
|
|
|
Utf8String TranslationUnit::filePath() const
|
2016-06-05 19:59:23 +02:00
|
|
|
{
|
|
|
|
|
return m_filePath;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 14:50:58 +02:00
|
|
|
CXIndex &TranslationUnit::cxIndex() const
|
2016-06-05 19:59:23 +02:00
|
|
|
{
|
|
|
|
|
return m_cxIndex;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 14:50:58 +02:00
|
|
|
CXTranslationUnit &TranslationUnit::cxTranslationUnit() const
|
2016-06-05 19:59:23 +02:00
|
|
|
{
|
|
|
|
|
return m_cxTranslationUnit;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 14:50:58 +02:00
|
|
|
TranslationUnitUpdateResult TranslationUnit::update(
|
2016-06-05 19:59:23 +02:00
|
|
|
const TranslationUnitUpdateInput &parseInput) const
|
|
|
|
|
{
|
2016-09-13 10:41:22 +02:00
|
|
|
TranslationUnitUpdater updater(id(), cxIndex(), cxTranslationUnit(), parseInput);
|
2016-06-05 19:59:23 +02:00
|
|
|
|
|
|
|
|
return updater.update(TranslationUnitUpdater::UpdateMode::AsNeeded);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 14:50:58 +02:00
|
|
|
TranslationUnitUpdateResult TranslationUnit::parse(
|
2016-06-05 19:59:23 +02:00
|
|
|
const TranslationUnitUpdateInput &parseInput) const
|
|
|
|
|
{
|
2016-09-13 10:41:22 +02:00
|
|
|
TranslationUnitUpdater updater(id(), cxIndex(), cxTranslationUnit(), parseInput);
|
2016-06-05 19:59:23 +02:00
|
|
|
|
|
|
|
|
return updater.update(TranslationUnitUpdater::UpdateMode::ParseIfNeeded);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 14:50:58 +02:00
|
|
|
TranslationUnitUpdateResult TranslationUnit::reparse(
|
2016-06-05 19:59:23 +02:00
|
|
|
const TranslationUnitUpdateInput &parseInput) const
|
|
|
|
|
{
|
2016-09-13 10:41:22 +02:00
|
|
|
TranslationUnitUpdater updater(id(), cxIndex(), cxTranslationUnit(), parseInput);
|
2016-06-05 19:59:23 +02:00
|
|
|
|
|
|
|
|
return updater.update(TranslationUnitUpdater::UpdateMode::ForceReparse);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-28 15:15:46 +02:00
|
|
|
bool TranslationUnit::suspend() const
|
|
|
|
|
{
|
|
|
|
|
return clang_suspendTranslationUnit(cxTranslationUnit());
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 14:50:58 +02:00
|
|
|
TranslationUnit::CodeCompletionResult TranslationUnit::complete(
|
2016-06-05 19:59:23 +02:00
|
|
|
UnsavedFiles &unsavedFiles,
|
|
|
|
|
uint line,
|
2017-08-10 11:05:48 +02:00
|
|
|
uint column,
|
|
|
|
|
int funcNameStartLine,
|
|
|
|
|
int funcNameStartColumn) const
|
2016-06-05 19:59:23 +02:00
|
|
|
{
|
|
|
|
|
CodeCompleter codeCompleter(*this, unsavedFiles);
|
|
|
|
|
|
2017-08-10 11:05:48 +02:00
|
|
|
const CodeCompletions completions = codeCompleter.complete(line, column,
|
|
|
|
|
funcNameStartLine,
|
|
|
|
|
funcNameStartColumn);
|
2016-06-05 19:59:23 +02:00
|
|
|
const CompletionCorrection correction = codeCompleter.neededCorrection();
|
|
|
|
|
|
|
|
|
|
return CodeCompletionResult{completions, correction};
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 14:50:58 +02:00
|
|
|
void TranslationUnit::extractDocumentAnnotations(
|
2016-10-04 16:23:42 +02:00
|
|
|
DiagnosticContainer &firstHeaderErrorDiagnostic,
|
|
|
|
|
QVector<DiagnosticContainer> &mainFileDiagnostics,
|
2016-06-05 19:59:23 +02:00
|
|
|
QVector<HighlightingMarkContainer> &highlightingMarks,
|
|
|
|
|
QVector<SourceRangeContainer> &skippedSourceRanges) const
|
|
|
|
|
{
|
2016-10-04 16:23:42 +02:00
|
|
|
extractDiagnostics(firstHeaderErrorDiagnostic, mainFileDiagnostics);
|
2016-06-05 19:59:23 +02:00
|
|
|
highlightingMarks = this->highlightingMarks().toHighlightingMarksContainers();
|
|
|
|
|
skippedSourceRanges = this->skippedSourceRanges().toSourceRangeContainers();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-09 12:19:09 +02:00
|
|
|
ReferencesResult TranslationUnit::references(uint line, uint column) const
|
|
|
|
|
{
|
|
|
|
|
return collectReferences(m_cxTranslationUnit, line, column);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 14:50:58 +02:00
|
|
|
DiagnosticSet TranslationUnit::diagnostics() const
|
2016-06-05 19:59:23 +02:00
|
|
|
{
|
|
|
|
|
return DiagnosticSet(clang_getDiagnosticSetFromTU(m_cxTranslationUnit));
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 14:50:58 +02:00
|
|
|
SourceLocation TranslationUnit::sourceLocationAt(uint line,uint column) const
|
2016-06-05 19:59:23 +02:00
|
|
|
{
|
|
|
|
|
return SourceLocation(m_cxTranslationUnit, m_filePath, line, column);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 14:50:58 +02:00
|
|
|
SourceLocation TranslationUnit::sourceLocationAt(const Utf8String &filePath,
|
|
|
|
|
uint line,
|
|
|
|
|
uint column) const
|
2016-06-05 19:59:23 +02:00
|
|
|
{
|
|
|
|
|
return SourceLocation(m_cxTranslationUnit, filePath, line, column);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 14:50:58 +02:00
|
|
|
SourceRange TranslationUnit::sourceRange(uint fromLine,
|
|
|
|
|
uint fromColumn,
|
|
|
|
|
uint toLine,
|
|
|
|
|
uint toColumn) const
|
2016-06-05 19:59:23 +02:00
|
|
|
{
|
|
|
|
|
return SourceRange(sourceLocationAt(fromLine, fromColumn),
|
|
|
|
|
sourceLocationAt(toLine, toColumn));
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 14:50:58 +02:00
|
|
|
Cursor TranslationUnit::cursorAt(uint line, uint column) const
|
2016-06-05 19:59:23 +02:00
|
|
|
{
|
|
|
|
|
return clang_getCursor(m_cxTranslationUnit, sourceLocationAt(line, column));
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 14:50:58 +02:00
|
|
|
Cursor TranslationUnit::cursorAt(const Utf8String &filePath,
|
2016-06-05 19:59:23 +02:00
|
|
|
uint line,
|
|
|
|
|
uint column) const
|
|
|
|
|
{
|
|
|
|
|
return clang_getCursor(m_cxTranslationUnit, sourceLocationAt(filePath, line, column));
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 14:50:58 +02:00
|
|
|
Cursor TranslationUnit::cursor() const
|
2016-06-05 19:59:23 +02:00
|
|
|
{
|
|
|
|
|
return clang_getTranslationUnitCursor(m_cxTranslationUnit);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 14:50:58 +02:00
|
|
|
HighlightingMarks TranslationUnit::highlightingMarks() const
|
2016-06-05 19:59:23 +02:00
|
|
|
{
|
|
|
|
|
return highlightingMarksInRange(cursor().sourceRange());
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 14:50:58 +02:00
|
|
|
HighlightingMarks TranslationUnit::highlightingMarksInRange(const SourceRange &range) const
|
2016-06-05 19:59:23 +02:00
|
|
|
{
|
|
|
|
|
CXToken *cxTokens = 0;
|
|
|
|
|
uint cxTokensCount = 0;
|
|
|
|
|
|
|
|
|
|
clang_tokenize(m_cxTranslationUnit, range, &cxTokens, &cxTokensCount);
|
|
|
|
|
|
|
|
|
|
return HighlightingMarks(m_cxTranslationUnit, cxTokens, cxTokensCount);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 14:50:58 +02:00
|
|
|
SkippedSourceRanges TranslationUnit::skippedSourceRanges() const
|
2016-06-05 19:59:23 +02:00
|
|
|
{
|
|
|
|
|
return SkippedSourceRanges(m_cxTranslationUnit, m_filePath.constData());
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-04 16:23:42 +02:00
|
|
|
static bool isMainFileDiagnostic(const Utf8String &mainFilePath, const Diagnostic &diagnostic)
|
|
|
|
|
{
|
|
|
|
|
return diagnostic.location().filePath() == mainFilePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool isHeaderErrorDiagnostic(const Utf8String &mainFilePath, const Diagnostic &diagnostic)
|
|
|
|
|
{
|
|
|
|
|
const bool isCritical = diagnostic.severity() == DiagnosticSeverity::Error
|
|
|
|
|
|| diagnostic.severity() == DiagnosticSeverity::Fatal;
|
|
|
|
|
return isCritical && diagnostic.location().filePath() != mainFilePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TranslationUnit::extractDiagnostics(DiagnosticContainer &firstHeaderErrorDiagnostic,
|
|
|
|
|
QVector<DiagnosticContainer> &mainFileDiagnostics) const
|
|
|
|
|
{
|
|
|
|
|
mainFileDiagnostics.clear();
|
|
|
|
|
mainFileDiagnostics.reserve(int(diagnostics().size()));
|
|
|
|
|
|
|
|
|
|
bool hasFirstHeaderErrorDiagnostic = false;
|
|
|
|
|
|
|
|
|
|
for (const Diagnostic &diagnostic : diagnostics()) {
|
|
|
|
|
if (!hasFirstHeaderErrorDiagnostic && isHeaderErrorDiagnostic(m_filePath, diagnostic)) {
|
|
|
|
|
hasFirstHeaderErrorDiagnostic = true;
|
|
|
|
|
firstHeaderErrorDiagnostic = diagnostic.toDiagnosticContainer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isMainFileDiagnostic(m_filePath, diagnostic))
|
|
|
|
|
mainFileDiagnostics.push_back(diagnostic.toDiagnosticContainer());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-08 09:33:35 +02:00
|
|
|
SourceRangeContainer TranslationUnit::followSymbol(uint line,
|
|
|
|
|
uint column,
|
|
|
|
|
const QVector<Utf8String> &dependentFiles,
|
|
|
|
|
const CommandLineArguments ¤tArgs) const
|
2017-07-27 13:20:22 +02:00
|
|
|
{
|
2017-09-08 09:33:35 +02:00
|
|
|
return FollowSymbol::followSymbol(m_cxTranslationUnit, m_cxIndex, cursorAt(line, column), line,
|
|
|
|
|
column, dependentFiles, currentArgs);
|
2017-07-27 13:20:22 +02:00
|
|
|
}
|
|
|
|
|
|
2016-06-05 19:59:23 +02:00
|
|
|
} // namespace ClangBackEnd
|