QmlJS: Fix compiler warnings

Change-Id: I94fccd913a4a06fb55acee3df974859f9a163b4f
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
This commit is contained in:
Eike Ziller
2019-09-04 08:59:07 +02:00
parent 6ef98541f7
commit 8d85a7c2bc
3 changed files with 55 additions and 55 deletions

View File

@@ -388,7 +388,7 @@ protected:
QTC_ASSERT(_block == 0, _block = 0); QTC_ASSERT(_block == 0, _block = 0);
} }
void postVisit(Node *ast) void postVisit(Node *ast) override
{ {
if (!_seenNonDeclarationStatement && ast->statementCast() if (!_seenNonDeclarationStatement && ast->statementCast()
&& !cast<VariableStatement *>(ast)) { && !cast<VariableStatement *>(ast)) {
@@ -396,7 +396,7 @@ protected:
} }
} }
bool visit(IdentifierExpression *ast) bool visit(IdentifierExpression *ast) override
{ {
if (ast->name.isEmpty()) if (ast->name.isEmpty())
return false; return false;
@@ -409,14 +409,14 @@ protected:
return false; return false;
} }
bool visit(VariableStatement *ast) bool visit(VariableStatement *ast) override
{ {
if (_seenNonDeclarationStatement) if (_seenNonDeclarationStatement)
addMessage(HintDeclarationsShouldBeAtStartOfFunction, ast->declarationKindToken); addMessage(HintDeclarationsShouldBeAtStartOfFunction, ast->declarationKindToken);
return true; return true;
} }
bool visit(PatternElement *ast) bool visit(PatternElement *ast) override
{ {
if (ast->bindingIdentifier.isEmpty() || !ast->isVariableDeclaration()) if (ast->bindingIdentifier.isEmpty() || !ast->isVariableDeclaration())
return true; return true;
@@ -433,12 +433,13 @@ protected:
if (_declaredVariables.contains(name)) { if (_declaredVariables.contains(name)) {
addMessage(WarnDuplicateDeclaration, ast->identifierToken, name); addMessage(WarnDuplicateDeclaration, ast->identifierToken, name);
} else { } else {
for (auto k : _declaredBlockVariables.keys()) { const auto found = std::find_if(_declaredBlockVariables.keyBegin(),
if (k.first == name) { _declaredBlockVariables.keyEnd(),
addMessage(WarnDuplicateDeclaration, ast->identifierToken, name); [name](const auto &key) {
break; return key.first == name;
} });
} if (found != _declaredBlockVariables.keyEnd())
addMessage(WarnDuplicateDeclaration, ast->identifierToken, name);
} }
} }
@@ -456,7 +457,7 @@ protected:
return true; return true;
} }
bool visit(FunctionDeclaration *ast) bool visit(FunctionDeclaration *ast) override
{ {
if (_seenNonDeclarationStatement) if (_seenNonDeclarationStatement)
addMessage(HintDeclarationsShouldBeAtStartOfFunction, ast->functionToken); addMessage(HintDeclarationsShouldBeAtStartOfFunction, ast->functionToken);
@@ -464,7 +465,7 @@ protected:
return visit(static_cast<FunctionExpression *>(ast)); return visit(static_cast<FunctionExpression *>(ast));
} }
bool visit(FunctionExpression *ast) bool visit(FunctionExpression *ast) override
{ {
if (ast->name.isEmpty()) if (ast->name.isEmpty())
return false; return false;
@@ -879,7 +880,7 @@ static bool checkTopLevelBindingForParentReference(ExpressionStatement *expStmt,
return false; return false;
SourceLocation location = locationFromRange(expStmt->firstSourceLocation(), expStmt->lastSourceLocation()); SourceLocation location = locationFromRange(expStmt->firstSourceLocation(), expStmt->lastSourceLocation());
QString stmtSource = source.mid(location.begin(), location.length); QString stmtSource = source.mid(int(location.begin()), int(location.length));
if (stmtSource.contains(QRegExp("(^|\\W)parent\\."))) if (stmtSource.contains(QRegExp("(^|\\W)parent\\.")))
return true; return true;
@@ -1245,8 +1246,8 @@ bool Check::visit(BinaryExpression *ast)
// check spacing // check spacing
SourceLocation op = ast->operatorToken; SourceLocation op = ast->operatorToken;
if ((op.begin() > 0 && !source.at(op.begin() - 1).isSpace()) if ((op.begin() > 0 && !source.at(int(op.begin()) - 1).isSpace())
|| (int(op.end()) < source.size() && !source.at(op.end()).isSpace())) { || (int(op.end()) < source.size() && !source.at(int(op.end())).isSpace())) {
addMessage(HintBinaryOperatorSpacing, op); addMessage(HintBinaryOperatorSpacing, op);
} }
@@ -1281,15 +1282,13 @@ bool Check::visit(BinaryExpression *ast)
} }
if (int(op.end()) + 1 < source.size()) { if (int(op.end()) + 1 < source.size()) {
const QChar next = source.at(op.end()); const QChar next = source.at(int(op.end()));
if (next.isSpace() && next != newline if (next.isSpace() && next != newline && source.at(int(op.end()) + 1) == match)
&& source.at(op.end() + 1) == match) addMessage(msg, SourceLocation((op.begin()), 3, op.startLine, op.startColumn));
addMessage(msg, SourceLocation(op.begin(), 3, op.startLine, op.startColumn));
} }
if (op.begin() >= 2) { if (op.begin() >= 2) {
const QChar prev = source.at(op.begin() - 1); const QChar prev = source.at(int(op.begin()) - 1);
if (prev.isSpace() && prev != newline if (prev.isSpace() && prev != newline && source.at(int(op.begin()) - 2) == match)
&& source.at(op.begin() - 2) == match)
addMessage(msg, SourceLocation(op.begin() - 2, 3, op.startLine, op.startColumn - 2)); addMessage(msg, SourceLocation(op.begin() - 2, 3, op.startLine, op.startColumn - 2));
} }
} }
@@ -1379,6 +1378,7 @@ bool Check::visit(ExpressionStatement *ast)
case QSOperator::InplaceURightShift: case QSOperator::InplaceURightShift:
case QSOperator::InplaceXor: case QSOperator::InplaceXor:
ok = true; ok = true;
break;
default: break; default: break;
} }
} }
@@ -1511,8 +1511,9 @@ static bool hasOnlySpaces(const QString &s)
void Check::addMessage(const Message &message) void Check::addMessage(const Message &message)
{ {
if (message.isValid() && _enabledMessages.contains(message.type)) { if (message.isValid() && _enabledMessages.contains(message.type)) {
if (m_disabledMessageTypesByLine.contains(message.location.startLine)) { if (m_disabledMessageTypesByLine.contains(int(message.location.startLine))) {
QList<MessageTypeAndSuppression> &disabledMessages = m_disabledMessageTypesByLine[message.location.startLine]; QList<MessageTypeAndSuppression> &disabledMessages
= m_disabledMessageTypesByLine[int(message.location.startLine)];
for (int i = 0; i < disabledMessages.size(); ++i) { for (int i = 0; i < disabledMessages.size(); ++i) {
if (disabledMessages[i].type == message.type) { if (disabledMessages[i].type == message.type) {
disabledMessages[i].wasSuppressed = true; disabledMessages[i].wasSuppressed = true;
@@ -1536,7 +1537,7 @@ void Check::scanCommentsForAnnotations()
QRegExp disableCommentPattern(Message::suppressionPattern()); QRegExp disableCommentPattern(Message::suppressionPattern());
foreach (const SourceLocation &commentLoc, _doc->engine()->comments()) { foreach (const SourceLocation &commentLoc, _doc->engine()->comments()) {
const QString &comment = _doc->source().mid(commentLoc.begin(), commentLoc.length); const QString &comment = _doc->source().mid(int(commentLoc.begin()), int(commentLoc.length));
// enable all checks annotation // enable all checks annotation
if (comment.contains("@enable-all-checks")) if (comment.contains("@enable-all-checks"))
@@ -1552,25 +1553,26 @@ void Check::scanCommentsForAnnotations()
MessageTypeAndSuppression entry; MessageTypeAndSuppression entry;
entry.type = static_cast<StaticAnalysis::Type>(disableCommentPattern.cap(1).toInt()); entry.type = static_cast<StaticAnalysis::Type>(disableCommentPattern.cap(1).toInt());
entry.wasSuppressed = false; entry.wasSuppressed = false;
entry.suppressionSource = SourceLocation(commentLoc.offset + lastOffset, entry.suppressionSource = SourceLocation(commentLoc.offset + quint32(lastOffset),
disableCommentPattern.matchedLength(), quint32(disableCommentPattern.matchedLength()),
commentLoc.startLine, commentLoc.startLine,
commentLoc.startColumn + lastOffset); commentLoc.startColumn + quint32(lastOffset));
disabledMessageTypes += entry; disabledMessageTypes += entry;
} }
if (!disabledMessageTypes.isEmpty()) { if (!disabledMessageTypes.isEmpty()) {
int appliesToLine = commentLoc.startLine; quint32 appliesToLine = commentLoc.startLine;
// if the comment is preceded by spaces only, it applies to the next line // if the comment is preceded by spaces only, it applies to the next line
// note: startColumn is 1-based and *after* the starting // or /* // note: startColumn is 1-based and *after* the starting // or /*
if (commentLoc.startColumn >= 3) { if (commentLoc.startColumn >= 3) {
const QString &beforeComment = _doc->source().mid(commentLoc.begin() - commentLoc.startColumn + 1, const QString &beforeComment = _doc->source().mid(int(commentLoc.begin()
commentLoc.startColumn - 3); - commentLoc.startColumn + 1),
int(commentLoc.startColumn) - 3);
if (hasOnlySpaces(beforeComment)) if (hasOnlySpaces(beforeComment))
++appliesToLine; ++appliesToLine;
} }
m_disabledMessageTypesByLine[appliesToLine] += disabledMessageTypes; m_disabledMessageTypesByLine[int(appliesToLine)] += disabledMessageTypes;
} }
} }
} }
@@ -1859,7 +1861,8 @@ void Check::checkCaseFallthrough(StatementList *statements, SourceLocation error
|| comment.end() > nextLoc.begin()) || comment.end() > nextLoc.begin())
continue; continue;
const QString &commentText = _doc->source().mid(comment.begin(), comment.length); const QString &commentText = _doc->source().mid(int(comment.begin()),
int(comment.length));
if (commentText.contains("fall through") if (commentText.contains("fall through")
|| commentText.contains("fall-through") || commentText.contains("fall-through")
|| commentText.contains("fallthrough")) { || commentText.contains("fallthrough")) {

View File

@@ -39,6 +39,8 @@
#include <algorithm> #include <algorithm>
static constexpr int sizeofQChar = int(sizeof(QChar));
using namespace QmlJS; using namespace QmlJS;
using namespace QmlJS::AST; using namespace QmlJS::AST;
@@ -248,12 +250,6 @@ public:
{} {}
void pragmaLibrary(int line, int column)
{
isLibrary = true;
addLocation(line, column);
}
void importFile(const QString &jsfile, const QString &module, void importFile(const QString &jsfile, const QString &module,
int line, int column) override int line, int column) override
{ {
@@ -306,12 +302,13 @@ bool Document::parse_helper(int startToken)
_parsedCorrectly = parser.parse(); _parsedCorrectly = parser.parse();
break; break;
case QmlJSGrammar::T_FEED_JS_SCRIPT: case QmlJSGrammar::T_FEED_JS_SCRIPT:
case QmlJSGrammar::T_FEED_JS_MODULE: case QmlJSGrammar::T_FEED_JS_MODULE: {
_parsedCorrectly = parser.parseProgram(); _parsedCorrectly = parser.parseProgram();
for (const auto &d: directives.locations()) { const QList<SourceLocation> locations = directives.locations();
for (const auto &d : locations) {
_jsdirectives << d; _jsdirectives << d;
} }
break; } break;
case QmlJSGrammar::T_FEED_JS_EXPRESSION: case QmlJSGrammar::T_FEED_JS_EXPRESSION:
_parsedCorrectly = parser.parseExpression(); _parsedCorrectly = parser.parseExpression();
@@ -383,10 +380,6 @@ LibraryInfo::LibraryInfo(const QmlDirParser &parser, const QByteArray &fingerpri
updateFingerprint(); updateFingerprint();
} }
LibraryInfo::~LibraryInfo()
{
}
QByteArray LibraryInfo::calculateFingerprint() const QByteArray LibraryInfo::calculateFingerprint() const
{ {
QCryptographicHash hash(QCryptographicHash::Sha1); QCryptographicHash hash(QCryptographicHash::Sha1);
@@ -396,12 +389,14 @@ QByteArray LibraryInfo::calculateFingerprint() const
foreach (const QmlDirParser::Component &component, _components) { foreach (const QmlDirParser::Component &component, _components) {
len = component.fileName.size(); len = component.fileName.size();
hash.addData(reinterpret_cast<const char *>(&len), sizeof(len)); hash.addData(reinterpret_cast<const char *>(&len), sizeof(len));
hash.addData(reinterpret_cast<const char *>(component.fileName.constData()), len * sizeof(QChar)); hash.addData(reinterpret_cast<const char *>(component.fileName.constData()),
len * sizeofQChar);
hash.addData(reinterpret_cast<const char *>(&component.majorVersion), sizeof(component.majorVersion)); hash.addData(reinterpret_cast<const char *>(&component.majorVersion), sizeof(component.majorVersion));
hash.addData(reinterpret_cast<const char *>(&component.minorVersion), sizeof(component.minorVersion)); hash.addData(reinterpret_cast<const char *>(&component.minorVersion), sizeof(component.minorVersion));
len = component.typeName.size(); len = component.typeName.size();
hash.addData(reinterpret_cast<const char *>(&len), sizeof(len)); hash.addData(reinterpret_cast<const char *>(&len), sizeof(len));
hash.addData(reinterpret_cast<const char *>(component.typeName.constData()), component.typeName.size() * sizeof(QChar)); hash.addData(reinterpret_cast<const char *>(component.typeName.constData()),
component.typeName.size() * sizeofQChar);
int flags = (component.singleton ? (1 << 0) : 0) + (component.internal ? (1 << 1) : 0); int flags = (component.singleton ? (1 << 0) : 0) + (component.internal ? (1 << 1) : 0);
hash.addData(reinterpret_cast<const char *>(&flags), sizeof(flags)); hash.addData(reinterpret_cast<const char *>(&flags), sizeof(flags));
} }
@@ -410,17 +405,18 @@ QByteArray LibraryInfo::calculateFingerprint() const
foreach (const QmlDirParser::Plugin &plugin, _plugins) { foreach (const QmlDirParser::Plugin &plugin, _plugins) {
len = plugin.path.size(); len = plugin.path.size();
hash.addData(reinterpret_cast<const char *>(&len), sizeof(len)); hash.addData(reinterpret_cast<const char *>(&len), sizeof(len));
hash.addData(reinterpret_cast<const char *>(plugin.path.constData()), len * sizeof(QChar)); hash.addData(reinterpret_cast<const char *>(plugin.path.constData()), len * sizeofQChar);
len = plugin.name.size(); len = plugin.name.size();
hash.addData(reinterpret_cast<const char *>(&len), sizeof(len)); hash.addData(reinterpret_cast<const char *>(&len), sizeof(len));
hash.addData(reinterpret_cast<const char *>(plugin.name.constData()), len * sizeof(QChar)); hash.addData(reinterpret_cast<const char *>(plugin.name.constData()), len * sizeofQChar);
} }
len = _typeinfos.size(); len = _typeinfos.size();
hash.addData(reinterpret_cast<const char *>(&len), sizeof(len)); hash.addData(reinterpret_cast<const char *>(&len), sizeof(len));
foreach (const QmlDirParser::TypeInfo &typeinfo, _typeinfos) { foreach (const QmlDirParser::TypeInfo &typeinfo, _typeinfos) {
len = typeinfo.fileName.size(); len = typeinfo.fileName.size();
hash.addData(reinterpret_cast<const char *>(&len), sizeof(len)); hash.addData(reinterpret_cast<const char *>(&len), sizeof(len));
hash.addData(reinterpret_cast<const char *>(typeinfo.fileName.constData()), len * sizeof(QChar)); hash.addData(reinterpret_cast<const char *>(typeinfo.fileName.constData()),
len * sizeofQChar);
} }
len = _metaObjects.size(); len = _metaObjects.size();
hash.addData(reinterpret_cast<const char *>(&len), sizeof(len)); hash.addData(reinterpret_cast<const char *>(&len), sizeof(len));
@@ -433,7 +429,7 @@ QByteArray LibraryInfo::calculateFingerprint() const
hash.addData(reinterpret_cast<const char *>(&_dumpStatus), sizeof(_dumpStatus)); hash.addData(reinterpret_cast<const char *>(&_dumpStatus), sizeof(_dumpStatus));
len = _dumpError.size(); // localization dependent (avoid?) len = _dumpError.size(); // localization dependent (avoid?)
hash.addData(reinterpret_cast<const char *>(&len), sizeof(len)); hash.addData(reinterpret_cast<const char *>(&len), sizeof(len));
hash.addData(reinterpret_cast<const char *>(_dumpError.constData()), len * sizeof(QChar)); hash.addData(reinterpret_cast<const char *>(_dumpError.constData()), len * sizeofQChar);
len = _moduleApis.size(); len = _moduleApis.size();
hash.addData(reinterpret_cast<const char *>(&len), sizeof(len)); hash.addData(reinterpret_cast<const char *>(&len), sizeof(len));
@@ -623,9 +619,9 @@ void ModuleApiInfo::addToHash(QCryptographicHash &hash) const
{ {
int len = uri.length(); int len = uri.length();
hash.addData(reinterpret_cast<const char *>(&len), sizeof(len)); hash.addData(reinterpret_cast<const char *>(&len), sizeof(len));
hash.addData(reinterpret_cast<const char *>(uri.constData()), len * sizeof(QChar)); hash.addData(reinterpret_cast<const char *>(uri.constData()), len * sizeofQChar);
version.addToHash(hash); version.addToHash(hash);
len = cppName.length(); len = cppName.length();
hash.addData(reinterpret_cast<const char *>(&len), sizeof(len)); hash.addData(reinterpret_cast<const char *>(&len), sizeof(len));
hash.addData(reinterpret_cast<const char *>(cppName.constData()), len * sizeof(QChar)); hash.addData(reinterpret_cast<const char *>(cppName.constData()), len * sizeofQChar);
} }

View File

@@ -167,7 +167,8 @@ public:
LibraryInfo(); LibraryInfo();
explicit LibraryInfo(Status status); explicit LibraryInfo(Status status);
explicit LibraryInfo(const QmlDirParser &parser, const QByteArray &fingerprint = QByteArray()); explicit LibraryInfo(const QmlDirParser &parser, const QByteArray &fingerprint = QByteArray());
~LibraryInfo(); ~LibraryInfo() = default;
LibraryInfo(const LibraryInfo &other) = default;
QByteArray calculateFingerprint() const; QByteArray calculateFingerprint() const;
void updateFingerprint(); void updateFingerprint();