Skip to content

Commit b3ce564

Browse files
committed
Implement incorrect stringification operator processing
1 parent f651117 commit b3ce564

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

source/tcppLibrary.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ namespace tcpp
274274
ELIF_BLOCK_AFTER_ELSE_FOUND,
275275
UNDEFINED_DIRECTIVE,
276276
INCORRECT_OPERATION_USAGE,
277+
INCORRECT_STRINGIFY_OPERATOR_USAGE,
277278
};
278279

279280

@@ -411,6 +412,8 @@ namespace tcpp
411412
return "#elif found after #else block";
412413
case E_ERROR_TYPE::UNDEFINED_DIRECTIVE:
413414
return "Undefined directive";
415+
case E_ERROR_TYPE::INCORRECT_STRINGIFY_OPERATOR_USAGE:
416+
return "Incorrect usage of stringification operation.";
414417
}
415418

416419
return "";
@@ -1291,6 +1294,8 @@ namespace tcpp
12911294
{
12921295
desc.mValue.push_back(currToken);
12931296

1297+
const bool isStringificationOperator = currToken.mType == E_TOKEN_TYPE::STRINGIZE_OP;
1298+
12941299
switch (currToken.mType)
12951300
{
12961301
case E_TOKEN_TYPE::CONCAT_OP:
@@ -1307,6 +1312,16 @@ namespace tcpp
13071312
continue;
13081313
}
13091314

1315+
if (E_TOKEN_TYPE::IDENTIFIER == currToken.mType && isStringificationOperator) // \note Validate argument of the operator
1316+
{
1317+
auto&& macroArgs = desc.mArgsNames;
1318+
1319+
if (std::find(macroArgs.cbegin(), macroArgs.cend(), currToken.mRawView) == macroArgs.cend())
1320+
{
1321+
mOnErrorCallback({ E_ERROR_TYPE::INCORRECT_STRINGIFY_OPERATOR_USAGE, mpLexer->GetCurrLineIndex() });
1322+
}
1323+
}
1324+
13101325
desc.mValue.push_back(currToken);
13111326
}
13121327
}

tests/coreTests.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ FIRST((1, 2) c, 3)
932932
std::string output = preprocessor.Process();
933933
REQUIRE((result && output == "\n(1, 2) c\n"));
934934
}
935-
#if 0
935+
936936
SECTION("TestProcess_StringifyOperatorInvokedOnNonParameterToken_ProcessingErrorOccurs")
937937
{
938938
std::string inputSource = R"(
@@ -947,17 +947,19 @@ TEST(3)
947947
Preprocessor preprocessor(lexer, { [&result](auto&& arg)
948948
{
949949
std::cerr << "Error: " << ErrorTypeToString(arg.mType) << std::endl;
950+
951+
REQUIRE(arg.mType == E_ERROR_TYPE::INCORRECT_STRINGIFY_OPERATOR_USAGE);
950952
result = false;
951953
}, [](auto&&, auto&&)
952954
{
953955
return nullptr;
954956
},
955957
true });
956958

957-
preprocessor.Process();
959+
const std::string& output = preprocessor.Process();
958960
REQUIRE(!result);
959961
}
960-
#endif
962+
961963
SECTION("TestProcess_PassEmptyArg_MacroExpanded")
962964
{
963965
std::string inputSource = R"(

0 commit comments

Comments
 (0)