2015-10-01 12:45:06 +02:00
|
|
|
/****************************************************************************
|
2015-06-01 18:51:55 +02:00
|
|
|
**
|
|
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
|
** Contact: http://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 http://www.qt.io/terms-conditions. For further information
|
|
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "translationunit.h"
|
|
|
|
|
|
2015-08-31 12:40:14 +02:00
|
|
|
#include "clangstring.h"
|
2015-06-01 18:51:55 +02:00
|
|
|
#include "codecompleter.h"
|
2015-08-31 16:28:26 +02:00
|
|
|
#include "diagnosticset.h"
|
2015-06-16 12:38:04 +02:00
|
|
|
#include "projectpart.h"
|
2015-06-01 18:51:55 +02:00
|
|
|
#include "translationunitfilenotexitexception.h"
|
2015-06-16 12:38:04 +02:00
|
|
|
#include "translationunitisnullexception.h"
|
2015-06-01 18:51:55 +02:00
|
|
|
#include "translationunitparseerrorexception.h"
|
2015-08-31 12:40:14 +02:00
|
|
|
#include "translationunits.h"
|
2015-06-01 18:51:55 +02:00
|
|
|
#include "unsavedfiles.h"
|
2015-06-16 12:38:04 +02:00
|
|
|
|
|
|
|
|
#include <utf8string.h>
|
|
|
|
|
|
2015-08-31 12:40:14 +02:00
|
|
|
#include <QDebug>
|
2015-06-16 12:38:04 +02:00
|
|
|
#include <QFileInfo>
|
2015-07-28 16:44:39 +02:00
|
|
|
#include <QLoggingCategory>
|
|
|
|
|
|
2015-08-31 12:40:14 +02:00
|
|
|
#include <ostream>
|
|
|
|
|
|
2015-07-28 16:44:39 +02:00
|
|
|
static Q_LOGGING_CATEGORY(verboseLibLog, "qtc.clangbackend.verboselib");
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2015-06-16 11:56:00 +02:00
|
|
|
namespace ClangBackEnd {
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
class TranslationUnitData
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
TranslationUnitData(const Utf8String &filePath,
|
2015-08-31 12:40:14 +02:00
|
|
|
const ProjectPart &projectPart,
|
|
|
|
|
TranslationUnits &translationUnits);
|
2015-06-01 18:51:55 +02:00
|
|
|
~TranslationUnitData();
|
|
|
|
|
|
|
|
|
|
public:
|
2015-08-31 12:40:14 +02:00
|
|
|
TranslationUnits &translationUnits;
|
|
|
|
|
time_point lastProjectPartChangeTimePoint;
|
|
|
|
|
QSet<Utf8String> dependedFilePaths;
|
2015-06-01 18:51:55 +02:00
|
|
|
ProjectPart projectPart;
|
|
|
|
|
Utf8String filePath;
|
|
|
|
|
CXTranslationUnit translationUnit = nullptr;
|
|
|
|
|
CXIndex index = nullptr;
|
2015-08-31 12:40:14 +02:00
|
|
|
uint documentRevision = 0;
|
|
|
|
|
bool needsToBeReparsed = false;
|
2015-08-26 16:33:32 +02:00
|
|
|
bool hasNewDiagnostics = false;
|
2015-06-01 18:51:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TranslationUnitData::TranslationUnitData(const Utf8String &filePath,
|
2015-08-31 12:40:14 +02:00
|
|
|
const ProjectPart &projectPart,
|
|
|
|
|
TranslationUnits &translationUnits)
|
|
|
|
|
: translationUnits(translationUnits),
|
|
|
|
|
lastProjectPartChangeTimePoint(std::chrono::steady_clock::now()),
|
2015-06-01 18:51:55 +02:00
|
|
|
projectPart(projectPart),
|
2015-08-31 12:40:14 +02:00
|
|
|
filePath(filePath)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2015-08-31 12:40:14 +02:00
|
|
|
dependedFilePaths.insert(filePath);
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TranslationUnitData::~TranslationUnitData()
|
|
|
|
|
{
|
|
|
|
|
clang_disposeTranslationUnit(translationUnit);
|
|
|
|
|
clang_disposeIndex(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TranslationUnit::TranslationUnit(const Utf8String &filePath,
|
2015-08-31 12:40:14 +02:00
|
|
|
const ProjectPart &projectPart,
|
|
|
|
|
TranslationUnits &translationUnits,
|
2015-06-01 18:51:55 +02:00
|
|
|
FileExistsCheck fileExistsCheck)
|
2015-08-31 12:40:14 +02:00
|
|
|
: d(std::make_shared<TranslationUnitData>(filePath, projectPart, translationUnits))
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
if (fileExistsCheck == CheckIfFileExists)
|
|
|
|
|
checkIfFileExists();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool TranslationUnit::isNull() const
|
|
|
|
|
{
|
|
|
|
|
return !d;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TranslationUnit::reset()
|
|
|
|
|
{
|
|
|
|
|
d.reset();
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-31 16:28:26 +02:00
|
|
|
void TranslationUnit::reparse() const
|
2015-07-21 19:44:08 +02:00
|
|
|
{
|
|
|
|
|
cxTranslationUnit();
|
|
|
|
|
|
|
|
|
|
reparseTranslationUnit();
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-01 18:51:55 +02:00
|
|
|
CXIndex TranslationUnit::index() const
|
|
|
|
|
{
|
|
|
|
|
checkIfNull();
|
|
|
|
|
|
2015-07-28 16:44:39 +02:00
|
|
|
if (!d->index) {
|
|
|
|
|
const bool displayDiagnostics = verboseLibLog().isDebugEnabled();
|
|
|
|
|
d->index = clang_createIndex(1, displayDiagnostics);
|
|
|
|
|
}
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
return d->index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CXTranslationUnit TranslationUnit::cxTranslationUnit() const
|
|
|
|
|
{
|
|
|
|
|
checkIfNull();
|
2015-10-12 12:10:58 +02:00
|
|
|
checkIfFileExists();
|
2015-08-31 12:40:14 +02:00
|
|
|
removeTranslationUnitIfProjectPartWasChanged();
|
2015-06-01 18:51:55 +02:00
|
|
|
createTranslationUnitIfNeeded();
|
2015-08-31 12:40:14 +02:00
|
|
|
reparseTranslationUnitIfFilesAreChanged();
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
return d->translationUnit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Utf8String &TranslationUnit::filePath() const
|
|
|
|
|
{
|
|
|
|
|
checkIfNull();
|
|
|
|
|
|
|
|
|
|
return d->filePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Utf8String &TranslationUnit::projectPartId() const
|
|
|
|
|
{
|
|
|
|
|
checkIfNull();
|
|
|
|
|
|
|
|
|
|
return d->projectPart.projectPartId();
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-31 16:28:26 +02:00
|
|
|
FileContainer TranslationUnit::fileContainer() const
|
|
|
|
|
{
|
|
|
|
|
checkIfNull();
|
|
|
|
|
|
2015-08-31 12:40:14 +02:00
|
|
|
return FileContainer(d->filePath,
|
|
|
|
|
d->projectPart.projectPartId(),
|
|
|
|
|
Utf8String(),
|
|
|
|
|
false,
|
|
|
|
|
d->documentRevision);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ProjectPart &TranslationUnit::projectPart() const
|
|
|
|
|
{
|
|
|
|
|
checkIfNull();
|
|
|
|
|
|
|
|
|
|
return d->projectPart;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TranslationUnit::setDocumentRevision(uint revision)
|
|
|
|
|
{
|
|
|
|
|
d->documentRevision = revision;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint TranslationUnit::documentRevision() const
|
|
|
|
|
{
|
|
|
|
|
return d->documentRevision;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const time_point &TranslationUnit::lastProjectPartChangeTimePoint() const
|
|
|
|
|
{
|
|
|
|
|
return d->lastProjectPartChangeTimePoint;
|
2015-08-31 16:28:26 +02:00
|
|
|
}
|
|
|
|
|
|
2015-08-31 12:40:14 +02:00
|
|
|
bool TranslationUnit::isNeedingReparse() const
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2015-08-31 12:40:14 +02:00
|
|
|
return d->needsToBeReparsed;
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2015-08-26 16:33:32 +02:00
|
|
|
bool TranslationUnit::hasNewDiagnostics() const
|
|
|
|
|
{
|
|
|
|
|
return d->hasNewDiagnostics;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-31 16:28:26 +02:00
|
|
|
DiagnosticSet TranslationUnit::diagnostics() const
|
|
|
|
|
{
|
2015-08-26 16:33:32 +02:00
|
|
|
d->hasNewDiagnostics = false;
|
|
|
|
|
|
2015-08-31 16:28:26 +02:00
|
|
|
return DiagnosticSet(clang_getDiagnosticSetFromTU(cxTranslationUnit()));
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-31 12:40:14 +02:00
|
|
|
const QSet<Utf8String> &TranslationUnit::dependedFilePaths() const
|
|
|
|
|
{
|
|
|
|
|
createTranslationUnitIfNeeded();
|
|
|
|
|
|
|
|
|
|
return d->dependedFilePaths;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-26 16:33:32 +02:00
|
|
|
void TranslationUnit::setDirtyIfDependencyIsMet(const Utf8String &filePath)
|
2015-08-31 12:40:14 +02:00
|
|
|
{
|
2015-10-12 17:36:18 +02:00
|
|
|
if (d->dependedFilePaths.contains(filePath) && isMainFileAndExistsOrIsOtherFile(filePath)) {
|
2015-08-31 12:40:14 +02:00
|
|
|
d->needsToBeReparsed = true;
|
2015-08-26 16:33:32 +02:00
|
|
|
d->hasNewDiagnostics = true;
|
|
|
|
|
}
|
2015-08-31 12:40:14 +02:00
|
|
|
}
|
|
|
|
|
|
2015-06-01 18:51:55 +02:00
|
|
|
void TranslationUnit::checkIfNull() const
|
|
|
|
|
{
|
|
|
|
|
if (isNull())
|
|
|
|
|
throw TranslationUnitIsNullException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TranslationUnit::checkIfFileExists() const
|
|
|
|
|
{
|
|
|
|
|
if (!QFileInfo::exists(d->filePath.toString()))
|
|
|
|
|
throw TranslationUnitFileNotExitsException(d->filePath);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-31 12:40:14 +02:00
|
|
|
void TranslationUnit::updateLastProjectPartChangeTimePoint() const
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2015-08-31 12:40:14 +02:00
|
|
|
d->lastProjectPartChangeTimePoint = std::chrono::steady_clock::now();
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2015-08-31 12:40:14 +02:00
|
|
|
void TranslationUnit::removeTranslationUnitIfProjectPartWasChanged() const
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2015-08-31 12:40:14 +02:00
|
|
|
if (projectPartIsOutdated()) {
|
2015-06-01 18:51:55 +02:00
|
|
|
clang_disposeTranslationUnit(d->translationUnit);
|
|
|
|
|
d->translationUnit = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-31 12:40:14 +02:00
|
|
|
bool TranslationUnit::projectPartIsOutdated() const
|
|
|
|
|
{
|
|
|
|
|
return d->projectPart.lastChangeTimePoint() >= d->lastProjectPartChangeTimePoint;
|
2015-10-12 17:36:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool TranslationUnit::isMainFileAndExistsOrIsOtherFile(const Utf8String &filePath) const
|
|
|
|
|
{
|
|
|
|
|
if (filePath == d->filePath)
|
|
|
|
|
return QFileInfo::exists(d->filePath);
|
|
|
|
|
|
|
|
|
|
return true;
|
2015-08-31 12:40:14 +02:00
|
|
|
}
|
|
|
|
|
|
2015-06-01 18:51:55 +02:00
|
|
|
void TranslationUnit::createTranslationUnitIfNeeded() const
|
|
|
|
|
{
|
|
|
|
|
if (!d->translationUnit) {
|
|
|
|
|
d->translationUnit = CXTranslationUnit();
|
|
|
|
|
CXErrorCode errorCode = clang_parseTranslationUnit2(index(),
|
|
|
|
|
d->filePath.constData(),
|
|
|
|
|
d->projectPart.cxArguments(),
|
|
|
|
|
d->projectPart.argumentCount(),
|
2015-08-31 12:40:14 +02:00
|
|
|
unsavedFiles().cxUnsavedFiles(),
|
|
|
|
|
unsavedFiles().count(),
|
2015-07-15 13:55:51 +02:00
|
|
|
defaultOptions(),
|
2015-06-01 18:51:55 +02:00
|
|
|
&d->translationUnit);
|
|
|
|
|
|
|
|
|
|
checkTranslationUnitErrorCode(errorCode);
|
|
|
|
|
|
2015-08-31 12:40:14 +02:00
|
|
|
updateIncludeFilePaths();
|
|
|
|
|
|
2015-07-15 13:55:51 +02:00
|
|
|
|
2015-08-31 12:40:14 +02:00
|
|
|
updateLastProjectPartChangeTimePoint();
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TranslationUnit::checkTranslationUnitErrorCode(CXErrorCode errorCode) const
|
|
|
|
|
{
|
|
|
|
|
switch (errorCode) {
|
|
|
|
|
case CXError_Success: break;
|
|
|
|
|
default: throw TranslationUnitParseErrorException(d->filePath, d->projectPart.projectPartId());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-15 13:55:51 +02:00
|
|
|
void TranslationUnit::reparseTranslationUnit() const
|
|
|
|
|
{
|
|
|
|
|
clang_reparseTranslationUnit(d->translationUnit,
|
2015-08-31 12:40:14 +02:00
|
|
|
unsavedFiles().count(),
|
|
|
|
|
unsavedFiles().cxUnsavedFiles(),
|
2015-07-15 13:55:51 +02:00
|
|
|
clang_defaultReparseOptions(d->translationUnit));
|
2015-08-31 12:40:14 +02:00
|
|
|
|
2015-09-01 10:09:17 +02:00
|
|
|
updateIncludeFilePaths();
|
|
|
|
|
|
2015-08-31 12:40:14 +02:00
|
|
|
d->needsToBeReparsed = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TranslationUnit::reparseTranslationUnitIfFilesAreChanged() const
|
|
|
|
|
{
|
|
|
|
|
if (isNeedingReparse())
|
|
|
|
|
reparseTranslationUnit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TranslationUnit::includeCallback(CXFile included_file,
|
2015-09-01 09:14:34 +02:00
|
|
|
CXSourceLocation * /*inclusion_stack*/,
|
2015-08-31 12:40:14 +02:00
|
|
|
unsigned /*include_len*/,
|
|
|
|
|
CXClientData clientData)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
ClangString includeFilePath(clang_getFileName(included_file));
|
|
|
|
|
|
|
|
|
|
TranslationUnit *translationUnit = static_cast<TranslationUnit*>(clientData);
|
|
|
|
|
|
|
|
|
|
translationUnit->d->dependedFilePaths.insert(includeFilePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UnsavedFiles &TranslationUnit::unsavedFiles() const
|
|
|
|
|
{
|
|
|
|
|
return d->translationUnits.unsavedFiles();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TranslationUnit::updateIncludeFilePaths() const
|
|
|
|
|
{
|
2015-09-01 10:23:45 +02:00
|
|
|
auto oldDependedFilePaths = d->dependedFilePaths;
|
|
|
|
|
|
2015-08-31 12:40:14 +02:00
|
|
|
d->dependedFilePaths.clear();
|
|
|
|
|
d->dependedFilePaths.insert(filePath());
|
|
|
|
|
|
|
|
|
|
clang_getInclusions(d->translationUnit, includeCallback, const_cast<TranslationUnit*>(this));
|
|
|
|
|
|
2015-09-01 10:23:45 +02:00
|
|
|
if (d->dependedFilePaths.size() == 1)
|
|
|
|
|
d->dependedFilePaths = oldDependedFilePaths;
|
|
|
|
|
|
2015-08-31 12:40:14 +02:00
|
|
|
d->translationUnits.addWatchedFiles(d->dependedFilePaths);
|
2015-07-15 13:55:51 +02:00
|
|
|
}
|
|
|
|
|
|
2015-09-01 12:00:21 +02:00
|
|
|
uint TranslationUnit::defaultOptions()
|
2015-07-15 13:55:51 +02:00
|
|
|
{
|
|
|
|
|
return CXTranslationUnit_CacheCompletionResults
|
2015-08-25 16:19:19 +02:00
|
|
|
| CXTranslationUnit_PrecompiledPreamble
|
|
|
|
|
| CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
|
2015-07-15 13:55:51 +02:00
|
|
|
}
|
|
|
|
|
|
2015-06-01 18:51:55 +02:00
|
|
|
uint TranslationUnit::unsavedFilesCount() const
|
|
|
|
|
{
|
2015-08-31 12:40:14 +02:00
|
|
|
return unsavedFiles().count();
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CXUnsavedFile *TranslationUnit::cxUnsavedFiles() const
|
|
|
|
|
{
|
2015-08-31 12:40:14 +02:00
|
|
|
return unsavedFiles().cxUnsavedFiles();
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TranslationUnit::~TranslationUnit() = default;
|
|
|
|
|
|
|
|
|
|
TranslationUnit::TranslationUnit(const TranslationUnit &) = default;
|
2015-06-10 13:52:45 +02:00
|
|
|
TranslationUnit &TranslationUnit::operator=(const TranslationUnit &) = default;
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
TranslationUnit::TranslationUnit(TranslationUnit &&other)
|
|
|
|
|
: d(std::move(other.d))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-10 13:52:45 +02:00
|
|
|
TranslationUnit &TranslationUnit::operator=(TranslationUnit &&other)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
d = std::move(other.d);
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-10 13:52:45 +02:00
|
|
|
bool operator==(const TranslationUnit &first, const TranslationUnit &second)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
return first.filePath() == second.filePath() && first.projectPartId() == second.projectPartId();
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-31 12:40:14 +02:00
|
|
|
void PrintTo(const TranslationUnit &translationUnit, ::std::ostream *os)
|
|
|
|
|
{
|
|
|
|
|
*os << "TranslationUnit("
|
|
|
|
|
<< translationUnit.filePath().constData() << ", "
|
|
|
|
|
<< translationUnit.projectPartId().constData() << ", "
|
|
|
|
|
<< translationUnit.documentRevision() << ")";
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-16 11:56:00 +02:00
|
|
|
} // namespace ClangBackEnd
|