File: qmakefilevisitor.cpp

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (256 lines) | stat: -rw-r--r-- 10,243 bytes parent folder | download
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
/*****************************************************************************
 * Copyright (c) 2010 Milian Wolff <mail@milianw.de>                         *
 *                                                                           *
 * This program is free software; you can redistribute it and/or modify      *
 * it under the terms of the GNU General Public License as published by      *
 * the Free Software Foundation; either version 2 of the License, or         *
 * (at your option) any later version.                                       *
 *                                                                           *
 * This program is distributed in the hope that it will be useful,           *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
 * GNU General Public License for more details.                              *
 *                                                                           *
 * You should have received a copy of the GNU General Public License         *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.     *
 *****************************************************************************/

#include "qmakefilevisitor.h"

#include "qmakefile.h"
#include "qmakeincludefile.h"

#include "parser/ast.h"

#include <QStringList>
#include <QFileInfo>
#include <QProcessEnvironment>

#include <debug.h>
#define ifDebug(x)

// BEGIN QMakeFileVisitor

QMakeFileVisitor::QMakeFileVisitor(const QMakeVariableResolver* resolver, QMakeFile* baseFile)
    : m_resolver(resolver)
    , m_baseFile(baseFile)
{
}

QMakeFileVisitor::~QMakeFileVisitor()
{
}

void QMakeFileVisitor::setVariables(const VariableMap& vars)
{
    m_variableValues = vars;
}

QMakeVariableResolver::VariableMap QMakeFileVisitor::visitFile(QMake::ProjectAST* node)
{
    visitProject(node);
    return m_variableValues;
}

QStringList QMakeFileVisitor::visitMacro(QMake::ScopeBodyAST* node, const QStringList& arguments)
{
    m_arguments = arguments;
    visitScopeBody(node);
    return m_lastReturn;
}

QStringList QMakeFileVisitor::resolveVariable(const QString& variable, VariableInfo::VariableType type) const
{
    if (type == VariableInfo::QMakeVariable) {
        const auto variableValueIt = m_variableValues.find(variable);
        if (variableValueIt != m_variableValues.end()) {
            return *variableValueIt;
        }
    }

    return m_resolver->resolveVariable(variable, type);
}

QStringList QMakeFileVisitor::getValueList(const QList<QMake::ValueAST*>& list) const
{
    QStringList result;
    for (QMake::ValueAST* v : list) {
        result += resolveVariables(v->value);
    }
    return result;
}

void QMakeFileVisitor::visitFunctionCall(QMake::FunctionCallAST* node)
{
    if (node->identifier->value == QLatin1String("include") || node->identifier->value == QLatin1String("!include")) {
        if (node->args.isEmpty())
            return;
        QStringList arguments = getValueList(node->args);

        ifDebug(qCDebug(KDEV_QMAKE) << "found include" << node->identifier->value << arguments;)
        QString argument = arguments.join(QString()).trimmed();
        if (!argument.isEmpty() && QFileInfo(argument).isRelative()) {
            argument = QFileInfo(m_baseFile->absoluteDir() + QLatin1Char('/') + argument).canonicalFilePath();
        }
        if (argument.isEmpty()) {
            qCWarning(KDEV_QMAKE) << "empty include file detected in line" << node->startLine;
            if (node->identifier->value.startsWith(QLatin1Char('!'))) {
                visitNode(node->body);
            }
            return;
        }
        ifDebug(qCDebug(KDEV_QMAKE) << "Reading Include file:" << argument;)
            QMakeIncludeFile includefile(argument, m_baseFile, m_variableValues);
        bool read = includefile.read();
        ifDebug(qCDebug(KDEV_QMAKE) << "successfully read:" << read;) if (read)
        {
            // TODO: optimize by using variableMap and iterator, don't compare values
            foreach (const QString& var, includefile.variables()) {
                if (m_variableValues.value(var) != includefile.variableValues(var)) {
                    m_variableValues[var] = includefile.variableValues(var);
                }
            }
            if (!node->identifier->value.startsWith(QLatin1Char('!'))) {
                visitNode(node->body);
            }
        }
        else if (node->identifier->value.startsWith(QLatin1Char('!'))) { visitNode(node->body); }
    } else if (node->body && (node->identifier->value == QLatin1String("defineReplace")
                              || node->identifier->value
                                  == QLatin1String("defineTest"))) { // TODO: differentiate between replace and test functions?
        QStringList args = getValueList(node->args);
        if (!args.isEmpty()) {
            m_userMacros[args.first()] = node->body;
        } // TODO: else return error
    } else if (node->identifier->value == QLatin1String("return")) {
        m_lastReturn = getValueList(node->args);
    } else { // TODO: only visit when test function returned true?
        qCWarning(KDEV_QMAKE) << "unhandled function call" << node->identifier->value;
        visitNode(node->body);
    }
}

