-
Notifications
You must be signed in to change notification settings - Fork 601
Expand file tree
/
Copy pathtscthreadexecutor.cpp
More file actions
565 lines (472 loc) · 15.1 KB
/
Copy pathtscthreadexecutor.cpp
File metadata and controls
565 lines (472 loc) · 15.1 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
#include "tscthreadexecutor.h"
#include "tscancode.h"
#include "tscexecutor.h"
#include <iostream>
#ifdef __SVR4 // Solaris
#include <sys/loadavg.h>
#endif
#ifdef TSC_THREADING_MODEL_NOT_WIN
#include <pthread.h>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <errno.h>
#include <time.h>
#include <cstring>
#include <sstream>
#endif
#ifdef TSC_THREADING_MODEL_WIN
#include <process.h>
#include <windows.h>
#include <algorithm>
#include <cstring>
#include <errno.h>
#endif
#include "globaltokenizer.h"
#include "preprocessor.h"
#include "path.h"
#include "globalmacros.h"
#include "checktscinvalidvarargs.h"
#include <cstdint>
#define MAXERRORCNT 30
#define AUTOFILTER_SUBID "FuncPossibleRetNULL|FuncRetNULL|dereferenceAfterCheck|dereferenceBeforeCheck|possibleNullDereferenced|nullpointerarg|nullpointerclass"
TscThreadExecutor::TscThreadExecutor(CFileDependTable* pFileTable, Settings &settings, ErrorLogger &errorLogger)
: _pFileTable(pFileTable)
, _curFile(nullptr)
, _settings(settings)
, _errorLogger(errorLogger)
, _fileCount(0)
, _analyzeFile(false)
, _threadIndex(0)
, _processedFiles(0)
, _totalFiles(0)
, _processedSize(0)
, _totalFileSize(0)
{
}
TscThreadExecutor::~TscThreadExecutor()
{
//dtor
}
void TscThreadExecutor::addFileContent(const std::string &path, const std::string &content)
{
_fileContents[path] = content;
}
void TscThreadExecutor::reportErr(const ErrorLogger::ErrorMessage &msg)
{
report(msg, REPORT_ERROR);
}
void TscThreadExecutor::reportInfo(const ErrorLogger::ErrorMessage &msg)
{
report(msg, REPORT_INFO);
}
bool CompareCodeFileSize(const CCodeFile* file1, const CCodeFile* file2)
{
return file1->GetSize() > file2->GetSize();
}
//#define TSC2_CHECK_ONE_FILE
//#define TSC2_JUST_ANALYZE
unsigned int TscThreadExecutor::check(bool bAnalyze, bool bCheck)
{
#ifdef TSC2_JUST_ANALYZE
if (!bAnalyze)
{
return 0;
}
#endif // TSC2_JUST_ANALYZE
if (!bAnalyze && bCheck)
{
return 0;
}
_analyzeFile = bAnalyze;
CGlobalTokenizer::Instance()->SetAnalyze(bAnalyze);
_checkList.clear();
_processedFiles = 0;
_processedSize = 0;
_totalFiles = 0;
_totalFileSize = 0;
CCodeFile* pFile = _pFileTable->GetFirstFile();
while (pFile)
{
if (Path::acceptFile(pFile->GetFullPath()) && !pFile->GetIgnore())
{
_checkList.push_back(pFile);
_totalFileSize += pFile->GetSize();
}
pFile = pFile->GetNext();
}
_checkList.sort(CompareCodeFileSize);
#ifdef TSC2_CHECK_ONE_FILE
_checkList.clear();
std::string oneFile = "";
oneFile = Path::fromNativeSeparators(oneFile);
CCodeFile newFile(oneFile.c_str(), 100);
_checkList.push_back(&newFile);
#endif // TSC2_CHECK_ONE_FILE
_checkList_iter = _checkList.begin();
_checkList_end = _checkList.end();
_totalFiles = _checkList.size();
unsigned ret = multi_thread(TscThreadExecutor::threadProc);
_checkList.clear();
#ifndef TSC2_CHECK_ONE_FILE
//find headers not expanded
pFile = _pFileTable->GetFirstFile();
_totalFileSize = 0;
while (pFile)
{
if (Path::isHeader(pFile->GetFullPath()) && !pFile->GetIgnore() && !pFile->IsExpaned())
{
_checkList.push_back(pFile);
_totalFileSize += pFile->GetSize();
}
pFile = pFile->GetNext();
}
if (!_checkList.empty())
{
std::cout << "Extra Header Start\n";
_checkList.sort(CompareCodeFileSize);
_checkList_iter = _checkList.begin();
_checkList_end = _checkList.end();
_totalFiles = _checkList.size();
_processedFiles = 0;
_processedSize = 0;
ret = multi_thread(TscThreadExecutor::threadProc);
std::cout << "Extra Header End\n";
}
#endif
if (bAnalyze)
{
if (_settings.debugDumpGlobal)
{
_pFileTable->DumpFileDependResults();
}
CGlobalTokenizer::Instance()->Merge(_settings.debugDumpGlobal);
}
else
{
CGlobalErrorList::Instance()->Merge(_settings);
std::set< std::string >& errorList = CGlobalErrorList::Instance()->GetOneData();
//autofilter
if (_settings.OpenAutofilter)
{
autoFilterResult(errorList);
}
for (std::set<std::string>::iterator I = errorList.begin(), E = errorList.end(); I != E; ++I)
{
_errorLogger.reportErr(*I);
}
}
return ret;
}
unsigned TscThreadExecutor::init()
{
CheckTSCInvalidVarArgs::InitFuncMap();
return initMacros();
}
#ifdef TSC_THREADING_MODEL_WIN
unsigned int __stdcall TscThreadExecutor::threadProc(void *args)
#else
void* TscThreadExecutor::threadProc(void *args)
#endif
{
TscThreadExecutor *threadExecutor = static_cast<TscThreadExecutor*>(args);
TSC_LOCK_ENTER(&threadExecutor->_fileSync);
int threadIndex = threadExecutor->_threadIndex;
++threadExecutor->_threadIndex;
TscanCode fileChecker(*threadExecutor, false);
CGlobalTokenizeData *pGlobalData = CGlobalTokenizer::Instance()->GetGlobalData(&fileChecker);
for (;;) {
if (threadExecutor->_checkList_iter == threadExecutor->_checkList_end) {
TSC_LOCK_LEAVE(&threadExecutor->_fileSync);
break;
}
CCodeFile* curFile = *threadExecutor->_checkList_iter;
const std::string &file = curFile->GetFullPath();
const std::size_t fileSize = curFile->GetSize();
threadExecutor->_checkList_iter++;
TSC_LOCK_LEAVE(&threadExecutor->_fileSync);
if (!threadExecutor->_settings.quiet && threadExecutor->_settings.debug) {
TSC_LOCK_ENTER(&threadExecutor->_reportSync);
TscanCodeExecutor::reportStatus(threadIndex, threadExecutor->_processedFiles, threadExecutor->_totalFiles, threadExecutor->_processedSize, threadExecutor->_totalFileSize, threadExecutor->_analyzeFile, true, file);
TSC_LOCK_LEAVE(&threadExecutor->_reportSync);
}
std::map<std::string, std::string>::const_iterator fileContent = threadExecutor->_fileContents.find(file);
if (fileContent != threadExecutor->_fileContents.end()) {
if (threadExecutor->_analyzeFile) {
pGlobalData->RecoredExportClass(true);
fileChecker.analyze(file, fileContent->second);
}
else {
pGlobalData->RecoredExportClass(false);
fileChecker.check(file, fileContent->second);
}
}
else {
if (threadExecutor->_analyzeFile) {
pGlobalData->RecoredExportClass(true);
fileChecker.analyze(file);
}
else {
pGlobalData->RecoredExportClass(false);
fileChecker.check(file);
}
}
TSC_LOCK_ENTER(&threadExecutor->_fileSync);
threadExecutor->_processedSize += fileSize;
threadExecutor->_processedFiles++;
if (!threadExecutor->_settings.quiet) {
TSC_LOCK_ENTER(&threadExecutor->_reportSync);
TscanCodeExecutor::reportStatus(threadIndex, threadExecutor->_processedFiles, threadExecutor->_totalFiles, threadExecutor->_processedSize, threadExecutor->_totalFileSize, threadExecutor->_analyzeFile, false, file);
TSC_LOCK_LEAVE(&threadExecutor->_reportSync);
}
}
if (!threadExecutor->_analyzeFile)
{
TSC_LOCK_ENTER(&threadExecutor->_fileSync);
std::list<std::string>& errorList = CGlobalErrorList::Instance()->GetThreadErrorList(&fileChecker);
TSC_LOCK_LEAVE(&threadExecutor->_fileSync);
fileChecker.SyncErrorList(errorList);
}
return NULL;
}
#ifdef TSC_THREADING_MODEL_WIN
unsigned int __stdcall TscThreadExecutor::threadProc_initMacros(void *args)
#else
void* TscThreadExecutor::threadProc_initMacros(void *args)
#endif
{
TscThreadExecutor *threadExecutor = static_cast<TscThreadExecutor*>(args);
TSC_LOCK_ENTER(&threadExecutor->_fileSync);
int threadIndex = threadExecutor->_threadIndex;
++threadExecutor->_threadIndex;
Preprocessor preprocessor(threadExecutor->_settings);
for (;;) {
if (nullptr == threadExecutor->_curFile) {
TSC_LOCK_LEAVE(&threadExecutor->_fileSync);
break;
}
CCodeFile* curFile = threadExecutor->_curFile;
const std::string &file = curFile->GetFullPath();
const std::size_t fileSize = curFile->GetSize();
threadExecutor->_curFile = curFile->GetNext();
TSC_LOCK_LEAVE(&threadExecutor->_fileSync);
if (!threadExecutor->_settings.quiet && threadExecutor->_settings.debug) {
TSC_LOCK_ENTER(&threadExecutor->_reportSync);
CGlobalMacros::reportStatus(threadIndex, true, threadExecutor->_processedFiles, threadExecutor->_totalFiles, threadExecutor->_processedSize, threadExecutor->_totalFileSize, file);
TSC_LOCK_LEAVE(&threadExecutor->_reportSync);
}
// get macros
preprocessor.getMacros(curFile);
TSC_LOCK_ENTER(&threadExecutor->_fileSync);
threadExecutor->_processedSize += fileSize;
threadExecutor->_processedFiles++;
if (!threadExecutor->_settings.quiet) {
TSC_LOCK_ENTER(&threadExecutor->_reportSync);
CGlobalMacros::reportStatus(threadIndex, false, threadExecutor->_processedFiles, threadExecutor->_totalFiles, threadExecutor->_processedSize, threadExecutor->_totalFileSize, file);
TSC_LOCK_LEAVE(&threadExecutor->_reportSync);
}
}
return NULL;
}
unsigned int TscThreadExecutor::initMacros()
{
CGlobalMacros::SetFileTable(_pFileTable);
_curFile = _pFileTable->GetFirstFile();
_processedFiles = 0;
_processedSize = 0;
_totalFiles = 0;
_totalFileSize = 0;
CCodeFile* pFile = _pFileTable->GetFirstFile();
while (pFile)
{
_totalFileSize += pFile->GetSize();
++_totalFiles;
pFile = pFile->GetNext();
}
TSC_LOCK_INIT(&CGlobalMacros::MacroLock);
TSC_LOCK_INIT(&CGlobalTypedefs::TypedefLock);
unsigned ret = multi_thread(TscThreadExecutor::threadProc_initMacros);
if (_settings.debugDumpGlobal)
{
CGlobalMacros::DumpMacros();
CGlobalTypedefs::DumpTypedef();
}
TSC_LOCK_DELETE(&CGlobalMacros::MacroLock);
TSC_LOCK_DELETE(&CGlobalTypedefs::TypedefLock);
return ret;
}
void TscThreadExecutor::reportOut(const std::string &outmsg)
{
TSC_LOCK_ENTER(&_reportSync);
_errorLogger.reportOut(outmsg);
TSC_LOCK_LEAVE(&_reportSync);
}
unsigned int TscThreadExecutor::multi_thread(ThreadProc threadProc)
{
TSC_THREAD *threadHandles = new TSC_THREAD[_settings._jobs];
TSC_LOCK_INIT(&_fileSync);
TSC_LOCK_INIT(&_errorSync);
TSC_LOCK_INIT(&_reportSync);
#ifdef TSC_THREADING_MODEL_WIN
for (unsigned int i = 0; i < _settings._jobs; ++i) {
threadHandles[i] = (HANDLE)_beginthreadex(NULL, 0, threadProc, this, 0, NULL);
if (!threadHandles[i]) {
std::cerr << "#### .\nTscThreadExecutor::check error, errno :" << errno << std::endl;
exit(EXIT_FAILURE);
}
}
DWORD waitResult = WaitForMultipleObjects(_settings._jobs, threadHandles, TRUE, INFINITE);
if (waitResult != WAIT_OBJECT_0) {
if (waitResult == WAIT_FAILED) {
std::cerr << "#### .\nTscThreadExecutor::check wait failed, result: " << waitResult << " error: " << GetLastError() << std::endl;
exit(EXIT_FAILURE);
}
else {
std::cerr << "#### .\nTscThreadExecutor::check wait failed, result: " << waitResult << std::endl;
exit(EXIT_FAILURE);
}
}
unsigned int result = 0;
for (unsigned int i = 0; i < _settings._jobs; ++i) {
DWORD exitCode;
if (!GetExitCodeThread(threadHandles[i], &exitCode)) {
std::cerr << "#### .\nTscThreadExecutor::check get exit code failed, error:" << GetLastError() << std::endl;
exit(EXIT_FAILURE);
}
result += exitCode;
if (!CloseHandle(threadHandles[i])) {
std::cerr << "#### .\nTscThreadExecutor::check close handle failed, error:" << GetLastError() << std::endl;
exit(EXIT_FAILURE);
}
}
#else
for (unsigned int i = 0; i < _settings._jobs; ++i) {
int ret = pthread_create(&threadHandles[i], nullptr, threadProc, this);
if (ret) {
std::cerr << "#### .\nTscThreadExecutor::check error, errno :" << ret << std::endl;
exit(EXIT_FAILURE);
}
}
unsigned int result = 0;
void* tret = nullptr;
for (int i = 0; i < _settings._jobs; ++i) {
int ret = pthread_join(threadHandles[i], &tret);
if (ret) {
std::cerr << "#### .\nTscThreadExecutor::check get exit code failed, error:" << ret << std::endl;
exit(EXIT_FAILURE);
}
result += (unsigned int)(intptr_t)(tret);
}
#endif
TSC_LOCK_DELETE(&_fileSync);
TSC_LOCK_DELETE(&_errorSync);
TSC_LOCK_DELETE(&_reportSync);
delete[] threadHandles;
return result;
}
void TscThreadExecutor::report(const ErrorLogger::ErrorMessage &msg, MessageType msgType)
{
std::string file;
unsigned int line(0);
if (!msg._callStack.empty()) {
file = msg._callStack.back().getfile(false);
line = msg._callStack.back().line;
}
if (_settings.nomsg.isSuppressed(msg._id, file, line))
return;
// Alert only about unique errors
bool reportError = false;
std::string errmsg = msg.toString(_settings._verbose);
TSC_LOCK_ENTER(&_errorSync);
if (std::find(_errorList.begin(), _errorList.end(), errmsg) == _errorList.end()) {
_errorList.push_back(errmsg);
reportError = true;
}
TSC_LOCK_LEAVE(&_errorSync);
if (reportError) {
TSC_LOCK_ENTER(&_reportSync);
switch (msgType) {
case REPORT_ERROR:
_errorLogger.reportErr(msg);
break;
case REPORT_INFO:
_errorLogger.reportInfo(msg);
break;
}
TSC_LOCK_LEAVE(&_reportSync);
}
}
int splitString(const std::string & strSrc, const std::string& strDelims, std::vector<std::string>& strDest)
{
typedef std::string::size_type ST;
std::string delims = strDelims;
std::string STR;
if (delims.empty()) delims = "/n/r";
ST pos = 0, LEN = strSrc.size();
while (pos < LEN){
STR = "";
while ((delims.find(strSrc[pos]) != std::string::npos) && (pos < LEN)) ++pos;
if (pos == LEN) return strDest.size();
while ((delims.find(strSrc[pos]) == std::string::npos) && (pos < LEN)) STR += strSrc[pos++];
if (!STR.empty()) strDest.push_back(STR);
}
return strDest.size();
}
bool TscThreadExecutor::isInAutoFilterSubID(const std::string &erroritem)
{
std::vector<std::string> autoFilterSubidList;
splitString(AUTOFILTER_SUBID, "|", autoFilterSubidList);
for (unsigned int i = 0; i<autoFilterSubidList.size(); i++)
{
if (std::strstr(erroritem.c_str(), autoFilterSubidList[i].c_str()))
{
return true;
}
}
return false;
}
void TscThreadExecutor::autoFilterResult(std::set< std::string>& errorList)
{
std::set<std::string>::iterator iter;
std::map<std::string, int> errCount;
for (iter = errorList.begin(); iter != errorList.end(); iter++)
{
std::string errkey = *iter;
std::string::size_type pos = 0;
if (_settings._xml)
{
if ((pos = errkey.find("subid=")) != std::string::npos)
{
std::string::size_type posend = 0;
if ((posend = errkey.find("func_info=")) != std::string::npos && posend>pos)
{
errkey = errkey.substr(pos, posend - pos - 1);
}
else
{
errkey = errkey.substr(pos);
}
errCount[errkey]++;
}
}
}
std::map<std::string, int>::iterator miter;
for (miter = errCount.begin(); miter != errCount.end(); miter++)
{
std::string errkey = miter->first;
if (miter->second >= MAXERRORCNT)
{
for (iter = errorList.begin(); iter != errorList.end();)
{
if ((*iter).find(errkey) != std::string::npos && isInAutoFilterSubID(*iter))
{
errorList.erase(iter++);
}
else
{
iter++;
}
}
}
}
}