Skip to content

Commit f651117

Browse files
atkurtulbnoazx005
authored andcommitted
Adding error callback on macro invocation with insufficent number of args, and adding tests for variadic macros
1 parent fdbf829 commit f651117

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

source/tcppLibrary.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1518,7 +1518,9 @@ namespace tcpp
15181518
}
15191519
}
15201520

1521-
if (processingTokens.size() != macroDesc.mArgsNames.size() && !macroDesc.mVariadic)
1521+
if(macroDesc.mVariadic ?
1522+
(processingTokens.size() + 1 < macroDesc.mArgsNames.size()) :
1523+
(processingTokens.size() != macroDesc.mArgsNames.size()))
15221524
{
15231525
mOnErrorCallback({ E_ERROR_TYPE::INCONSISTENT_MACRO_ARITY, mpLexer->GetCurrLineIndex() });
15241526
}

tests/coreTests.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,8 +513,44 @@ TEST_CASE("Preprocessor Tests")
513513
REQUIRE((result && (actualResult == expectedResult)));
514514
}
515515

516-
SECTION("TestProcess_PassDefineThatSeparatedWithSpaces_ReturnsCorrectProcessedSource")
516+
SECTION("TestProcess_VA_ARGS")
517517
{
518+
std::string inputSource = "#define DO_STUFF(x, ...) x.do_stuf(); __VA_ARGS__\n int main() { DO_STUFF(2, 3+5, 4); }";
519+
520+
Lexer lexer(std::make_unique<StringInputStream>(inputSource));
521+
522+
Preprocessor preprocessor(lexer, { [](const TErrorInfo& err)
523+
{
524+
std::cout << "Error" << ErrorTypeToString(err.mType) << "\n";
525+
} });
526+
527+
std::cout << preprocessor.Process() << "\n";
528+
}
529+
530+
SECTION("TestProcess_VA_ARGS_MultiExpand")
531+
{
532+
std::string inputSource = R"(
533+
#define HEAD(x, ...) x
534+
#define TAIL(x, ...) __VA_ARGS__
535+
536+
int main()
537+
{
538+
return TAIL(1, 2, 3);
539+
}
540+
)";
541+
542+
Lexer lexer(std::make_unique<StringInputStream>(inputSource));
543+
544+
Preprocessor preprocessor(lexer, { [](const TErrorInfo& err)
545+
{
546+
std::cout << "Error" << ErrorTypeToString(err.mType) << "\n";
547+
} });
548+
549+
std::cout << preprocessor.Process() << "\n";
550+
}
551+
552+
SECTION("TestProcess_PassDefineThatSeparatedWithSpaces_ReturnsCorrectProcessedSource")
553+
{
518554
std::string inputSource = "# define Foo";
519555

520556
Lexer lexer(std::make_unique<StringInputStream>(inputSource));

0 commit comments

Comments
 (0)