void QMakeFileVisitor::visitAssignment(QMake::AssignmentAST* node)
{
    QString op = node->op->value;
    const QStringList values = getValueList(node->values);
    if (op == QLatin1String("=")) {
        m_variableValues[node->identifier->value] = values;
    } else if (op == QLatin1String("+=")) {
        m_variableValues[node->identifier->value] += values;
    } else if (op == QLatin1String("-=")) {
        for (const QString& value : values) {
            m_variableValues[node->identifier->value].removeAll(value);
        }
    } else if (op == QLatin1String("*=")) {
        for (const QString& value : values) {
            if (!m_variableValues.value(node->identifier->value).contains(value)) {
                m_variableValues[node->identifier->value].append(value);
            }
        }
    } else if (op == QLatin1String("~=")) {
        if (values.isEmpty())
            return;
        QString value = values.first().trimmed();
        QString regex = value.mid(2, value.indexOf(QLatin1Char('/'), 2));
        QString replacement = value.mid(value.indexOf(QLatin1Char('/'), 2) + 1, value.lastIndexOf(QLatin1Char('/')));
        m_variableValues[node->identifier->value].replaceInStrings(QRegExp(regex), replacement);
    }
}

/// return 1-n for numeric variable, 0 otherwise
int functionArgument(const QString& var)
{
    bool ok;
    int arg = var.toInt(&ok);
    if (!ok) {
        return 0;
    } else {
        return arg;
    }
}

QStringList QMakeFileVisitor::resolveVariables(const QString& var) const
{
    VariableReferenceParser parser;
    parser.setContent(var);
    if (!parser.parse()) {
        qCWarning(KDEV_QMAKE) << "Couldn't parse" << var << "to replace variables in it";
        return QStringList() << var;
    }
    if (parser.variableReferences().isEmpty()) {
        return QStringList() << var;
    }

    /// TODO: multiple vars in one place will make the offsets go bonkers
    QString value = var;
    foreach (const QString& variable, parser.variableReferences()) {
        VariableInfo vi = parser.variableInfo(variable);
        QString varValue;

        switch (vi.type) {
        case VariableInfo::QMakeVariable:
            if (int arg = functionArgument(variable)) {
                if (arg > 0 && arg <= m_arguments.size()) {
                    varValue = m_arguments.at(arg - 1);
                } else {
                    qCWarning(KDEV_QMAKE) << "undefined macro argument:" << variable;
                }
            } else {
                varValue = resolveVariable(variable, vi.type).join(QLatin1Char(' '));
            }
            break;
        case VariableInfo::ShellVariableResolveQMake:
        case VariableInfo::ShellVariableResolveMake:
            /// TODO: make vs qmake time
            varValue = QProcessEnvironment::systemEnvironment().value(variable);
            break;
        case VariableInfo::QtConfigVariable:
            varValue = resolveVariable(variable, vi.type).join(QLatin1Char(' '));
            break;
        case VariableInfo::FunctionCall: {
            QStringList arguments;
            arguments.reserve(vi.positions.size());
            foreach (const VariableInfo::Position& pos, vi.positions) {
                int start = pos.start + 3 + variable.length();
                QString args = value.mid(start, pos.end - start);
                varValue = resolveVariables(args).join(QLatin1Char(' '));
                arguments << varValue;
            }
            varValue = evaluateMacro(variable, arguments).join(QLatin1Char(' '));
            break;
        }
        case VariableInfo::Invalid:
            qCWarning(KDEV_QMAKE) << "invalid qmake variable:" << variable;
            continue;
        }

        foreach (const VariableInfo::Position& pos, vi.positions) {
            value.replace(pos.start, pos.end - pos.start + 1, varValue);
        }
    }

#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
    const QStringList ret = value.split(QLatin1Char(' '), Qt::SkipEmptyParts);
#else
    QStringList ret = value.split(QLatin1Char(' '), QString::SkipEmptyParts);
#endif
    ifDebug(qCDebug(KDEV_QMAKE) << "resolved variable" << var << "to" << ret;) return ret;
}

QStringList QMakeFileVisitor::evaluateMacro(const QString& function, const QStringList& arguments) const
{
    if (function == QLatin1String("qtLibraryTarget")) {
        return QStringList() << arguments.first();
    } ///  TODO: support more built-in qmake functions

    QHash<QString, QMake::ScopeBodyAST*>::const_iterator it = m_userMacros.find(function);
    if (it != m_userMacros.constEnd()) {
        qCDebug(KDEV_QMAKE) << "calling user macro:" << function << arguments;
        QMakeFileVisitor visitor(this, m_baseFile);
        return visitor.visitMacro(it.value(), arguments);
    } else {
        qCWarning(KDEV_QMAKE) << "unhandled macro call:" << function << arguments;
    }
    return QStringList();
}