aboutsummaryrefslogtreecommitdiffstats
path: root/src/tools/clangrefactoringbackend/source/symbolscollector.cpp
blob: 1634512b5002f5f3a099e37888b214a90ca2fb12 (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
/****************************************************************************
**
** Copyright (C) 2017 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 "symbolscollector.h"

#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/FrontendActions.h>
#include <clang/Lex/PreprocessorOptions.h>

namespace ClangBackEnd {

SymbolsCollector::SymbolsCollector(Sqlite::Database &database)
    : m_filePathCache(database),
      m_indexDataConsumer(std::make_shared<IndexDataConsumer>(m_symbolEntries, m_sourceLocationEntries, m_filePathCache, m_sourcesManager)),
      m_collectSymbolsAction(m_indexDataConsumer),
      m_collectMacrosSourceFileCallbacks(m_symbolEntries, m_sourceLocationEntries, m_filePathCache, m_sourcesManager)
{
}

void SymbolsCollector::addFiles(const FilePathIds &filePathIds,
                                const Utils::SmallStringVector &arguments)
{
    m_clangTool.addFiles(m_filePathCache.filePaths(filePathIds), arguments);
    m_collectMacrosSourceFileCallbacks.addSourceFiles(filePathIds);
}

void SymbolsCollector::setFile(FilePathId filePathId, const Utils::SmallStringVector &arguments)
{
    addFiles({filePathId}, arguments);
}

void SymbolsCollector::setUnsavedFiles(const V2::FileContainers &unsavedFiles)
{
    m_clangTool.addUnsavedFiles(unsavedFiles);
}

void SymbolsCollector::clear()
{
    m_indexDataConsumer->clear();
    m_collectMacrosSourceFileCallbacks.clear();
    m_symbolEntries.clear();
    m_sourceLocationEntries.clear();
    m_clangTool = ClangTool();
}

template <typename Factory>
std::unique_ptr<clang::tooling::FrontendActionFactory>
newFrontendActionFactory(Factory *consumerFactory,
                         clang::tooling::SourceFileCallbacks *sourceFileCallbacks)
{
    class FrontendActionFactoryAdapter : public clang::tooling::FrontendActionFactory
    {
    public:
        explicit FrontendActionFactoryAdapter(Factory *consumerFactory,
                                              clang::tooling::SourceFileCallbacks *sourceFileCallbacks)
            : m_consumerFactory(consumerFactory),
              m_sourceFileCallbacks(sourceFileCallbacks)
        {}

        clang::FrontendAction *create() override {
            return new ConsumerFactoryAdaptor(m_consumerFactory, m_sourceFileCallbacks);
        }

    private:
        class ConsumerFactoryAdaptor : public clang::ASTFrontendAction {
        public:
            ConsumerFactoryAdaptor(Factory *consumerFactory,
                                   clang::tooling::SourceFileCallbacks *sourceFileCallbacks)
                : m_consumerFactory(consumerFactory),
                  m_sourceFileCallbacks(sourceFileCallbacks)
            {}

            std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(clang::CompilerInstance &instance,
                                                                  llvm::StringRef inFile) override
            {
                return m_consumerFactory->newASTConsumer(instance, inFile);
            }

        protected:
            bool BeginInvocation(clang::CompilerInstance &compilerInstance) override
            {
                compilerInstance.getPreprocessorOpts().AllowPCHWithCompilerErrors = true;
                compilerInstance.getDiagnosticOpts().ErrorLimit = 1;

                return clang::ASTFrontendAction::BeginInvocation(compilerInstance);
            }

            bool BeginSourceFileAction(clang::CompilerInstance &compilerInstance) override
            {
                compilerInstance.getPreprocessor().SetSuppressIncludeNotFoundError(true);

                if (!clang::ASTFrontendAction::BeginSourceFileAction(compilerInstance))
                    return false;
                if (m_sourceFileCallbacks)
                    return m_sourceFileCallbacks->handleBeginSource(compilerInstance);
                return true;
            }

            void EndSourceFileAction() override
            {
                if (m_sourceFileCallbacks)
                    m_sourceFileCallbacks->handleEndSource();
                clang::ASTFrontendAction::EndSourceFileAction();
            }

        private:
            Factory *m_consumerFactory;
            clang::tooling::SourceFileCallbacks *m_sourceFileCallbacks;
        };
        Factory *m_consumerFactory;
        clang::tooling::SourceFileCallbacks *m_sourceFileCallbacks;
    };

  return std::unique_ptr<clang::tooling::FrontendActionFactory>(
      new FrontendActionFactoryAdapter(consumerFactory, sourceFileCallbacks));
}

bool SymbolsCollector::collectSymbols()
{
    auto tool = m_clangTool.createTool();

    auto actionFactory = ClangBackEnd::newFrontendActionFactory(&m_collectSymbolsAction,
                                                                &m_collectMacrosSourceFileCallbacks);

    return tool.run(actionFactory.get()) != 1;
}

void SymbolsCollector::doInMainThreadAfterFinished()
{
}

const SymbolEntries &SymbolsCollector::symbols() const
{
    return m_symbolEntries;
}

const SourceLocationEntries &SymbolsCollector::sourceLocations() const
{
    return m_sourceLocationEntries;
}

const FilePathIds &SymbolsCollector::sourceFiles() const
{
    return m_collectMacrosSourceFileCallbacks.sourceFiles();
}

const UsedMacros &SymbolsCollector::usedMacros() const
{
    return m_collectMacrosSourceFileCallbacks.usedMacros();
}

const FileStatuses &SymbolsCollector::fileStatuses() const
{
    return m_collectMacrosSourceFileCallbacks.fileStatuses();
}

const SourceDependencies &SymbolsCollector::sourceDependencies() const
{
    return m_collectMacrosSourceFileCallbacks.sourceDependencies();
}

bool SymbolsCollector::isUsed() const
{
    return m_isUsed;
}

void SymbolsCollector::setIsUsed(bool isUsed)
{
    m_isUsed = isUsed;
}

} // namespace ClangBackEnd