diff --git a/simplecpp.cpp b/simplecpp.cpp index 51dfbc69..7ef713ec 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1803,7 +1803,9 @@ namespace simplecpp { throw invalidHashHash(tok->location, name()); bool canBeConcatenatedWithEqual = A->isOneOf("+-*/%&|^") || A->str() == "<<" || A->str() == ">>"; - if (!A->name && !A->number && A->op != ',' && !A->str().empty() && !canBeConcatenatedWithEqual) + bool isStringConstant = A->str().length() >= 2U && *A->str().begin() == '"' && *A->str().rbegin() == '"'; + bool isCharConstant = A->str().length() == 3U && *A->str().begin() == '\'' && *A->str().rbegin() == '\''; + if (!A->name && !A->number && A->op != ',' && !A->str().empty() && !canBeConcatenatedWithEqual && !isStringConstant && !isCharConstant) throw invalidHashHash(tok->location, name()); Token *B = tok->next->next; @@ -1814,6 +1816,9 @@ namespace simplecpp { (!canBeConcatenatedWithEqual && B->op == '=')) throw invalidHashHash(tok->location, name()); + if ((isStringConstant || isCharConstant) && !B->name) + throw invalidHashHash(tok->location, name()); + std::string strAB; const bool varargs = variadic && args.size() >= 1U && B->str() == args[args.size()-1U]; diff --git a/test.cpp b/test.cpp index c4a403f0..456118fa 100644 --- a/test.cpp +++ b/test.cpp @@ -741,6 +741,31 @@ static void hashhash9() ASSERT_EQUALS("file0,1,syntax_error,failed to expand 'A', Invalid ## usage when expanding 'A'.\n", toString(outputList)); } +static void hashhash10() //Paste string constant, integer, real to user defined literal +{ + const char *code = "#define A(x) x##_y\n" + "A(\"A\");\n" + "A('A');\n" + "A(1.0);\n" + "A(1);"; + + const char expected[] = "\n\"A\"_y ;\n" + "'A'_y ;\n" + "1.0_y ;\n" + "1_y ;"; + + ASSERT_EQUALS(expected, preprocess(code)); + + const simplecpp::DUI dui; + simplecpp::OutputList outputList; + + code = "#define A(x) x##123\n" + "A(\"A\");"; + outputList.clear(); + preprocess(code, dui, &outputList); + ASSERT_EQUALS("file0,1,syntax_error,failed to expand 'A', Invalid ## usage when expanding 'A'.\n", toString(outputList)); +} + static void hashhash_invalid_1() { std::istringstream istr("#define f(a) (##x)\nf(1)"); @@ -1882,6 +1907,7 @@ int main(int argc, char **argv) TEST_CASE(hashhash7); // # ## # (C standard; 6.10.3.3.p4) TEST_CASE(hashhash8); TEST_CASE(hashhash9); + TEST_CASE(hashhash10); TEST_CASE(hashhash_invalid_1); TEST_CASE(hashhash_invalid_2);