Skip to content

Commit b9bfbaa

Browse files
Fix #321 End of double #define not recognized (#322)
1 parent 977220f commit b9bfbaa

3 files changed

Lines changed: 20 additions & 2 deletions

File tree

simplecpp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ void simplecpp::TokenList::combineOperators()
949949
if (tok->previous && tok->previous->number && sameline(tok->previous, tok)) {
950950
tok->setstr(tok->previous->str() + '.');
951951
deleteToken(tok->previous);
952-
if (isFloatSuffix(tok->next) || (tok->next && tok->next->startsWithOneOf("AaBbCcDdEeFfPp"))) {
952+
if (sameline(tok, tok->next) && (isFloatSuffix(tok->next) || (tok->next && tok->next->startsWithOneOf("AaBbCcDdEeFfPp")))) {
953953
tok->setstr(tok->str() + tok->next->str());
954954
deleteToken(tok->next);
955955
}

simplecpp.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ namespace simplecpp {
120120
name = (std::isalpha(static_cast<unsigned char>(string[0])) || string[0] == '_' || string[0] == '$')
121121
&& (std::memchr(string.c_str(), '\'', string.size()) == nullptr);
122122
comment = string.size() > 1U && string[0] == '/' && (string[1] == '/' || string[1] == '*');
123-
number = std::isdigit(static_cast<unsigned char>(string[0])) || (string.size() > 1U && (string[0] == '-' || string[0] == '+') && std::isdigit(static_cast<unsigned char>(string[1])));
123+
number = isNumberLike(string);
124124
op = (string.size() == 1U && !name && !comment && !number) ? string[0] : '\0';
125125
}
126126

@@ -135,6 +135,10 @@ namespace simplecpp {
135135
bool isOneOf(const char ops[]) const;
136136
bool startsWithOneOf(const char c[]) const;
137137
bool endsWithOneOf(const char c[]) const;
138+
static bool SIMPLECPP_LIB isNumberLike(const std::string& str) {
139+
return std::isdigit(static_cast<unsigned char>(str[0])) ||
140+
(str.size() > 1U && (str[0] == '-' || str[0] == '+') && std::isdigit(static_cast<unsigned char>(str[1])));
141+
}
138142

139143
TokenString macro;
140144
char op;

test.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,19 @@ static void define12()
593593
"} ;", preprocess(code));
594594
}
595595

596+
static void define13()
597+
{
598+
const char code[] = "#define M 180.\n"
599+
"extern void g();\n"
600+
"void f(double d) {\n"
601+
" if (d > M) {}\n"
602+
"}\n";
603+
ASSERT_EQUALS("\nextern void g ( ) ;\n"
604+
"void f ( double d ) {\n"
605+
"if ( d > 180. ) { }\n"
606+
"}", preprocess(code));
607+
}
608+
596609

597610

598611
static void define_invalid_1()
@@ -2646,6 +2659,7 @@ int main(int argc, char **argv)
26462659
TEST_CASE(define10);
26472660
TEST_CASE(define11);
26482661
TEST_CASE(define12);
2662+
TEST_CASE(define13);
26492663
TEST_CASE(define_invalid_1);
26502664
TEST_CASE(define_invalid_2);
26512665
TEST_CASE(define_define_1);

0 commit comments

Comments
 (0)