1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
|
/*
This file is part of KDevelop
Copyright 2013 Olivier de Gaalon <olivier.jg@gmail.com>
Copyright 2013 Milian Wolff <mail@milianw.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "clangparsejob.h"
#include <interfaces/icore.h>
#include <interfaces/iprojectcontroller.h>
#include <interfaces/iproject.h>
#include <interfaces/ilanguagecontroller.h>
#include <interfaces/idocumentcontroller.h>
#include <language/interfaces/icodehighlighting.h>
#include <language/backgroundparser/urlparselock.h>
#include <language/backgroundparser/backgroundparser.h>
#include <language/duchain/duchainlock.h>
#include <language/duchain/duchainutils.h>
#include <language/duchain/duchain.h>
#include <language/duchain/parsingenvironment.h>
#include <custom-definesandincludes/idefinesandincludesmanager.h>
#include <project/projectmodel.h>
#include <project/interfaces/ibuildsystemmanager.h>
#include "clangsettings/clangsettingsmanager.h"
#include "duchain/clanghelpers.h"
#include "duchain/clangpch.h"
#include "duchain/duchainutils.h"
#include "duchain/parsesession.h"
#include "duchain/clangindex.h"
#include "duchain/clangparsingenvironmentfile.h"
#include "util/clangdebug.h"
#include "util/clangtypes.h"
#include "util/clangutils.h"
#include "clangsupport.h"
#include "duchain/documentfinderhelpers.h"
#include <QFile>
#include <QStringList>
#include <QFileInfo>
#include <QReadLocker>
#include <memory>
using namespace KDevelop;
namespace {
QString findConfigFile(const QString& forFile, const QString& configFileName)
{
QDir dir = QFileInfo(forFile).dir();
while (dir.exists()) {
const QFileInfo customIncludePaths(dir, configFileName);
if (customIncludePaths.exists()) {
return customIncludePaths.absoluteFilePath();
}
if (!dir.cdUp()) {
break;
}
}
return {};
}
Path::List readPathListFile(const QString& filepath)
{
if (filepath.isEmpty()) {
return {};
}
QFile f(filepath);
if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) {
return {};
}
const QString text = QString::fromLocal8Bit(f.readAll());
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
const QStringList lines = text.split(QLatin1Char('\n'), Qt::SkipEmptyParts);
#else
const QStringList lines = text.split(QLatin1Char('\n'), QString::SkipEmptyParts);
#endif
Path::List paths;
paths.reserve(lines.length());
for (const auto& line : lines) {
paths << Path(line);
}
return paths;
}
/**
* File should contain the header to precompile and use while parsing
* @returns the first path in the file
*/
Path userDefinedPchIncludeForFile(const QString& sourcefile)
{
const QString pchIncludeFilename = QStringLiteral(".kdev_pch_include");
const auto paths = readPathListFile(findConfigFile(sourcefile, pchIncludeFilename));
return paths.isEmpty() ? Path() : paths.first();
}
ProjectFileItem* findProjectFileItem(const IndexedString& url, bool* hasBuildSystemInfo)
{
ProjectFileItem* file = nullptr;
*hasBuildSystemInfo = false;
const auto& projects = ICore::self()->projectController()->projects();
for (auto project : projects) {
const auto files = project->filesForPath(url);
if (files.isEmpty()) {
continue;
}
file = files.last();
// A file might be defined in different targets.
// Prefer file items defined inside a target with non-empty includes.
for (auto f: files) {
if (!dynamic_cast<ProjectTargetItem*>(f->parent())) {
continue;
}
file = f;
if (!IDefinesAndIncludesManager::manager()->includes(f, IDefinesAndIncludesManager::ProjectSpecific).isEmpty()) {
break;
}
}
}
if (file && file->project()) {
if (auto bsm = file->project()->buildSystemManager()) {
*hasBuildSystemInfo = bsm->hasBuildInfo(file);
}
}
return file;
}
ClangParsingEnvironmentFile* parsingEnvironmentFile(const TopDUContext* context)
{
return dynamic_cast<ClangParsingEnvironmentFile*>(context->parsingEnvironmentFile().data());
}
DocumentChangeTracker* trackerForUrl(const IndexedString& url)
{
return ICore::self()->languageController()->backgroundParser()->trackerForUrl(url);
}
}
ClangParseJob::ClangParseJob(const IndexedString& url, ILanguageSupport* languageSupport)
: ParseJob(url, languageSupport)
, m_options(ParseSessionData::NoOption)
{
const auto tuUrl = clang()->index()->translationUnitForUrl(url);
bool hasBuildSystemInfo;
if (auto file = findProjectFileItem(tuUrl, &hasBuildSystemInfo)) {
m_environment.addIncludes(IDefinesAndIncludesManager::manager()->includes(file));
m_environment.addFrameworkDirectories(IDefinesAndIncludesManager::manager()->frameworkDirectories(file));
m_environment.addDefines(IDefinesAndIncludesManager::manager()->defines(file));
m_environment.setParserSettings(ClangSettingsManager::self()->parserSettings(file));
if (hasBuildSystemInfo) {
// Assume the builder invokes the compiler in the build directory.
m_environment.setWorkingDirectory(file->project()->buildSystemManager()->buildDirectory(file));
}
} else {
m_environment.addIncludes(IDefinesAndIncludesManager::manager()->includes(tuUrl.str()));
m_environment.addFrameworkDirectories(IDefinesAndIncludesManager::manager()->frameworkDirectories(tuUrl.str()));
m_environment.addDefines(IDefinesAndIncludesManager::manager()->defines(tuUrl.str()));
m_environment.setParserSettings(ClangSettingsManager::self()->parserSettings(tuUrl.str()));
}
const bool isSource = ClangHelpers::isSource(tuUrl.str());
m_environment.setQuality(
isSource ? (hasBuildSystemInfo ? ClangParsingEnvironment::BuildSystem : ClangParsingEnvironment::Source)
: ClangParsingEnvironment::Unknown
);
m_environment.setTranslationUnitUrl(tuUrl);
Path::List projectPaths;
const auto& projects = ICore::self()->projectController()->projects();
projectPaths.reserve(projects.size());
for (auto project : projects) {
projectPaths.append(project->path());
}
m_environment.setProjectPaths(projectPaths);
m_unsavedFiles = ClangUtils::unsavedFiles();
const auto documents = ICore::self()->documentController()->openDocuments();
for (auto* document : documents) {
auto textDocument = document->textDocument();
if ( !textDocument ) {
continue;
}
const IndexedString indexedUrl(textDocument->url());
if (indexedUrl == tuUrl) {
m_tuDocumentIsUnsaved = true;
}
m_unsavedRevisions.insert(indexedUrl, ModificationRevision::revisionForFile(indexedUrl));
}
if (auto tracker = trackerForUrl(url)) {
tracker->reset();
m_options |= ParseSessionData::OpenedInEditor;
} else if (tuUrl != url && trackerForUrl(tuUrl)) {
m_options |= ParseSessionData::OpenedInEditor;
}
}
ClangSupport* ClangParseJob::clang() const
{
return static_cast<ClangSupport*>(languageSupport());
}
void ClangParseJob::run(ThreadWeaver::JobPointer /*self*/, ThreadWeaver::Thread* /*thread*/)
{
QReadLocker parseLock(languageSupport()->parseLock());
if (abortRequested()) {
return;
}
{
const auto tuUrlStr = m_environment.translationUnitUrl().str();
if (!m_tuDocumentIsUnsaved && !QFile::exists(tuUrlStr)) {
// maybe we requested a parse job some time ago but now the file
// does not exist anymore. return early then
clang()->index()->unpinTranslationUnitForUrl(document());
return;
}
m_environment.addIncludes(IDefinesAndIncludesManager::manager()->includesInBackground(tuUrlStr));
m_environment.addFrameworkDirectories(IDefinesAndIncludesManager::manager()->frameworkDirectoriesInBackground(tuUrlStr));
m_environment.addDefines(IDefinesAndIncludesManager::manager()->definesInBackground(tuUrlStr));
m_environment.addParserArguments(IDefinesAndIncludesManager::manager()->parserArgumentsInBackground(tuUrlStr));
m_environment.setPchInclude(userDefinedPchIncludeForFile(tuUrlStr));
}
if (abortRequested()) {
return;
}
// NOTE: we must have all declarations, contexts and uses available for files that are opened in the editor
// it is very hard to check this for all included files of this TU, and previously lead to problems
// when we tried to skip function bodies as an optimization for files that where not open in the editor.
// now, we always build everything, which is correct but a tad bit slower. we can try to optimize later.
setMinimumFeatures(static_cast<TopDUContext::Features>(minimumFeatures() | TopDUContext::AllDeclarationsContextsAndUses));
if (minimumFeatures() & AttachASTWithoutUpdating) {
// The context doesn't need to be updated, but has no AST attached (restored from disk),
// so attach AST to it, without updating DUChain
ParseSession session(createSessionData());
DUChainWriteLocker lock;
auto ctx = DUChainUtils::standardContextForUrl(document().toUrl());
if (!ctx) {
clangDebug() << "Lost context while attaching AST";
return;
}
ctx->setAst(IAstContainer::Ptr(session.data()));
if (minimumFeatures() & UpdateHighlighting) {
lock.unlock();
languageSupport()->codeHighlighting()->highlightDUChain(ctx);
}
return;
}
{
UrlParseLock urlLock(document());
if (abortRequested() || !isUpdateRequired(ParseSession::languageString())) {
return;
}
}
ParseSession session(ClangIntegration::DUChainUtils::findParseSessionData(document(), m_environment.translationUnitUrl()));
if (abortRequested()) {
return;
}
if (!session.data() || !session.reparse(m_unsavedFiles, m_environment)) {
session.setData(createSessionData());
}
if (!session.unit()) {
// failed to parse file, unpin and don't try again
clang()->index()->unpinTranslationUnitForUrl(document());
return;
}
if (!clang_getFile(session.unit(), document().byteArray().constData())) {
// this parse job's document does not exist in the pinned translation unit
// so we need to unpin and re-add this document
// Ideally we'd reset m_environment and session, but this is much simpler
// and shouldn't be a common case
clang()->index()->unpinTranslationUnitForUrl(document());
if (!(minimumFeatures() & Rescheduled)) {
auto features = static_cast<TopDUContext::Features>(minimumFeatures() | Rescheduled);
ICore::self()->languageController()->backgroundParser()->addDocument(document(), features, priority());
}
return;
}
Imports imports = ClangHelpers::tuImports(session.unit());
IncludeFileContexts includedFiles;
if (auto pch = clang()->index()->pch(m_environment)) {
auto pchFile = pch->mapFile(session.unit());
includedFiles = pch->mapIncludes(session.unit());
includedFiles.insert(pchFile, pch->context());
auto tuFile = clang_getFile(session.unit(), m_environment.translationUnitUrl().byteArray().constData());
imports.insert(tuFile, { pchFile, CursorInRevision(0, 0) } );
}
if (abortRequested()) {
return;
}
auto context = ClangHelpers::buildDUChain(session.mainFile(), imports, session,
minimumFeatures(), includedFiles,
clang()->index(), [this] { return abortRequested(); });
setDuChain(context);
if (abortRequested()) {
return;
}
if (context) {
if (minimumFeatures() & TopDUContext::AST) {
DUChainWriteLocker lock;
context->setAst(IAstContainer::Ptr(session.data()));
}
#ifdef QT_DEBUG
DUChainReadLocker lock;
auto file = parsingEnvironmentFile(context);
Q_ASSERT(file);
// verify that features and environment where properly set in ClangHelpers::buildDUChain
Q_ASSERT(file->featuresSatisfied(TopDUContext::Features(minimumFeatures() & ~TopDUContext::ForceUpdateRecursive)));
if (trackerForUrl(context->url())) {
Q_ASSERT(file->featuresSatisfied(TopDUContext::AllDeclarationsContextsAndUses));
}
#endif
}
for (const auto& context : qAsConst(includedFiles)) {
if (!context) {
continue;
}
{
// prefer the editor modification revision, instead of the on-disk revision
auto it = m_unsavedRevisions.find(context->url());
if (it != m_unsavedRevisions.end()) {
DUChainWriteLocker lock;
auto file = parsingEnvironmentFile(context);
Q_ASSERT(file);
file->setModificationRevision(it.value());
}
}
if (trackerForUrl(context->url())) {
if (clang()->index()->translationUnitForUrl(context->url()) == m_environment.translationUnitUrl()) {
// cache the parse session and the contained translation unit for this chain
// this then allows us to quickly reparse the document if it is changed by
// the user
// otherwise no editor component is open for this document and we can dispose
// the TU to save memory
// share the session data with all contexts that are pinned to this TU
DUChainWriteLocker lock;
context->setAst(IAstContainer::Ptr(session.data()));
}
languageSupport()->codeHighlighting()->highlightDUChain(context);
}
}
}
ParseSessionData::Ptr ClangParseJob::createSessionData() const
{
return ParseSessionData::Ptr(new ParseSessionData(m_unsavedFiles, clang()->index(), m_environment, m_options));
}
const ParsingEnvironment* ClangParseJob::environment() const
{
return &m_environment;
}
|