Skip to content

Commit 8b21aff

Browse files
committed
adjusted lookup of file from simplecpp::Location [skip ci]]
1 parent 47f1c11 commit 8b21aff

File tree

4 files changed

+27
-26
lines changed

4 files changed

+27
-26
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: 21 additions & 21 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

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

@@ -126,7 +126,7 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std:
126126
else if ("macro" == suppressTypeString)
127127
errorType = SuppressionList::Type::macro;
128128
else {
129-
bad.emplace_back(tok->location.file(), tok->location.line, 0, "unknown suppression type '" + suppressCmdString + "'"); // TODO: set column
129+
bad.emplace_back(tokens.file(tok->location), tok->location.line, 0, "unknown suppression type '" + suppressCmdString + "'"); // TODO: set column
130130
return false;
131131
}
132132
}
@@ -143,7 +143,7 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std:
143143
}
144144

145145
if (!errmsg.empty())
146-
bad.emplace_back(tok->location.file(), tok->location.line, tok->location.col, std::move(errmsg));
146+
bad.emplace_back(tokens.file(tok->location), tok->location.line, tok->location.col, std::move(errmsg));
147147

148148
std::copy_if(suppressions.cbegin(), suppressions.cend(), std::back_inserter(inlineSuppressions), [](const SuppressionList::Suppression& s) {
149149
return !s.errorId.empty();
@@ -163,16 +163,16 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std:
163163
inlineSuppressions.push_back(std::move(s));
164164

165165
if (!errmsg.empty())
166-
bad.emplace_back(tok->location.file(), tok->location.line, tok->location.col, std::move(errmsg));
166+
bad.emplace_back(tokens.file(tok->location), tok->location.line, tok->location.col, std::move(errmsg));
167167
}
168168

169169
return true;
170170
}
171171

