aboutsummaryrefslogtreecommitdiffstats
path: root/src/tools/clangbackend/source/clangtranslationunitupdater.cpp
blob: b70f828d8f7c6226cb09812530cddd76c472a23e (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
/****************************************************************************
**
** 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 "clangtranslationunitupdater.h"

#include "clangbackend_global.h"
#include "clangfilepath.h"
#include "clangstring.h"
#include "clangunsavedfilesshallowarguments.h"

#include <QLoggingCategory>

static Q_LOGGING_CATEGORY(verboseLibLog, "qtc.clangbackend.verboselib", QtWarningMsg);

static bool isVerboseModeEnabled()
{
    return verboseLibLog().isDebugEnabled();
}

namespace ClangBackEnd {

TranslationUnitUpdater::TranslationUnitUpdater(const Utf8String translationUnitId,
                                               CXIndex &index,
                                               CXTranslationUnit &cxTranslationUnit,
                                               const TranslationUnitUpdateInput &updateData)
    : m_cxIndex(index)
    , m_cxTranslationUnit(cxTranslationUnit)
    , m_in(updateData)
{
    m_out.translationUnitId = translationUnitId;
}

TranslationUnitUpdateResult TranslationUnitUpdater::update(UpdateMode mode)
{
    createIndexIfNeeded();

    switch (mode) {
    case UpdateMode::AsNeeded:
        recreateAndParseIfNeeded();
        reparseIfNeeded();
        break;
    case UpdateMode::ParseIfNeeded:
        recreateAndParseIfNeeded();
        break;
    case UpdateMode::ForceReparse:
        reparse();
        break;
    }

    return m_out;
}

void TranslationUnitUpdater::recreateAndParseIfNeeded()
{
    removeTranslationUnitIfProjectPartWasChanged();
    createTranslationUnitIfNeeded();
}

void TranslationUnitUpdater::removeTranslationUnitIfProjectPartWasChanged()
{
    if (m_in.parseNeeded) {
        clang_disposeTranslationUnit(m_cxTranslationUnit);
        m_cxTranslationUnit = nullptr;
    }
}

#define RETURN_TEXT_FOR_CASE(enumValue) case enumValue: return #enumValue
static const char *errorCodeToText(CXErrorCode errorCode)
{
    switch (errorCode) {
        RETURN_TEXT_FOR_CASE(CXError_Success);
        RETURN_TEXT_FOR_CASE(CXError_Failure);
        RETURN_TEXT_FOR_CASE(CXError_Crashed);
        RETURN_TEXT_FOR_CASE(CXError_InvalidArguments);
        RETURN_TEXT_FOR_CASE(CXError_ASTReadError);
    }

    return "UnknownCXErrorCode";
}
#undef RETURN_TEXT_FOR_CASE

void TranslationUnitUpdater::createTranslationUnitIfNeeded()
{
    if (!m_cxTranslationUnit) {
        m_cxTranslationUnit = CXTranslationUnit();

        const auto args = commandLineArguments();
        if (isVerboseModeEnabled())
            args.print();

        UnsavedFilesShallowArguments unsaved = m_in.unsavedFiles.shallowArguments();

        m_parseErrorCode = clang_parseTranslationUnit2(m_cxIndex,
                                                     nullptr,
                                                     args.data(),
                                                     args.count(),
                                                     unsaved.data(),
                                                     unsaved.count(),
                                                     defaultParseOptions(),
                                                     &m_cxTranslationUnit);



        if (parseWasSuccessful()) {
            updateIncludeFilePaths();
            m_out.parseTimePoint = Clock::now();
        } else {
            qWarning() << "Parsing" << m_in.filePath << "failed:"
                       << errorCodeToText(m_parseErrorCode);
            m_out.hasParseOrReparseFailed = true;
        }
    }
}

void TranslationUnitUpdater::reparseIfNeeded()
{
    if (m_in.reparseNeeded)
        reparse();
}

void TranslationUnitUpdater::reparse()
{
    UnsavedFilesShallowArguments unsaved = m_in.unsavedFiles.shallowArguments();

    m_reparseErrorCode = clang_reparseTranslationUnit(
                            m_cxTranslationUnit,
                            unsaved.count(),
                            unsaved.data(),
                            clang_defaultReparseOptions(m_cxTranslationUnit));


    if (reparseWasSuccessful()) {
        updateIncludeFilePaths();

        m_out.reparseTimePoint = Clock::now();
        m_out.needsToBeReparsedChangeTimePoint = m_in.needsToBeReparsedChangeTimePoint;
    } else {
        qWarning() << "Reparsing" << m_in.filePath << "failed:" << m_reparseErrorCode;
        m_out.hasParseOrReparseFailed = true;
    }
}

void TranslationUnitUpdater::updateIncludeFilePaths()
{
    m_out.dependedOnFilePaths.clear();
    m_out.dependedOnFilePaths.insert(m_in.filePath);

    clang_getInclusions(m_cxTranslationUnit,
                        includeCallback,
                        const_cast<TranslationUnitUpdater *>(this));
}

uint TranslationUnitUpdater::defaultParseOptions()
{
    return CXTranslationUnit_CacheCompletionResults
         | CXTranslationUnit_PrecompiledPreamble
         | CXTranslationUnit_CreatePreambleOnFirstParse
         | CXTranslationUnit_SkipFunctionBodies
         | CXTranslationUnit_LimitSkipFunctionBodiesToPreamble
#ifdef IS_SKIPWARNINGSFROMINCLUDEDFILES_SUPPORTED
         | CXTranslationUnit_IgnoreNonErrorsFromIncludedFiles
#endif
         | CXTranslationUnit_IncludeBriefCommentsInCodeCompletion
         | CXTranslationUnit_DetailedPreprocessingRecord
         | CXTranslationUnit_KeepGoing;
}

void TranslationUnitUpdater::createIndexIfNeeded()
{
    if (!m_cxIndex) {
        const bool displayDiagnostics = isVerboseModeEnabled();
        m_cxIndex = clang_createIndex(1, displayDiagnostics);
    }
}

void TranslationUnitUpdater::includeCallback(CXFile included_file,
                                             CXSourceLocation *,
                                             unsigned, CXClientData clientData)
{
    ClangString includeFilePath(clang_getFileName(included_file));

    TranslationUnitUpdater *updater = static_cast<TranslationUnitUpdater *>(clientData);

    updater->m_out.dependedOnFilePaths.insert(FilePath::fromNativeSeparators(includeFilePath));
}

bool TranslationUnitUpdater::parseWasSuccessful() const
{
    return m_parseErrorCode == CXError_Success;
}

bool TranslationUnitUpdater::reparseWasSuccessful() const
{
    return m_reparseErrorCode == 0;
}

CommandLineArguments TranslationUnitUpdater::commandLineArguments() const
{
    return CommandLineArguments(m_in.filePath.constData(),
                                m_in.compilationArguments,
                                isVerboseModeEnabled());
}

} // namespace ClangBackEnd