@@ -2046,6 +2046,52 @@ static void ifUndefFuncStyleMacro()
20462046 ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition, undefined function-like macro invocation: A( ... )\n", toString(outputList));
20472047}
20482048
2049+ static void testIfIntegerLiteral(const std::string &value, const std::string &literal, bool expectError)
2050+ {
2051+ const std::string code = "#define A " + value + "\n"
2052+ "#if A == " + literal + "\n"
2053+ "int x = " + literal + ";\n"
2054+ "#endif\n";
2055+
2056+ simplecpp::OutputList outputList;
2057+
2058+ std::string literalPreprocessed = literal;
2059+ if (literalPreprocessed[0] == '-')
2060+ {
2061+ literalPreprocessed = "- " + literalPreprocessed.substr(1);
2062+ }
2063+
2064+ if (expectError) {
2065+ ASSERT_EQUALS("", preprocess(code.c_str(), &outputList));
2066+ ASSERT_EQUALS("file0,2,syntax_error,failed to evaluate #if condition, invalid integer literal\n", toString(outputList));
2067+ } else {
2068+ ASSERT_EQUALS("\n\nint x = " + literalPreprocessed + " ;", preprocess(code.c_str(), &outputList));
2069+ ASSERT_EQUALS("", toString(outputList));
2070+ }
2071+ }
2072+
2073+ static void ifIntegerLiteral()
2074+ {
2075+ testIfIntegerLiteral("123", "123", false);
2076+ testIfIntegerLiteral("-123", "-123", false);
2077+
2078+ testIfIntegerLiteral("123", "0x7b", false);
2079+ testIfIntegerLiteral("-123", "-0x7b", false);
2080+
2081+ testIfIntegerLiteral("123", "0x7B", false);
2082+ testIfIntegerLiteral("-123", "-0x7B", false);
2083+
2084+ testIfIntegerLiteral("123", "0173", false);
2085+ testIfIntegerLiteral("-123", "-0173", false);
2086+
2087+ testIfIntegerLiteral("123", "0b1111011", false);
2088+ testIfIntegerLiteral("-123", "-0b1111011", false);
2089+
2090+ testIfIntegerLiteral("123", "0xGH", true);
2091+ testIfIntegerLiteral("123", "019", true);
2092+ testIfIntegerLiteral("123", "0b30", true);
2093+ }
2094+
20492095static void location1()
20502096{
20512097 const char *code;
@@ -3688,6 +3734,7 @@ int main(int argc, char **argv)
36883734 TEST_CASE(ifalt); // using "and", "or", etc
36893735 TEST_CASE(ifexpr);
36903736 TEST_CASE(ifUndefFuncStyleMacro);
3737+ TEST_CASE(ifIntegerLiteral);
36913738
36923739 TEST_CASE(location1);
36933740 TEST_CASE(location2);
0 commit comments