forked from qt-creator/qt-creator
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:
committed by
André Hartmann
parent
c9ad2a49b6
commit
6f56feef29
@@ -1495,6 +1495,10 @@ void ConvertNumericLiteral::match(const CppQuickFixInterface &interface, QuickFi
|
|||||||
const int start = file->startOf(literal);
|
const int start = file->startOf(literal);
|
||||||
const char * const str = numeric->chars();
|
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()) {
|
if (!numeric->isHex()) {
|
||||||
/*
|
/*
|
||||||
Convert integer literal to hex representation.
|
Convert integer literal to hex representation.
|
||||||
@@ -1514,49 +1518,43 @@ void ConvertNumericLiteral::match(const CppQuickFixInterface &interface, QuickFi
|
|||||||
result << op;
|
result << op;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value != 0) {
|
if (!isOctal) {
|
||||||
if (!(numberLength > 1 && str[0] == '0'
|
/*
|
||||||
&& str[1] != 'x' && str[1] != 'X'
|
Convert integer literal to octal representation.
|
||||||
&& str[1] != 'b' && str[1] != 'B')) {
|
Replace
|
||||||
/*
|
0b100000
|
||||||
Convert integer literal to octal representation.
|
32
|
||||||
Replace
|
0x20
|
||||||
0b100000
|
With
|
||||||
32
|
040
|
||||||
0x20
|
*/
|
||||||
With
|
QString replacement;
|
||||||
040
|
replacement.sprintf("0%lo", value);
|
||||||
*/
|
auto op = new ConvertNumericLiteralOp(interface, start, start + numberLength, replacement);
|
||||||
QString replacement;
|
op->setDescription(QApplication::translate("CppTools::QuickFix", "Convert to Octal"));
|
||||||
replacement.sprintf("0%lo", value);
|
op->setPriority(priority);
|
||||||
auto op = new ConvertNumericLiteralOp(interface, start, start + numberLength, replacement);
|
result << op;
|
||||||
op->setDescription(QApplication::translate("CppTools::QuickFix", "Convert to Octal"));
|
|
||||||
op->setPriority(priority);
|
|
||||||
result << op;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value != 0 || numeric->isHex()) {
|
if (!isDecimal) {
|
||||||
if (!(numberLength > 1 && str[0] != '0')) {
|
/*
|
||||||
/*
|
Convert integer literal to decimal representation.
|
||||||
Convert integer literal to decimal representation.
|
Replace
|
||||||
Replace
|
0b100000
|
||||||
0b100000
|
0x20
|
||||||
0x20
|
040
|
||||||
040
|
With
|
||||||
With
|
32
|
||||||
32
|
*/
|
||||||
*/
|
QString replacement;
|
||||||
QString replacement;
|
replacement.sprintf("%lu", value);
|
||||||
replacement.sprintf("%lu", value);
|
auto op = new ConvertNumericLiteralOp(interface, start, start + numberLength, replacement);
|
||||||
auto op = new ConvertNumericLiteralOp(interface, start, start + numberLength, replacement);
|
op->setDescription(QApplication::translate("CppTools::QuickFix", "Convert to Decimal"));
|
||||||
op->setDescription(QApplication::translate("CppTools::QuickFix", "Convert to Decimal"));
|
op->setPriority(priority);
|
||||||
op->setPriority(priority);
|
result << op;
|
||||||
result << op;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(numberLength > 1 && str[0] == '0' && (str[1] == 'b' || str[1] == 'B'))) {
|
if (!isBinary) {
|
||||||
/*
|
/*
|
||||||
Convert integer literal to binary representation.
|
Convert integer literal to binary representation.
|
||||||
Replace
|
Replace
|
||||||
|
|||||||
Reference in New Issue
Block a user