Skip to content

Commit 5b1426b

Browse files
committed
Adding fixes for signedness warnings in liblangutil
1 parent de5e283 commit 5b1426b

11 files changed

+71
-55
lines changed

liblangutil/CharStream.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ string CharStream::lineAtPosition(int _position) const
8585
{
8686
// if _position points to \n, it returns the line before the \n
8787
using size_type = string::size_type;
88-
size_type searchStart = min<size_type>(m_source.size(), _position);
88+
size_type searchStart = min<size_type>(m_source.size(), size_type(_position));
8989
if (searchStart > 0)
9090
searchStart--;
9191
size_type lineStart = m_source.rfind('\n', searchStart);
@@ -105,8 +105,9 @@ string CharStream::lineAtPosition(int _position) const
105105
tuple<int, int> CharStream::translatePositionToLineColumn(int _position) const
106106
{
107107
using size_type = string::size_type;
108-
size_type searchPosition = min<size_type>(m_source.size(), _position);
109-
int lineNumber = count(m_source.begin(), m_source.begin() + searchPosition, '\n');
108+
using diff_type = string::difference_type;
109+
size_type searchPosition = min<size_type>(m_source.size(), size_type(_position));
110+
int lineNumber = count(m_source.begin(), m_source.begin() + diff_type(searchPosition), '\n');
110111
size_type lineStart;
111112
if (searchPosition == 0)
112113
lineStart = 0;

liblangutil/CharStream.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class CharStream
7272
explicit CharStream(std::string _source, std::string name):
7373
m_source(std::move(_source)), m_name(std::move(name)) {}
7474

75-
int position() const { return m_position; }
75+
size_t position() const { return m_position; }
7676
bool isPastEndOfInput(size_t _charsForward = 0) const { return (m_position + _charsForward) >= m_source.size(); }
7777

7878
char get(size_t _charsForward = 0) const { return m_source[m_position + _charsForward]; }

liblangutil/ParserBase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ void ParserBase::expectTokenOrConsumeUntil(Token _value, string const& _currentN
106106
if (m_scanner->currentToken() == Token::EOS)
107107
{
108108
// rollback to where the token started, and raise exception to be caught at a higher level.
109-
m_scanner->setPosition(startPosition);
109+
m_scanner->setPosition(static_cast<size_t>(startPosition));
110110
m_inParserRecovery = true;
111111
fatalParserError(1957_error, errorLoc, msg);
112112
}

liblangutil/Scanner.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ void Scanner::supportPeriodInIdentifier(bool _value)
171171
bool Scanner::scanHexByte(char& o_scannedByte)
172172
{
173173
char x = 0;
174-
for (int i = 0; i < 2; i++)
174+
for (size_t i = 0; i < 2; i++)
175175
{
176176
int d = hexValue(m_char);
177177
if (d < 0)
@@ -189,15 +189,15 @@ bool Scanner::scanHexByte(char& o_scannedByte)
189189
std::optional<unsigned> Scanner::scanUnicode()
190190
{
191191
unsigned x = 0;
192-
for (int i = 0; i < 4; i++)
192+
for (size_t i = 0; i < 4; i++)
193193
{
194194
int d = hexValue(m_char);
195195
if (d < 0)
196196
{
197197
rollback(i);
198198
return {};
199199
}
200-
x = x * 16 + d;
200+
x = x * 16 + static_cast<size_t>(d);
201201
advance();
202202
}
203203
return x;
@@ -207,28 +207,28 @@ std::optional<unsigned> Scanner::scanUnicode()
207207
void Scanner::addUnicodeAsUTF8(unsigned codepoint)
208208
{
209209
if (codepoint <= 0x7f)
210-
addLiteralChar(codepoint);
210+
addLiteralChar(char(codepoint));
211211
else if (codepoint <= 0x7ff)
212212
{
213-
addLiteralChar(0xc0 | (codepoint >> 6));
214-
addLiteralChar(0x80 | (codepoint & 0x3f));
213+
addLiteralChar(char(0xc0u | (codepoint >> 6u)));
214+
addLiteralChar(char(0x80u | (codepoint & 0x3fu)));
215215
}
216216
else
217217
{
218-
addLiteralChar(0xe0 | (codepoint >> 12));
219-
addLiteralChar(0x80 | ((codepoint >> 6) & 0x3f));
220-
addLiteralChar(0x80 | (codepoint & 0x3f));
218+
addLiteralChar(char(0xe0u | (codepoint >> 12u)));
219+
addLiteralChar(char(0x80u | ((codepoint >> 6u) & 0x3fu)));
220+
addLiteralChar(char(0x80u | (codepoint & 0x3fu)));
221221
}
222222
}
223223

224224
void Scanner::rescan()
225225
{
226226
size_t rollbackTo = 0;
227227
if (m_skippedComments[Current].literal.empty())
228-
rollbackTo = m_tokens[Current].location.start;
228+
rollbackTo = static_cast<size_t>(m_tokens[Current].location.start);
229229
else
230-
rollbackTo = m_skippedComments[Current].location.start;
231-
m_char = m_source->rollback(size_t(m_source->position()) - rollbackTo);
230+
rollbackTo = static_cast<size_t>(m_skippedComments[Current].location.start);
231+
m_char = m_source->rollback(m_source->position() - rollbackTo);
232232
next();
233233
next();
234234
next();
@@ -260,7 +260,7 @@ Token Scanner::selectToken(char _next, Token _then, Token _else)
260260

261261
bool Scanner::skipWhitespace()
262262
{
263-
int const startPosition = sourcePos();
263+
size_t const startPosition = sourcePos();
264264
while (isWhiteSpace(m_char))
265265
advance();
266266
// Return whether or not we skipped any characters.
@@ -269,7 +269,7 @@ bool Scanner::skipWhitespace()
269269

270270
bool Scanner::skipWhitespaceExceptUnicodeLinebreak()
271271
{
272-
int const startPosition = sourcePos();
272+
size_t const startPosition = sourcePos();
273273
while (isWhiteSpace(m_char) && !isUnicodeLinebreak())
274274
advance();
275275
// Return whether or not we skipped any characters.
@@ -309,10 +309,10 @@ bool Scanner::tryScanEndOfLine()
309309
return false;
310310
}
311311

312-
int Scanner::scanSingleLineDocComment()
312+
size_t Scanner::scanSingleLineDocComment()
313313
{
314314
LiteralScope literal(this, LITERAL_TYPE_COMMENT);
315-
int endPosition = m_source->position();
315+
size_t endPosition = m_source->position();
316316
advance(); //consume the last '/' at ///
317317

318318
skipWhitespaceExceptUnicodeLinebreak();
@@ -429,7 +429,7 @@ Token Scanner::scanMultiLineDocComment()
429429

430430
Token Scanner::scanSlash()
431431
{
432-
int firstSlashPosition = sourcePos();
432+
int firstSlashPosition = static_cast<int>(sourcePos());
433433
advance();
434434
if (m_char == '/')
435435
{
@@ -441,7 +441,7 @@ Token Scanner::scanSlash()
441441
m_skippedComments[NextNext].location.start = firstSlashPosition;
442442
m_skippedComments[NextNext].location.source = m_source;
443443
m_skippedComments[NextNext].token = Token::CommentLiteral;
444-
m_skippedComments[NextNext].location.end = scanSingleLineDocComment();
444+
m_skippedComments[NextNext].location.end = static_cast<int>(scanSingleLineDocComment());
445445
return Token::Whitespace;
446446
}
447447
else
@@ -467,7 +467,7 @@ Token Scanner::scanSlash()
467467
m_skippedComments[NextNext].location.start = firstSlashPosition;
468468
m_skippedComments[NextNext].location.source = m_source;
469469
comment = scanMultiLineDocComment();
470-
m_skippedComments[NextNext].location.end = sourcePos();
470+
m_skippedComments[NextNext].location.end = static_cast<int>(sourcePos());
471471
m_skippedComments[NextNext].token = comment;
472472
if (comment == Token::Illegal)
473473
return Token::Illegal; // error already set
@@ -495,7 +495,7 @@ void Scanner::scanToken()
495495
do
496496
{
497497
// Remember the position of the next token
498-
m_tokens[NextNext].location.start = sourcePos();
498+
m_tokens[NextNext].location.start = static_cast<int>(sourcePos());
499499
switch (m_char)
500500
{
501501
case '"':
@@ -690,7 +690,7 @@ void Scanner::scanToken()
690690
// whitespace.
691691
}
692692
while (token == Token::Whitespace);
693-
m_tokens[NextNext].location.end = sourcePos();
693+
m_tokens[NextNext].location.end = static_cast<int>(sourcePos());
694694
m_tokens[NextNext].location.source = m_source;
695695
m_tokens[NextNext].token = token;
696696
m_tokens[NextNext].extendedTokenInfo = make_tuple(m, n);

liblangutil/Scanner.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class Scanner
196196
///@}
197197

198198
bool advance() { m_char = m_source->advanceAndGet(); return !m_source->isPastEndOfInput(); }
199-
void rollback(int _amount) { m_char = m_source->rollback(_amount); }
199+
void rollback(size_t _amount) { m_char = m_source->rollback(_amount); }
200200
/// Rolls back to the start of the current token and re-runs the scanner.
201201
void rescan();
202202

@@ -231,7 +231,7 @@ class Scanner
231231
Token scanString();
232232
Token scanHexString();
233233
/// Scans a single line comment and returns its corrected end position.
234-
int scanSingleLineDocComment();
234+
size_t scanSingleLineDocComment();
235235
Token scanMultiLineDocComment();
236236
/// Scans a slash '/' and depending on the characters returns the appropriate token
237237
Token scanSlash();
@@ -245,7 +245,7 @@ class Scanner
245245
bool isUnicodeLinebreak();
246246

247247
/// Return the current source position.
248-
int sourcePos() const { return m_source->position(); }
248+
size_t sourcePos() const { return m_source->position(); }
249249
bool isSourcePastEndOfInput() const { return m_source->isPastEndOfInput(); }
250250

251251
bool m_supportPeriodInIdentifier = false;

liblangutil/SemVerHandler.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ SemVerVersion::SemVerVersion(string const& _versionString)
3737
{
3838
unsigned v = 0;
3939
for (; i != end && '0' <= *i && *i <= '9'; ++i)
40-
v = v * 10 + (*i - '0');
40+
v = v * 10 + unsigned(*i - '0');
4141
numbers[level] = v;
4242
if (level < 2)
4343
{
@@ -100,10 +100,10 @@ bool SemVerMatchExpression::MatchComponent::matches(SemVerVersion const& _versio
100100
int cmp = 0;
101101
bool didCompare = false;
102102
for (unsigned i = 0; i < levelsPresent && cmp == 0; i++)
103-
if (version.numbers[i] != unsigned(-1))
103+
if (version.numbers[i] != std::numeric_limits<unsigned>::max())
104104
{
105105
didCompare = true;
106-
cmp = _version.numbers[i] - version.numbers[i];
106+
cmp = static_cast<int>(_version.numbers[i] - version.numbers[i]);
107107
}
108108

109109
if (cmp == 0 && !_version.prerelease.empty() && didCompare)
@@ -245,14 +245,14 @@ unsigned SemVerMatchExpressionParser::parseVersionPart()
245245
return 0;
246246
else if ('1' <= c && c <= '9')
247247
{
248-
unsigned v = c - '0';
248+
unsigned v(c - '0');
249249
// If we skip to the next token, the current number is terminated.
250250
while (m_pos == startPos && '0' <= currentChar() && currentChar() <= '9')
251251
{
252252
c = currentChar();
253-
if (v * 10 < v || v * 10 + (c - '0') < v * 10)
253+
if (v * 10 < v || v * 10 + unsigned(c - '0') < v * 10)
254254
throw SemVerError();
255-
v = v * 10 + c - '0';
255+
v = v * 10 + unsigned(c - '0');
256256
nextChar();
257257
}
258258
return v;

liblangutil/SourceLocation.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ struct SourceLocation
8585
assertThrow(0 <= start, SourceLocationError, "Invalid source location.");
8686
assertThrow(start <= end, SourceLocationError, "Invalid source location.");
8787
assertThrow(end <= int(source->source().length()), SourceLocationError, "Invalid source location.");
88-
return source->source().substr(start, end - start);
88+
return source->source().substr(size_t(start), size_t(end - start));
8989
}
9090

9191
/// @returns the smallest SourceLocation that contains both @param _a and @param _b.
@@ -113,7 +113,11 @@ struct SourceLocation
113113
std::shared_ptr<CharStream> source;
114114
};
115115

116-
SourceLocation const parseSourceLocation(std::string const& _input, std::string const& _sourceName, size_t _maxIndex = -1);
116+
SourceLocation const parseSourceLocation(
117+
std::string const& _input,
118+
std::string const& _sourceName,
119+
size_t _maxIndex = std::numeric_limits<size_t>::max()
120+
);
117121

118122
/// Stream output for Location (used e.g. in boost exceptions).
119123
inline std::ostream& operator<<(std::ostream& _out, SourceLocation const& _location)

liblangutil/SourceReferenceExtractor.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,37 @@ SourceReference SourceReferenceExtractor::extract(SourceLocation const* _locatio
5858

5959
string line = source->lineAtPosition(_location->start);
6060

61-
int locationLength = isMultiline ? line.length() - start.column : end.column - start.column;
61+
int locationLength =
62+
isMultiline ?
63+
int(line.length()) - start.column :
64+
end.column - start.column;
65+
6266
if (locationLength > 150)
6367
{
64-
int const lhs = start.column + 35;
65-
int const rhs = (isMultiline ? line.length() : end.column) - 35;
68+
auto const lhs = static_cast<size_t>(start.column) + 35;
69+
string::size_type const rhs = (isMultiline ? line.length() : static_cast<size_t>(end.column)) - 35;
6670
line = line.substr(0, lhs) + " ... " + line.substr(rhs);
6771
end.column = start.column + 75;
6872
locationLength = 75;
6973
}
7074

7175
if (line.length() > 150)
7276
{
73-
int const len = line.length();
74-
line = line.substr(max(0, start.column - 35), min(start.column, 35) + min(locationLength + 35, len - start.column));
77+
int const len = static_cast<int>(line.length());
78+
line = line.substr(
79+
static_cast<size_t>(max(0, start.column - 35)),
80+
static_cast<size_t>(min(start.column, 35)) + static_cast<size_t>(
81+
min(locationLength + 35,len - start.column)
82+
)
83+
);
7584
if (start.column + locationLength + 35 < len)
7685
line += " ...";
7786
if (start.column > 35)
7887
{
7988
line = " ... " + line;
8089
start.column = 40;
8190
}
82-
end.column = start.column + locationLength;
91+
end.column = start.column + static_cast<int>(locationLength);
8392
}
8493

8594
return SourceReference{

liblangutil/SourceReferenceFormatter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void SourceReferenceFormatter::printSourceLocation(SourceReference const& _ref)
5151
);
5252
m_stream << "^";
5353
if (_ref.endColumn > _ref.startColumn + 2)
54-
m_stream << string(_ref.endColumn - _ref.startColumn - 2, '-');
54+
m_stream << string(static_cast<size_t>(_ref.endColumn - _ref.startColumn - 2), '-');
5555
if (_ref.endColumn > _ref.startColumn + 1)
5656
m_stream << "^";
5757
m_stream << endl;
@@ -60,7 +60,7 @@ void SourceReferenceFormatter::printSourceLocation(SourceReference const& _ref)
6060
m_stream <<
6161
_ref.text <<
6262
endl <<
63-
string(_ref.startColumn, ' ') <<
63+
string(static_cast<size_t>(_ref.startColumn), ' ') <<
6464
"^ (Relevant source part starts here and spans across multiple lines)." <<
6565
endl;
6666
}

liblangutil/SourceReferenceFormatterHuman.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ void SourceReferenceFormatterHuman::printSourceLocation(SourceReference const& _
104104

105105
if (!_ref.multiline)
106106
{
107-
int const locationLength = _ref.endColumn - _ref.startColumn;
107+
auto const locationLength = static_cast<size_t>(_ref.endColumn - _ref.startColumn);
108108

109109
// line 1:
110110
m_stream << leftpad << ' ';
@@ -113,15 +113,17 @@ void SourceReferenceFormatterHuman::printSourceLocation(SourceReference const& _
113113

114114
// line 2:
115115
frameColored() << line << " |";
116-
m_stream << ' ' << text.substr(0, _ref.startColumn);
117-
highlightColored() << text.substr(_ref.startColumn, locationLength);
118-
m_stream << text.substr(_ref.endColumn) << '\n';
116+
117+
m_stream << ' ' << text.substr(0, static_cast<size_t>(_ref.startColumn));
118+
highlightColored() << text.substr(static_cast<size_t>(_ref.startColumn), locationLength);
119+
m_stream << text.substr(static_cast<size_t>(_ref.endColumn)) << '\n';
119120

120121
// line 3:
121122
m_stream << leftpad << ' ';
122123
frameColored() << '|';
123-
m_stream << ' ' << replaceNonTabs(text.substr(0, _ref.startColumn), ' ');
124-
diagColored() << replaceNonTabs(text.substr(_ref.startColumn, locationLength), '^');
124+
125+
m_stream << ' ' << replaceNonTabs(text.substr(0, static_cast<size_t>(_ref.startColumn)), ' ');
126+
diagColored() << replaceNonTabs(text.substr(static_cast<size_t>(_ref.startColumn), locationLength), '^');
125127
m_stream << '\n';
126128
}
127129
else
@@ -133,13 +135,13 @@ void SourceReferenceFormatterHuman::printSourceLocation(SourceReference const& _
133135

134136
// line 2:
135137
frameColored() << line << " |";
136-
m_stream << ' ' << text.substr(0, _ref.startColumn);
137-
highlightColored() << text.substr(_ref.startColumn) << '\n';
138+
m_stream << ' ' << text.substr(0, static_cast<size_t>(_ref.startColumn));
139+
highlightColored() << text.substr(static_cast<size_t>(_ref.startColumn)) << '\n';
138140

139141
// line 3:
140142
m_stream << leftpad << ' ';
141143
frameColored() << '|';
142-
m_stream << ' ' << replaceNonTabs(text.substr(0, _ref.startColumn), ' ');
144+
m_stream << ' ' << replaceNonTabs(text.substr(0, static_cast<size_t>(_ref.startColumn)), ' ');
143145
diagColored() << "^ (Relevant source part starts here and spans across multiple lines).";
144146
m_stream << '\n';
145147
}

0 commit comments

Comments
 (0)