Skip to content

Commit

Permalink
Add extra information tto directives
Browse files Browse the repository at this point in the history
  • Loading branch information
olabetskyi committed Jun 14, 2024
1 parent 83f890a commit dc47148
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lib/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ 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 @@ -328,7 +334,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, emptyString);
Directive directive(tok->location.file(), tok->location.line, tok);
for (const simplecpp::Token *tok2 = tok; tok2 && tok2->location.line == directive.linenr; tok2 = tok2->next) {
if (tok2->comment)
continue;
Expand All @@ -338,6 +344,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;
}
directives.push_back(std::move(directive));
}
Expand Down
6 changes: 5 additions & 1 deletion lib/preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class SuppressionList;
* Each preprocessor directive (\#include, \#define, \#undef, \#if, \#ifdef, \#else, \#endif)
* will be recorded as an instance of this class.
*
* file and linenr denote the location where where the directive is defined.
* file and linenr denote the location where the directive is defined.
*
*/

Expand All @@ -56,8 +56,12 @@ struct CPPCHECKLIB Directive {
/** the actual directive text */
std::string str;

const simplecpp::Token * startToken{nullptr};
const simplecpp::Token * endToken{nullptr};

/** 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
20 changes: 20 additions & 0 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5957,8 +5957,28 @@ void Tokenizer::dump(std::ostream &out) const
// could result in invalid XML, so run it through toxml().
outs += "str=\"";
outs += ErrorLogger::toxml(dir.str);
outs +="\">";
outs += '\n';
for (const simplecpp::Token * tok = dir.startToken; tok && tok != dir.endToken; tok = tok->next) {
outs += " <token ";
outs += "column=\"";
outs += std::to_string(tok->location.col);
outs += "\" ";
outs += "str=\"";
outs += ErrorLogger::toxml(tok->str());
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';
}
outs += " </directivelist>";
outs += '\n';
Expand Down

0 comments on commit dc47148

Please sign in to comment.