Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
111 changes: 89 additions & 22 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1485,7 +1485,7 @@ namespace simplecpp {

class Macro {
public:
explicit Macro(std::vector<std::string> &f) : nameTokDef(nullptr), valueToken(nullptr), endToken(nullptr), files(f), tokenListDefine(f), variadic(false), valueDefinedInCode_(false) {}
explicit Macro(std::vector<std::string> &f) : nameTokDef(nullptr), valueToken(nullptr), endToken(nullptr), files(f), tokenListDefine(f), variadic(false), variadicOpt(false), optExpandValue(nullptr), optNoExpandValue(nullptr), valueDefinedInCode_(false) {}

Macro(const Token *tok, std::vector<std::string> &f) : nameTokDef(nullptr), files(f), tokenListDefine(f), valueDefinedInCode_(true) {
if (sameline(tok->previousSkipComments(), tok))
Expand Down Expand Up @@ -1515,6 +1515,11 @@ namespace simplecpp {
*this = other;
}

~Macro() {
delete optExpandValue;
delete optNoExpandValue;
}

Macro &operator=(const Macro &other) {
if (this != &other) {
files = other.files;
Expand Down Expand Up @@ -1707,6 +1712,9 @@ namespace simplecpp {
bool parseDefine(const Token *nametoken) {
nameTokDef = nametoken;
variadic = false;
variadicOpt = false;
optExpandValue = nullptr;
optNoExpandValue = nullptr;
if (!nameTokDef) {
valueToken = endToken = nullptr;
args.clear();
Expand Down Expand Up @@ -1744,8 +1752,54 @@ namespace simplecpp {
if (!sameline(valueToken, nameTokDef))
valueToken = nullptr;
endToken = valueToken;
while (sameline(endToken, nameTokDef))
while (sameline(endToken, nameTokDef)) {
if (variadic && endToken->str() == "__VA_OPT__")
variadicOpt = true;
endToken = endToken->next;
}

if (variadicOpt) {
TokenList expandValue(files);
TokenList noExpandValue(files);
for (const Token *tok = valueToken; tok && tok != endToken;) {
if (tok->str() == "__VA_OPT__") {
if (!sameline(tok, tok->next) || tok->next->op != '(')
throw Error(tok->location, "Missing opening parenthesis for __VA_OPT__");
tok = tok->next->next;
int par = 1;
while (tok && tok != endToken) {
if (tok->op == '(')
par++;
else if (tok->op == ')')
par--;
else if (tok->str() == "__VA_OPT__")
throw Error(tok->location, "__VA_OPT__ cannot be nested");
if (par == 0) {
tok = tok->next;
break;
}
expandValue.push_back(new Token(*tok));
tok = tok->next;
}
if (par != 0) {
const Token *const lastTok = expandValue.back() ? expandValue.back() : valueToken->next;
throw Error(lastTok->location, "Missing closing parenthesis for __VA_OPT__");
}
} else {
expandValue.push_back(new Token(*tok));
noExpandValue.push_back(new Token(*tok));
tok = tok->next;
}
}
#if __cplusplus >= 201103L
optExpandValue = new TokenList(std::move(expandValue));
optNoExpandValue = new TokenList(std::move(noExpandValue));
#else
optExpandValue = new TokenList(expandValue);
optNoExpandValue = new TokenList(noExpandValue);
#endif
}

return true;
}

Expand Down Expand Up @@ -1900,8 +1954,22 @@ namespace simplecpp {

Token * const output_end_1 = output->back();

const Token *valueToken2;
const Token *endToken2;

if (variadicOpt) {
if (parametertokens2.size() > args.size() && parametertokens2[args.size() - 1]->next->op != ')')
valueToken2 = optExpandValue->cfront();
else
valueToken2 = optNoExpandValue->cfront();
endToken2 = nullptr;
} else {
valueToken2 = valueToken;
endToken2 = endToken;
}

// expand
for (const Token *tok = valueToken; tok != endToken;) {
for (const Token *tok = valueToken2; tok != endToken2;) {
if (tok->op != '#') {
// A##B => AB
if (sameline(tok, tok->next) && tok->next && tok->next->op == '#' && tok->next->next && tok->next->next->op == '#') {
Expand Down Expand Up @@ -1950,7 +2018,7 @@ namespace simplecpp {
}

tok = tok->next;
if (tok == endToken) {
if (tok == endToken2) {
output->push_back(new Token(*tok->previous));
break;
}
Expand Down Expand Up @@ -2020,24 +2088,6 @@ namespace simplecpp {
// Macro parameter..
{
TokenList temp(files);
if (tok->str() == "__VA_OPT__") {
if (sameline(tok, tok->next) && tok->next->str() == "(") {
tok = tok->next;
int paren = 1;
while (sameline(tok, tok->next)) {
if (tok->next->str() == "(")
++paren;
else if (tok->next->str() == ")")
--paren;
if (paren == 0)
return tok->next->next;
tok = tok->next;
if (parametertokens.size() > args.size() && parametertokens.front()->next->str() != ")")
tok = expandToken(output, loc, tok, macros, expandedmacros, parametertokens)->previous;
}
}
throw Error(tok->location, "Missing parenthesis for __VA_OPT__(content)");
}
if (expandArg(&temp, tok, loc, macros, expandedmacros, parametertokens)) {
if (tok->str() == "__VA_ARGS__" && temp.empty() && output->cback() && output->cback()->str() == "," &&
tok->nextSkipComments() && tok->nextSkipComments()->str() == ")")
Expand Down Expand Up @@ -2338,6 +2388,13 @@ namespace simplecpp {
/** is macro variadic? */
bool variadic;

/** does the macro expansion have __VA_OPT__? */
bool variadicOpt;

/** Expansion value for varadic macros with __VA_OPT__ expanded and discarded respecively */
Comment thread
danmar marked this conversation as resolved.
Outdated
const TokenList *optExpandValue;
const TokenList *optNoExpandValue;

/** was the value of this macro actually defined in the code? */
bool valueDefinedInCode_;
};
Expand Down Expand Up @@ -3621,6 +3678,16 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
}
output.clear();
return;
} catch (simplecpp::Macro::Error &err) {
if (outputList) {
simplecpp::Output out(files);
out.type = simplecpp::Output::SYNTAX_ERROR;
out.location = err.location;
out.msg = "Failed to parse #define, " + err.what;
outputList->push_back(out);
}
output.clear();
return;
}
} else if (ifstates.top() == True && rawtok->str() == INCLUDE) {
TokenList inc1(files);
Expand Down
51 changes: 46 additions & 5 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ static void define_va_opt_3()

simplecpp::OutputList outputList;
ASSERT_EQUALS("", preprocess(code1, &outputList));
ASSERT_EQUALS("file0,1,syntax_error,failed to expand 'err', Missing parenthesis for __VA_OPT__(content)\n",
ASSERT_EQUALS("file0,1,syntax_error,Failed to parse #define, Missing closing parenthesis for __VA_OPT__\n",
toString(outputList));

outputList.clear();
Expand All @@ -934,7 +934,7 @@ static void define_va_opt_3()
"err()";

ASSERT_EQUALS("", preprocess(code2, &outputList));
ASSERT_EQUALS("file0,1,syntax_error,failed to expand 'err', Missing parenthesis for __VA_OPT__(content)\n",
ASSERT_EQUALS("file0,1,syntax_error,Failed to parse #define, Missing opening parenthesis for __VA_OPT__\n",
Comment thread
danmar marked this conversation as resolved.
Outdated
toString(outputList));
}

Expand All @@ -946,7 +946,7 @@ static void define_va_opt_4()

simplecpp::OutputList outputList;
ASSERT_EQUALS("", preprocess(code1, &outputList));
ASSERT_EQUALS("file0,1,syntax_error,failed to expand 'err', Missing parenthesis for __VA_OPT__(content)\n",
ASSERT_EQUALS("file0,1,syntax_error,Failed to parse #define, Missing opening parenthesis for __VA_OPT__\n",
toString(outputList));

outputList.clear();
Expand All @@ -956,7 +956,7 @@ static void define_va_opt_4()
"err()";

ASSERT_EQUALS("", preprocess(code2, &outputList));
ASSERT_EQUALS("file0,1,syntax_error,failed to expand 'err', Missing parenthesis for __VA_OPT__(content)\n",
ASSERT_EQUALS("file0,1,syntax_error,Failed to parse #define, Missing opening parenthesis for __VA_OPT__\n",
toString(outputList));
}

Expand All @@ -968,7 +968,46 @@ static void define_va_opt_5()

simplecpp::OutputList outputList;
ASSERT_EQUALS("", preprocess(code, &outputList));
ASSERT_EQUALS("file0,1,syntax_error,failed to expand 'err', Missing parenthesis for __VA_OPT__(content)\n",
ASSERT_EQUALS("file0,1,syntax_error,Failed to parse #define, Missing opening parenthesis for __VA_OPT__\n",
toString(outputList));
}

static void define_va_opt_6()
{
// nested __VA_OPT__
const char code[] = "#define err(...) __VA_OPT__(__VA_OPT__(something))\n"
"err()";

simplecpp::OutputList outputList;
ASSERT_EQUALS("", preprocess(code, &outputList));
ASSERT_EQUALS("file0,1,syntax_error,Failed to parse #define, __VA_OPT__ cannot be nested\n",
toString(outputList));
}

static void define_va_opt_7()
{
// eof in __VA_OPT__
const char code1[] = "#define err(...) __VA_OPT__";

simplecpp::OutputList outputList;
ASSERT_EQUALS("", preprocess(code1, &outputList));
ASSERT_EQUALS("file0,1,syntax_error,Failed to parse #define, Missing opening parenthesis for __VA_OPT__\n",
toString(outputList));

outputList.clear();

const char code2[] = "#define err(...) __VA_OPT__(";

ASSERT_EQUALS("", preprocess(code2, &outputList));
ASSERT_EQUALS("file0,1,syntax_error,Failed to parse #define, Missing closing parenthesis for __VA_OPT__\n",
toString(outputList));

outputList.clear();

const char code3[] = "#define err(...) __VA_OPT__(x";

ASSERT_EQUALS("", preprocess(code3, &outputList));
ASSERT_EQUALS("file0,1,syntax_error,Failed to parse #define, Missing closing parenthesis for __VA_OPT__\n",
toString(outputList));
}

Expand Down Expand Up @@ -3063,6 +3102,8 @@ int main(int argc, char **argv)
TEST_CASE(define_va_opt_3);
TEST_CASE(define_va_opt_4);
TEST_CASE(define_va_opt_5);
TEST_CASE(define_va_opt_6);
TEST_CASE(define_va_opt_7);

TEST_CASE(pragma_backslash); // multiline pragma directive

Expand Down
13 changes: 13 additions & 0 deletions testsuite/clang-preprocessor-tests/macro_fn_va_opt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: %clang_cc1 -E %s | grep '^ printf( "%%s" , "Hello" );$'

#define P( x, ...) printf( x __VA_OPT__(,) __VA_ARGS__ )
#define PF( x, ...) P( x __VA_OPT__(,) __VA_ARGS__ )

int main()
{
PF( "%s", "Hello" );
PF( "Hello", );
PF( "Hello" );
PF( , );
PF( );
}
Loading