Skip to content

Commit fd7bd50

Browse files
authored
cleaned up ErrorMessage::FileLocation constructors (#6183)
1 parent 51f8b53 commit fd7bd50

File tree

9 files changed

+40
-44
lines changed

9 files changed

+40
-44
lines changed

lib/checkunusedfunctions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ void CheckUnusedFunctions::unusedFunctionError(ErrorLogger& errorLogger,
355355
{
356356
std::list<ErrorMessage::FileLocation> locationList;
357357
if (!filename.empty()) {
358-
locationList.emplace_back(filename, lineNumber);
358+
locationList.emplace_back(filename, lineNumber, 0);
359359
locationList.back().fileIndex = fileIndex;
360360
}
361361

lib/cppcheck.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,9 @@ static bool reportClangErrors(std::istream &is, const std::function<void(const E
404404
const std::string msg = line.substr(line.find(':', pos3+1) + 2);
405405

406406
const std::string locFile = Path::toNativeSeparators(filename);
407-
ErrorMessage::FileLocation loc;
408-
loc.setfile(locFile);
409-
loc.line = strToInt<int>(linenr);
410-
loc.column = strToInt<unsigned int>(colnr);
407+
const int line_i = strToInt<int>(linenr);
408+
const int column = strToInt<unsigned int>(colnr);
409+
ErrorMessage::FileLocation loc(locFile, line_i, column);
411410
ErrorMessage errmsg({std::move(loc)},
412411
locFile,
413412
Severity::error,
@@ -1010,8 +1009,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
10101009
msg += '\n' + s;
10111010

10121011
const std::string locFile = Path::toNativeSeparators(filename);
1013-
ErrorMessage::FileLocation loc;
1014-
loc.setfile(locFile);
1012+
ErrorMessage::FileLocation loc(locFile, 0, 0);
10151013
ErrorMessage errmsg({std::move(loc)},
10161014
locFile,
10171015
Severity::information,
@@ -1113,8 +1111,7 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer)
11131111

11141112
if (maxTime > 0 && std::time(nullptr) > maxTime) {
11151113
if (mSettings.debugwarnings) {
1116-
ErrorMessage::FileLocation loc;
1117-
loc.setfile(tokenizer.list.getFiles()[0]);
1114+
ErrorMessage::FileLocation loc(tokenizer.list.getFiles()[0], 0, 0);
11181115
ErrorMessage errmsg({std::move(loc)},
11191116
emptyString,
11201117
Severity::debug,
@@ -1406,20 +1403,21 @@ void CppCheck::executeRules(const std::string &tokenlist, const Tokenizer &token
14061403
pos = (int)pos2;
14071404

14081405
// determine location..
1409-
ErrorMessage::FileLocation loc;
1410-
loc.setfile(tokenizer.list.getSourceFilePath());
1411-
loc.line = 0;
1406+
std::string file = tokenizer.list.getSourceFilePath();
1407+
int line = 0;
14121408

14131409
std::size_t len = 0;
14141410
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) {
14151411
len = len + 1U + tok->str().size();
14161412
if (len > pos1) {
1417-
loc.setfile(tokenizer.list.getFiles().at(tok->fileIndex()));
1418-
loc.line = tok->linenr();
1413+
file = tokenizer.list.getFiles().at(tok->fileIndex());
1414+
line = tok->linenr();
14191415
break;
14201416
}
14211417
}
14221418

1419+
ErrorMessage::FileLocation loc(file, line, 0);
1420+
14231421
const std::list<ErrorMessage::FileLocation> callStack(1, loc);
14241422

14251423
// Create error message
@@ -1570,7 +1568,7 @@ void CppCheck::tooManyConfigsError(const std::string &file, const int numberOfCo
15701568

15711569
std::list<ErrorMessage::FileLocation> loclist;
15721570
if (!file.empty()) {
1573-
loclist.emplace_back(file);
1571+
loclist.emplace_back(file, 0, 0);
15741572
}
15751573

15761574
std::ostringstream msg;
@@ -1606,7 +1604,7 @@ void CppCheck::purgedConfigurationMessage(const std::string &file, const std::st
16061604

16071605
std::list<ErrorMessage::FileLocation> loclist;
16081606
if (!file.empty()) {
1609-
loclist.emplace_back(file);
1607+
loclist.emplace_back(file, 0, 0);
16101608
}
16111609

16121610
ErrorMessage errmsg(std::move(loclist),

lib/ctu.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,12 @@ bool CTU::FileInfo::FunctionCall::loadFromXml(const tinyxml2::XMLElement *xmlEle
209209
for (const tinyxml2::XMLElement *e2 = xmlElement->FirstChildElement(); !error && e2; e2 = e2->NextSiblingElement()) {
210210
if (std::strcmp(e2->Name(), "path") != 0)
211211
continue;
212-
ErrorMessage::FileLocation loc;
213-
loc.setfile(readAttrString(e2, ATTR_LOC_FILENAME, &error));
214-
loc.line = readAttrInt(e2, ATTR_LOC_LINENR, &error);
215-
loc.column = readAttrInt(e2, ATTR_LOC_COLUMN, &error);
212+
std::string file = readAttrString(e2, ATTR_LOC_FILENAME, &error);
213+
const int line = readAttrInt(e2, ATTR_LOC_LINENR, &error);
214+
const int column = readAttrInt(e2, ATTR_LOC_COLUMN, &error);
215+
ErrorMessage::FileLocation loc(file, line, column);
216216
loc.setinfo(readAttrString(e2, ATTR_INFO, &error));
217+
// TODO: loc is unused
217218
}
218219
return !error;
219220
}
@@ -345,10 +346,10 @@ CTU::FileInfo *CTU::getFileInfo(const Tokenizer *tokenizer)
345346
functionCall.callArgValue = value.intvalue;
346347
functionCall.warning = !value.errorSeverity();
347348
for (const ErrorPathItem &i : value.errorPath) {
348-
ErrorMessage::FileLocation loc;
349-
loc.setfile(tokenizer->list.file(i.first));
350-
loc.line = i.first->linenr();
351-
loc.column = i.first->column();
349+
const std::string& file = tokenizer->list.file(i.first);
350+
const int line = i.first->linenr();
351+
const int column = i.first->column();
352+
ErrorMessage::FileLocation loc(file, line, column);
352353
loc.setinfo(i.second);
353354
functionCall.callValuePath.push_back(std::move(loc));
354355
}

lib/errorlogger.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,9 @@ class CPPCHECKLIB ErrorMessage {
5252
* Internally paths are stored with / separator. When getting the filename
5353
* it is by default converted to native separators.
5454
*/
55-
class CPPCHECKLIB FileLocation {
55+
class CPPCHECKLIB WARN_UNUSED FileLocation {
5656
public:
57-
FileLocation()
58-
: fileIndex(0), line(0), column(0) {}
59-
60-
explicit FileLocation(const std::string &file, int line = 0, unsigned int column = 0)
57+
FileLocation(const std::string &file, int line, unsigned int column)
6158
: fileIndex(0), line(line), column(column), mOrigFileName(file), mFileName(file) {}
6259

6360
FileLocation(const std::string &file, std::string info, int line, unsigned int column)

lib/preprocessor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ void Preprocessor::missingInclude(const std::string &filename, unsigned int line
926926

927927
std::list<ErrorMessage::FileLocation> locationList;
928928
if (!filename.empty()) {
929-
locationList.emplace_back(filename, linenr);
929+
locationList.emplace_back(filename, linenr, 0);
930930
}
931931
ErrorMessage errmsg(std::move(locationList), mFile0, Severity::information,
932932
(headerType==SystemHeader) ?

lib/templatesimplifier.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3163,8 +3163,7 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
31633163

31643164
if (maxtime > 0 && std::time(nullptr) > maxtime) {
31653165
if (mSettings.debugwarnings) {
3166-
ErrorMessage::FileLocation loc;
3167-
loc.setfile(mTokenList.getFiles()[0]);
3166+
ErrorMessage::FileLocation loc(mTokenList.getFiles()[0], 0, 0);
31683167
ErrorMessage errmsg({std::move(loc)},
31693168
emptyString,
31703169
Severity::debug,
@@ -3234,8 +3233,7 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
32343233

32353234
if (maxtime > 0 && std::time(nullptr) > maxtime) {
32363235
if (mSettings.debugwarnings) {
3237-
ErrorMessage::FileLocation loc;
3238-
loc.setfile(mTokenList.getFiles()[0]);
3236+
ErrorMessage::FileLocation loc(mTokenList.getFiles()[0], 0, 0);
32393237
ErrorMessage errmsg({std::move(loc)},
32403238
emptyString,
32413239
Severity::debug,

lib/tokenize.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,8 +1150,7 @@ void Tokenizer::simplifyTypedefCpp()
11501150

11511151
if (maxTime > 0 && std::time(nullptr) > maxTime) {
11521152
if (mSettings.debugwarnings) {
1153-
ErrorMessage::FileLocation loc;
1154-
loc.setfile(list.getFiles()[0]);
1153+
ErrorMessage::FileLocation loc(list.getFiles()[0], 0, 0);
11551154
ErrorMessage errmsg({std::move(loc)},
11561155
emptyString,
11571156
Severity::debug,

lib/valueflow.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9404,8 +9404,7 @@ struct ValueFlowPassRunner {
94049404
}
94059405
if (state.settings.debugwarnings) {
94069406
if (n == 0 && values != getTotalValues()) {
9407-
ErrorMessage::FileLocation loc;
9408-
loc.setfile(state.tokenlist.getFiles()[0]);
9407+
ErrorMessage::FileLocation loc(state.tokenlist.getFiles()[0], 0, 0);
94099408
ErrorMessage errmsg({std::move(loc)},
94109409
emptyString,
94119410
Severity::debug,

test/testerrorlogger.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class TestErrorLogger : public TestFixture {
3838

3939
void run() override {
4040
TEST_CASE(PatternSearchReplace);
41-
TEST_CASE(FileLocationDefaults);
41+
TEST_CASE(FileLocationConstruct);
4242
TEST_CASE(FileLocationSetFile);
4343
TEST_CASE(ErrorMessageConstruct);
4444
TEST_CASE(ErrorMessageConstructLocations);
@@ -101,17 +101,21 @@ class TestErrorLogger : public TestFixture {
101101
TestPatternSearchReplace(idPlaceholder, longIdValue);
102102
}
103103

104-
void FileLocationDefaults() const {
105-
ErrorMessage::FileLocation loc;
106-
ASSERT_EQUALS("", loc.getfile());
107-
ASSERT_EQUALS(0, loc.line);
104+
void FileLocationConstruct() const {
105+
ErrorMessage::FileLocation loc("foo.cpp", 1, 2);
106+
ASSERT_EQUALS("foo.cpp", loc.getOrigFile());
107+
ASSERT_EQUALS("foo.cpp", loc.getfile());
108+
ASSERT_EQUALS(1, loc.line);
109+
ASSERT_EQUALS(2, loc.column);
108110
}
109111

110112
void FileLocationSetFile() const {
111-
ErrorMessage::FileLocation loc;
113+
ErrorMessage::FileLocation loc("foo1.cpp", 0, 0);
112114
loc.setfile("foo.cpp");
115+
ASSERT_EQUALS("foo1.cpp", loc.getOrigFile());
113116
ASSERT_EQUALS("foo.cpp", loc.getfile());
114117
ASSERT_EQUALS(0, loc.line);
118+
ASSERT_EQUALS(0, loc.column);
115119
}
116120

117121
void ErrorMessageConstruct() const {

0 commit comments

Comments
 (0)