diff --git a/liblangutil/CharStream.cpp b/liblangutil/CharStream.cpp index 046aca5d3..6f2d47c48 100644 --- a/liblangutil/CharStream.cpp +++ b/liblangutil/CharStream.cpp @@ -85,7 +85,7 @@ string CharStream::lineAtPosition(int _position) const { // if _position points to \n, it returns the line before the \n using size_type = string::size_type; - size_type searchStart = min(m_source.size(), _position); + size_type searchStart = min(m_source.size(), size_type(_position)); if (searchStart > 0) searchStart--; size_type lineStart = m_source.rfind('\n', searchStart); @@ -105,8 +105,9 @@ string CharStream::lineAtPosition(int _position) const tuple CharStream::translatePositionToLineColumn(int _position) const { using size_type = string::size_type; - size_type searchPosition = min(m_source.size(), _position); - int lineNumber = count(m_source.begin(), m_source.begin() + searchPosition, '\n'); + using diff_type = string::difference_type; + size_type searchPosition = min(m_source.size(), size_type(_position)); + int lineNumber = count(m_source.begin(), m_source.begin() + diff_type(searchPosition), '\n'); size_type lineStart; if (searchPosition == 0) lineStart = 0; diff --git a/liblangutil/CharStream.h b/liblangutil/CharStream.h index 6962864b9..aa8f01af2 100644 --- a/liblangutil/CharStream.h +++ b/liblangutil/CharStream.h @@ -72,7 +72,7 @@ public: explicit CharStream(std::string _source, std::string name): m_source(std::move(_source)), m_name(std::move(name)) {} - int position() const { return m_position; } + size_t position() const { return m_position; } bool isPastEndOfInput(size_t _charsForward = 0) const { return (m_position + _charsForward) >= m_source.size(); } char get(size_t _charsForward = 0) const { return m_source[m_position + _charsForward]; } diff --git a/liblangutil/ParserBase.cpp b/liblangutil/ParserBase.cpp index be8ec3eb9..c2162044a 100644 --- a/liblangutil/ParserBase.cpp +++ b/liblangutil/ParserBase.cpp @@ -106,7 +106,7 @@ void ParserBase::expectTokenOrConsumeUntil(Token _value, string const& _currentN if (m_scanner->currentToken() == Token::EOS) { // rollback to where the token started, and raise exception to be caught at a higher level. - m_scanner->setPosition(startPosition); + m_scanner->setPosition(static_cast(startPosition)); m_inParserRecovery = true; fatalParserError(1957_error, errorLoc, msg); } diff --git a/liblangutil/Scanner.cpp b/liblangutil/Scanner.cpp index 6cf319067..e703b5376 100644 --- a/liblangutil/Scanner.cpp +++ b/liblangutil/Scanner.cpp @@ -171,7 +171,7 @@ void Scanner::supportPeriodInIdentifier(bool _value) bool Scanner::scanHexByte(char& o_scannedByte) { char x = 0; - for (int i = 0; i < 2; i++) + for (size_t i = 0; i < 2; i++) { int d = hexValue(m_char); if (d < 0) @@ -189,7 +189,7 @@ bool Scanner::scanHexByte(char& o_scannedByte) std::optional Scanner::scanUnicode() { unsigned x = 0; - for (int i = 0; i < 4; i++) + for (size_t i = 0; i < 4; i++) { int d = hexValue(m_char); if (d < 0) @@ -197,7 +197,7 @@ std::optional Scanner::scanUnicode() rollback(i); return {}; } - x = x * 16 + d; + x = x * 16 + static_cast(d); advance(); } return x; @@ -207,17 +207,17 @@ std::optional Scanner::scanUnicode() void Scanner::addUnicodeAsUTF8(unsigned codepoint) { if (codepoint <= 0x7f) - addLiteralChar(codepoint); + addLiteralChar(char(codepoint)); else if (codepoint <= 0x7ff) { - addLiteralChar(0xc0 | (codepoint >> 6)); - addLiteralChar(0x80 | (codepoint & 0x3f)); + addLiteralChar(char(0xc0u | (codepoint >> 6u))); + addLiteralChar(char(0x80u | (codepoint & 0x3fu))); } else { - addLiteralChar(0xe0 | (codepoint >> 12)); - addLiteralChar(0x80 | ((codepoint >> 6) & 0x3f)); - addLiteralChar(0x80 | (codepoint & 0x3f)); + addLiteralChar(char(0xe0u | (codepoint >> 12u))); + addLiteralChar(char(0x80u | ((codepoint >> 6u) & 0x3fu))); + addLiteralChar(char(0x80u | (codepoint & 0x3fu))); } } @@ -225,10 +225,10 @@ void Scanner::rescan() { size_t rollbackTo = 0; if (m_skippedComments[Current].literal.empty()) - rollbackTo = m_tokens[Current].location.start; + rollbackTo = static_cast(m_tokens[Current].location.start); else - rollbackTo = m_skippedComments[Current].location.start; - m_char = m_source->rollback(size_t(m_source->position()) - rollbackTo); + rollbackTo = static_cast(m_skippedComments[Current].location.start); + m_char = m_source->rollback(m_source->position() - rollbackTo); next(); next(); next(); @@ -260,7 +260,7 @@ Token Scanner::selectToken(char _next, Token _then, Token _else) bool Scanner::skipWhitespace() { - int const startPosition = sourcePos(); + size_t const startPosition = sourcePos(); while (isWhiteSpace(m_char)) advance(); // Return whether or not we skipped any characters. @@ -269,7 +269,7 @@ bool Scanner::skipWhitespace() bool Scanner::skipWhitespaceExceptUnicodeLinebreak() { - int const startPosition = sourcePos(); + size_t const startPosition = sourcePos(); while (isWhiteSpace(m_char) && !isUnicodeLinebreak()) advance(); // Return whether or not we skipped any characters. @@ -309,10 +309,10 @@ bool Scanner::tryScanEndOfLine() return false; } -int Scanner::scanSingleLineDocComment() +size_t Scanner::scanSingleLineDocComment() { LiteralScope literal(this, LITERAL_TYPE_COMMENT); - int endPosition = m_source->position(); + size_t endPosition = m_source->position(); advance(); //consume the last '/' at /// skipWhitespaceExceptUnicodeLinebreak(); @@ -429,7 +429,7 @@ Token Scanner::scanMultiLineDocComment() Token Scanner::scanSlash() { - int firstSlashPosition = sourcePos(); + int firstSlashPosition = static_cast(sourcePos()); advance(); if (m_char == '/') { @@ -441,7 +441,7 @@ Token Scanner::scanSlash() m_skippedComments[NextNext].location.start = firstSlashPosition; m_skippedComments[NextNext].location.source = m_source; m_skippedComments[NextNext].token = Token::CommentLiteral; - m_skippedComments[NextNext].location.end = scanSingleLineDocComment(); + m_skippedComments[NextNext].location.end = static_cast(scanSingleLineDocComment()); return Token::Whitespace; } else @@ -467,7 +467,7 @@ Token Scanner::scanSlash() m_skippedComments[NextNext].location.start = firstSlashPosition; m_skippedComments[NextNext].location.source = m_source; comment = scanMultiLineDocComment(); - m_skippedComments[NextNext].location.end = sourcePos(); + m_skippedComments[NextNext].location.end = static_cast(sourcePos()); m_skippedComments[NextNext].token = comment; if (comment == Token::Illegal) return Token::Illegal; // error already set @@ -495,7 +495,7 @@ void Scanner::scanToken() do { // Remember the position of the next token - m_tokens[NextNext].location.start = sourcePos(); + m_tokens[NextNext].location.start = static_cast(sourcePos()); switch (m_char) { case '"': @@ -690,7 +690,7 @@ void Scanner::scanToken() // whitespace. } while (token == Token::Whitespace); - m_tokens[NextNext].location.end = sourcePos(); + m_tokens[NextNext].location.end = static_cast(sourcePos()); m_tokens[NextNext].location.source = m_source; m_tokens[NextNext].token = token; m_tokens[NextNext].extendedTokenInfo = make_tuple(m, n); diff --git a/liblangutil/Scanner.h b/liblangutil/Scanner.h index d54bb7532..5e613cfdf 100644 --- a/liblangutil/Scanner.h +++ b/liblangutil/Scanner.h @@ -196,7 +196,7 @@ private: ///@} bool advance() { m_char = m_source->advanceAndGet(); return !m_source->isPastEndOfInput(); } - void rollback(int _amount) { m_char = m_source->rollback(_amount); } + void rollback(size_t _amount) { m_char = m_source->rollback(_amount); } /// Rolls back to the start of the current token and re-runs the scanner. void rescan(); @@ -231,7 +231,7 @@ private: Token scanString(); Token scanHexString(); /// Scans a single line comment and returns its corrected end position. - int scanSingleLineDocComment(); + size_t scanSingleLineDocComment(); Token scanMultiLineDocComment(); /// Scans a slash '/' and depending on the characters returns the appropriate token Token scanSlash(); @@ -245,7 +245,7 @@ private: bool isUnicodeLinebreak(); /// Return the current source position. - int sourcePos() const { return m_source->position(); } + size_t sourcePos() const { return m_source->position(); } bool isSourcePastEndOfInput() const { return m_source->isPastEndOfInput(); } bool m_supportPeriodInIdentifier = false; diff --git a/liblangutil/SemVerHandler.cpp b/liblangutil/SemVerHandler.cpp index 04b81206e..a626c5aa7 100644 --- a/liblangutil/SemVerHandler.cpp +++ b/liblangutil/SemVerHandler.cpp @@ -37,7 +37,7 @@ SemVerVersion::SemVerVersion(string const& _versionString) { unsigned v = 0; for (; i != end && '0' <= *i && *i <= '9'; ++i) - v = v * 10 + (*i - '0'); + v = v * 10 + unsigned(*i - '0'); numbers[level] = v; if (level < 2) { @@ -100,10 +100,10 @@ bool SemVerMatchExpression::MatchComponent::matches(SemVerVersion const& _versio int cmp = 0; bool didCompare = false; for (unsigned i = 0; i < levelsPresent && cmp == 0; i++) - if (version.numbers[i] != unsigned(-1)) + if (version.numbers[i] != std::numeric_limits::max()) { didCompare = true; - cmp = _version.numbers[i] - version.numbers[i]; + cmp = static_cast(_version.numbers[i] - version.numbers[i]); } if (cmp == 0 && !_version.prerelease.empty() && didCompare) @@ -245,14 +245,14 @@ unsigned SemVerMatchExpressionParser::parseVersionPart() return 0; else if ('1' <= c && c <= '9') { - unsigned v = c - '0'; + unsigned v(c - '0'); // If we skip to the next token, the current number is terminated. while (m_pos == startPos && '0' <= currentChar() && currentChar() <= '9') { c = currentChar(); - if (v * 10 < v || v * 10 + (c - '0') < v * 10) + if (v * 10 < v || v * 10 + unsigned(c - '0') < v * 10) throw SemVerError(); - v = v * 10 + c - '0'; + v = v * 10 + unsigned(c - '0'); nextChar(); } return v; diff --git a/liblangutil/SourceLocation.h b/liblangutil/SourceLocation.h index e6f97942e..283c8a524 100644 --- a/liblangutil/SourceLocation.h +++ b/liblangutil/SourceLocation.h @@ -85,7 +85,7 @@ struct SourceLocation assertThrow(0 <= start, SourceLocationError, "Invalid source location."); assertThrow(start <= end, SourceLocationError, "Invalid source location."); assertThrow(end <= int(source->source().length()), SourceLocationError, "Invalid source location."); - return source->source().substr(start, end - start); + return source->source().substr(size_t(start), size_t(end - start)); } /// @returns the smallest SourceLocation that contains both @param _a and @param _b. @@ -113,7 +113,11 @@ struct SourceLocation std::shared_ptr source; }; -SourceLocation const parseSourceLocation(std::string const& _input, std::string const& _sourceName, size_t _maxIndex = -1); +SourceLocation const parseSourceLocation( + std::string const& _input, + std::string const& _sourceName, + size_t _maxIndex = std::numeric_limits::max() +); /// Stream output for Location (used e.g. in boost exceptions). inline std::ostream& operator<<(std::ostream& _out, SourceLocation const& _location) diff --git a/liblangutil/SourceReferenceExtractor.cpp b/liblangutil/SourceReferenceExtractor.cpp index 0817e7750..0a6ff0421 100644 --- a/liblangutil/SourceReferenceExtractor.cpp +++ b/liblangutil/SourceReferenceExtractor.cpp @@ -58,11 +58,15 @@ SourceReference SourceReferenceExtractor::extract(SourceLocation const* _locatio string line = source->lineAtPosition(_location->start); - int locationLength = isMultiline ? line.length() - start.column : end.column - start.column; + int locationLength = + isMultiline ? + int(line.length()) - start.column : + end.column - start.column; + if (locationLength > 150) { - int const lhs = start.column + 35; - int const rhs = (isMultiline ? line.length() : end.column) - 35; + auto const lhs = static_cast(start.column) + 35; + string::size_type const rhs = (isMultiline ? line.length() : static_cast(end.column)) - 35; line = line.substr(0, lhs) + " ... " + line.substr(rhs); end.column = start.column + 75; locationLength = 75; @@ -70,8 +74,13 @@ SourceReference SourceReferenceExtractor::extract(SourceLocation const* _locatio if (line.length() > 150) { - int const len = line.length(); - line = line.substr(max(0, start.column - 35), min(start.column, 35) + min(locationLength + 35, len - start.column)); + int const len = static_cast(line.length()); + line = line.substr( + static_cast(max(0, start.column - 35)), + static_cast(min(start.column, 35)) + static_cast( + min(locationLength + 35,len - start.column) + ) + ); if (start.column + locationLength + 35 < len) line += " ..."; if (start.column > 35) @@ -79,7 +88,7 @@ SourceReference SourceReferenceExtractor::extract(SourceLocation const* _locatio line = " ... " + line; start.column = 40; } - end.column = start.column + locationLength; + end.column = start.column + static_cast(locationLength); } return SourceReference{ diff --git a/liblangutil/SourceReferenceFormatter.cpp b/liblangutil/SourceReferenceFormatter.cpp index 6b1a8286a..965755a10 100644 --- a/liblangutil/SourceReferenceFormatter.cpp +++ b/liblangutil/SourceReferenceFormatter.cpp @@ -51,7 +51,7 @@ void SourceReferenceFormatter::printSourceLocation(SourceReference const& _ref) ); m_stream << "^"; if (_ref.endColumn > _ref.startColumn + 2) - m_stream << string(_ref.endColumn - _ref.startColumn - 2, '-'); + m_stream << string(static_cast(_ref.endColumn - _ref.startColumn - 2), '-'); if (_ref.endColumn > _ref.startColumn + 1) m_stream << "^"; m_stream << endl; @@ -60,7 +60,7 @@ void SourceReferenceFormatter::printSourceLocation(SourceReference const& _ref) m_stream << _ref.text << endl << - string(_ref.startColumn, ' ') << + string(static_cast(_ref.startColumn), ' ') << "^ (Relevant source part starts here and spans across multiple lines)." << endl; } diff --git a/liblangutil/SourceReferenceFormatterHuman.cpp b/liblangutil/SourceReferenceFormatterHuman.cpp index b8fdba163..3560955c7 100644 --- a/liblangutil/SourceReferenceFormatterHuman.cpp +++ b/liblangutil/SourceReferenceFormatterHuman.cpp @@ -104,7 +104,7 @@ void SourceReferenceFormatterHuman::printSourceLocation(SourceReference const& _ if (!_ref.multiline) { - int const locationLength = _ref.endColumn - _ref.startColumn; + auto const locationLength = static_cast(_ref.endColumn - _ref.startColumn); // line 1: m_stream << leftpad << ' '; @@ -113,15 +113,17 @@ void SourceReferenceFormatterHuman::printSourceLocation(SourceReference const& _ // line 2: frameColored() << line << " |"; - m_stream << ' ' << text.substr(0, _ref.startColumn); - highlightColored() << text.substr(_ref.startColumn, locationLength); - m_stream << text.substr(_ref.endColumn) << '\n'; + + m_stream << ' ' << text.substr(0, static_cast(_ref.startColumn)); + highlightColored() << text.substr(static_cast(_ref.startColumn), locationLength); + m_stream << text.substr(static_cast(_ref.endColumn)) << '\n'; // line 3: m_stream << leftpad << ' '; frameColored() << '|'; - m_stream << ' ' << replaceNonTabs(text.substr(0, _ref.startColumn), ' '); - diagColored() << replaceNonTabs(text.substr(_ref.startColumn, locationLength), '^'); + + m_stream << ' ' << replaceNonTabs(text.substr(0, static_cast(_ref.startColumn)), ' '); + diagColored() << replaceNonTabs(text.substr(static_cast(_ref.startColumn), locationLength), '^'); m_stream << '\n'; } else @@ -133,13 +135,13 @@ void SourceReferenceFormatterHuman::printSourceLocation(SourceReference const& _ // line 2: frameColored() << line << " |"; - m_stream << ' ' << text.substr(0, _ref.startColumn); - highlightColored() << text.substr(_ref.startColumn) << '\n'; + m_stream << ' ' << text.substr(0, static_cast(_ref.startColumn)); + highlightColored() << text.substr(static_cast(_ref.startColumn)) << '\n'; // line 3: m_stream << leftpad << ' '; frameColored() << '|'; - m_stream << ' ' << replaceNonTabs(text.substr(0, _ref.startColumn), ' '); + m_stream << ' ' << replaceNonTabs(text.substr(0, static_cast(_ref.startColumn)), ' '); diagColored() << "^ (Relevant source part starts here and spans across multiple lines)."; m_stream << '\n'; } diff --git a/liblangutil/Token.cpp b/liblangutil/Token.cpp index e051bb552..70b645992 100644 --- a/liblangutil/Token.cpp +++ b/liblangutil/Token.cpp @@ -130,7 +130,7 @@ int parseSize(string::const_iterator _begin, string::const_iterator _end) { try { - unsigned int m = boost::lexical_cast(boost::make_iterator_range(_begin, _end)); + int m = boost::lexical_cast(boost::make_iterator_range(_begin, _end)); return m; } catch(boost::bad_lexical_cast const&)