aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/cppeditor/cppprojectinfogenerator.cpp
blob: 629f44c43a10f9febe1179ccd11ba867c6b5e0ea (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
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "cppprojectinfogenerator.h"

#include "cppprojectfilecategorizer.h"

#include <projectexplorer/headerpath.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/taskhub.h>

#include <utils/qtcassert.h>

#include <QTimer>

#include <set>

using namespace ProjectExplorer;
using namespace Utils;

namespace CppEditor::Internal {

ProjectInfoGenerator::ProjectInfoGenerator(
        const QFutureInterface<ProjectInfo::ConstPtr> &futureInterface,
        const ProjectUpdateInfo &projectUpdateInfo)
    : m_futureInterface(futureInterface)
    , m_projectUpdateInfo(projectUpdateInfo)
{
}

ProjectInfo::ConstPtr ProjectInfoGenerator::generate()
{
    QVector<ProjectPart::ConstPtr> projectParts;
    for (const RawProjectPart &rpp : m_projectUpdateInfo.rawProjectParts) {
        if (m_futureInterface.isCanceled())
            return {};
        for (const ProjectPart::ConstPtr &part : createProjectParts(
                 rpp, m_projectUpdateInfo.projectFilePath)) {
            projectParts << part;
        }
    }
    const auto projectInfo = ProjectInfo::create(m_projectUpdateInfo, projectParts);

    static const auto showWarning = [](const QString &message) {
        QTimer::singleShot(0, TaskHub::instance(), [message] {
            TaskHub::addTask(BuildSystemTask(Task::Warning, message));
        });
    };
    if (m_cToolchainMissing) {
        showWarning(QCoreApplication::translate("CppEditor",
                "The project contains C source files, but the currently active kit "
                "has no C compiler. The code model will not be fully functional."));
    }
    if (m_cxxToolchainMissing) {
        showWarning(QCoreApplication::translate("CppEditor",
                "The project contains C++ source files, but the currently active kit "
                "has no C++ compiler. The code model will not be fully functional."));
    }
    return projectInfo;
}

const QVector<ProjectPart::ConstPtr> ProjectInfoGenerator::createProjectParts(
    const RawProjectPart &rawProjectPart, const FilePath &projectFilePath)
{
    QVector<ProjectPart::ConstPtr> result;
    ProjectFileCategorizer cat(rawProjectPart.displayName,
                               rawProjectPart.files,
                               rawProjectPart.fileIsActive,
                               rawProjectPart.getMimeType);

    if (!cat.hasParts())
        return result;

    if (m_projectUpdateInfo.cxxToolChainInfo.isValid()) {
        if (cat.hasCxxSources()) {
            result << createProjectPart(projectFilePath,
                                        rawProjectPart,
                                        cat.cxxSources(),
                                        cat.partName("C++"),
                                        Language::Cxx,
                                        LanguageExtension::None);
        }
        if (cat.hasObjcxxSources()) {
            result << createProjectPart(projectFilePath,
                                        rawProjectPart,
                                        cat.objcxxSources(),
                                        cat.partName("Obj-C++"),
                                        Language::Cxx,
                                        LanguageExtension::ObjectiveC);
        }
    } else if (cat.hasCxxSources() || cat.hasObjcxxSources()) {
        m_cxxToolchainMissing = true;
    }

    if (m_projectUpdateInfo.cToolChainInfo.isValid()) {
        if (cat.hasCSources()) {
            result << createProjectPart(projectFilePath,
                                        rawProjectPart,
                                        cat.cSources(),
                                        cat.partName("C"),
                                        Language::C,
                                        LanguageExtension::None);
        }

        if (cat.hasObjcSources()) {
            result << createProjectPart(projectFilePath,
                                        rawProjectPart,
                                        cat.objcSources(),
                                        cat.partName("Obj-C"),
                                        Language::C,
                                        LanguageExtension::ObjectiveC);
        }
    } else if (cat.hasCSources() || cat.hasObjcSources()) {
        m_cToolchainMissing = true;
    }

    return result;
}

ProjectPart::ConstPtr ProjectInfoGenerator::createProjectPart(
        const FilePath &projectFilePath,
        const RawProjectPart &rawProjectPart,
        const ProjectFiles &projectFiles,
        const QString &partName,
        Language language,
        LanguageExtensions languageExtensions)
{
    RawProjectPartFlags flags;
    ToolChainInfo tcInfo;
    if (language == Language::C) {
        flags = rawProjectPart.flagsForC;
        tcInfo = m_projectUpdateInfo.cToolChainInfo;
    }
    // Use Cxx toolchain for C projects without C compiler in kit and for C++ code
    if (!tcInfo.isValid()) {
        flags = rawProjectPart.flagsForCxx;
        tcInfo = m_projectUpdateInfo.cxxToolChainInfo;
    }

    return ProjectPart::create(projectFilePath, rawProjectPart, partName, projectFiles,
                               language, languageExtensions, flags, tcInfo);
}

} // namespace CppEditor::Internal