FastPreprocessor.cpp 5.82 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11 12
** 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 Digia.  For licensing terms and
Eike Ziller's avatar
Eike Ziller committed
13 14
** conditions see http://www.qt.io/licensing.  For further information
** use the contact form at http://www.qt.io/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24 25 26
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
30 31

#include "FastPreprocessor.h"
32 33 34 35

#include <cplusplus/Literals.h>
#include <cplusplus/TranslationUnit.h>

36
#include <QDir>
37 38 39 40

using namespace CPlusPlus;

FastPreprocessor::FastPreprocessor(const Snapshot &snapshot)
41 42
    : _snapshot(snapshot)
    , _preproc(this, &_env)
43 44
{ }

45
QByteArray FastPreprocessor::run(Document::Ptr newDoc, const QByteArray &source)
46
{
47 48
    std::swap(newDoc, _currentDoc);
    const QString fileName = _currentDoc->fileName();
49
    _preproc.setExpandFunctionlikeMacros(false);
50
    _preproc.setKeepComments(true);
51

52
    if (Document::Ptr doc = _snapshot.document(fileName)) {
53 54
        _merged.insert(fileName);

55
        for (Snapshot::const_iterator i = _snapshot.begin(), ei = _snapshot.end(); i != ei; ++i) {
56 57
            if (isInjectedFile(i.key().toString()))
                mergeEnvironment(i.key().toString());
58 59
        }

60
        foreach (const Document::Include &i, doc->resolvedIncludes())
61
            mergeEnvironment(i.resolvedFileName());
62 63
    }

64
    const QByteArray preprocessed = _preproc.run(fileName, source);
65
//    qDebug("FastPreprocessor::run for %s produced [[%s]]", fileName.toUtf8().constData(), preprocessed.constData());
66
    std::swap(newDoc, _currentDoc);
67 68 69
    return preprocessed;
}

70 71
void FastPreprocessor::sourceNeeded(unsigned line, const QString &fileName, IncludeType mode,
                                    const QStringList &initialIncludes)
72
{
73
    Q_UNUSED(initialIncludes)
74
    Q_ASSERT(_currentDoc);
75 76
    // CHECKME: Is that cleanName needed?
    QString cleanName = QDir::cleanPath(fileName);
77
    _currentDoc->addIncludeFile(Document::Include(fileName, cleanName, line, mode));
78 79
    mergeEnvironment(fileName);
}
80

81 82 83 84 85
void FastPreprocessor::mergeEnvironment(const QString &fileName)
{
    if (! _merged.contains(fileName)) {
        _merged.insert(fileName);

86
        if (Document::Ptr doc = _snapshot.document(fileName)) {
87
            foreach (const Document::Include &i, doc->resolvedIncludes())
88
                mergeEnvironment(i.resolvedFileName());
89 90 91 92 93

            _env.addMacros(doc->definedMacros());
        }
    }
}
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

void FastPreprocessor::macroAdded(const Macro &macro)
{
    Q_ASSERT(_currentDoc);

    _currentDoc->appendMacro(macro);
}

static const Macro revision(const Snapshot &s, const Macro &m)
{
    if (Document::Ptr d = s.document(m.fileName())) {
        Macro newMacro(m);
        newMacro.setFileRevision(d->revision());
        return newMacro;
    }

    return m;
}

113 114
void FastPreprocessor::passedMacroDefinitionCheck(unsigned bytesOffset, unsigned utf16charsOffset,
                                                  unsigned line, const Macro &macro)
115 116 117 118
{
    Q_ASSERT(_currentDoc);

    _currentDoc->addMacroUse(revision(_snapshot, macro),
119 120 121
                             bytesOffset, macro.name().size(),
                             utf16charsOffset, macro.nameToQString().size(),
                             line, QVector<MacroArgumentReference>());
122 123
}

124 125
void FastPreprocessor::failedMacroDefinitionCheck(unsigned bytesOffset, unsigned utf16charsOffset,
                                                  const ByteArrayRef &name)
126 127 128
{
    Q_ASSERT(_currentDoc);

129 130
    _currentDoc->addUndefinedMacroUse(QByteArray(name.start(), name.size()),
                                      bytesOffset, utf16charsOffset);
131 132
}

133 134
void FastPreprocessor::notifyMacroReference(unsigned bytesOffset, unsigned utf16charsOffset,
                                            unsigned line, const Macro &macro)
135 136 137 138
{
    Q_ASSERT(_currentDoc);

    _currentDoc->addMacroUse(revision(_snapshot, macro),
139 140 141
                             bytesOffset, macro.name().size(),
                             utf16charsOffset, macro.nameToQString().size(),
                             line, QVector<MacroArgumentReference>());
142 143
}

144 145 146
void FastPreprocessor::startExpandingMacro(unsigned bytesOffset, unsigned utf16charsOffset,
                                           unsigned line, const Macro &macro,
                                           const QVector<MacroArgumentReference> &actuals)
147 148 149 150
{
    Q_ASSERT(_currentDoc);

    _currentDoc->addMacroUse(revision(_snapshot, macro),
151 152 153
                             bytesOffset, macro.name().size(),
                             utf16charsOffset, macro.nameToQString().size(),
                             line, actuals);
154
}
155 156 157 158 159 160 161 162

void FastPreprocessor::markAsIncludeGuard(const QByteArray &macroName)
{
    if (!_currentDoc)
        return;

    _currentDoc->setIncludeGuardMacroName(macroName);
}