Visitar URL original
Improve support for hexadecimal floating point literal (#181) · versat/simplecpp@8e260b2 · GitHub
Skip to content

Commit 8e260b2

Browse files
rikardfalkeborndanmar
authored andcommitted
Improve support for hexadecimal floating point literal (cppcheck-opensource#181)
Handle plus and minus sign in the exponent. Hexadecimal floating point numbers were added in C99 and C++17. References: https://en.cppreference.com/w/cpp/language/floating_literal https://en.cppreference.com/w/c/language/floating_constant
1 parent ef7712e commit 8e260b2

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

‎simplecpp.cpp‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,10 @@ void simplecpp::TokenList::combineOperators()
780780
}
781781
// match: [0-9.]+E [+-] [0-9]+
782782
const char lastChar = tok->str()[tok->str().size() - 1];
783-
if (tok->number && !isHex(tok->str()) && !isOct(tok->str()) && (lastChar == 'E' || lastChar == 'e') && tok->next && tok->next->isOneOf("+-") && tok->next->next && tok->next->next->number) {
783+
if (tok->number && !isOct(tok->str()) &&
784+
((!isHex(tok->str()) && (lastChar == 'E' || lastChar == 'e')) ||
785+
(isHex(tok->str()) && (lastChar == 'P' || lastChar == 'p'))) &&
786+
tok->next && tok->next->isOneOf("+-") && tok->next->next && tok->next->next->number) {
784787
tok->setstr(tok->str() + tok->next->op + tok->next->next->str());
785788
deleteToken(tok->next);
786789
deleteToken(tok->next);

‎test.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ static void combineOperators_floatliteral()
154154
ASSERT_EQUALS("1E-7", preprocess("1E-7"));
155155
ASSERT_EQUALS("1E+7", preprocess("1E+7"));
156156
ASSERT_EQUALS("0x1E + 7", preprocess("0x1E+7"));
157+
ASSERT_EQUALS("0x1.2p3", preprocess("0x1.2p3"));
158+
ASSERT_EQUALS("0x1p+3", preprocess("0x1p+3"));
159+
ASSERT_EQUALS("0x1p+3f", preprocess("0x1p+3f"));
160+
ASSERT_EQUALS("0x1p+3L", preprocess("0x1p+3L"));
161+
ASSERT_EQUALS("1p + 3", preprocess("1p+3"));
157162
}
158163

159164
static void combineOperators_increment()

0 commit comments

Comments
 (0)