172-
static std::string getRelativeFilename(const simplecpp::Token* tok, const Settings &settings) {
172+
static std::string getRelativeFilename(const simplecpp::TokenList &tokens, const simplecpp::Token* tok, const Settings &settings) {
173173
if (!tok)
174174
return "";
175-
std::string relativeFilename(tok->location.file());
175+
std::string relativeFilename(tokens.file(tok->location));
176176
if (settings.relativePaths) {
177177
for (const std::string & basePath : settings.basePaths) {
178178
const std::string bp = basePath + "/";
@@ -197,7 +197,7 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett
197197
}
198198

199199
std::list<SuppressionList::Suppression> inlineSuppressions;
200-
if (!parseInlineSuppressionCommentToken(tok, inlineSuppressions, bad))
200+
if (!parseInlineSuppressionCommentToken(tokens, tok, inlineSuppressions, bad))
201201
continue;
202202

203203
if (!sameline(tok->previous, tok)) {
@@ -206,7 +206,7 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett
206206
tok = tok->next;
207207

208208
while (tok->comment) {
209-
parseInlineSuppressionCommentToken(tok, inlineSuppressions, bad);
209+
parseInlineSuppressionCommentToken(tokens, tok, inlineSuppressions, bad);
210210
if (tok->next) {
211211
tok = tok->next;
212212
} else {
@@ -224,7 +224,7 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett
224224
continue;
225225

226226
// Relative filename
227-
const std::string relativeFilename = getRelativeFilename(tok, settings);
227+
const std::string relativeFilename = getRelativeFilename(tokens, tok, settings);
228228

229229
// Macro name
230230
std::string macroName;
@@ -345,7 +345,7 @@ std::list<Directive> Preprocessor::createDirectives() const
345345
continue;
346346
if (tok->next && tok->next->str() == "endfile")
347347
continue;
348-
Directive directive(tok->location, "");
348+
Directive directive(mTokens, tok->location, "");
349349
for (const simplecpp::Token *tok2 = tok; tok2 && tok2->location.line == directive.linenr; tok2 = tok2->next) {
350350
if (tok2->comment)
351351
continue;
@@ -816,7 +816,7 @@ std::string Preprocessor::getcode(const std::string &cfg, std::vector<std::strin
816816
std::ostringstream ret;
817817
for (const simplecpp::Token *tok = tokens2.cfront(); tok; tok = tok->next) {
818818
if (writeLocations && tok->location.fileIndex != prevfile) {
819-
ret << "\n#line " << tok->location.line << " \"" << tok->location.file() << "\"\n";
819+
ret << "\n#line " << tok->location.line << " \"" << mTokens.file(tok->location) << "\"\n";
820820
prevfile = tok->location.fileIndex;
821821
line = tok->location.line;
822822
}
@@ -844,7 +844,7 @@ const simplecpp::Output* Preprocessor::reportOutput(const simplecpp::OutputList
844844
case simplecpp::Output::ERROR:
845845
out_ret = &out;
846846
if (!startsWith(out.msg,"#error") || showerror)
847-
error(out.location.file(), out.location.line, out.location.col, out.msg, out.type);
847+
error(mTokens.file(out.location), out.location.line, out.location.col, out.msg, out.type);
848848
break;
849849
case simplecpp::Output::WARNING:
850850
case simplecpp::Output::PORTABILITY_BACKSLASH:
@@ -854,14 +854,14 @@ const simplecpp::Output* Preprocessor::reportOutput(const simplecpp::OutputList
854854
const std::string::size_type pos1 = out.msg.find_first_of("<\"");
855855
const std::string::size_type pos2 = out.msg.find_first_of(">\"", pos1 + 1U);
856856
if (pos1 < pos2 && pos2 != std::string::npos)
857-
missingInclude(out.location.file(), out.location.line, out.location.col, out.msg.substr(pos1+1, pos2-pos1-1), out.msg[pos1] == '\"' ? UserHeader : SystemHeader);
857+
missingInclude(mTokens.file(out.location), out.location.line, out.location.col, out.msg.substr(pos1+1, pos2-pos1-1), out.msg[pos1] == '\"' ? UserHeader : SystemHeader);
858858
}
859859
break;
860860
case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY:
861861
case simplecpp::Output::SYNTAX_ERROR:
862862
case simplecpp::Output::UNHANDLED_CHAR_ERROR:
863863
out_ret = &out;
864-
error(out.location.file(), out.location.line, out.location.col, out.msg, out.type);
864+
error(mTokens.file(out.location), out.location.line, out.location.col, out.msg, out.type);
865865
break;
866866
case simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND:
867867
case simplecpp::Output::FILE_NOT_FOUND:
@@ -974,10 +974,10 @@ void Preprocessor::dump(std::ostream &out) const
974974
for (const simplecpp::MacroUsage &macroUsage: mMacroUsage) {
975975
out << " <macro"
976976
<< " name=\"" << macroUsage.macroName << "\""
977-
<< " file=\"" << ErrorLogger::toxml(macroUsage.macroLocation.file()) << "\""
977+
<< " file=\"" << ErrorLogger::toxml(mTokens.file(macroUsage.macroLocation)) << "\""
978978
<< " line=\"" << macroUsage.macroLocation.line << "\""
979979
<< " column=\"" << macroUsage.macroLocation.col << "\""
980-
<< " usefile=\"" << ErrorLogger::toxml(macroUsage.useLocation.file()) << "\""
980+
<< " usefile=\"" << ErrorLogger::toxml(mTokens.file(macroUsage.useLocation)) << "\""
981981
<< " useline=\"" << macroUsage.useLocation.line << "\""
982982
<< " usecolumn=\"" << macroUsage.useLocation.col << "\""
983983
<< " is-known-value=\"" << bool_to_string(macroUsage.macroValueKnown) << "\""
@@ -990,7 +990,7 @@ void Preprocessor::dump(std::ostream &out) const
990990
out << " <simplecpp-if-cond>" << std::endl;
991991
for (const simplecpp::IfCond &ifCond: mIfCond) {
992992
out << " <if-cond"
993-
<< " file=\"" << ErrorLogger::toxml(ifCond.location.file()) << "\""
993+
<< " file=\"" << ErrorLogger::toxml(mTokens.file(ifCond.location)) << "\""
994994
<< " line=\"" << ifCond.location.line << "\""
995995
<< " column=\"" << ifCond.location.col << "\""
996996
<< " E=\"" << ErrorLogger::toxml(ifCond.E) << "\""
@@ -1117,7 +1117,7 @@ void Preprocessor::addRemarkComments(const simplecpp::TokenList &tokens, std::ve
11171117
continue;
11181118

11191119
// Relative filename
1120-
const std::string relativeFilename = getRelativeFilename(remarkedToken, mSettings);
1120+
const std::string relativeFilename = getRelativeFilename(tokens, remarkedToken, mSettings);
11211121

11221122
// Add the suppressions.
11231123
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)