From d7be70afd44f47496478b4e8dc6cef657ba275df Mon Sep 17 00:00:00 2001 From: Frank Meerkoetter Date: Sun, 16 Sep 2018 21:18:45 +0200 Subject: [PATCH] CppEditor: binary literal support for ConvertNumericLiteral MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extend the ConvertNumeric literal quickfix to support C++14 binary literals. Change-Id: Ia1cf8633e80ddf7d968a881d17ce2a07c5de89d3 Reviewed-by: André Hartmann --- src/plugins/cppeditor/cppquickfixes.cpp | 44 +++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp index 1b3f79a61fe..5b0db2ee5ea 100644 --- a/src/plugins/cppeditor/cppquickfixes.cpp +++ b/src/plugins/cppeditor/cppquickfixes.cpp @@ -66,12 +66,15 @@ #include #include #include +#include #include #include #include #include +#include #include +#include using namespace CPlusPlus; using namespace CppTools; @@ -1478,8 +1481,14 @@ void ConvertNumericLiteral::match(const CppQuickFixInterface &interface, QuickFi // convert to number bool valid; - ulong value = QString::fromUtf8(spell).left(numberLength).toULong(&valid, 0); - if (!valid) // e.g. octal with digit > 7 + ulong value = 0; + const QString x = QString::fromUtf8(spell).left(numberLength); + if (x.startsWith("0b", Qt::CaseInsensitive)) + value = x.midRef(2).toULong(&valid, 2); + else + value = x.toULong(&valid, 0); + + if (!valid) return; const int priority = path.size() - 1; // very high priority @@ -1490,6 +1499,7 @@ void ConvertNumericLiteral::match(const CppQuickFixInterface &interface, QuickFi /* Convert integer literal to hex representation. Replace + 0b100000 32 040 With @@ -1505,10 +1515,13 @@ void ConvertNumericLiteral::match(const CppQuickFixInterface &interface, QuickFi } if (value != 0) { - if (!(numberLength > 1 && str[0] == '0' && str[1] != 'x' && str[1] != 'X')) { + 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 @@ -1528,6 +1541,7 @@ void ConvertNumericLiteral::match(const CppQuickFixInterface &interface, QuickFi /* Convert integer literal to decimal representation. Replace + 0b100000 0x20 040 With @@ -1541,6 +1555,30 @@ void ConvertNumericLiteral::match(const CppQuickFixInterface &interface, QuickFi result << op; } } + + if (!(numberLength > 1 && str[0] == '0' && (str[1] == 'b' || str[1] == 'B'))) { + /* + Convert integer literal to binary representation. + Replace + 32 + 0x20 + 040 + With + 0b100000 + */ + QString replacement = "0b"; + if (value == 0) { + replacement.append('0'); + } else { + std::bitset::digits> b(value); + QRegularExpression re("^[0]*"); + replacement.append(QString::fromStdString(b.to_string()).remove(re)); + } + auto op = new ConvertNumericLiteralOp(interface, start, start + numberLength, replacement); + op->setDescription(QApplication::translate("CppTools::QuickFix", "Convert to Binary")); + op->setPriority(priority); + result << op; + } } namespace {