summaryrefslogtreecommitdiff
path: root/src/plugins/clangpchmanager/projectupdater.cpp
blob: e006357e0c52bd09cd109fe4e1dee8702e099c60 (plain)
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/****************************************************************************
**
** 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 "projectupdater.h"

#include "pchmanagerclient.h"

#include <clangindexingprojectsettings.h>
#include <clangindexingsettingsmanager.h>
#include <filepathid.h>
#include <pchmanagerserverinterface.h>
#include <removegeneratedfilesmessage.h>
#include <removeprojectpartsmessage.h>
#include <updategeneratedfilesmessage.h>
#include <updateprojectpartsmessage.h>

#include <cpptools/compileroptionsbuilder.h>
#include <cpptools/projectpart.h>
#include <cpptools/headerpathfilter.h>
#include <projectexplorer/project.h>
#include <projectexplorer/target.h>
#include <projectexplorer/buildconfiguration.h>

#include <utils/algorithm.h>
#include <utils/namevalueitem.h>

#include <QDirIterator>

#include <algorithm>
#include <functional>
#include <iostream>

namespace ClangPchManager {

class HeaderAndSources
{
public:
    void reserve(std::size_t size)
    {
        headers.reserve(size);
        sources.reserve(size);
    }

    ClangBackEnd::FilePathIds headers;
    ClangBackEnd::FilePathIds sources;
};

void ProjectUpdater::updateProjectParts(const std::vector<CppTools::ProjectPart *> &projectParts,
                                        Utils::SmallStringVector &&toolChainArguments)
{
    addProjectFilesToFilePathCache(projectParts);
    fetchProjectPartIds(projectParts);

    m_server.updateProjectParts(
        ClangBackEnd::UpdateProjectPartsMessage{toProjectPartContainers(projectParts),
                                                std::move(toolChainArguments)});
}

void ProjectUpdater::removeProjectParts(ClangBackEnd::ProjectPartIds projectPartIds)
{
    auto sortedIds{projectPartIds};
    std::sort(sortedIds.begin(), sortedIds.end());

    m_server.removeProjectParts(ClangBackEnd::RemoveProjectPartsMessage{std::move(sortedIds)});
}

void ProjectUpdater::updateGeneratedFiles(ClangBackEnd::V2::FileContainers &&generatedFiles)
{
    std::sort(generatedFiles.begin(), generatedFiles.end());

    m_generatedFiles.update(generatedFiles);

    m_excludedPaths = createExcludedPaths(m_generatedFiles.fileContainers());

    m_server.updateGeneratedFiles(
                ClangBackEnd::UpdateGeneratedFilesMessage{std::move(generatedFiles)});
}

void ProjectUpdater::removeGeneratedFiles(ClangBackEnd::FilePaths &&filePaths)
{
    m_generatedFiles.remove(filePaths);

    m_excludedPaths = createExcludedPaths(m_generatedFiles.fileContainers());

    m_server.removeGeneratedFiles(
                ClangBackEnd::RemoveGeneratedFilesMessage{std::move(filePaths)});
}

void ProjectUpdater::setExcludedPaths(ClangBackEnd::FilePaths &&excludedPaths)
{
    m_excludedPaths = std::move(excludedPaths);
}

const ClangBackEnd::FilePaths &ProjectUpdater::excludedPaths() const
{
    return m_excludedPaths;
}

const ClangBackEnd::GeneratedFiles &ProjectUpdater::generatedFiles() const
{
    return m_generatedFiles;
}

void ProjectUpdater::addToHeaderAndSources(HeaderAndSources &headerAndSources,
                                           const CppTools::ProjectFile &projectFile) const
{
    using ClangBackEnd::FilePathView;

    Utils::PathString path = projectFile.path;
    bool exclude = std::binary_search(m_excludedPaths.begin(), m_excludedPaths.end(), path);

    if (!exclude) {
        ClangBackEnd::FilePathId filePathId = m_filePathCache.filePathId(FilePathView(path));
        if (projectFile.isSource())
            headerAndSources.sources.push_back(filePathId);
        else if (projectFile.isHeader())
            headerAndSources.headers.push_back(filePathId);
    }
}

HeaderAndSources ProjectUpdater::headerAndSourcesFromProjectPart(
        CppTools::ProjectPart *projectPart) const
{
    HeaderAndSources headerAndSources;
    headerAndSources.reserve(std::size_t(projectPart->files.size()) * 3 / 2);

    for (const CppTools::ProjectFile &projectFile : projectPart->files) {
        if (projectFile.active)
            addToHeaderAndSources(headerAndSources, projectFile);
    }

    std::sort(headerAndSources.sources.begin(), headerAndSources.sources.end());
    std::sort(headerAndSources.headers.begin(), headerAndSources.headers.end());

    return headerAndSources;
}

QStringList ProjectUpdater::toolChainArguments(CppTools::ProjectPart *projectPart)
{
    using CppTools::CompilerOptionsBuilder;
    CompilerOptionsBuilder builder(*projectPart, CppTools::UseSystemHeader::Yes);

    builder.addWordWidth();
    // builder.addTargetTriple(); TODO resarch why target triples are different
    builder.addExtraCodeModelFlags();
    builder.undefineClangVersionMacrosForMsvc();

    builder.undefineCppLanguageFeatureMacrosForMsvc2015();
    builder.addProjectConfigFileInclude();
    builder.addMsvcCompatibilityVersion();

    return builder.options();
}

namespace {
void cleanupMacros(ClangBackEnd::CompilerMacros &macros)
{
    auto newEnd = std::partition(macros.begin(),
                                 macros.end(),
                                 [](const ClangBackEnd::CompilerMacro &macro) {
                                     return macro.key != "QT_TESTCASE_BUILDDIR";
                                 });

    macros.erase(newEnd, macros.end());
}

void updateWithSettings(ClangBackEnd::CompilerMacros &macros,
                        Utils::NameValueItems &&settingsItems,
                        int &index)
{
    std::sort(settingsItems.begin(), settingsItems.end(), [](const auto &first, const auto &second) {
        return std::tie(first.operation, first.name, first.value)
               < std::tie(first.operation, second.name, second.value);
    });

    auto point = std::partition_point(settingsItems.begin(), settingsItems.end(), [](const auto &entry) {
        return entry.operation == Utils::NameValueItem::SetEnabled;
    });

    std::transform(
        settingsItems.begin(),
        point,
        std::back_inserter(macros),
        [&](const Utils::NameValueItem &settingsMacro) {
            return ClangBackEnd::CompilerMacro{settingsMacro.name, settingsMacro.value, ++index};
        });

    std::sort(macros.begin(), macros.end(), [](const auto &first, const auto &second) {
        return std::tie(first.key, first.value) < std::tie(second.key, second.value);
    });

    ClangBackEnd::CompilerMacros result;
    result.reserve(macros.size());

    ClangBackEnd::CompilerMacros convertedSettingsMacros;
    convertedSettingsMacros.resize(std::distance(point, settingsItems.end()));
    std::transform(
        point,
        settingsItems.end(),
        std::back_inserter(convertedSettingsMacros),
        [&](const Utils::NameValueItem &settingsMacro) {
            return ClangBackEnd::CompilerMacro{settingsMacro.name, settingsMacro.value, ++index};
        });

    std::set_difference(macros.begin(),
                        macros.end(),
                        convertedSettingsMacros.begin(),
                        convertedSettingsMacros.end(),
                        std::back_inserter(result),
                        [](const ClangBackEnd::CompilerMacro &first,
                           const ClangBackEnd::CompilerMacro &second) {
                            return std::tie(first.key, first.value)
                                   < std::tie(second.key, second.value);
                        });

    macros = std::move(result);
}

} // namespace

ClangBackEnd::CompilerMacros ProjectUpdater::createCompilerMacros(
    const ProjectExplorer::Macros &projectMacros, Utils::NameValueItems &&settingsMacros)
{
    int index = 0;
    auto macros = Utils::transform<ClangBackEnd::CompilerMacros>(
        projectMacros, [&](const ProjectExplorer::Macro &macro) {
            return ClangBackEnd::CompilerMacro{macro.key, macro.value, ++index};
        });

    cleanupMacros(macros);
    updateWithSettings(macros, std::move(settingsMacros), index);

    std::sort(macros.begin(), macros.end());

    return macros;
}

namespace  {
ClangBackEnd::IncludeSearchPathType convertType(ProjectExplorer::HeaderPathType sourceType)
{
    using ProjectExplorer::HeaderPathType;
    using ClangBackEnd::IncludeSearchPathType;

    switch (sourceType) {
    case HeaderPathType::User:
        return IncludeSearchPathType::User;
    case HeaderPathType::System:
        return IncludeSearchPathType::System;
    case HeaderPathType::BuiltIn:
        return IncludeSearchPathType::BuiltIn;
    case HeaderPathType::Framework:
        return IncludeSearchPathType::Framework;
    }

    return IncludeSearchPathType::Invalid;
}

ClangBackEnd::IncludeSearchPaths convertToIncludeSearchPaths(ProjectExplorer::HeaderPaths headerPaths)
{
    ClangBackEnd::IncludeSearchPaths paths;
    paths.reserve(Utils::usize(headerPaths));

    int index = 0;
    for (const ProjectExplorer::HeaderPath &headerPath : headerPaths)
        paths.emplace_back(headerPath.path, ++index, convertType(headerPath.type));

    std::sort(paths.begin(), paths.end());

    return paths;
}

ClangBackEnd::IncludeSearchPaths convertToIncludeSearchPaths(
    ProjectExplorer::HeaderPaths headerPaths, ProjectExplorer::HeaderPaths headerPaths2)
{
    ClangBackEnd::IncludeSearchPaths paths;
    paths.reserve(Utils::usize(headerPaths) + Utils::usize(headerPaths2));

    int index = 0;
    for (const ProjectExplorer::HeaderPath &headerPath : headerPaths)
        paths.emplace_back(headerPath.path, ++index, convertType(headerPath.type));

    for (const ProjectExplorer::HeaderPath &headerPath : headerPaths2)
        paths.emplace_back(headerPath.path, ++index, convertType(headerPath.type));

    std::sort(paths.begin(), paths.end());

    return paths;
}

QString projectDirectory(ProjectExplorer::Project *project)
{
    if (project)
        return project->rootProjectDirectory().toString();

    return {};
}

QString buildDirectory(ProjectExplorer::Project *project)
{
    if (project && project->activeTarget() && project->activeTarget()->activeBuildConfiguration())
        return project->activeTarget()->activeBuildConfiguration()->buildDirectory().toString();

    return {};
}
} // namespace

ProjectUpdater::SystemAndProjectIncludeSearchPaths ProjectUpdater::createIncludeSearchPaths(
    const CppTools::ProjectPart &projectPart)
{
    CppTools::HeaderPathFilter filter(projectPart,
                                      CppTools::UseTweakedHeaderPaths::Yes,
                                      CLANG_VERSION,
                                      CLANG_RESOURCE_DIR,
                                      projectDirectory(projectPart.project),
                                      buildDirectory(projectPart.project));
    filter.process();

    return {convertToIncludeSearchPaths(filter.systemHeaderPaths, filter.builtInHeaderPaths),
            convertToIncludeSearchPaths(filter.userHeaderPaths)};
}

ClangBackEnd::ProjectPartContainer ProjectUpdater::toProjectPartContainer(
        CppTools::ProjectPart *projectPart) const
{

    QStringList arguments = toolChainArguments(projectPart);

    HeaderAndSources headerAndSources = headerAndSourcesFromProjectPart(projectPart);

    auto includeSearchPaths = createIncludeSearchPaths(*projectPart);

    ClangBackEnd::ProjectPartId projectPartId = m_projectPartIdCache.stringId(
        Utils::PathString{projectPart->id()}, [&](Utils::SmallStringView projectPartName) {
            return m_projectPartsStorage.fetchProjectPartId(projectPartName);
        });

    ClangIndexingProjectSettings *settings = m_settingsManager.settings(projectPart->project);

    return ClangBackEnd::ProjectPartContainer(projectPartId,
                                              Utils::SmallStringVector(arguments),
                                              createCompilerMacros(projectPart->projectMacros,
                                                                   settings->readMacros()),
                                              std::move(includeSearchPaths.system),
                                              std::move(includeSearchPaths.project),
                                              std::move(headerAndSources.headers),
                                              std::move(headerAndSources.sources),
                                              projectPart->language,
                                              projectPart->languageVersion,
                                              static_cast<Utils::LanguageExtension>(
                                                  int(projectPart->languageExtensions)));
}

ClangBackEnd::ProjectPartContainers ProjectUpdater::toProjectPartContainers(
        std::vector<CppTools::ProjectPart *> projectParts) const
{
    using namespace std::placeholders;

    projectParts.erase(std::remove_if(projectParts.begin(),
                                      projectParts.end(),
                                      [](const CppTools::ProjectPart *projectPart) {
                                          return !projectPart->selectedForBuilding;
                                      }),
                       projectParts.end());

    std::vector<ClangBackEnd::ProjectPartContainer> projectPartContainers;
    projectPartContainers.reserve(projectParts.size());

    std::transform(projectParts.begin(),
                   projectParts.end(),
                   std::back_inserter(projectPartContainers),
                   std::bind(&ProjectUpdater::toProjectPartContainer, this, _1));

    std::sort(projectPartContainers.begin(), projectPartContainers.end());

    return projectPartContainers;
}

ClangBackEnd::FilePaths ProjectUpdater::createExcludedPaths(
        const ClangBackEnd::V2::FileContainers &generatedFiles)
{
    ClangBackEnd::FilePaths excludedPaths;
    excludedPaths.reserve(generatedFiles.size());

    auto convertToPath = [] (const ClangBackEnd::V2::FileContainer &fileContainer) {
        return fileContainer.filePath;
    };

    std::transform(generatedFiles.begin(),
                   generatedFiles.end(),
                   std::back_inserter(excludedPaths),
                   convertToPath);

    std::sort(excludedPaths.begin(), excludedPaths.end());

    return excludedPaths;
}

QString ProjectUpdater::fetchProjectPartName(ClangBackEnd::ProjectPartId projectPartId) const
{
    return QString(m_projectPartIdCache.string(projectPartId.projectPathId,
                                               [&](ClangBackEnd::ProjectPartId projectPartId) {
                                                   return m_projectPartsStorage.fetchProjectPartName(
                                                       projectPartId);
                                               }));
}

ClangBackEnd::ProjectPartIds ProjectUpdater::toProjectPartIds(
    const QStringList &projectPartNamesAsQString) const
{
    ClangBackEnd::ProjectPartIds projectPartIds;
    projectPartIds.reserve(projectPartIds.size());

    auto projectPartNames = Utils::transform<std::vector<Utils::PathString>>(
        projectPartNamesAsQString, [&](const QString &projectPartName) { return projectPartName; });

    return m_projectPartIdCache.stringIds(projectPartNames, [&](Utils::SmallStringView projectPartName) {
        return m_projectPartsStorage.fetchProjectPartId(projectPartName);
    });
}

void ProjectUpdater::addProjectFilesToFilePathCache(const std::vector<CppTools::ProjectPart *> &projectParts)
{
    ClangBackEnd::FilePaths filePaths;
    filePaths.reserve(10000);

    for (CppTools::ProjectPart *projectPart : projectParts) {
        for (const CppTools::ProjectFile &file : projectPart->files)
            if (file.active)
                filePaths.emplace_back(file.path);
    }

    m_filePathCache.addFilePaths(filePaths);
}

void ProjectUpdater::fetchProjectPartIds(const std::vector<CppTools::ProjectPart *> &projectParts)
{
    std::unique_ptr<Sqlite::DeferredTransaction> transaction;

    auto projectPartNames = Utils::transform<std::vector<Utils::PathString>>(
        projectParts, [](CppTools::ProjectPart *projectPart) { return projectPart->id(); });

    auto projectPartNameViews = Utils::transform<std::vector<Utils::SmallStringView>>(
        projectPartNames,
        [](const Utils::PathString &projectPartName) { return projectPartName.toStringView(); });

    m_projectPartIdCache
        .addStrings(std::move(projectPartNameViews), [&](Utils::SmallStringView projectPartName) {
            if (!transaction)
                transaction = std::make_unique<Sqlite::DeferredTransaction>(
                    m_projectPartsStorage.transactionBackend());
            return m_projectPartsStorage.fetchProjectPartIdUnguarded(projectPartName);
        });

    if (transaction)
        transaction->commit();

} // namespace ClangPchManager

} // namespace ClangPchManager