aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/projectexplorer/gnumakeparser.cpp
blob: d31ce499700d67e9bf7a0ed2a7d913ddc5aaeb51 (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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "gnumakeparser.h"

#include "task.h"

#include <utils/qtcassert.h>
#include <utils/temporaryfile.h>

#include <QDir>
#include <QFile>

using namespace Utils;

namespace ProjectExplorer {

namespace {
    // optional full path, make executable name, optional exe extension, optional number in square brackets, colon space
    const char * const MAKEEXEC_PATTERN("^(.*?[/\\\\])?(mingw(32|64)-|g)?make(.exe)?(\\[\\d+\\])?:\\s");
    const char * const MAKEFILE_PATTERN("^((.*?[/\\\\])?[Mm]akefile(\\.[a-zA-Z]+)?):(\\d+):\\s");
}

GnuMakeParser::GnuMakeParser()
{
    setObjectName(QLatin1String("GnuMakeParser"));
    m_makeDir.setPattern(QLatin1String(MAKEEXEC_PATTERN) +
                         QLatin1String("(\\w+) directory .(.+).$"));
    QTC_CHECK(m_makeDir.isValid());
    m_makeLine.setPattern(QLatin1String(MAKEEXEC_PATTERN) + QLatin1String("(.*)$"));
    QTC_CHECK(m_makeLine.isValid());
    m_errorInMakefile.setPattern(QLatin1String(MAKEFILE_PATTERN) + QLatin1String("(.*)$"));
    QTC_CHECK(m_errorInMakefile.isValid());
}

class Result {
public:
    Result() = default;

    QString description;
    bool isFatal = false;
    Task::TaskType type = Task::Error;
};

static Task::TaskType taskTypeFromDescription(const QString &description)
{
    if (description.contains(". Stop."))
        return Task::Error;
    if (description.contains("not found"))
        return Task::Error;
    if (description.contains("No rule to make target"))
        return Task::Error;
    // Extend as needed.
    return Task::Warning;
}

static Result parseDescription(const QString &description)
{
    Result result;
    if (description.startsWith(QLatin1String("warning: "), Qt::CaseInsensitive)) {
        result.description = description.mid(9);
        result.type = Task::Warning;
        result.isFatal = false;
    } else if (description.startsWith(QLatin1String("*** "))) {
        result.description = description.mid(4);
        result.type = Task::Error;
        result.isFatal = true;
    } else {
        result.description = description;
        result.type = taskTypeFromDescription(description);
        result.isFatal = false;
    }
    return result;
}

void GnuMakeParser::emitTask(const ProjectExplorer::Task &task)
{
    if (task.type == Task::Error) // Assume that all make errors will be follow up errors.
        m_suppressIssues = true;
    scheduleTask(task, 1, 0);
}

OutputLineParser::Result GnuMakeParser::handleLine(const QString &line, OutputFormat type)
{
    const QString lne = rightTrimmed(line);
    if (type == StdOutFormat) {
        QRegularExpressionMatch match = m_makeDir.match(lne);
        if (match.hasMatch()) {
            if (match.captured(6) == QLatin1String("Leaving"))
                emit searchDirExpired(FilePath::fromString(match.captured(7)));
            else
                emit newSearchDirFound(FilePath::fromString(match.captured(7)));
            return Status::Done;
        }
        return Status::NotHandled;
    }
    QRegularExpressionMatch match = m_errorInMakefile.match(lne);
    if (match.hasMatch()) {
        ProjectExplorer::Result res = parseDescription(match.captured(5));
        if (res.isFatal)
            ++m_fatalErrorCount;
        LinkSpecs linkSpecs;
        if (!m_suppressIssues) {
            const FilePath file = absoluteFilePath(FilePath::fromUserInput(match.captured(1)));
            const int lineNo = match.captured(4).toInt();
            addLinkSpecForAbsoluteFilePath(linkSpecs, file, lineNo, match, 1);
            emitTask(BuildSystemTask(res.type, res.description, file, lineNo));
        }
        return {Status::Done, linkSpecs};
    }
    match = m_makeLine.match(lne);
    if (match.hasMatch()) {
        ProjectExplorer::Result res = parseDescription(match.captured(6));
        if (res.isFatal)
            ++m_fatalErrorCount;
        if (!m_suppressIssues)
            emitTask(BuildSystemTask(res.type, res.description));
        return Status::Done;
    }

    return Status::NotHandled;
}

bool GnuMakeParser::hasFatalErrors() const
{
    return m_fatalErrorCount > 0;
}

} // ProjectExplorer

#ifdef WITH_TESTS
#   include <QTest>

#   include <QUuid>

#   include "outputparser_test.h"
#   include "projectexplorer_test.h"

namespace ProjectExplorer::Internal {

class GnuMakeParserTester : public QObject
{
public:
    explicit GnuMakeParserTester(GnuMakeParser *p, QObject *parent = nullptr) :
        QObject(parent),
        parser(p)
    { }

    void parserIsAboutToBeDeleted()
    {
        directories = parser->searchDirectories();
    }

    FilePaths directories;
    GnuMakeParser *parser;
};

void ProjectExplorerTest::testGnuMakeParserParsing_data()
{
    QTest::addColumn<QStringList>("extraSearchDirs");
    QTest::addColumn<QString>("input");
    QTest::addColumn<OutputParserTester::Channel>("inputChannel");
    QTest::addColumn<QString>("childStdOutLines");
    QTest::addColumn<QString>("childStdErrLines");
    QTest::addColumn<Tasks >("tasks");
    QTest::addColumn<QString>("outputLines");
    QTest::addColumn<QStringList>("additionalSearchDirs");

    QTest::newRow("pass-through stdout")
            << QStringList()
            << QString::fromLatin1("Sometext") << OutputParserTester::STDOUT
            << QString::fromLatin1("Sometext\n") << QString()
            << Tasks()
            << QString()
            << QStringList();
    QTest::newRow("pass-through stderr")
            << QStringList()
            << QString::fromLatin1("Sometext") << OutputParserTester::STDERR
            << QString() << QString::fromLatin1("Sometext\n")
            << Tasks()
            << QString()
            << QStringList();
    QTest::newRow("pass-through gcc infos")
            << QStringList()
            << QString::fromLatin1("/temp/test/untitled8/main.cpp: In function `int main(int, char**)':\n"
                                   "../../scriptbug/main.cpp: At global scope:\n"
                                   "../../scriptbug/main.cpp: In instantiation of void bar(i) [with i = double]:\n"
                                   "../../scriptbug/main.cpp:8: instantiated from void foo(i) [with i = double]\n"
                                   "../../scriptbug/main.cpp:22: instantiated from here")
            << OutputParserTester::STDERR
            << QString()
            << QString::fromLatin1("/temp/test/untitled8/main.cpp: In function `int main(int, char**)':\n"
                                   "../../scriptbug/main.cpp: At global scope:\n"
                                   "../../scriptbug/main.cpp: In instantiation of void bar(i) [with i = double]:\n"
                                   "../../scriptbug/main.cpp:8: instantiated from void foo(i) [with i = double]\n"
                                   "../../scriptbug/main.cpp:22: instantiated from here\n")
            << Tasks()
            << QString()
            << QStringList();

    // make sure adding directories works (once;-)
    QTest::newRow("entering directory")
            << QStringList("/test/dir")
            << QString::fromLatin1("make[4]: Entering directory `/home/code/build/qt/examples/opengl/grabber'\n"
                                   "make[4]: Entering directory `/home/code/build/qt/examples/opengl/grabber'")
            << OutputParserTester::STDOUT
            << QString() << QString()
            << Tasks()
            << QString()
            << QStringList({"/home/code/build/qt/examples/opengl/grabber",
                            "/home/code/build/qt/examples/opengl/grabber", "/test/dir"});
    QTest::newRow("leaving directory")
            << QStringList({"/home/code/build/qt/examples/opengl/grabber", "/test/dir"})
            << QString::fromLatin1("make[4]: Leaving directory `/home/code/build/qt/examples/opengl/grabber'")
            << OutputParserTester::STDOUT
            << QString() << QString()
            << Tasks()
            << QString()
            << QStringList("/test/dir");

    QTest::newRow("make error")
            << QStringList()
            << QString::fromLatin1("make: *** No rule to make target `hello.c', needed by `hello.o'.  Stop.")
            << OutputParserTester::STDERR
            << QString() << QString()
            << (Tasks()
                << BuildSystemTask(Task::Error,
                                   "No rule to make target `hello.c', needed by `hello.o'.  Stop."))
            << QString()
            << QStringList();

    QTest::newRow("multiple fatals")
            << QStringList()
            << QString::fromLatin1("make[3]: *** [.obj/debug-shared/gnumakeparser.o] Error 1\n"
                                   "make[3]: *** Waiting for unfinished jobs....\n"
                                   "make[2]: *** [sub-projectexplorer-make_default] Error 2")
            << OutputParserTester::STDERR
            << QString() << QString()
            << (Tasks()
                << BuildSystemTask(Task::Error,
                                   "[.obj/debug-shared/gnumakeparser.o] Error 1"))
            << QString()
            << QStringList();

    QTest::newRow("Makefile error")
            << QStringList()
            << QString::fromLatin1("Makefile:360: *** missing separator (did you mean TAB instead of 8 spaces?). Stop.")
            << OutputParserTester::STDERR
            << QString() << QString()
            << (Tasks()
                << BuildSystemTask(Task::Error,
                                   "missing separator (did you mean TAB instead of 8 spaces?). Stop.",
                                   Utils::FilePath::fromUserInput("Makefile"), 360))
            << QString()
            << QStringList();

    QTest::newRow("mingw32-make error")
            << QStringList()
            << QString::fromLatin1("mingw32-make[1]: *** [debug/qplotaxis.o] Error 1\n"
                                   "mingw32-make: *** [debug] Error 2")
            << OutputParserTester::STDERR
            << QString() << QString()
            << (Tasks()
                << BuildSystemTask(Task::Error,
                                   "[debug/qplotaxis.o] Error 1"))
            << QString()
            << QStringList();

    QTest::newRow("mingw64-make error")
            << QStringList()
            << QString::fromLatin1("mingw64-make.exe[1]: *** [dynlib.inst] Error -1073741819")
            << OutputParserTester::STDERR
            << QString() << QString()
            << (Tasks()
                << BuildSystemTask(Task::Error,
                                   "[dynlib.inst] Error -1073741819"))
            << QString()
            << QStringList();

    QTest::newRow("make warning")
            << QStringList()
            << QString::fromLatin1("make[2]: warning: jobserver unavailable: using -j1. Add `+' to parent make rule.")
            << OutputParserTester::STDERR
            << QString() << QString()
            << (Tasks()
                << BuildSystemTask(Task::Warning,
                                   "jobserver unavailable: using -j1. Add `+' to parent make rule."))
            << QString()
            << QStringList();

    QTest::newRow("pass-trough note")
            << QStringList()
            << QString::fromLatin1("/home/dev/creator/share/qtcreator/debugger/dumper.cpp:1079: note: initialized from here")
            << OutputParserTester::STDERR
            << QString() << QString::fromLatin1("/home/dev/creator/share/qtcreator/debugger/dumper.cpp:1079: note: initialized from here\n")
            << Tasks()
            << QString()
            << QStringList();

    QTest::newRow("Full path make exe")
            << QStringList()
            << QString::fromLatin1("C:\\Qt\\4.6.2-Symbian\\s60sdk\\epoc32\\tools\\make.exe: *** [sis] Error 2")
            << OutputParserTester::STDERR
            << QString() << QString()
            << (Tasks()
                << BuildSystemTask(Task::Error,
                                   "[sis] Error 2"))
            << QString()
            << QStringList();

    QTest::newRow("missing g++")
            << QStringList()
            << QString::fromLatin1("make: g++: Command not found")
            << OutputParserTester::STDERR
            << QString() << QString()
            << (Tasks()
                << BuildSystemTask(Task::Error,
                                   "g++: Command not found"))
            << QString()
            << QStringList();

    QTest::newRow("warning in Makefile")
            << QStringList()
            << QString::fromLatin1("Makefile:794: warning: overriding commands for target `xxxx.app/Contents/Info.plist'")
            << OutputParserTester::STDERR
            << QString() << QString()
            << (Tasks()
                << BuildSystemTask(Task::Warning,
                                   "overriding commands for target `xxxx.app/Contents/Info.plist'",
                                   "Makefile", 794))
            << QString()
            << QStringList();
}

void ProjectExplorerTest::testGnuMakeParserParsing()
{
    OutputParserTester testbench;
    auto *childParser = new GnuMakeParser;
    auto *tester = new GnuMakeParserTester(childParser);
    connect(&testbench, &OutputParserTester::aboutToDeleteParser,
            tester, &GnuMakeParserTester::parserIsAboutToBeDeleted);

    testbench.addLineParser(childParser);
    QFETCH(QStringList, extraSearchDirs);
    QFETCH(QString, input);
    QFETCH(OutputParserTester::Channel, inputChannel);
    QFETCH(Tasks, tasks);
    QFETCH(QString, childStdOutLines);
    QFETCH(QString, childStdErrLines);
    QFETCH(QString, outputLines);
    QFETCH(QStringList, additionalSearchDirs);

    FilePaths searchDirs = childParser->searchDirectories();

    // add extra directories:
    for (const QString &dir : std::as_const(extraSearchDirs))
        testbench.addSearchDir(FilePath::fromString(dir));

    testbench.testParsing(input, inputChannel,
                          tasks, childStdOutLines, childStdErrLines,
                          outputLines);

    // make sure we still have all the original dirs
    FilePaths newSearchDirs = tester->directories;
    for (const FilePath &dir : std::as_const(searchDirs)) {
        QVERIFY(newSearchDirs.contains(dir));
        newSearchDirs.removeOne(dir);
    }

    // make sure we have all additional dirs:
    for (const QString &dir : std::as_const(additionalSearchDirs)) {
        const FilePath fp = FilePath::fromString(dir);
        QVERIFY(newSearchDirs.contains(fp));
        newSearchDirs.removeOne(fp);
    }
    // make sure we have no extra cruft:
    QVERIFY(newSearchDirs.isEmpty());
    delete tester;
}

void ProjectExplorerTest::testGnuMakeParserTaskMangling()
{
    TemporaryFile theMakeFile("Makefile.XXXXXX");
    QVERIFY2(theMakeFile.open(), qPrintable(theMakeFile.errorString()));
    QFileInfo fi(theMakeFile);
    QVERIFY2(fi.fileName().startsWith("Makefile"), qPrintable(theMakeFile.fileName()));

    OutputParserTester testbench;
    auto *childParser = new GnuMakeParser;
    testbench.addLineParser(childParser);
    childParser->addSearchDir(FilePath::fromString(fi.absolutePath()));
    testbench.testParsing(
        fi.fileName() + ":360: *** missing separator (did you mean TAB instead of 8 spaces?). Stop.",
        OutputParserTester::STDERR,
        {BuildSystemTask(Task::Error,
                         "missing separator (did you mean TAB instead of 8 spaces?). Stop.",
                         FilePath::fromString(theMakeFile.fileName()), 360)},
        QString(), QString(), QString());
}

} // ProjectExplorer::Internal

#endif // WITH_TESTS