diff options
author | Frank Meerkoetter <frank.meerkoetter@basyskom.com> | 2018-09-16 21:18:45 +0200 |
---|---|---|
committer | Frank Meerkoetter <frank.meerkoetter@basyskom.com> | 2018-09-20 19:29:03 +0000 |
commit | d7be70afd44f47496478b4e8dc6cef657ba275df (patch) | |
tree | 1388c14ce5fa99efeaad8abfbdd4c76a8e30dbff /src/plugins/cppeditor/cppquickfixes.cpp | |
parent | 3502a0f7f23fda56bb8faf7f8f369042f358d6cb (diff) |
CppEditor: binary literal support for ConvertNumericLiteral
Extend the ConvertNumeric literal quickfix to support
C++14 binary literals.
Change-Id: Ia1cf8633e80ddf7d968a881d17ce2a07c5de89d3
Reviewed-by: André Hartmann <aha_1980@gmx.de>
Diffstat (limited to 'src/plugins/cppeditor/cppquickfixes.cpp')
-rw-r--r-- | src/plugins/cppeditor/cppquickfixes.cpp | 44 |
1 files changed, 41 insertions, 3 deletions
diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp index 1b3f79a61f..5b0db2ee5e 100644 --- a/src/plugins/cppeditor/cppquickfixes.cpp +++ b/src/plugins/cppeditor/cppquickfixes.cpp @@ -66,12 +66,15 @@ #include <QFormLayout> #include <QInputDialog> #include <QPushButton> +#include <QRegularExpression> #include <QSharedPointer> #include <QStack> #include <QTextCursor> #include <QTextCodec> +#include <bitset> #include <cctype> +#include <limits> 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<std::numeric_limits<decltype (value)>::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 { |