diff options
author | Frank Meerkoetter <frank.meerkoetter@basyskom.com> | 2018-09-18 21:20:39 +0200 |
---|---|---|
committer | André Hartmann <aha_1980@gmx.de> | 2018-09-27 07:01:39 +0000 |
commit | 6f56feef29af3f6d6ff7bc490f825550d75d2a9c (patch) | |
tree | d657add0f3dcbe071dc413a73d6d2a3c4c708db4 /src/plugins/cppeditor/cppquickfixes.cpp | |
parent | c9ad2a49b6f0a2b16e39d4fba841c596ca081536 (diff) |
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>
Diffstat (limited to 'src/plugins/cppeditor/cppquickfixes.cpp')
-rw-r--r-- | src/plugins/cppeditor/cppquickfixes.cpp | 84 |
1 files changed, 41 insertions, 43 deletions
diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp index 5b0db2ee5e..c5004b8024 100644 --- a/src/plugins/cppeditor/cppquickfixes.cpp +++ b/src/plugins/cppeditor/cppquickfixes.cpp @@ -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 (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 (!(numberLength > 1 && str[0] == '0' && (str[1] == 'b' || str[1] == 'B'))) { + 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 (!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 (!isBinary) { /* Convert integer literal to binary representation. Replace |