Skip to content

Commit 4fc774d

Browse files
committed
Multicore cpu support for Linux (currently disabled and compiling produces warnings)
"no errors" output removed.
1 parent efeaac7 commit 4fc774d

File tree

12 files changed

+419
-20
lines changed

12 files changed

+419
-20
lines changed

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ OBJECTS = src/checkbufferoverrun.o \
2020
src/main.o \
2121
src/preprocessor.o \
2222
src/settings.o \
23+
src/threadexecutor.o \
2324
src/token.o \
2425
src/tokenize.o
2526

@@ -62,6 +63,7 @@ TESTOBJ = test/testbufferoverrun.o \
6263
src/filelister.o \
6364
src/preprocessor.o \
6465
src/settings.o \
66+
src/threadexecutor.o \
6567
src/token.o \
6668
src/tokenize.o
6769

@@ -127,7 +129,7 @@ src/checkvalidate.o: src/checkvalidate.cpp src/checkvalidate.h src/errorlogger.h
127129
src/cppcheck.o: src/cppcheck.cpp src/cppcheck.h src/settings.h src/errorlogger.h src/checkfunctionusage.h src/tokenize.h src/token.h src/preprocessor.h src/checkmemoryleak.h src/checkbufferoverrun.h src/checkdangerousfunctions.h src/checkclass.h src/checkheaders.h src/checkother.h src/checkstl.h src/filelister.h
128130
g++ $(CXXFLAGS) -c -o src/cppcheck.o src/cppcheck.cpp
129131

130-
src/cppcheckexecutor.o: src/cppcheckexecutor.cpp src/cppcheckexecutor.h src/errorlogger.h src/settings.h src/cppcheck.h src/checkfunctionusage.h src/tokenize.h src/token.h
132+
src/cppcheckexecutor.o: src/cppcheckexecutor.cpp src/cppcheckexecutor.h src/errorlogger.h src/settings.h src/cppcheck.h src/checkfunctionusage.h src/tokenize.h src/token.h src/threadexecutor.h
131133
g++ $(CXXFLAGS) -c -o src/cppcheckexecutor.o src/cppcheckexecutor.cpp
132134

133135
src/errorlogger.o: src/errorlogger.cpp src/errorlogger.h src/settings.h src/tokenize.h src/token.h
@@ -145,6 +147,9 @@ src/preprocessor.o: src/preprocessor.cpp src/preprocessor.h src/tokenize.h src/s
145147
src/settings.o: src/settings.cpp src/settings.h
146148
g++ $(CXXFLAGS) -c -o src/settings.o src/settings.cpp
147149

150+
src/threadexecutor.o: src/threadexecutor.cpp src/threadexecutor.h src/settings.h src/errorlogger.h src/cppcheck.h src/checkfunctionusage.h src/tokenize.h src/token.h
151+
g++ $(CXXFLAGS) -c -o src/threadexecutor.o src/threadexecutor.cpp
152+
148153
src/token.o: src/token.cpp src/token.h
149154
g++ $(CXXFLAGS) -c -o src/token.o src/token.cpp
150155

cppcheck.cbp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
<Unit filename="src/preprocessor.h" />
6060
<Unit filename="src/settings.cpp" />
6161
<Unit filename="src/settings.h" />
62+
<Unit filename="src/threadexecutor.cpp" />
63+
<Unit filename="src/threadexecutor.h" />
6264
<Unit filename="src/token.cpp" />
6365
<Unit filename="src/token.h" />
6466
<Unit filename="src/tokenize.cpp" />

src/cppcheck.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -262,16 +262,7 @@ unsigned int CppCheck::check()
262262
++checkCount;
263263
}
264264

265-
if (_settings._errorsOnly == false && _errout.str().empty())
266-
{
267-
std::ostringstream oss;
268-
oss << "No errors found ("
269-
<< (c + 1) << "/" << _filenames.size()
270-
<< " files checked " <<
271-
static_cast<int>(static_cast<double>((c + 1)) / _filenames.size()*100)
272-
<< "% done)";
273-
_errorLogger->reportOut(oss.str());
274-
}
265+
_errorLogger->reportStatus(c + 1, _filenames.size());
275266
}
276267

