Skip to content

Commit

Permalink
Small rework + tests ish
Browse files Browse the repository at this point in the history
  • Loading branch information
olabetskyi committed Jun 17, 2024
1 parent dc47148 commit 10a3ce3
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 49 deletions.
12 changes: 3 additions & 9 deletions lib/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -334,7 +328,7 @@ std::list<Directive> 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;
Expand All @@ -344,8 +338,8 @@ std::list<Directive> 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));
}
Expand Down
4 changes: 1 addition & 3 deletions lib/preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::pair<std::string, const int>> 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 {
Expand Down
16 changes: 4 additions & 12 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 += " <token ";
for (auto strToken : dir.strTokens) {
outs += " <token ";
outs += "column=\"";
outs += std::to_string(tok->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 += " <token ";
outs += "column=\"";
outs += std::to_string(dir.endToken->location.col);
outs += "\" ";
outs += "str=\"";
outs += ErrorLogger::toxml(dir.endToken->str());
outs +="\"/>";
outs += '\n';
outs += " </directive>";
outs += '\n';
}
Expand Down
158 changes: 133 additions & 25 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8075,19 +8075,80 @@ class TestTokenizer : public TestFixture {
"#warning some warning message\n"
"#error some error message\n";
const char dumpdata[] = " <directivelist>\n"
" <directive file=\"test.c\" linenr=\"1\" str=\"#define macro some definition\"/>\n"
" <directive file=\"test.c\" linenr=\"2\" str=\"#undef macro\"/>\n"
" <directive file=\"test.c\" linenr=\"3\" str=\"#ifdef macro\"/>\n"
" <directive file=\"test.c\" linenr=\"4\" str=\"#elif some (complex) condition\"/>\n"
" <directive file=\"test.c\" linenr=\"5\" str=\"#else\"/>\n"
" <directive file=\"test.c\" linenr=\"6\" str=\"#endif\"/>\n"
" <directive file=\"test.c\" linenr=\"7\" str=\"#if some other condition\"/>\n"
" <directive file=\"test.c\" linenr=\"8\" str=\"#pragma some proprietary content\"/>\n"
" <directive file=\"test.c\" linenr=\"9\" str=\"#\"/>\n"
" <directive file=\"test.c\" linenr=\"10\" str=\"#ident some text\"/>\n"
" <directive file=\"test.c\" linenr=\"11\" str=\"#unknownmacro some unpredictable text\"/>\n"
" <directive file=\"test.c\" linenr=\"12\" str=\"#warning some warning message\"/>\n"
" <directive file=\"test.c\" linenr=\"13\" str=\"#error some error message\"/>\n"
" <directive file=\"test.c\" linenr=\"1\" str=\"#define macro some definition\">\n"
" <token column=\"1\" str=\"#\"/>\n"
" <token column=\"2\" str=\"define\"/>\n"
" <token column=\"9\" str=\"macro\"/>\n"
" <token column=\"15\" str=\"some\"/>\n"
" <token column=\"20\" str=\"definition\"/>\n"
" </directive>\n"
" <directive file=\"test.c\" linenr=\"2\" str=\"#undef macro\">\n"
" <token column=\"1\" str=\"#\"/>\n"
" <token column=\"2\" str=\"undef\"/>\n"
" <token column=\"8\" str=\"macro\"/>\n"
" </directive>\n"
" <directive file=\"test.c\" linenr=\"3\" str=\"#ifdef macro\">\n"
" <token column=\"1\" str=\"#\"/>\n"
" <token column=\"2\" str=\"ifdef\"/>\n"
" <token column=\"8\" str=\"macro\"/>\n"
" </directive>\n"
" <directive file=\"test.c\" linenr=\"4\" str=\"#elif some (complex) condition\">\n"
" <token column=\"1\" str=\"#\"/>\n"
" <token column=\"2\" str=\"elif\"/>\n"
" <token column=\"7\" str=\"some\"/>\n"
" <token column=\"12\" str=\"(\"/>\n"
" <token column=\"13\" str=\"complex\"/>\n"
" <token column=\"20\" str=\")\"/>\n"
" <token column=\"22\" str=\"condition\"/>\n"
" </directive>\n"
" <directive file=\"test.c\" linenr=\"5\" str=\"#else\">\n"
" <token column=\"1\" str=\"#\"/>\n"
" <token column=\"2\" str=\"else\"/>\n"
" </directive>\n"
" <directive file=\"test.c\" linenr=\"6\" str=\"#endif\">\n"
" <token column=\"1\" str=\"#\"/>\n"
" <token column=\"2\" str=\"endif\"/>\n"
" </directive>\n"
" <directive file=\"test.c\" linenr=\"7\" str=\"#if some other condition\">\n"
" <token column=\"1\" str=\"#\"/>\n"
" <token column=\"2\" str=\"if\"/>\n"
" <token column=\"5\" str=\"some\"/>\n"
" <token column=\"10\" str=\"other\"/>\n"
" <token column=\"16\" str=\"condition\"/>\n"
" </directive>\n"
" <directive file=\"test.c\" linenr=\"8\" str=\"#pragma some proprietary content\">\n"
" <token column=\"1\" str=\"#\"/>\n"
" <token column=\"2\" str=\"pragma\"/>\n"
" <token column=\"9\" str=\"some\"/>\n"
" <token column=\"14\" str=\"proprietary\"/>\n"
" <token column=\"26\" str=\"content\"/>\n"
" </directive>\n"
" <directive file=\"test.c\" linenr=\"9\" str=\"#\">\n"
" <token column=\"1\" str=\"#\"/>\n"
" </directive>\n"
" <directive file=\"test.c\" linenr=\"10\" str=\"#ident some text\">\n"
" <token column=\"1\" str=\"#\"/>\n"
" <token column=\"2\" str=\"ident\"/>\n"
" <token column=\"8\" str=\"some\"/>\n"
" <token column=\"13\" str=\"text\"/>\n"
" </directive>\n"
" <directive file=\"test.c\" linenr=\"11\" str=\"#unknownmacro some unpredictable text\">\n"
" <token column=\"1\" str=\"#\"/>\n"
" <token column=\"2\" str=\"unknownmacro\"/>\n"
" <token column=\"15\" str=\"some\"/>\n"
" <token column=\"20\" str=\"unpredictable\"/>\n"
" <token column=\"34\" str=\"text\"/>\n"
" </directive>\n"
" <directive file=\"test.c\" linenr=\"12\" str=\"#warning some warning message\">\n"
" <token column=\"1\" str=\"#\"/>\n"
" <token column=\"2\" str=\"warning\"/>\n"
" <token column=\"10\" str=\"some warning message\"/>\n"
" </directive>\n"
" <directive file=\"test.c\" linenr=\"13\" str=\"#error some error message\">\n"
" <token column=\"1\" str=\"#\"/>\n"
" <token column=\"2\" str=\"error\"/>\n"
" <token column=\"8\" str=\"some error message\"/>\n"
" </directive>\n"
" </directivelist>\n"
" <tokenlist>\n"
" </tokenlist>\n";
Expand All @@ -8108,17 +8169,49 @@ class TestTokenizer : public TestFixture {
"#endfile\n"
"#define macro5 val\n";
const char dumpdata[] = " <directivelist>\n"
" <directive file=\"test.c\" linenr=\"1\" str=\"#define macro1 val\"/>\n"
" <directive file=\"test.c\" linenr=\"2\" str=\"#include &quot;inc1.h&quot;\"/>\n"
" <directive file=\"inc1.h\" linenr=\"1\" str=\"#define macro2 val\"/>\n"
" <directive file=\"inc1.h\" linenr=\"2\" str=\"#include &quot;inc2.h&quot;\"/>\n"
" <directive file=\"inc2.h\" linenr=\"1\" str=\"#define macro3 val\"/>\n"
" <directive file=\"inc1.h\" linenr=\"3\" str=\"#define macro4 val\"/>\n"
" <directive file=\"test.c\" linenr=\"3\" str=\"#define macro5 val\"/>\n"
" <directive file=\"test.c\" linenr=\"1\" str=\"#define macro1 val\">\n"
" <token column=\"1\" str=\"#\"/>\n"
" <token column=\"2\" str=\"define\"/>\n"
" <token column=\"9\" str=\"macro1\"/>\n"
" <token column=\"16\" str=\"val\"/>\n"
" </directive>\n"
" <directive file=\"test.c\" linenr=\"2\" str=\"#include &quot;inc1.h&quot;\">\n"
" <token column=\"1\" str=\"#\"/>\n"
" <token column=\"2\" str=\"file\"/>\n"
" <token column=\"7\" str=\"&quot;inc1.h&quot;\"/>\n"
" </directive>\n"
" <directive file=\"inc1.h\" linenr=\"1\" str=\"#define macro2 val\">\n"
" <token column=\"1\" str=\"#\"/>\n"
" <token column=\"2\" str=\"define\"/>\n"
" <token column=\"9\" str=\"macro2\"/>\n"
" <token column=\"16\" str=\"val\"/>\n"
" </directive>\n"
" <directive file=\"inc1.h\" linenr=\"2\" str=\"#include &quot;inc2.h&quot;\">\n"
" <token column=\"1\" str=\"#\"/>\n"
" <token column=\"2\" str=\"file\"/>\n"
" <token column=\"7\" str=\"&quot;inc2.h&quot;\"/>\n"
" </directive>\n"
" <directive file=\"inc2.h\" linenr=\"1\" str=\"#define macro3 val\">\n"
" <token column=\"1\" str=\"#\"/>\n"
" <token column=\"2\" str=\"define\"/>\n"
" <token column=\"9\" str=\"macro3\"/>\n"
" <token column=\"16\" str=\"val\"/>\n"
" </directive>\n"
" <directive file=\"inc1.h\" linenr=\"3\" str=\"#define macro4 val\">\n"
" <token column=\"1\" str=\"#\"/>\n"
" <token column=\"2\" str=\"define\"/>\n"
" <token column=\"9\" str=\"macro4\"/>\n"
" <token column=\"16\" str=\"val\"/>\n"
" </directive>\n"
" <directive file=\"test.c\" linenr=\"3\" str=\"#define macro5 val\">\n"
" <token column=\"1\" str=\"#\"/>\n"
" <token column=\"2\" str=\"define\"/>\n"
" <token column=\"9\" str=\"macro5\"/>\n"
" <token column=\"16\" str=\"val\"/>\n"
" </directive>\n"
" </directivelist>\n"
" <tokenlist>\n"
" </tokenlist>\n";

std::ostringstream ostr;
directiveDump(filedata, ostr);
ASSERT_EQUALS(dumpdata, ostr.str());
Expand All @@ -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[] = " <directivelist>\n"
" <directive file=\"test.c\" linenr=\"1\" str=\"#ifdef macro2\"/>\n"
" <directive file=\"test.c\" linenr=\"2\" str=\"#else\"/>\n"
" <directive file=\"test.c\" linenr=\"3\" str=\"#endif\"/>\n"
" <directive file=\"test.c\" linenr=\"1\" str=\"#ifdef macro2\">\n"
" <token column=\"1\" str=\"#\"/>\n"
" <token column=\"2\" str=\"ifdef\"/>\n"
" <token column=\"8\" str=\"macro2\"/>\n"
" </directive>\n"
" <directive file=\"test.c\" linenr=\"2\" str=\"#else\">\n"
" <token column=\"1\" str=\"#\"/>\n"
" <token column=\"2\" str=\"else\"/>\n"
" </directive>\n"
" <directive file=\"test.c\" linenr=\"3\" str=\"#endif\">\n"
" <token column=\"1\" str=\"#\"/>\n"
" <token column=\"2\" str=\"endif\"/>\n"
" </directive>\n"
" </directivelist>\n"
" <tokenlist>\n"
" </tokenlist>\n";
Expand All @@ -8144,7 +8247,12 @@ class TestTokenizer : public TestFixture {
void testDirectiveRelativePath() {
const char filedata[] = "#define macro 1\n";
const char dumpdata[] = " <directivelist>\n"
" <directive file=\"test.c\" linenr=\"1\" str=\"#define macro 1\"/>\n"
" <directive file=\"test.c\" linenr=\"1\" str=\"#define macro 1\">\n"
" <token column=\"1\" str=\"#\"/>\n"
" <token column=\"2\" str=\"define\"/>\n"
" <token column=\"9\" str=\"macro\"/>\n"
" <token column=\"15\" str=\"1\"/>\n"
" </directive>\n"
" </directivelist>\n"
" <tokenlist>\n"
" </tokenlist>\n";
Expand Down

0 comments on commit 10a3ce3

Please sign in to comment.