Skip to content

Commit a5ba82c

Browse files
committed
Fixed #9260 (--template=gcc does not work correctly with -j)
1 parent e07cb80 commit a5ba82c

4 files changed

Lines changed: 40 additions & 39 deletions

File tree

lib/cppcheck.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -559,19 +559,15 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
559559

560560
} catch (const InternalError &e) {
561561
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
562-
ErrorLogger::ErrorMessage::FileLocation loc;
563562
if (e.token) {
564-
loc.line = e.token->linenr();
565-
loc.column = e.token->column();
566-
const std::string fixedpath = Path::toNativeSeparators(mTokenizer.list.file(e.token));
567-
loc.setfile(fixedpath);
563+
ErrorLogger::ErrorMessage::FileLocation loc(e.token, &mTokenizer.list);
564+
locationList.push_back(loc);
568565
} else {
569-
ErrorLogger::ErrorMessage::FileLocation loc2;
570-
loc2.setfile(Path::toNativeSeparators(filename));
566+
ErrorLogger::ErrorMessage::FileLocation loc(mTokenizer.list.getSourceFilePath(), 0, 0);
567+
ErrorLogger::ErrorMessage::FileLocation loc2(filename, 0, 0);
571568
locationList.push_back(loc2);
572-
loc.setfile(mTokenizer.list.getSourceFilePath());
569+
locationList.push_back(loc);
573570
}
574-
locationList.push_back(loc);
575571
ErrorLogger::ErrorMessage errmsg(locationList,
576572
mTokenizer.list.getSourceFilePath(),
577573
Severity::error,

lib/errorlogger.cpp

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ std::string ErrorLogger::ErrorMessage::serialize() const
247247

248248
for (std::list<ErrorLogger::ErrorMessage::FileLocation>::const_iterator loc = callStack.begin(); loc != callStack.end(); ++loc) {
249249
std::ostringstream smallStream;
250-
smallStream << (*loc).line << ':' << (*loc).column << ':' << (*loc).getfile() << '\t' << loc->getinfo();
250+
smallStream << (*loc).line << '\t' << (*loc).column << '\t' << (*loc).getfile(false) << '\t' << loc->getOrigFile(false) << '\t' << loc->getinfo();
251251
oss << smallStream.str().length() << " " << smallStream.str();
252252
}
253253

@@ -309,28 +309,29 @@ bool ErrorLogger::ErrorMessage::deserialize(const std::string &data)
309309
temp.append(1, c);
310310
}
311311

312-
const std::string::size_type colonPos1 = temp.find(':');
313-
if (colonPos1 == std::string::npos)
314-
throw InternalError(nullptr, "Internal Error: No colon found in <line:col:filename> pattern");
315-
const std::string::size_type colonPos2 = temp.find(':', colonPos1+1);
316-
if (colonPos2 == std::string::npos)
317-
throw InternalError(nullptr, "Internal Error: second colon not found in <line:col:filename> pattern");
318-
const std::string::size_type tabPos = temp.find('\t');
319-
if (tabPos == std::string::npos)
320-
throw InternalError(nullptr, "Internal Error: No tab found in <filename:line> pattern");
321-
322-
const std::string tempinfo = temp.substr(tabPos + 1);
323-
temp.erase(tabPos);
324-
const std::string tempfile = temp.substr(colonPos2 + 1);
325-
temp.erase(colonPos2);
326-
const std::string tempcolumn = temp.substr(colonPos1 + 1);
327-
temp.erase(colonPos1);
328-
const std::string templine = temp;
329-
ErrorLogger::ErrorMessage::FileLocation loc;
330-
loc.setfile(tempfile);
331-
loc.setinfo(tempinfo);
332-
loc.column = MathLib::toLongNumber(tempcolumn);
333-
loc.line = MathLib::toLongNumber(templine);
312+
std::vector<std::string> substrings;
313+
for (std::string::size_type pos = 0; pos < temp.size() && substrings.size() < 5; ++pos) {
314+
if (substrings.size() == 4) {
315+
substrings.push_back(temp.substr(pos));
316+
break;
317+
}
318+
const std::string::size_type start = pos;
319+
pos = temp.find("\t", pos);
320+
if (pos == std::string::npos) {
321+
substrings.push_back(temp.substr(start));
322+
break;
323+
}
324+
substrings.push_back(temp.substr(start, pos - start));
325+
}
326+
if (substrings.size() < 4)
327+
throw InternalError(nullptr, "Internal Error: serializing/deserializing of error message failed!");
328+
329+
// (*loc).line << '\t' << (*loc).column << '\t' << (*loc).getfile(false) << '\t' << loc->getOrigFile(false) << '\t' << loc->getinfo();
330+
331+
ErrorLogger::ErrorMessage::FileLocation loc(substrings[3], MathLib::toLongNumber(substrings[0]), MathLib::toLongNumber(substrings[1]));
332+
loc.setfile(substrings[2]);
333+
if (substrings.size() == 5)
334+
loc.setinfo(substrings[4]);
334335

335336
callStack.push_back(loc);
336337

@@ -441,7 +442,7 @@ void ErrorLogger::ErrorMessage::findAndReplace(std::string &source, const std::s
441442
}
442443

443444
// TODO: read info from some shared resource instead?
444-
static std::string readCode(const std::string &file, unsigned int linenr, unsigned int column, const char endl[])
445+
static std::string readCode(const std::string &file, int linenr, int column, const char endl[])
445446
{
446447
std::ifstream fin(file);
447448
std::string line;

lib/errorlogger.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ class CPPCHECKLIB ErrorLogger {
211211
*/
212212
std::string getfile(bool convert = true) const;
213213

214+
/**
215+
* Filename with the whole path (no --rp)
216+
* @param convert If true convert path to native separators.
217+
* @return filename.
218+
*/
214219
std::string getOrigFile(bool convert = true) const;
215220

216221
/**

test/testerrorlogger.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,8 @@ class TestErrorLogger : public TestFixture {
291291
}
292292

293293
void SerializeFileLocation() const {
294-
ErrorLogger::ErrorMessage::FileLocation loc1;
295-
loc1.setfile(":/,");
296-
loc1.line = 643;
297-
loc1.column = 33;
294+
ErrorLogger::ErrorMessage::FileLocation loc1(":/,;", 654, 33);
295+
loc1.setfile("[]:;,()");
298296
loc1.setinfo("abcd:/,");
299297

300298
std::list<ErrorLogger::ErrorMessage::FileLocation> locs{loc1};
@@ -303,8 +301,9 @@ class TestErrorLogger : public TestFixture {
303301

304302
ErrorMessage msg2;
305303
msg2.deserialize(msg.serialize());
306-
ASSERT_EQUALS(":/,", msg2.callStack.front().getfile(false));
307-
ASSERT_EQUALS(643, msg2.callStack.front().line);
304+
ASSERT_EQUALS("[]:;,()", msg2.callStack.front().getfile(false));
305+
ASSERT_EQUALS(":/,;", msg2.callStack.front().getOrigFile(false));
306+
ASSERT_EQUALS(654, msg2.callStack.front().line);
308307
ASSERT_EQUALS(33, msg2.callStack.front().column);
309308
ASSERT_EQUALS("abcd:/,", msg2.callStack.front().getinfo());
310309
}

0 commit comments

Comments
 (0)