Visitar URL original
refs #507 - cleaned up and improved parsing of line preprocessor dire… · firewave/simplecpp@56f488f · GitHub
Skip to content

Commit 56f488f

Browse files
committed
refs cppcheck-opensource#507 - cleaned up and improved parsing of line preprocessor directive [skip ci]
1 parent 24a7e87 commit 56f488f

2 files changed

Lines changed: 50 additions & 37 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -660,8 +660,6 @@ static const std::string COMMENT_END("*/");
660660

661661
void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename, OutputList *outputList)
662662
{
663-
std::stack<simplecpp::Location> loc;
664-
665663
unsigned int multiline = 0U;
666664

667665
const Token *oldLastToken = nullptr;
@@ -700,42 +698,44 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
700698

701699
if (oldLastToken != cback()) {
702700
oldLastToken = cback();
703-
const Token * const llTok = isLastLinePreprocessor();
704-
if (!llTok)
701+
702+
// #line 3
703+
// #line 3 "file.c"
704+
// #3
705+
// #3 "file.c"
706+
const Token * ppTok = isLastLinePreprocessor();
707+
if (!ppTok)
705708
continue;
706-
const Token * const llNextToken = llTok->next;
707-
if (!llTok->next)
709+
710+
const auto advanceAndSkipComments = [](const Token* tok) {
711+
do {
712+
tok = tok->next;
713+
} while (tok && tok->comment);
714+
return tok;
715+
};
716+
717+
// skip #
718+
ppTok = advanceAndSkipComments(ppTok);
719+
if (!ppTok)
708720
continue;
709-
if (llNextToken->next) {
710-
// TODO: add support for "# 3"
711-
// #3 "file.c"
712-
// #line 3 "file.c"
713-
if ((llNextToken->number &&
714-
llNextToken->next->str()[0] == '\"') ||
715-
(llNextToken->str() == "line" &&
716-
llNextToken->next->number &&
717-
llNextToken->next->next &&
718-
llNextToken->next->next->str()[0] == '\"'))
719-
{
720-
const Token *strtok = cback();
721-
while (strtok->comment)
722-
strtok = strtok->previous;
723-
const Token *numtok = strtok->previous;
724-
while (numtok->comment)
725-
numtok = numtok->previous;
726-
lineDirective(fileIndex(replaceAll(strtok->str().substr(1U, strtok->str().size() - 2U),"\\\\","\\")),
727-
std::atol(numtok->str().c_str()), location);
728-
}
729-
// #line 3
730-
else if (llNextToken->str() == "line" &&
731-
llNextToken->next->number)
732-
{
733-
const Token *numtok = cback();
734-
while (numtok->comment)
735-
numtok = numtok->previous;
736-
lineDirective(location.fileIndex, std::atol(numtok->str().c_str()), location);
737-
}
738-
}
721+
722+
if (ppTok->str() == "line")
723+
ppTok = advanceAndSkipComments(ppTok);
724+
725+
if (!ppTok || !ppTok->number)
726+
continue;
727+
728+
const unsigned int line = std::atol(ppTok->str().c_str());
729+
ppTok = advanceAndSkipComments(ppTok);
730+
731+
unsigned int fileindex;
732+
733+
if (ppTok && ppTok->str()[0] == '\"')
734+
fileindex = fileIndex(replaceAll(ppTok->str().substr(1U, ppTok->str().size() - 2U),"\\\\","\\"));
735+
else
736+
fileindex = location.fileIndex;
737+
738+
lineDirective(fileindex, line, &location);
739739
}
740740

741741
continue;

‎test.cpp‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2412,7 +2412,8 @@ static void location8()
24122412
"# 3\n"
24132413
"__LINE__ __FILE__\n";
24142414
ASSERT_EQUALS("\n"
2415-
"2 \"\"", // TODO: should say 3
2415+
"\n"
2416+
"3 \"\"",
24162417
preprocess(code));
24172418
}
24182419

@@ -2453,6 +2454,17 @@ static void location11()
24532454
preprocess(code));
24542455
}
24552456

2457+
static void location12()
2458+
{
2459+
const char code[] =
2460+
"/**//**/#/**//**/line/**//**/3/**//**/\"file.c\"/**/\n"
2461+
"__LINE__ __FILE__\n";
2462+
ASSERT_EQUALS("\n"
2463+
"#line 3 \"file.c\"\n"
2464+
"3 \"file.c\"",
2465+
preprocess(code));
2466+
}
2467+
24562468
static void missingHeader1()
24572469
{
24582470
const char code[] = "#include \"notexist.h\"\n";
@@ -4105,6 +4117,7 @@ static void runTests(int argc, char **argv, Input input)
41054117
TEST_CASE(location9);
41064118
TEST_CASE(location10);
41074119
TEST_CASE(location11);
4120+
TEST_CASE(location12);
41084121

41094122
TEST_CASE(missingHeader1);
41104123
TEST_CASE(missingHeader2);

0 commit comments

Comments
 (0)