277268
// This generates false positives - especially for libraries
@@ -458,3 +449,13 @@ void CppCheck::reportOut(const std::string & /*outmsg*/)
458449
// This is currently never called. It is here just to comply with
459450
// the interface.
460451
}
452+
453+
const std::vector<std::string> &CppCheck::filenames() const
454+
{
455+
return _filenames;
456+
}
457+
458+
void CppCheck::reportStatus(unsigned int /*index*/, unsigned int /*max*/)
459+
{
460+
461+
}

src/cppcheck.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ class CppCheck : public ErrorLogger
100100
*/
101101
std::string parseFromArgs(int argc, const char* const argv[]);
102102

103+
const std::vector<std::string> &filenames() const;
104+
105+
virtual void reportStatus(unsigned int index, unsigned int max);
106+
103107
private:
104108
void checkFile(const std::string &code, const char FileName[]);
105109

src/cppcheckexecutor.cpp

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,18 @@
1919

2020
#include "cppcheckexecutor.h"
2121
#include "cppcheck.h"
22+
#include "threadexecutor.h"
2223
#include <fstream>
2324
#include <iostream>
2425

2526
CppCheckExecutor::CppCheckExecutor()
2627
{
27-
_useXML = false;
28+
2829
}
2930

3031
CppCheckExecutor::~CppCheckExecutor()
3132
{
32-
//dtor
33+
3334
}
3435

3536
unsigned int CppCheckExecutor::check(int argc, const char* const argv[])
@@ -38,15 +39,29 @@ unsigned int CppCheckExecutor::check(int argc, const char* const argv[])
3839
std::string result = cppCheck.parseFromArgs(argc, argv);
3940
if (result.length() == 0)
4041
{
41-
if (cppCheck.settings()._xml)
42+
_settings = cppCheck.settings();
43+
if (_settings._xml)
4244
{
43-
_useXML = true;
4445
reportErr("<?xml version=\"1.0\"?>");
4546
reportErr("<results>");
4647
}
4748

48-
unsigned int returnValue = cppCheck.check();
49-
if (_useXML)
49+
unsigned int returnValue = 0;
50+
if (1)
51+
{
52+
// Single process
53+
returnValue = cppCheck.check();
54+
}
55+
else
56+
{
57+
// Multiple processes
58+
const std::vector<std::string> &filenames = cppCheck.filenames();
59+
Settings settings = cppCheck.settings();
60+
ThreadExecutor executor(filenames, settings, *this);
61+
returnValue = executor.check();
62+
}
63+
64+
if (_settings._xml)
5065
{
5166
reportErr("</results>");
5267
}
@@ -70,9 +85,22 @@ void CppCheckExecutor::reportOut(const std::string &outmsg)
7085
std::cout << outmsg << std::endl;
7186
}
7287

88+
void CppCheckExecutor::reportStatus(unsigned int index, unsigned int max)
89+
{
90+
if (max > 1 && !_settings._errorsOnly)
91+
{
92+
std::ostringstream oss;
93+
oss << index << "/" << max
94+
<< " files checked " <<
95+
static_cast<int>(static_cast<double>(index) / max*100)
96+
<< "% done";
97+
std::cout << oss.str() << std::endl;
98+
}
99+
}
100+
73101
void CppCheckExecutor::reportErr(const ErrorLogger::ErrorMessage &msg)
74102
{
75-
if (_useXML)
103+
if (_settings._xml)
76104
{
77105
reportErr(msg.toXML());
78106
}

src/cppcheckexecutor.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#define CPPCHECKEXECUTOR_H
2222

2323
#include "errorlogger.h"
24+
#include "settings.h"
2425

2526
/**
2627
* This class works as an example of how CppCheck can be used in external
@@ -67,6 +68,8 @@ class CppCheckExecutor : public ErrorLogger
6768
/** xml output of errors */
6869
virtual void reportErr(const ErrorLogger::ErrorMessage &msg);
6970

71+
virtual void reportStatus(unsigned int index, unsigned int max);
72+
7073
private:
7174

7275
/**
@@ -75,7 +78,7 @@ class CppCheckExecutor : public ErrorLogger
7578
*/
7679
void reportErr(const std::string &errmsg);
7780

78-
bool _useXML;
81+
Settings _settings;
7982
};
8083

