Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 99 additions & 2 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ static std::string readfile(const char code[], std::size_t size, simplecpp::Outp
return makeTokenList(code,size,files,std::string(),outputList).stringify();
}

static std::string preprocess(const char code[], const simplecpp::DUI &dui, simplecpp::OutputList *outputList, std::list<simplecpp::MacroUsage> *macroUsage = nullptr, std::list<simplecpp::IfCond> *ifCond = nullptr, const std::string &file = std::string())
static std::string preprocess(const char code[], std::size_t size, const simplecpp::DUI &dui, simplecpp::OutputList *outputList, std::list<simplecpp::MacroUsage> *macroUsage = nullptr, std::list<simplecpp::IfCond> *ifCond = nullptr, const std::string &file = std::string())
{
std::vector<std::string> files;
simplecpp::FileDataCache cache;
simplecpp::TokenList tokens = makeTokenList(code,files, file);
simplecpp::TokenList tokens = makeTokenList(code, size, files, file);
if (dui.removeComments)
tokens.removeComments();
simplecpp::TokenList tokens2(files);
Expand All @@ -145,6 +145,11 @@ static std::string preprocess(const char code[], const simplecpp::DUI &dui, simp
return tokens2.stringify();
}

static std::string preprocess(const char code[], const simplecpp::DUI &dui, simplecpp::OutputList *outputList, std::list<simplecpp::MacroUsage> *macroUsage = nullptr, std::list<simplecpp::IfCond> *ifCond = nullptr, const std::string &file = std::string())
{
return preprocess(code, strlen(code), dui, outputList, macroUsage, ifCond, file);
}

static std::string preprocess(const char code[])
{
return preprocess(code, simplecpp::DUI(), nullptr);
Expand All @@ -165,6 +170,11 @@ static std::string preprocess(const char code[], simplecpp::OutputList *outputLi
return preprocess(code, simplecpp::DUI(), outputList);
}

static std::string preprocess(const char code[], std::size_t size, simplecpp::OutputList *outputList)
{
return preprocess(code, size, simplecpp::DUI(), outputList);
}

static std::string preprocess(const char code[], std::list<simplecpp::IfCond> *ifCond)
{
return preprocess(code, simplecpp::DUI(), nullptr, nullptr, ifCond);
Expand Down Expand Up @@ -913,6 +923,84 @@ static void define_invalid_2()
ASSERT_EQUALS("file0,1,syntax_error,Failed to parse #define, bad macro syntax\n", toString(outputList));
}

static void define_invalid_3()
{
const char code[] = "#if'\\u'";
simplecpp::OutputList outputList;
ASSERT_EQUALS("", preprocess(code, &outputList));
ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition, expected digit\n", toString(outputList));
}

static void define_invalid_4()
{
const char code[] = "#define R()\n"
"R";
simplecpp::OutputList outputList;
ASSERT_EQUALS("", preprocess(code, &outputList));
ASSERT_EQUALS("file0,2,syntax_error,failed to expand 'R', Wrong number of parameters for macro 'R'.\n", toString(outputList));
}

static void define_invalid_5()
{
const char code[] = "#define X(...)\n"
"X";
simplecpp::OutputList outputList;
ASSERT_EQUALS("", preprocess(code, &outputList));
ASSERT_EQUALS("file0,2,syntax_error,failed to expand 'X', Wrong number of parameters for macro 'X'.\n", toString(outputList));
}

static void define_invalid_6()
{
const char code[] = "#if-0xBBB4444444444444%~B";
simplecpp::OutputList outputList;
ASSERT_EQUALS("", preprocess(code, &outputList));
ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition, division overflow\n", toString(outputList));
}

static void define_invalid_7()
{
const char code[] = "#define A)__has_include\n"
"#if\u000BA";
simplecpp::OutputList outputList;
ASSERT_EQUALS("", preprocess(code, &outputList));
ASSERT_EQUALS("file0,2,syntax_error,failed to evaluate #if condition, missing __has_include argument\n", toString(outputList));
}

static void define_invalid_8()
{
const char code[] = "#define f __has_include\n"
"#if#f<";
simplecpp::OutputList outputList;
ASSERT_EQUALS("", preprocess(code, &outputList));
ASSERT_EQUALS("file0,2,syntax_error,failed to evaluate #if condition, missing __has_include argument\n", toString(outputList));
}

static void define_invalid_9()
{
const char code[] = "#if@u'\\udefa'";
simplecpp::OutputList outputList;
ASSERT_EQUALS("", preprocess(code, &outputList));
ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition, surrogate code points not allowed in universal character names\n", toString(outputList));
}

static void define_invalid_10()
{
const char code[] = "#define\u0000X\u0007__has_include(\n"
"#if%X&";
simplecpp::OutputList outputList;
ASSERT_EQUALS("", preprocess(code, sizeof(code), &outputList));
ASSERT_EQUALS("file0,2,syntax_error,failed to evaluate #if condition, invalid __has_include expression\n", toString(outputList));
}

static void define_invalid_11()
{
const char code[] = "#define\u0000X\u0000__has_include<2\n"
"#if*X";
simplecpp::OutputList outputList;
ASSERT_EQUALS("", preprocess(code, sizeof(code), &outputList));
ASSERT_EQUALS("file0,2,syntax_error,failed to evaluate #if condition, invalid __has_include expression\n", toString(outputList));
}

static void define_define_1()
{
const char code[] = "#define A(x) (x+1)\n"
Expand Down Expand Up @@ -3929,6 +4017,15 @@ static void runTests(int argc, char **argv, Input input)
TEST_CASE(define23); // #40
TEST_CASE(define_invalid_1);
TEST_CASE(define_invalid_2);
TEST_CASE(define_invalid_3);
TEST_CASE(define_invalid_4);
TEST_CASE(define_invalid_5);
TEST_CASE(define_invalid_6);
TEST_CASE(define_invalid_7);
TEST_CASE(define_invalid_8);
TEST_CASE(define_invalid_9);
TEST_CASE(define_invalid_10);
TEST_CASE(define_invalid_11);
TEST_CASE(define_define_1);
TEST_CASE(define_define_2);
TEST_CASE(define_define_3);
Expand Down
Loading