diff options
author | Ahmad Samir <a.samirh78@gmail.com> | 2023-05-31 04:05:43 +0300 |
---|---|---|
committer | Ahmad Samir <a.samirh78@gmail.com> | 2023-06-05 22:23:01 +0000 |
commit | 9cb08c4c0de1685551c18e3b7958a00afa2a4c6a (patch) | |
tree | 12b615d06d56d2bc67dbaddf2336e734bd0b6021 /src/tools/moc | |
parent | e22f766bda7c405ab4daa27b553d4100dcfa811f (diff) |
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: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).
Pick-to: 6.6
Change-Id: Iddc96e08315fae415be6a84928f845d7bceb4c5f
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/tools/moc')
-rw-r--r-- | src/tools/moc/parser.cpp | 58 | ||||
-rw-r--r-- | src/tools/moc/parser.h | 3 |
2 files changed, 47 insertions, 14 deletions
diff --git a/src/tools/moc/parser.cpp b/src/tools/moc/parser.cpp index 6b0308fc15..f1493518f8 100644 --- a/src/tools/moc/parser.cpp +++ b/src/tools/moc/parser.cpp @@ -14,43 +14,73 @@ Symbol::LexemStore Symbol::lexemStore; static const char *error_msg = nullptr; +/*! \internal + Base implementation for printing diagnostic messages. + + 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) +{ + if (sym.lineNum != -1) { #ifdef Q_CC_MSVC -#define ErrorFormatString "%s(%d:%d): " + QByteArray formatString = "%s(%d:%d): " + formatStringSuffix; #else -#define ErrorFormatString "%s:%d:%d: " + 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()); + } +} -static void defaultErrorMsg(const QByteArray &fileName, const Symbol &sym) +void Parser::defaultErrorMsg(const Symbol &sym) { - fprintf(stderr, ErrorFormatString "error: Parse error at \"%s\"\n", - fileName.constData(), sym.lineNum, 1, sym.lexem().data()); + 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 diff --git a/src/tools/moc/parser.h b/src/tools/moc/parser.h index dc86cdec73..715c441e89 100644 --- a/src/tools/moc/parser.h +++ b/src/tools/moc/parser.h @@ -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); }; |