@@ -2326,6 +2326,52 @@ static void ifUndefFuncStyleMacro()
23262326 ASSERT_EQUALS (" file0,1,syntax_error,failed to evaluate #if condition, undefined function-like macro invocation: A( ... )\n " , toString (outputList));
23272327}
23282328
2329+ static void testIfIntegerLiteral (const std::string &value, const std::string &literal, bool expectError)
2330+ {
2331+ const std::string code = " #define A " + value + " \n "
2332+ " #if A == " + literal + " \n "
2333+ " int x = " + literal + " ;\n "
2334+ " #endif\n " ;
2335+
2336+ simplecpp::OutputList outputList;
2337+
2338+ std::string literalPreprocessed = literal;
2339+ if (literalPreprocessed[0 ] == ' -' )
2340+ {
2341+ literalPreprocessed = " - " + literalPreprocessed.substr (1 );
2342+ }
2343+
2344+ if (expectError) {
2345+ ASSERT_EQUALS (" " , preprocess (code.c_str (), &outputList));
2346+ ASSERT_EQUALS (" file0,2,syntax_error,failed to evaluate #if condition, invalid integer literal\n " , toString (outputList));
2347+ } else {
2348+ ASSERT_EQUALS (" \n\n int x = " + literalPreprocessed + " ;" , preprocess (code.c_str (), &outputList));
2349+ ASSERT_EQUALS (" " , toString (outputList));
2350+ }
2351+ }
2352+
2353+ static void ifIntegerLiteral ()
2354+ {
2355+ testIfIntegerLiteral (" 123" , " 123" , false );
2356+ testIfIntegerLiteral (" -123" , " -123" , false );
2357+
2358+ testIfIntegerLiteral (" 123" , " 0x7b" , false );
2359+ testIfIntegerLiteral (" -123" , " -0x7b" , false );
2360+
2361+ testIfIntegerLiteral (" 123" , " 0x7B" , false );
2362+ testIfIntegerLiteral (" -123" , " -0x7B" , false );
2363+
2364+ testIfIntegerLiteral (" 123" , " 0173" , false );
2365+ testIfIntegerLiteral (" -123" , " -0173" , false );
2366+
2367+ testIfIntegerLiteral (" 123" , " 0b1111011" , false );
2368+ testIfIntegerLiteral (" -123" , " -0b1111011" , false );
2369+
2370+ testIfIntegerLiteral (" 123" , " 0xGH" , true );
2371+ testIfIntegerLiteral (" 123" , " 019" , true );
2372+ testIfIntegerLiteral (" 123" , " 0b30" , true );
2373+ }
2374+
23292375static void location1 ()
23302376{
23312377 const char *code;
@@ -4060,6 +4106,7 @@ static void runTests(int argc, char **argv, Input input)
40604106 TEST_CASE (ifalt); // using "and", "or", etc
40614107 TEST_CASE (ifexpr);
40624108 TEST_CASE (ifUndefFuncStyleMacro);
4109+ TEST_CASE (ifIntegerLiteral);
40634110
40644111 TEST_CASE (location1);
40654112 TEST_CASE (location2);
0 commit comments