FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cppcheck/cli/cppcheckexecutor.cpp at master · mathieu/cppcheck · GitHub
mathieu
/
cppcheck
Public
forked from
cppcheck-opensource/cppcheck
Notifications
You must be signed in to change notification settings
Fork
1
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cppcheck
/
cli
/
cppcheckexecutor.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
286 lines (251 loc) · 8.83 KB
Breadcrumbs
cppcheck
/
cli
/
cppcheckexecutor.cpp
Copy path
File metadata and controls
286 lines (251 loc) · 8.83 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2011 Daniel Marjamäki and Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#
include
"
cppcheckexecutor.h
"
#
include
"
cppcheck.h
"
#
include
"
threadexecutor.h
"
#
include
"
preprocessor.h
"
#
include
"
errorlogger.h
"
#
include
<
fstream
>
#
include
<
iostream
>
#
include
<
cstdlib
>
//
EXIT_SUCCESS and EXIT_FAILURE
#
include
"
cmdlineparser.h
"
#
include
"
filelister.h
"
#
include
"
path.h
"
#
include
"
pathmatch.h
"
CppCheckExecutor::CppCheckExecutor
()
{
time1 =
0
;
errorlist =
false
;
}
CppCheckExecutor::~CppCheckExecutor
()
{
}
bool
CppCheckExecutor::parseFromArgs
(CppCheck *cppcheck,
int
argc,
const
char
*
const
argv[])
{
CmdLineParser
parser
(&_settings);
bool
success = parser.
ParseFromArgs
(argc, argv);
cppcheck->
settings
(_settings);
//
copy the settings
if
(success)
{
if
(parser.
GetShowVersion
() && !parser.
GetShowErrorMessages
())
{
std::cout <<
"
Cppcheck
"
<< cppcheck->
version
() << std::endl;
}
if
(parser.
GetShowErrorMessages
())
{
errorlist =
true
;
std::cout <<
ErrorLogger::ErrorMessage::getXMLHeader
(_settings.
_xml_version
);
cppcheck->
getErrorMessages
();
std::cout <<
ErrorLogger::ErrorMessage::getXMLFooter
(_settings.
_xml_version
) << std::endl;
}
if
(parser.
ExitAfterPrinting
())
std::exit
(
0
);
}
//
Check that all include paths exist
{
std::list<std::string>::iterator iter;
for
(iter = _settings.
_includePaths
.
begin
();
iter != _settings.
_includePaths
.
end
();
)
{
const
std::string
path
(
Path::toNativeSeparators
(*iter));
if
(
FileLister::isDirectory
(path))
++iter;
else
{
//
If the include path is not found, warn user and remove the
//
non-existing path from the list.
std::cout <<
"
cppcheck: warning: Couldn't find path given by -I '
"
+ path +
"
'
"
<< std::endl;
iter = _settings.
_includePaths
.
erase
(iter);
}
}
}
std::vector<std::string> pathnames = parser.
GetPathNames
();
std::vector<std::string> filenames;
std::map<std::string,
long
> filesizes;
if
(!pathnames.
empty
())
{
//
Execute recursiveAddFiles() to each given file parameter
std::vector<std::string>::const_iterator iter;
for
(iter = pathnames.
begin
(); iter != pathnames.
end
(); ++iter)
FileLister::recursiveAddFiles
(filenames, filesizes,
Path::toNativeSeparators
(*iter));
}
if
(!filenames.
empty
())
{
PathMatch
matcher
(parser.
GetIgnoredPaths
());
std::vector<std::string>::iterator iterBegin = filenames.
begin
();
for
(
int
i = (
int
)filenames.
size
() -
1
; i >=
0
; i--)
{
if
(matcher.
Match
(filenames[(
unsigned
int
)i]))
filenames.
erase
(iterBegin + i);
}
}
else
{
std::cout <<
"
cppcheck: error: could not find or open any of the paths given.
"
<< std::endl;
return
false
;
}
if
(!filenames.
empty
())
{
std::vector<std::string>::iterator iter;
for
(iter = filenames.
begin
(); iter != filenames.
end
(); ++iter)
{
_filenames.
push_back
(*iter);
_filesizes[*iter] = filesizes[*iter];
}
return
true
;
}
else
{
std::cout <<
"
cppcheck: error: no files to check - all paths ignored.
"
<< std::endl;
return
false
;
}
}
int
CppCheckExecutor::check
(
int
argc,
const
char
*
const
argv[])
{
Preprocessor::missingIncludeFlag =
false
;
CppCheck
cppCheck
(*
this
,
true
);
if
(!
parseFromArgs
(&cppCheck, argc, argv))
{
return
EXIT_FAILURE
;
}
if
(cppCheck.
settings
().
reportProgress
)
time1 =
std::time
(
0
);
_settings = cppCheck.
settings
();
if
(_settings.
_xml
)
{
reportErr
(
ErrorLogger::ErrorMessage::getXMLHeader
(_settings.
_xml_version
));
}
unsigned
int
returnValue =
0
;
if
(_settings.
_jobs
==
1
)
{
//
Single process
long
totalfilesize =
0
;
for
(std::map<std::string,
long
>::const_iterator i = _filesizes.
begin
(); i != _filesizes.
end
(); ++i)
{
totalfilesize += i->
second
;
}
long
processedsize =
0
;
for
(
unsigned
int
c =
0
; c < _filenames.
size
(); c++)
{
returnValue += cppCheck.
check
(_filenames[c]);
if
(_filesizes.
find
(_filenames[c]) != _filesizes.
end
())
{
processedsize += _filesizes[_filenames[c]];
}
if
(!_settings.
_errorsOnly
)
reportStatus
(c +
1
, _filenames.
size
(), processedsize, totalfilesize);
}
}
else
if
(!
ThreadExecutor::isEnabled
())
{
std::cout <<
"
No thread support yet implemented for this platform.
"
<< std::endl;
}
else
{
//
Multiple processes
Settings &settings = cppCheck.
settings
();
ThreadExecutor
executor
(_filenames, _filesizes, settings, *
this
);
returnValue = executor.
check
();
}
if
(!cppCheck.
settings
().
checkConfiguration
)
{
reportUnmatchedSuppressions
(cppCheck.
settings
().
nomsg
.
getUnmatchedGlobalSuppressions
());
if
(Preprocessor::missingIncludeFlag)
{
const
std::list<ErrorLogger::ErrorMessage::FileLocation> callStack;
ErrorLogger::ErrorMessage
msg
(callStack,
Severity::information,
"
Cppcheck cannot find all the include files (use --check-config for details)
\n
"
"
Cppcheck cannot find all the include files. Cpppcheck can check the code without the
"
"
include files found. But the results will probably be more accurate if all the include
"
"
files are found. Please check your project's include directories and add all of them
"
"
as include directories for Cppcheck. To see what files Cppcheck cannot find use
"
"
--check-config.
"
,
"
missingInclude
"
,
false
);
reportErr
(msg);
}
}
if
(_settings.
_xml
)
{
reportErr
(
ErrorLogger::ErrorMessage::getXMLFooter
(_settings.
_xml_version
));
}
if
(returnValue)
return
_settings.
_exitCode
;
else
return
0
;
}
void
CppCheckExecutor::reportErr
(
const
std::string &errmsg)
{
std::cerr << errmsg << std::endl;
}
void
CppCheckExecutor::reportOut
(
const
std::string &outmsg)
{
std::cout << outmsg << std::endl;
}
void
CppCheckExecutor::reportProgress
(
const
std::string &filename,
const
char
stage[],
const
unsigned
int
value)
{
(
void
)filename;
if
(!time1)
return
;
//
Report progress messages every 10 seconds
const
std::
time_t
time2 =
std::time
(
NULL
);
if
(time2 >= (time1 +
10
))
{
time1 = time2;
//
current time in the format "Www Mmm dd hh:mm:ss yyyy"
const
std::string
str
(
std::ctime
(&time2));
//
format a progress message
std::ostringstream ostr;
ostr <<
"
progress:
"
<< stage
<<
"
"
<<
int
(value) <<
"
%
"
;
if
(_settings.
_verbose
)
ostr <<
"
time=
"
<< str.
substr
(
11
,
8
);
//
Report progress message
reportOut
(ostr.
str
());
}
}
void
CppCheckExecutor::reportStatus
(
unsigned
int
fileindex,
unsigned
int
filecount,
long
sizedone,
long
sizetotal)
{
if
(filecount >
1
)
{
std::ostringstream oss;
oss << fileindex <<
"
/
"
<< filecount
<<
"
files checked
"
<<
(sizetotal >
0
?
static_cast
<
long
>(
static_cast
<
double
>(sizedone) / sizetotal*
100
) :
0
)
<<
"
% done
"
;
std::cout << oss.
str
() << std::endl;
}
}
void
CppCheckExecutor::reportErr
(
const
ErrorLogger::ErrorMessage &msg)
{
if
(errorlist)
{
reportOut
(msg.
toXML
(
false
, _settings.
_xml_version
));
}
else
if
(_settings.
_xml
)
{
reportErr
(msg.
toXML
(_settings.
_verbose
, _settings.
_xml_version
));
}
else
{
reportErr
(msg.
toString
(_settings.
_verbose
, _settings.
_outputFormat
));
}
}
Back
|
FazBrowse Home
|
New Git URL