Skip to content

Commit 98c176f

Browse files
committed
adjusted file lookups from simplecpp::Location
1 parent a55c90b commit 98c176f

File tree

4 files changed

+29
-28
lines changed

4 files changed

+29
-28
lines changed

lib/cppcheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,12 +1154,12 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
11541154
}
11551155
else {
11561156
// #error etc during preprocessing
1157-
configurationError.push_back((currentConfig.empty() ? "\'\'" : currentConfig) + " : [" + o->location.file() + ':' + std::to_string(o->location.line) + "] " + o->msg);
1157+
configurationError.push_back((currentConfig.empty() ? "\'\'" : currentConfig) + " : [" + tokensP.file(o->location) + ':' + std::to_string(o->location.line) + "] " + o->msg);
11581158
--checkCount; // don't count invalid configurations
11591159

11601160
if (!hasValidConfig && currCfg == *configurations.rbegin()) {
11611161
// If there is no valid configuration then report error..
1162-
preprocessor.error(o->location.file(), o->location.line, o->location.col, o->msg, o->type);
1162+
preprocessor.error(tokensP.file(o->location), o->location.line, o->location.col, o->msg, o->type);
11631163
}
11641164
skipCfg = true;
11651165
}

lib/preprocessor.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ static bool sameline(const simplecpp::Token *tok1, const simplecpp::Token *tok2)
4545
return tok1 && tok2 && tok1->location.sameline(tok2->location);
4646
}
4747

48-
Directive::Directive(const simplecpp::Location & _loc, std::string _str) :
49-
file(_loc.file()),
48+
Directive::Directive(const simplecpp::TokenList &tokens, const simplecpp::Location & _loc, std::string _str) :
49+
file(tokens.file(_loc)),
5050
linenr(_loc.line),
5151
str(std::move(_str))
5252
{}
@@ -78,7 +78,7 @@ namespace {
7878
};
7979
}
8080

