CppEditor: better detection of the system a numeric literal is in

Fixes:
The literal 1 (or any decimal of length 1) was misdetected as not
already a
decimal literal which resulted in a quickfix "Convert to decimal".

Improves:
More consistent handling of 0. Octal is now handled like the other
cases. The user is offered to turn 0, 0x0, or 0b0 into 00.

Change-Id: I9a559fc328d0b49bfe0e53b933e8b4fee5af27a5
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
Frank Meerkoetter
2018-09-18 21:20:39 +02:00
committed by André Hartmann
parent c9ad2a49b6
commit 6f56feef29

View File

@@ -1495,6 +1495,10 @@ void ConvertNumericLiteral::match(const CppQuickFixInterface &interface, QuickFi
const int start = file->startOf(literal);
const char * const str = numeric->chars();
const bool isBinary = numberLength > 2 && str[0] == '0' && tolower(str[1]) == 'b';
const bool isOctal = numberLength >= 2 && str[0] == '0' && str[1] >= '0' && str[1] <= '7';
const bool isDecimal = !(isBinary || isOctal || numeric->isHex());
if (!numeric->isHex()) {
/*
Convert integer literal to hex representation.
@@ -1514,49 +1518,43 @@ void ConvertNumericLiteral::match(const CppQuickFixInterface &interface, QuickFi
result << op;
}
if (value != 0) {
if (!(numberLength > 1 && str[0] == '0'
&& str[1] != 'x' && str[1] != 'X'
&& str[1] != 'b' && str[1] != 'B')) {
/*
Convert integer literal to octal representation.
Replace
0b100000
32
0x20
With
040
*/
QString replacement;
replacement.sprintf("0%lo", value);
auto op = new ConvertNumericLiteralOp(interface, start, start + numberLength, replacement);
op->setDescription(QApplication::translate("CppTools::QuickFix", "Convert to Octal"));
op->setPriority(priority);
result << op;
}
if (!isOctal) {
/*
Convert integer literal to octal representation.
Replace
0b100000
32
0x20
With
040
*/
QString replacement;
replacement.sprintf("0%lo", value);
auto op = new ConvertNumericLiteralOp(interface, start, start + numberLength, replacement);
op->setDescription(QApplication::translate("CppTools::QuickFix", "Convert to Octal"));
op->setPriority(priority);
result << op;
}
if (value != 0 || numeric->isHex()) {
if (!(numberLength > 1 && str[0] != '0')) {
/*
Convert integer literal to decimal representation.
Replace
0b100000
0x20
040
With
32
*/
QString replacement;
replacement.sprintf("%lu", value);
auto op = new ConvertNumericLiteralOp(interface, start, start + numberLength, replacement);
op->setDescription(QApplication::translate("CppTools::QuickFix", "Convert to Decimal"));
op->setPriority(priority);
result << op;
}
if (!isDecimal) {
/*
Convert integer literal to decimal representation.
Replace
0b100000
0x20
040
With
32
*/
QString replacement;
replacement.sprintf("%lu", value);
auto op = new ConvertNumericLiteralOp(interface, start, start + numberLength, replacement);
op->setDescription(QApplication::translate("CppTools::QuickFix", "Convert to Decimal"));
op->setPriority(priority);
result << op;
}
if (!(numberLength > 1 && str[0] == '0' && (str[1] == 'b' || str[1] == 'B'))) {
if (!isBinary) {
/*
Convert integer literal to binary representation.
Replace