8184
#endif // CPPCHECKEXECUTOR_H

src/errorlogger.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323

2424
#include <sstream>
2525

26+
ErrorLogger::ErrorMessage::ErrorMessage()
27+
{
28+
29+
}
30+
#include <iostream>
2631
ErrorLogger::ErrorMessage::ErrorMessage(const std::list<FileLocation> &callStack, const std::string &severity, const std::string &msg, const std::string &id)
2732
{
2833
_callStack = callStack;
@@ -31,6 +36,78 @@ ErrorLogger::ErrorMessage::ErrorMessage(const std::list<FileLocation> &callStack
3136
_id = id;
3237
}
3338

39+
std::string ErrorLogger::ErrorMessage::serialize() const
40+
{
41+
std::ostringstream oss;
42+
oss << _id.length() << " " << _id;
43+
oss << _severity.length() << " " << _severity;
44+
oss << _msg.length() << " " << _msg;
45+
oss << _callStack.size() << " ";
46+
47+
for (std::list<ErrorLogger::ErrorMessage::FileLocation>::const_iterator tok = _callStack.begin(); tok != _callStack.end(); ++tok)
48+
{
49+
std::ostringstream smallStream;
50+
smallStream << (*tok).line << ":" << (*tok).file;
51+
oss << smallStream.str().length() << " " << smallStream.str();
52+
}
53+
return oss.str();
54+
}
55+
56+
bool ErrorLogger::ErrorMessage::deserialize(const std::string &data)
57+
{
58+
_callStack.clear();
59+
std::istringstream iss(data);
60+
std::vector<std::string> results;
61+
while (iss.good())
62+
{
63+
unsigned int len = 0;
64+
if (!(iss >> len))
65+
return false;
66+
67+
iss.get();
68+
std::string temp;
69+
for (unsigned int i = 0; i < len && iss.good(); ++i)
70+
temp.append(1, iss.get());
71+
72+
results.push_back(temp);
73+
if (results.size() == 3)
74+
break;
75+
}
76+
77+
_id = results[0];
78+
_severity = results[1];
79+
_msg = results[2];
80+
81+
unsigned int stackSize = 0;
82+
if (!(iss >> stackSize))
83+
return false;
84+
85+
while (iss.good())
86+
{
87+
unsigned int len = 0;
88+
if (!(iss >> len))
89+
return false;
90+
91+
iss.get();
92+
std::string temp;
93+
for (unsigned int i = 0; i < len && iss.good(); ++i)
94+
temp.append(1, iss.get());
95+
96+
ErrorLogger::ErrorMessage::FileLocation loc;
97+
loc.file = temp.substr(temp.find(':') + 1);
98+
temp = temp.substr(0, temp.find(':'));
99+
std::istringstream fiss(temp);
100+
fiss >> loc.line;
101+
102+
_callStack.push_back(loc);
103+
104+
if (_callStack.size() >= stackSize)
105+
break;
106+
}
107+
108+
return true;
109+
}
110+
34111
std::string ErrorLogger::ErrorMessage::toXML() const
35112
{
36113
std::ostringstream xml;

src/errorlogger.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,11 @@ class ErrorLogger
5252
};
5353

5454
ErrorMessage(const std::list<FileLocation> &callStack, const std::string &severity, const std::string &msg, const std::string &id);
55+
ErrorMessage();
5556
std::string toXML() const;
5657
std::string toText() const;
58+
std::string serialize() const;
59+
bool deserialize( const std::string &data );
5760
private:
5861
std::list<FileLocation> _callStack;
5962
std::string _severity;
@@ -81,6 +84,14 @@ class ErrorLogger
8184
*/
8285
virtual void reportErr(const ErrorLogger::ErrorMessage &msg) = 0;
8386

87+
/**
88+
* Information about how many files have been checked
89+
*
90+
* @param index This many files have been checked.
91+
* @param max This many files there are in total.
92+
*/
93+
virtual void reportStatus(unsigned int index, unsigned int max) = 0;
94+
8495
void arrayIndexOutOfBounds(const Tokenizer *tokenizer, const std::list<const Token *> &Location)
8596
{
8697
_writemsg(tokenizer, Location, "all", "Array index out of bounds", "arrayIndexOutOfBounds");

0 commit comments

Comments
 (0)