81-
static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std::list<SuppressionList::Suppression> &inlineSuppressions, std::list<BadInlineSuppression> &bad)
81+
static bool parseInlineSuppressionCommentToken(const simplecpp::TokenList &tokens, const simplecpp::Token *tok, std::list<SuppressionList::Suppression> &inlineSuppressions, std::list<BadInlineSuppression> &bad)
8282
{
8383
const std::string cppchecksuppress("cppcheck-suppress");
8484

@@ -91,7 +91,7 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std:
9191
if (comment.substr(pos1, cppchecksuppress.size()) != cppchecksuppress)
9292
return false;
9393
if (pos1 + cppchecksuppress.size() >= comment.size()) {
94-
bad.emplace_back(tok->location.file(), tok->location.line, 0, "suppression without error ID");
94+
bad.emplace_back(tokens.file(tok->location), tok->location.line, 0, "suppression without error ID");
9595
return false;
9696
}
9797

@@ -101,7 +101,7 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std:
101101
// skip spaces after "cppcheck-suppress" and its possible prefix
102102
const std::string::size_type pos2 = comment.find_first_not_of(' ', posEndComment);
103103
if (pos2 == std::string::npos) {
104-
bad.emplace_back(tok->location.file(), tok->location.line, 0, "suppression without error ID");
104+
bad.emplace_back(tokens.file(tok->location), tok->location.line, 0, "suppression without error ID");
105105
return false;
106106
}
107107

@@ -111,7 +111,7 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std:
111111
if (posEndComment >= (pos1 + cppchecksuppress.size() + 1)) {
112112
const std::string suppressCmdString = comment.substr(pos1, pos2-pos1-1);
113113
if (comment.at(pos1 + cppchecksuppress.size()) != '-') {
114-
bad.emplace_back(tok->location.file(), tok->location.line, 0, "unknown suppression type '" + suppressCmdString + "'"); // TODO: set column
114+
bad.emplace_back(tokens.file(tok->location), tok->location.line, 0, "unknown suppression type '" + suppressCmdString + "'"); // TODO: set column
115115
return false;
116116
}
117117

@@ -130,7 +130,7 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std:
130130
else if ("macro" == suppressTypeString)
131131
errorType = SuppressionList::Type::macro;
132132
else {
133-
bad.emplace_back(tok->location.file(), tok->location.line, 0, "unknown suppression type '" + suppressCmdString + "'"); // TODO: set column
133+
bad.emplace_back(tokens.file(tok->location), tok->location.line, 0, "unknown suppression type '" + suppressCmdString + "'"); // TODO: set column
134134
return false;
135135
}
136136
}
@@ -148,7 +148,7 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std:
148148

149149
// TODO: return false?
150150
if (!errmsg.empty())
151-
bad.emplace_back(tok->location.file(), tok->location.line, tok->location.col, std::move(errmsg));
151+
bad.emplace_back(tokens.file(tok->location), tok->location.line, tok->location.col, std::move(errmsg));
152152

153153
// TODO: report ones without ID - return false?
154154
std::copy_if(suppressions.cbegin(), suppressions.cend(), std::back_inserter(inlineSuppressions), [](const SuppressionList::Suppression& s) {
@@ -172,16 +172,16 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std:
172172
// TODO: unreachable?
173173
// TODO: return false?
174174
if (!errmsg.empty())
175-
bad.emplace_back(tok->location.file(), tok->location.line, tok->location.col, std::move(errmsg));
175+
bad.emplace_back(tokens.file(tok->location), tok->location.line, tok->location.col, std::move(errmsg));
176176
}
177177

178178
return true;
179179
}
180180

181-
static std::string getRelativeFilename(const simplecpp::Token* tok, const Settings &settings) {
181+
static std::string getRelativeFilename(const simplecpp::TokenList &tokens, const simplecpp::Token* tok, const Settings &settings) {
182182
if (!tok)
183183
return "";
184-
std::string relativeFilename(tok->location.file());
184+
std::string relativeFilename(tokens.file(tok->location));
185185
if (settings.relativePaths) {
186186
for (const std::string & basePath : settings.basePaths) {
187187
const std::string bp = basePath + "/";
@@ -206,7 +206,7 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett
206206
}
207207

208208
std::list<SuppressionList::Suppression> inlineSuppressions;
209-
if (!parseInlineSuppressionCommentToken(tok, inlineSuppressions, bad))
209+
if (!parseInlineSuppressionCommentToken(tokens, tok, inlineSuppressions, bad))
210210
continue;
211211

212212
if (!sameline(tok->previous, tok)) {
@@ -215,7 +215,7 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett
215215
tok = tok->next;
216216

217217
while (tok->comment) {
218-
parseInlineSuppressionCommentToken(tok, inlineSuppressions, bad);
218+
parseInlineSuppressionCommentToken(tokens, tok, inlineSuppressions, bad);
219219
if (tok->next) {
220220
tok = tok->next;
221221
} else {
@@ -233,7 +233,7 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett
233233
continue;
234234

235235
// Relative filename
236-
const std::string relativeFilename = getRelativeFilename(tok, settings);
236+
const std::string relativeFilename = getRelativeFilename(tokens, tok, settings);
237237

238238
// Macro name
239239
std::string macroName;
@@ -354,7 +354,7 @@ std::list<Directive> Preprocessor::createDirectives() const
354354
continue;
355355
if (tok->next && tok->next->str() == "endfile")
356356
continue;
357-
Directive directive(tok->location, "");
357+
Directive directive(mTokens, tok->location, "");
358358
for (const simplecpp::Token *tok2 = tok; tok2 && tok2->location.line == directive.linenr; tok2 = tok2->next) {
359359
if (tok2->comment)
360360
continue;
@@ -825,7 +825,7 @@ std::string Preprocessor::getcode(const std::string &cfg, std::vector<std::strin
825825
std::ostringstream ret;
826826
for (const simplecpp::Token *tok = tokens2.cfront(); tok; tok = tok->next) {
827827
if (writeLocations && tok->location.fileIndex != prevfile) {
828-
ret << "\n#line " << tok->location.line << " \"" << tok->location.file() << "\"\n";
828+
ret << "\n#line " << tok->location.line << " \"" << mTokens.file(tok->location) << "\"\n";
829829
prevfile = tok->location.fileIndex;
830830
line = tok->location.line;
831831
}
@@ -853,7 +853,7 @@ const simplecpp::Output* Preprocessor::reportOutput(const simplecpp::OutputList
853853
case simplecpp::Output::ERROR:
854854
out_ret = &out;
855855
if (!startsWith(out.msg,"#error") || showerror)
856-
error(out.location.file(), out.location.line, out.location.col, out.msg, out.type);
856+
error(mTokens.file(out.location), out.location.line, out.location.col, out.msg, out.type);
857857
break;
858858
case simplecpp::Output::WARNING:
859859
case simplecpp::Output::PORTABILITY_BACKSLASH:
@@ -863,14 +863,14 @@ const simplecpp::Output* Preprocessor::reportOutput(const simplecpp::OutputList
863863
const std::string::size_type pos1 = out.msg.find_first_of("<\"");
864864
const std::string::size_type pos2 = out.msg.find_first_of(">\"", pos1 + 1U);
865865
if (pos1 < pos2 && pos2 != std::string::npos)
866-
missingInclude(out.location.file(), out.location.line, out.location.col, out.msg.substr(pos1+1, pos2-pos1-1), out.msg[pos1] == '\"' ? UserHeader : SystemHeader);
866+
missingInclude(mTokens.file(out.location), out.location.line, out.location.col, out.msg.substr(pos1+1, pos2-pos1-1), out.msg[pos1] == '\"' ? UserHeader : SystemHeader);
867867
}
868868
break;
869869
case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY:
870870
case simplecpp::Output::SYNTAX_ERROR:
871871
case simplecpp::Output::UNHANDLED_CHAR_ERROR:
872872
out_ret = &out;
873-
error(out.location.file(), out.location.line, out.location.col, out.msg, out.type);
873+
error(mTokens.file(out.location), out.location.line, out.location.col, out.msg, out.type);
874874
break;
875875
case simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND:
876876
case simplecpp::Output::FILE_NOT_FOUND:
@@ -983,10 +983,10 @@ void Preprocessor::dump(std::ostream &out) const
983983
for (const simplecpp::MacroUsage &macroUsage: mMacroUsage) {
984984
out << " <macro"
985985
<< " name=\"" << macroUsage.macroName << "\""
986-
<< " file=\"" << ErrorLogger::toxml(macroUsage.macroLocation.file()) << "\""
986+
<< " file=\"" << ErrorLogger::toxml(mTokens.file(macroUsage.macroLocation)) << "\""
987987
<< " line=\"" << macroUsage.macroLocation.line << "\""
988988
<< " column=\"" << macroUsage.macroLocation.col << "\""
989-
<< " usefile=\"" << ErrorLogger::toxml(macroUsage.useLocation.file()) << "\""
989+
<< " usefile=\"" << ErrorLogger::toxml(mTokens.file(macroUsage.useLocation)) << "\""
990990
<< " useline=\"" << macroUsage.useLocation.line << "\""
991991
<< " usecolumn=\"" << macroUsage.useLocation.col << "\""
992992
<< " is-known-value=\"" << bool_to_string(macroUsage.macroValueKnown) << "\""
@@ -999,7 +999,7 @@ void Preprocessor::dump(std::ostream &out) const
999999
out << " <simplecpp-if-cond>" << std::endl;
10001000
for (const simplecpp::IfCond &ifCond: mIfCond) {
10011001
out << " <if-cond"
1002-
<< " file=\"" << ErrorLogger::toxml(ifCond.location.file()) << "\""
1002+
<< " file=\"" << ErrorLogger::toxml(mTokens.file(ifCond.location)) << "\""
10031003
<< " line=\"" << ifCond.location.line << "\""
10041004
<< " column=\"" << ifCond.location.col << "\""
10051005
<< " E=\"" << ErrorLogger::toxml(ifCond.E) << "\""
@@ -1126,7 +1126,7 @@ void Preprocessor::addRemarkComments(const simplecpp::TokenList &tokens, std::ve
11261126
continue;
11271127

11281128
// Relative filename
1129-
const std::string relativeFilename = getRelativeFilename(remarkedToken, mSettings);
1129+
const std::string relativeFilename = getRelativeFilename(tokens, remarkedToken, mSettings);
11301130

11311131
// Add the suppressions.
11321132
remarkComments.emplace_back(relativeFilename, remarkedToken->location.line, remarkText);

lib/preprocessor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ struct CPPCHECKLIB Directive {
6868
std::vector<DirectiveToken> strTokens;
6969

7070
/** record a directive (possibly filtering src) */
71-
Directive(const simplecpp::Location & _loc, std::string _str);
71+
Directive(const simplecpp::TokenList &tokens, const simplecpp::Location & _loc, std::string _str);
7272
};
7373

7474
class CPPCHECKLIB RemarkComment {

test/testunusedvar.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,9 +1633,10 @@ class TestUnusedVar : public TestFixture {
16331633
void structmember15() { // #3088
16341634
std::list<Directive> directives;
16351635
std::vector<std::string> f = { "test.cpp" };
1636-
simplecpp::Location loc(f);
1636+
simplecpp::TokenList tokenList(f);
1637+
simplecpp::Location loc;
16371638
loc.line = 1;
1638-
directives.emplace_back(loc, "#pragma pack(1)");
1639+
directives.emplace_back(tokenList, loc, "#pragma pack(1)");
16391640
checkStructMemberUsage("\nstruct Foo { int x; int y; };", dinit(CheckStructMemberUsageOptions, $.directives = &directives));
16401641
ASSERT_EQUALS("", errout_str());
16411642
}

0 commit comments

Comments
 (0)