From 10a3ce383aff3fc0d0ab048e3d9bd59bff888883 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Mon, 17 Jun 2024 13:00:59 +0300 Subject: [PATCH] Small rework + tests ish --- lib/preprocessor.cpp | 12 +--- lib/preprocessor.h | 4 +- lib/tokenize.cpp | 16 ++--- test/testtokenize.cpp | 158 +++++++++++++++++++++++++++++++++++------- 4 files changed, 141 insertions(+), 49 deletions(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 0589a5225d3..c5b4ef685c5 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -49,12 +49,6 @@ Directive::Directive(std::string _file, const int _linenr, const std::string &_s str(trim(_str)) {} -Directive::Directive(std::string _file, const int _linenr, const simplecpp::Token * _startToken) : - file(std::move(_file)), - linenr(_linenr), - startToken(_startToken) -{} - char Preprocessor::macroChar = char(1); Preprocessor::Preprocessor(const Settings& settings, ErrorLogger &errorLogger) : mSettings(settings), mErrorLogger(errorLogger) @@ -334,7 +328,7 @@ std::list Preprocessor::createDirectives(const simplecpp::TokenList & continue; if (tok->next && tok->next->str() == "endfile") continue; - Directive directive(tok->location.file(), tok->location.line, tok); + Directive directive(tok->location.file(), tok->location.line, emptyString); for (const simplecpp::Token *tok2 = tok; tok2 && tok2->location.line == directive.linenr; tok2 = tok2->next) { if (tok2->comment) continue; @@ -344,8 +338,8 @@ std::list Preprocessor::createDirectives(const simplecpp::TokenList & directive.str += "include"; else directive.str += tok2->str(); - if (!tok2->next || tok2->next->location.line != directive.linenr) - directive.endToken = tok2; + + directive.strTokens.push_back(std::make_pair(tok2->str(), tok2->location.col)); } directives.push_back(std::move(directive)); } diff --git a/lib/preprocessor.h b/lib/preprocessor.h index cf8fd5dbcb1..28c2bfb96d0 100644 --- a/lib/preprocessor.h +++ b/lib/preprocessor.h @@ -56,12 +56,10 @@ struct CPPCHECKLIB Directive { /** the actual directive text */ std::string str; - const simplecpp::Token * startToken{nullptr}; - const simplecpp::Token * endToken{nullptr}; + std::vector> strTokens; /** record a directive (possibly filtering src) */ Directive(std::string _file, const int _linenr, const std::string &_str); - Directive(std::string _file, const int _linenr, const simplecpp::Token * _startToken); }; class CPPCHECKLIB RemarkComment { diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index de6d07223e2..16a1d3abc14 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5959,24 +5959,16 @@ void Tokenizer::dump(std::ostream &out) const outs += ErrorLogger::toxml(dir.str); outs +="\">"; outs += '\n'; - for (const simplecpp::Token * tok = dir.startToken; tok && tok != dir.endToken; tok = tok->next) { - outs += " location.col); + outs += std::to_string(strToken.second); outs += "\" "; outs += "str=\""; - outs += ErrorLogger::toxml(tok->str()); + outs += ErrorLogger::toxml(strToken.first); outs +="\"/>"; outs += '\n'; } - outs += " location.col); - outs += "\" "; - outs += "str=\""; - outs += ErrorLogger::toxml(dir.endToken->str()); - outs +="\"/>"; - outs += '\n'; outs += " "; outs += '\n'; } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 353b4f35998..4345090f565 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -8075,19 +8075,80 @@ class TestTokenizer : public TestFixture { "#warning some warning message\n" "#error some error message\n"; const char dumpdata[] = " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" " \n" " \n" " \n"; @@ -8108,17 +8169,49 @@ class TestTokenizer : public TestFixture { "#endfile\n" "#define macro5 val\n"; const char dumpdata[] = " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" " \n" " \n" " \n"; - std::ostringstream ostr; directiveDump(filedata, ostr); ASSERT_EQUALS(dumpdata, ostr.str()); @@ -8129,9 +8222,19 @@ class TestTokenizer : public TestFixture { "#else /* this will be removed too */\n" "#endif /* this will also be removed */\n"; const char dumpdata[] = " \n" - " \n" - " \n" - " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" " \n" " \n" " \n"; @@ -8144,7 +8247,12 @@ class TestTokenizer : public TestFixture { void testDirectiveRelativePath() { const char filedata[] = "#define macro 1\n"; const char dumpdata[] = " \n" - " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" " \n" " \n" " \n";