Moc: streamline how diagnostic messages are printed

If we don't have a valid Symbol to get a line number from, or if the
symbol.lineNum is -1, print a shorter message containing only the file
path. Printing: '/path/to/file👎1' isn't useful (and looks wrong).

Change error/defaultErrorMsg/warning/note() to delegate to one central
method, so that they all behave the same; e.g. previously warning() and
note(), guarded against printing "-1" for the line number, whereas
error() didn't.

This also makes it possible to use error() for reporting other issues
(e.g. the size of generator.strings list exceeding INT_MAX, which will
happen in a later commit).

Change-Id: Iddc96e08315fae415be6a84928f845d7bceb4c5f
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
(cherry picked from commit 9cb08c4c0de1685551c18e3b7958a00afa2a4c6a)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Ahmad Samir 2023-05-31 04:05:43 +03:00 committed by Qt Cherry-pick Bot
parent 059acac8f9
commit 7126a58f6f
3 changed files with 52 additions and 19 deletions

View File

@ -14,43 +14,73 @@ Symbol::LexemStore Symbol::lexemStore;
static const char *error_msg = nullptr;
#ifdef Q_CC_MSVC
#define ErrorFormatString "%s(%d:%d): "
#else
#define ErrorFormatString "%s:%d:%d: "
#endif
/*! \internal
Base implementation for printing diagnostic messages.
static void defaultErrorMsg(const QByteArray &fileName, const Symbol &sym)
For example:
"/path/to/file:line:column: error: %s\n"
'%s' is replaced by \a msg. (Currently "column" is always 1).
If sym.lineNum is -1, the line and column parts aren't printed:
"/path/to/file: error: %s\n"
\a formatStringSuffix specifies the type of the message e.g.:
"error: %s\n"
"warning: %s\n"
"note: %s\n"
"Parse error at %s\n" (from defaultErrorMsg())
*/
void Parser::printMsg(QByteArrayView formatStringSuffix, QByteArrayView msg, const Symbol &sym)
{
fprintf(stderr, ErrorFormatString "error: Parse error at \"%s\"\n",
fileName.constData(), sym.lineNum, 1, sym.lexem().data());
if (sym.lineNum != -1) {
#ifdef Q_CC_MSVC
QByteArray formatString = "%s(%d:%d): " + formatStringSuffix;
#else
QByteArray formatString = "%s:%d:%d: " + formatStringSuffix;
#endif
fprintf(stderr, formatString.constData(),
currentFilenames.top().constData(), sym.lineNum, 1, msg.data());
} else {
QByteArray formatString = "%s: " + formatStringSuffix;
fprintf(stderr, formatString.constData(),
currentFilenames.top().constData(), msg.data());
}
}
void Parser::defaultErrorMsg(const Symbol &sym)
{
if (sym.lineNum != -1)
printMsg("error: Parse error at \"%s\"\n", sym.lexem().data(), sym);
else
printMsg("error: could not parse file\n", "", sym);
}
void Parser::error(const Symbol &sym)
{
defaultErrorMsg(currentFilenames.top(), sym);
defaultErrorMsg(sym);
exit(EXIT_FAILURE);
}
void Parser::error(const char *msg) {
void Parser::error(const char *msg)
{
if (msg || error_msg)
fprintf(stderr, ErrorFormatString "error: %s\n",
currentFilenames.top().constData(), symbol().lineNum, 1, msg?msg:error_msg);
printMsg("error: %s\n",
msg ? msg : error_msg,
index > 0 ? symbol() : Symbol{});
else
defaultErrorMsg(currentFilenames.top(), symbol());
defaultErrorMsg(symbol());
exit(EXIT_FAILURE);
}
void Parser::warning(const char *msg) {
if (displayWarnings && msg)
fprintf(stderr, ErrorFormatString "warning: %s\n",
currentFilenames.top().constData(), qMax(0, index > 0 ? symbol().lineNum : 0), 1, msg);
printMsg("warning: %s\n", msg, index > 0 ? symbol() : Symbol{});
}
void Parser::note(const char *msg) {
if (displayNotes && msg)
fprintf(stderr, ErrorFormatString "note: %s\n",
currentFilenames.top().constData(), qMax(0, index > 0 ? symbol().lineNum : 0), 1, msg);
printMsg("note: %s\n", msg, index > 0 ? symbol() : Symbol{});
}
QT_END_NAMESPACE

View File

@ -5,6 +5,7 @@
#define PARSER_H
#include "symbols.h"
#include <QtCore/qbytearrayview.h>
#include <stack>
@ -48,6 +49,8 @@ public:
Q_NORETURN void error(const char *msg = nullptr);
void warning(const char * = nullptr);
void note(const char * = nullptr);
void defaultErrorMsg(const Symbol &sym);
void printMsg(QByteArrayView formatStringSuffix, QByteArrayView msg, const Symbol &sym);
};

View File

@ -2184,7 +2184,7 @@ void tst_Moc::warnings_data()
<< QStringList()
<< 0
<< QString()
<< QString("standard input:0:1: note: No relevant classes found. No output generated.");
<< QString("standard input: note: No relevant classes found. No output generated.");
// passing "-nn" should suppress "no relevant classes" note
QTest::newRow("-nn")
@ -2326,7 +2326,7 @@ void tst_Moc::warnings_data()
<< QStringList()
<< 1
<< QString("IGNORE_ALL_STDOUT")
<< QString(":-1:1: error: Unexpected character in macro argument list.");
<< QString(": error: Unexpected character in macro argument list.");
QTest::newRow("Missing header warning")
<< QByteArray("class X : public QObject { Q_OBJECT };")