From f5fde1eaaff08213a895f8051a625ef693222dad Mon Sep 17 00:00:00 2001 From: Ivan Komissarov Date: Sat, 4 Apr 2020 16:53:28 +0200 Subject: clang-tidy: Fix 'bugprone-narrowing-conversions' warnings Also, treat those as errors Change-Id: I0771aa656273fd0a01c7787870d9de9b4c631823 Reviewed-by: Christian Kandeler --- src/lib/corelib/api/projectfileupdater.cpp | 8 +++---- src/lib/corelib/buildgraph/nodetreedumper.cpp | 2 +- src/lib/corelib/buildgraph/nodetreedumper.h | 2 +- src/lib/corelib/jsextensions/binaryfile.cpp | 2 +- src/lib/corelib/jsextensions/fileinfoextension.cpp | 2 +- src/lib/corelib/language/asttools.cpp | 4 ++-- src/lib/corelib/parser/qmljslexer.cpp | 26 +++++++++++++--------- src/plugins/scanner/cpp/Lexer.cpp | 6 ++--- src/plugins/scanner/cpp/cppscanner.cpp | 4 ++-- 9 files changed, 30 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/lib/corelib/api/projectfileupdater.cpp b/src/lib/corelib/api/projectfileupdater.cpp index bc2648adf..0bc5bc7c4 100644 --- a/src/lib/corelib/api/projectfileupdater.cpp +++ b/src/lib/corelib/api/projectfileupdater.cpp @@ -221,7 +221,7 @@ void ProjectFileGroupInserter::doApply(QString &fileContent, UiProgram *ast) Rewriter rewriter(fileContent, &changeSet, QStringList()); QString groupItemString; const int productItemIndentation - = itemFinder.item()->qualifiedTypeNameId->firstSourceLocation().startColumn - 1; + = int(itemFinder.item()->qualifiedTypeNameId->firstSourceLocation().startColumn - 1); const int groupItemIndentation = productItemIndentation + 4; const QString groupItemIndentationString = QString(groupItemIndentation, QLatin1Char(' ')); groupItemString += groupItemIndentationString + QLatin1String("Group {\n"); @@ -251,7 +251,7 @@ static QString getNodeRepresentation(const QString &fileContent, const QbsQmlJS: { const quint32 start = node->firstSourceLocation().offset; const quint32 end = node->lastSourceLocation().end(); - return fileContent.mid(start, end - start); + return fileContent.mid(start, int(end - start)); } static const ChangeSet::EditOp &getEditOp(const ChangeSet &changeSet) @@ -318,7 +318,7 @@ void ProjectFileFilesAdder::doApply(QString &fileContent, UiProgram *ast) } const int itemIndentation - = itemFinder.item()->qualifiedTypeNameId->firstSourceLocation().startColumn - 1; + = int(itemFinder.item()->qualifiedTypeNameId->firstSourceLocation().startColumn - 1); const int bindingIndentation = itemIndentation + 4; const int arrayElemIndentation = bindingIndentation + 4; @@ -443,7 +443,7 @@ void ProjectFileFilesRemover::doApply(QString &fileContent, UiProgram *ast) Rewriter rewriter(fileContent, &changeSet, QStringList()); const int itemIndentation - = itemFinder.item()->qualifiedTypeNameId->firstSourceLocation().startColumn - 1; + = int(itemFinder.item()->qualifiedTypeNameId->firstSourceLocation().startColumn - 1); const int bindingIndentation = itemIndentation + 4; const int arrayElemIndentation = bindingIndentation + 4; diff --git a/src/lib/corelib/buildgraph/nodetreedumper.cpp b/src/lib/corelib/buildgraph/nodetreedumper.cpp index df55bcca3..8475a46cf 100644 --- a/src/lib/corelib/buildgraph/nodetreedumper.cpp +++ b/src/lib/corelib/buildgraph/nodetreedumper.cpp @@ -51,7 +51,7 @@ namespace qbs { namespace Internal { -static unsigned int indentWidth() { return 4; } +static int indentWidth() { return 4; } NodeTreeDumper::NodeTreeDumper(QIODevice &outDevice) : m_outDevice(outDevice) { diff --git a/src/lib/corelib/buildgraph/nodetreedumper.h b/src/lib/corelib/buildgraph/nodetreedumper.h index b171a0cdf..38ccd6dae 100644 --- a/src/lib/corelib/buildgraph/nodetreedumper.h +++ b/src/lib/corelib/buildgraph/nodetreedumper.h @@ -74,7 +74,7 @@ private: QIODevice &m_outDevice; ResolvedProductPtr m_currentProduct; NodeSet m_visited; - unsigned int m_indentation = 0; + int m_indentation = 0; }; } // namespace Internal diff --git a/src/lib/corelib/jsextensions/binaryfile.cpp b/src/lib/corelib/jsextensions/binaryfile.cpp index 5f28f689b..f02f0bff6 100644 --- a/src/lib/corelib/jsextensions/binaryfile.cpp +++ b/src/lib/corelib/jsextensions/binaryfile.cpp @@ -235,7 +235,7 @@ void BinaryFile::write(const QVariantList &data) QByteArray bytes; std::for_each(data.constBegin(), data.constEnd(), [&bytes](const QVariant &v) { - bytes.append(v.toUInt() & 0xFF); }); + bytes.append(char(v.toUInt() & 0xFF)); }); const qint64 size = m_file->write(bytes); if (Q_UNLIKELY(size == -1)) { diff --git a/src/lib/corelib/jsextensions/fileinfoextension.cpp b/src/lib/corelib/jsextensions/fileinfoextension.cpp index f11408098..038f3db41 100644 --- a/src/lib/corelib/jsextensions/fileinfoextension.cpp +++ b/src/lib/corelib/jsextensions/fileinfoextension.cpp @@ -59,7 +59,7 @@ static QString uniqueSeparators(QString path) const auto it = std::unique(path.begin(), path.end(), [](QChar c1, QChar c2) { return c1 == c2 && c1 == QLatin1Char('/'); }); - path.resize(it - path.begin()); + path.resize(int(it - path.begin())); return path; } diff --git a/src/lib/corelib/language/asttools.cpp b/src/lib/corelib/language/asttools.cpp index 617c8b95b..1b6abac7f 100644 --- a/src/lib/corelib/language/asttools.cpp +++ b/src/lib/corelib/language/asttools.cpp @@ -61,13 +61,13 @@ QString textOf(const QString &source, QbsQmlJS::AST::Node *node) if (!node) return {}; return source.mid(node->firstSourceLocation().begin(), - node->lastSourceLocation().end() - node->firstSourceLocation().begin()); + int(node->lastSourceLocation().end() - node->firstSourceLocation().begin())); } QStringRef textRefOf(const QString &source, QbsQmlJS::AST::Node *node) { const quint32 firstBegin = node->firstSourceLocation().begin(); - return source.midRef(firstBegin, node->lastSourceLocation().end() - firstBegin); + return source.midRef(firstBegin, int(node->lastSourceLocation().end() - firstBegin)); } } // namespace Internal diff --git a/src/lib/corelib/parser/qmljslexer.cpp b/src/lib/corelib/parser/qmljslexer.cpp index 9a09ec348..815f1ef0d 100644 --- a/src/lib/corelib/parser/qmljslexer.cpp +++ b/src/lib/corelib/parser/qmljslexer.cpp @@ -178,7 +178,7 @@ int Lexer::lex() _tokenSpell = QStringRef(); _tokenKind = scanToken(); - _tokenLength = _codePtr - _tokenStartPtr - 1; + _tokenLength = int(_codePtr - _tokenStartPtr - 1); _delimited = false; _restrictedKeyword = false; @@ -397,7 +397,8 @@ again: scanChar(); if (_engine) { - _engine->addComment(tokenOffset() + 2, _codePtr - _tokenStartPtr - 1 - 4, + _engine->addComment(tokenOffset() + 2, + int(_codePtr - _tokenStartPtr - 1 - 4), tokenStartLine(), tokenStartColumn() + 2); } @@ -412,7 +413,7 @@ again: scanChar(); } if (_engine) { - _engine->addComment(tokenOffset() + 2, _codePtr - _tokenStartPtr - 1 - 2, + _engine->addComment(tokenOffset() + 2, int(_codePtr - _tokenStartPtr - 1 - 2), tokenStartLine(), tokenStartColumn() + 2); } goto again; @@ -554,7 +555,8 @@ again: if (_char == QLatin1Char('\n') || _char == QLatin1Char('\\')) { break; } else if (_char == quote) { - _tokenSpell = _engine->midRef(startCode - _code.unicode() - 1, _codePtr - startCode); + _tokenSpell = _engine->midRef( + int(startCode - _code.unicode() - 1), int(_codePtr - startCode)); scanChar(); return T_STRING_LITERAL; @@ -706,7 +708,7 @@ again: if (! identifierWithEscapeChars) { identifierWithEscapeChars = true; _tokenText.resize(0); - _tokenText.insert(0, _tokenStartPtr, _codePtr - _tokenStartPtr - 1); + _tokenText.insert(0, _tokenStartPtr, int(_codePtr - _tokenStartPtr - 1)); _validTokenText = true; } @@ -719,7 +721,7 @@ again: return T_ERROR; } } else { - _tokenLength = _codePtr - _tokenStartPtr - 1; + _tokenLength = int(_codePtr - _tokenStartPtr - 1); int kind = T_IDENTIFIER; @@ -727,10 +729,12 @@ again: kind = classify(_tokenStartPtr, _tokenLength, _qmlMode); if (_engine) { - if (kind == T_IDENTIFIER && identifierWithEscapeChars) + if (kind == T_IDENTIFIER && identifierWithEscapeChars) { _tokenSpell = _engine->newStringRef(_tokenText); - else - _tokenSpell = _engine->midRef(_tokenStartPtr - _code.unicode(), _tokenLength); + } else { + _tokenSpell = _engine->midRef( + int(_tokenStartPtr - _code.unicode()), _tokenLength); + } } return kind; @@ -891,7 +895,7 @@ bool Lexer::scanRegExp(RegExpBodyPrefix prefix) scanChar(); } - _tokenLength = _codePtr - _tokenStartPtr - 1; + _tokenLength = int(_codePtr - _tokenStartPtr - 1); return true; case '\\': @@ -995,7 +999,7 @@ int Lexer::tokenEndLine() const int Lexer::tokenEndColumn() const { - return _codePtr - _lastLinePtr; + return int(_codePtr - _lastLinePtr); } QString Lexer::tokenText() const diff --git a/src/plugins/scanner/cpp/Lexer.cpp b/src/plugins/scanner/cpp/Lexer.cpp index 1e31b5ed4..5bf5b2367 100644 --- a/src/plugins/scanner/cpp/Lexer.cpp +++ b/src/plugins/scanner/cpp/Lexer.cpp @@ -224,7 +224,7 @@ void Lexer::scan_helper(Token *tok) goto _Lagain; case '"': case '\'': { - const char quote = ch; + const unsigned char quote = ch; tok->f.kind = quote == '"' ? T_STRING_LITERAL @@ -402,7 +402,7 @@ void Lexer::scan_helper(Token *tok) bool doxy = false; if (_yychar == '*' || _yychar == '!') { - const char ch = _yychar; + const unsigned char ch = _yychar; yyinp(); @@ -608,7 +608,7 @@ void Lexer::scan_helper(Token *tok) ch = _yychar; yyinp(); - const char quote = ch; + const unsigned char quote = ch; tok->f.kind = quote == '"' ? T_WIDE_STRING_LITERAL diff --git a/src/plugins/scanner/cpp/cppscanner.cpp b/src/plugins/scanner/cpp/cppscanner.cpp index 1cebc52eb..4b7f0eb03 100644 --- a/src/plugins/scanner/cpp/cppscanner.cpp +++ b/src/plugins/scanner/cpp/cppscanner.cpp @@ -66,7 +66,7 @@ using namespace CPlusPlus; struct ScanResult { char *fileName; - unsigned int size; + int size; int flags; }; @@ -163,7 +163,7 @@ static void scanCppFile(void *opaq, CPlusPlus::Lexer &yylex, bool scanForFileTag yylex.setScanAngleStringLiteralTokens(false); if (!tk.newline() && (tk.is(T_STRING_LITERAL) || tk.is(T_ANGLE_STRING_LITERAL))) { - scanResult.size = tk.length() - 2; + scanResult.size = int(tk.length() - 2); if (tk.is(T_STRING_LITERAL)) scanResult.flags = SC_LOCAL_INCLUDE_FLAG; else -- cgit v1.2.1