FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cppcheck/gui/resultstree.cpp at main · cppchecksolutions/cppcheck · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
cppchecksolutions
/
cppcheck
Public
forked from
cppcheck-opensource/cppcheck
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
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
/
gui
/
resultstree.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
1316 lines (1109 loc) · 44.2 KB
Breadcrumbs
cppcheck
/
gui
/
resultstree.cpp
Copy path
File metadata and controls
1316 lines (1109 loc) · 44.2 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
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2026 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
"
resultstree.h
"
#
include
"
application.h
"
#
include
"
applicationlist.h
"
#
include
"
checkers.h
"
#
include
"
common.h
"
#
include
"
erroritem.h
"
#
include
"
errorlogger.h
"
#
include
"
errortypes.h
"
#
include
"
path.h
"
#
include
"
projectfile.h
"
#
include
"
report.h
"
#
include
"
resultitem.h
"
#
include
"
showtypes.h
"
#
include
"
suppressions.h
"
#
include
"
threadhandler.h
"
#
include
"
utils.h
"
#
include
"
xmlreportv2.h
"
#
include
<
algorithm
>
#
include
<
utility
>
#
include
<
QAction
>
#
include
<
QApplication
>
#
include
<
QClipboard
>
#
include
<
QContextMenuEvent
>
#
include
<
QDebug
>
#
include
<
QDesktopServices
>
#
include
<
QDir
>
#
include
<
QFileInfo
>
#
include
<
QFileDialog
>
#
include
<
QIcon
>
#
include
<
QItemSelectionModel
>
#
include
<
QKeyEvent
>
#
include
<
QLocale
>
#
include
<
QMenu
>
#
include
<
QMessageBox
>
#
include
<
QModelIndex
>
#
include
<
QObject
>
#
include
<
QProcess
>
#
include
<
QSet
>
#
include
<
QSettings
>
#
include
<
QSignalMapper
>
#
include
<
QStandardItemModel
>
#
include
<
QUrl
>
#
include
<
Qt
>
#
include
<
QHeaderView
>
//
These must match column headers given in ResultsTree::translate()
static
constexpr
int
COLUMN_FILE
=
0
;
static
constexpr
int
COLUMN_LINE
=
1
;
static
constexpr
int
COLUMN_SEVERITY
=
2
;
static
constexpr
int
COLUMN_MISRA_CLASSIFICATION
=
3
;
static
constexpr
int
COLUMN_CERT_LEVEL
=
4
;
static
constexpr
int
COLUMN_INCONCLUSIVE
=
5
;
static
constexpr
int
COLUMN_SUMMARY
=
6
;
static
constexpr
int
COLUMN_ID
=
7
;
static
constexpr
int
COLUMN_MISRA_GUIDELINE
=
8
;
static
constexpr
int
COLUMN_CERT_RULE
=
9
;
static
constexpr
int
COLUMN_SINCE_DATE
=
10
;
static
constexpr
int
COLUMN_TAGS
=
11
;
static
constexpr
int
COLUMN_CWE
=
12
;
static
QString
getGuideline
(ReportType reportType,
const
std::map<std::string, std::string> &guidelineMapping,
const
QString& errorId, Severity severity) {
return
QString::fromStdString
(
getGuideline
(errorId.
toStdString
(),
reportType, guidelineMapping,
severity));
}
static
QString
getClassification
(ReportType reportType,
const
QString& guideline) {
return
QString::fromStdString
(
getClassification
(guideline.
toStdString
(), reportType));
}
static
Severity
getSeverityFromClassification
(
const
QString &c) {
if
(c == checkers::Man)
return
Severity::error;
if
(c == checkers::Req)
return
Severity::warning;
if
(c == checkers::Adv)
return
Severity::style;
if
(c == checkers::Doc)
return
Severity::information;
if
(c ==
"
L1
"
)
return
Severity::error;
if
(c ==
"
L2
"
)
return
Severity::warning;
if
(c ==
"
L3
"
)
return
Severity::style;
return
Severity::none;
}
static
QStringList
getLabels
() {
return
QStringList{
QObject::tr
(
"
File
"
),
QObject::tr
(
"
Line
"
),
QObject::tr
(
"
Severity
"
),
QObject::tr
(
"
Classification
"
),
QObject::tr
(
"
Level
"
),
QObject::tr
(
"
Inconclusive
"
),
QObject::tr
(
"
Summary
"
),
QObject::tr
(
"
Id
"
),
QObject::tr
(
"
Guideline
"
),
QObject::tr
(
"
Rule
"
),
QObject::tr
(
"
Since date
"
),
QObject::tr
(
"
Tags
"
),
"
CWE
"
};
}
static
Severity
getSeverity
(ReportType reportType,
const
ErrorItem& errorItem) {
return
reportType == ReportType::normal ? errorItem.
severity
:
getSeverityFromClassification
(errorItem.
classification
);
}
ResultsTree::ResultsTree
(QWidget * parent) :
QTreeView(parent),
mModel(
new
QStandardItemModel)
{
setModel
(
mModel
);
translate
();
//
Adds columns to grid
clear
();
setExpandsOnDoubleClick
(
false
);
setSortingEnabled
(
true
);
connect
(
this
, &ResultsTree::doubleClicked,
this
, &ResultsTree::quickStartApplication);
}
void
ResultsTree::keyPressEvent
(QKeyEvent *event)
{
if
(event->
key
() == Qt::Key_Enter || event->
key
() == Qt::Key_Return) {
quickStartApplication
(
this
->
currentIndex
());
}
QTreeView::keyPressEvent
(event);
}
void
ResultsTree::setReportType
(ReportType reportType) {
mReportType
= reportType;
mGuideline
=
createGuidelineMapping
(reportType);
for
(
int
i =
0
; i <
mModel
->
rowCount
(); ++i) {
auto
*fileItem =
dynamic_cast
<ResultItem*>(
mModel
->
item
(i,
COLUMN_FILE
));
if
(!fileItem)
continue
;
for
(
int
j =
0
; j < fileItem->
rowCount
(); ++j) {
QSharedPointer<ErrorItem>& errorItem =
dynamic_cast
<ResultItem*>(fileItem->
child
(j,
0
))->
errorItem
;
errorItem->
guideline
=
getGuideline
(
mReportType
,
mGuideline
, errorItem->
errorId
, errorItem->
severity
);
errorItem->
classification
=
getClassification
(
mReportType
, errorItem->
guideline
);
dynamic_cast
<ResultItem*>(fileItem->
child
(j,
COLUMN_FILE
))->
setIconFileName
(
severityToIcon
(
getSeverity
(reportType, *errorItem)));
fileItem->
child
(j,
COLUMN_CERT_LEVEL
)->
setText
(errorItem->
classification
);
fileItem->
child
(j,
COLUMN_CERT_RULE
)->
setText
(errorItem->
guideline
);
fileItem->
child
(j,
COLUMN_MISRA_CLASSIFICATION
)->
setText
(errorItem->
classification
);
fileItem->
child
(j,
COLUMN_MISRA_GUIDELINE
)->
setText
(errorItem->
guideline
);
}
}
if
(
isAutosarMisraReport
()) {
showColumn
(
COLUMN_MISRA_CLASSIFICATION
);
showColumn
(
COLUMN_MISRA_GUIDELINE
);
}
else
{
hideColumn
(
COLUMN_MISRA_CLASSIFICATION
);
hideColumn
(
COLUMN_MISRA_GUIDELINE
);
}
if
(
isCertReport
()) {
showColumn
(
COLUMN_CERT_LEVEL
);
showColumn
(
COLUMN_CERT_RULE
);
}
else
{
hideColumn
(
COLUMN_CERT_LEVEL
);
hideColumn
(
COLUMN_CERT_RULE
);
}
if
(
mReportType
== ReportType::normal) {
showColumn
(
COLUMN_SEVERITY
);
}
else
{
hideColumn
(
COLUMN_SEVERITY
);
}
refreshTree
();
}
void
ResultsTree::initialize
(QSettings *settings, ApplicationList *list, ThreadHandler *checkThreadHandler)
{
mSettings
= settings;
mApplications
= list;
mThread
= checkThreadHandler;
loadSettings
();
}
ResultItem *
ResultsTree::createNormalItem
(
const
QString &text, QSharedPointer<ErrorItem> errorItem, ResultItem::Type type,
int
errorPathIndex)
{
auto
*item =
new
ResultItem
(
std::move
(errorItem), type, errorPathIndex);
item->
setText
(text);
item->
setEditable
(
false
);
return
item;
}
ResultItem *
ResultsTree::createFilenameItem
(
const
QSharedPointer<ErrorItem>& errorItem, ResultItem::Type type,
int
errorPathIndex)
{
auto
*item =
new
ResultItem
(errorItem, type, errorPathIndex);
item->
setText
(
QDir::toNativeSeparators
(
stripPath
(errorItem->
errorPath
[errorPathIndex].
file
,
false
)));
item->
setEditable
(
false
);
return
item;
}
ResultItem *
ResultsTree::createCheckboxItem
(
bool
checked, QSharedPointer<ErrorItem> errorItem, ResultItem::Type type,
int
errorPathIndex)
{
auto
*item =
new
ResultItem
(
std::move
(errorItem), type, errorPathIndex);
item->
setCheckable
(
true
);
item->
setCheckState
(checked ? Qt::Checked : Qt::Unchecked);
item->
setEnabled
(
false
);
return
item;
}
ResultItem *
ResultsTree::createLineNumberItem
(
int
linenumber, QSharedPointer<ErrorItem> errorItem, ResultItem::Type type,
int
errorPathIndex)
{
auto
*item =
new
ResultItem
(
std::move
(errorItem), type, errorPathIndex);
item->
setText
(
QString::number
(linenumber));
item->
setTextAlignment
(Qt::AlignRight | Qt::AlignVCenter);
item->
setEditable
(
false
);
return
item;
}
bool
ResultsTree::addErrorItem
(
const
ErrorItem& errorItem)
{
if
(errorItem.
errorPath
.
isEmpty
())
return
false
;
const
QString s = errorItem.
toString
();
if
(
mErrorList
.
contains
(s))
return
false
;
mErrorList
.
append
(s);
QSharedPointer<ErrorItem> errorItemPtr{
new
ErrorItem
(errorItem)};
if
(
mReportType
!= ReportType::normal) {
errorItemPtr->
guideline
=
getGuideline
(
mReportType
,
mGuideline
, errorItemPtr->
errorId
, errorItemPtr->
severity
);
errorItemPtr->
classification
=
getClassification
(
mReportType
, errorItemPtr->
guideline
);
}
const
bool
showItem = !
isErrorItemHidden
(errorItemPtr);
//
if there is at least one error that is not hidden, we have a visible error
mVisibleErrors
|= showItem;
if
(
const
ProjectFile *activeProject =
ProjectFile::getActiveProject
())
errorItemPtr->
tags
= activeProject->
getWarningTags
(errorItemPtr->
hash
);
//
Create the base item for the error and ensure it has a proper
//
file item as a parent
ResultItem* fileItem =
ensureFileItem
(errorItemPtr, !showItem);
ResultItem* stditem =
addBacktraceFiles
(fileItem,
errorItemPtr,
!showItem,
severityToIcon
(
getSeverity
(
mReportType
, *errorItemPtr)),
ResultItem::Type::message,
errorItemPtr->
getMainLocIndex
());
if
(!stditem)
return
false
;
//
Add backtrace files as children
if
(errorItemPtr->
errorPath
.
size
() >
1
) {
for
(
int
i =
0
; i < errorItemPtr->
errorPath
.
size
(); i++) {
addBacktraceFiles
(stditem,
errorItemPtr,
false
,
"
:images/go-down.png
"
,
ResultItem::Type::note,
i);
}
}
return
true
;
}
ResultItem *
ResultsTree::addBacktraceFiles
(ResultItem *parent,
const
QSharedPointer<ErrorItem>& errorItem,
const
bool
hide,
const
QString &icon,
ResultItem::Type type,
int
errorPathIndex)
{
if
(!parent)
return
nullptr
;
//
TODO message has parameter names so we'll need changes to the core
//
cppcheck so we can get proper translations
const
bool
childOfMessage = (type == ResultItem::Type::note);
const
QString itemSeverity = childOfMessage ?
tr
(
"
note
"
) :
severityToTranslatedString
(errorItem->
severity
);
const
auto
& loc = errorItem->
errorPath
[errorPathIndex];
//
Check for duplicate rows and don't add them if found
for
(
int
i =
0
; i < errorPathIndex; i++) {
//
The first column is the file name and is always the same
const
auto
& e = errorItem->
errorPath
[i];
if
(loc.
line
== e.
line
&& loc.
info
== e.
info
)
return
nullptr
;
}
const
QString text = childOfMessage ? loc.
info
: errorItem->
summary
;
const
int
numberOfColumns =
getLabels
().
size
();
QList<ResultItem*>
columns
(numberOfColumns);
columns[
COLUMN_FILE
] =
createFilenameItem
(errorItem, type, errorPathIndex);
columns[
COLUMN_FILE
]->
setIconFileName
(icon);
columns[
COLUMN_LINE
] =
createLineNumberItem
(loc.
line
, errorItem, type, errorPathIndex);
columns[
COLUMN_SEVERITY
] =
createNormalItem
(itemSeverity, errorItem, type, errorPathIndex);
columns[
COLUMN_SUMMARY
] =
createNormalItem
(text, errorItem, type, errorPathIndex);
if
(type == ResultItem::Type::message) {
columns[
COLUMN_CERT_LEVEL
] =
createNormalItem
(errorItem->
classification
, errorItem, type, errorPathIndex);
columns[
COLUMN_CERT_RULE
] =
createNormalItem
(errorItem->
guideline
, errorItem, type, errorPathIndex);
columns[
COLUMN_CWE
] =
createNormalItem
(errorItem->
cwe
>
0
?
QString::number
(errorItem->
cwe
) :
QString
(), errorItem, type, errorPathIndex);
columns[
COLUMN_ID
] =
createNormalItem
(errorItem->
errorId
, errorItem, type, errorPathIndex);
columns[
COLUMN_INCONCLUSIVE
] =
createCheckboxItem
(errorItem->
inconclusive
, errorItem, type, errorPathIndex);
columns[
COLUMN_MISRA_CLASSIFICATION
] =
createNormalItem
(errorItem->
classification
, errorItem, type, errorPathIndex);
columns[
COLUMN_MISRA_GUIDELINE
] =
createNormalItem
(errorItem->
guideline
, errorItem, type, errorPathIndex);
columns[
COLUMN_SINCE_DATE
] =
createNormalItem
(errorItem->
sinceDate
, errorItem, type, errorPathIndex);
columns[
COLUMN_TAGS
] =
createNormalItem
(errorItem->
tags
, errorItem, type, errorPathIndex);
}
QList<QStandardItem*> list;
for
(
int
i =
0
; i < numberOfColumns; ++i)
list << (columns[i] ? columns[i] :
createNormalItem
(
QString
(), errorItem, type, errorPathIndex));
parent->
appendRow
(list);
setRowHidden
(parent->
rowCount
() -
1
, parent->
index
(), hide);
return
columns[
COLUMN_FILE
];
}
QString
ResultsTree::severityToTranslatedString
(Severity severity)
{
switch
(severity) {
case
Severity::style:
return
tr
(
"
style
"
);
case
Severity::error:
return
tr
(
"
error
"
);
case
Severity::warning:
return
tr
(
"
warning
"
);
case
Severity::performance:
return
tr
(
"
performance
"
);
case
Severity::portability:
return
tr
(
"
portability
"
);
case
Severity::information:
return
tr
(
"
information
"
);
case
Severity::debug:
return
tr
(
"
debug
"
);
case
Severity::internal:
return
tr
(
"
internal
"
);
case
Severity::none:
return
QString
();
}
cppcheck::unreachable
();
}
ResultItem *
ResultsTree::findFileItem
(
const
QString &name)
const
{
//
The first column contains the file name. In Windows we can get filenames
//
"header.h" and "Header.h" and must compare them as identical.
for
(
int
i =
0
; i <
mModel
->
rowCount
(); i++) {
#
ifdef
_WIN32
if
(
QString::compare
(
mModel
->
item
(i,
COLUMN_FILE
)->
text
(), name, Qt::CaseInsensitive) ==
0
)
#
else
if
(
mModel
->
item
(i,
COLUMN_FILE
)->
text
() == name)
#
endif
return
dynamic_cast
<ResultItem*>(
mModel
->
item
(i,
COLUMN_FILE
));
}
return
nullptr
;
}
void
ResultsTree::clear
()
{
mErrorList
.
clear
();
mModel
->
removeRows
(
0
,
mModel
->
rowCount
());
if
(
const
ProjectFile *activeProject =
ProjectFile::getActiveProject
()) {
hideColumn
(
COLUMN_SINCE_DATE
);
if
(activeProject->
getTags
().
isEmpty
())
hideColumn
(
COLUMN_TAGS
);
else
showColumn
(
COLUMN_TAGS
);
}
else
{
hideColumn
(
COLUMN_SINCE_DATE
);
hideColumn
(
COLUMN_TAGS
);
}
}
void
ResultsTree::clear
(
const
QString &filename)
{
const
QString stripped =
QDir::toNativeSeparators
(
stripPath
(filename,
false
));
for
(
int
i =
0
; i <
mModel
->
rowCount
(); ++i) {
const
auto
*fileItem =
dynamic_cast
<ResultItem*>(
mModel
->
item
(i,
COLUMN_FILE
));
if
(!fileItem)
continue
;
if
(stripped == fileItem->
text
() ||
filename == fileItem->
errorItem
->
file0
) {
mErrorList
.
removeAll
(fileItem->
errorItem
->
toString
());
mModel
->
removeRow
(i);
break
;
}
}
}
void
ResultsTree::clearRecheckFile
(
const
QString &filename)
{
for
(
int
i =
0
; i <
mModel
->
rowCount
(); ++i) {
const
auto
*fileItem =
dynamic_cast
<ResultItem*>(
mModel
->
item
(i,
COLUMN_FILE
));
if
(!fileItem)
continue
;
QString
actualfile
((!
mCheckPath
.
isEmpty
() && filename.
startsWith
(
mCheckPath
)) ? filename.
mid
(
mCheckPath
.
length
() +
1
) : filename);
QString storedfile = fileItem->
getErrorPathItem
().
file
;
storedfile = ((!
mCheckPath
.
isEmpty
() && storedfile.
startsWith
(
mCheckPath
)) ? storedfile.
mid
(
mCheckPath
.
length
() +
1
) : storedfile);
if
(actualfile == storedfile) {
mErrorList
.
removeAll
(fileItem->
errorItem
->
toString
());
mModel
->
removeRow
(i);
break
;
}
}
}
void
ResultsTree::loadSettings
()
{
for
(
int
i =
0
; i <
mModel
->
columnCount
(); i++) {
QString temp =
QString
(
SETTINGS_RESULT_COLUMN_WIDTH
).
arg
(i);
setColumnWidth
(i,
qMax
(
20
,
mSettings
->
value
(temp,
800
/
mModel
->
columnCount
()).
toInt
()));
}
mSaveFullPath
=
mSettings
->
value
(
SETTINGS_SAVE_FULL_PATH
,
false
).
toBool
();
mSaveAllErrors
=
mSettings
->
value
(
SETTINGS_SAVE_ALL_ERRORS
,
false
).
toBool
();
mShowFullPath
=
mSettings
->
value
(
SETTINGS_SHOW_FULL_PATH
,
false
).
toBool
();
showIdColumn
(
mSettings
->
value
(
SETTINGS_SHOW_ERROR_ID
,
true
).
toBool
());
showInconclusiveColumn
(
mSettings
->
value
(
SETTINGS_INCONCLUSIVE_ERRORS
,
false
).
toBool
());
}
void
ResultsTree::saveSettings
()
const
{
for
(
int
i =
0
; i <
mModel
->
columnCount
(); i++) {
QString temp =
QString
(
SETTINGS_RESULT_COLUMN_WIDTH
).
arg
(i);
mSettings
->
setValue
(temp,
columnWidth
(i));
}
}
void
ResultsTree::showResults
(ShowTypes::ShowType type,
bool
show)
{
if
(type != ShowTypes::ShowNone &&
mShowSeverities
.
isShown
(type) != show) {
mShowSeverities
.
show
(type, show);
refreshTree
();
}
}
void
ResultsTree::showCppcheckResults
(
bool
show)
{
mShowCppcheck
= show;
refreshTree
();
}
void
ResultsTree::showClangResults
(
bool
show)
{
mShowClang
= show;
refreshTree
();
}
void
ResultsTree::filterResults
(
const
QString& filter)
{
mFilter
= filter;
refreshTree
();
}
void
ResultsTree::showHiddenResults
()
{
//
Clear the "hide" flag for each item
mHiddenMessageId
.
clear
();
refreshTree
();
emit
resultsHidden
(
false
);
}
void
ResultsTree::refreshTree
()
{
mVisibleErrors
=
false
;
//
Get the amount of files in the tree
const
int
filecount =
mModel
->
rowCount
();
for
(
int
i =
0
; i < filecount; i++) {
//
Get file i
auto
*fileItem =
dynamic_cast
<ResultItem*>(
mModel
->
item
(i,
0
));
if
(!fileItem) {
continue
;
}
//
Get the amount of errors this file contains
const
int
errorcount = fileItem->
rowCount
();
//
By default it shouldn't be visible
bool
showFile =
false
;
for
(
int
j =
0
; j < errorcount; j++) {
//
Get the error itself
const
auto
*child =
dynamic_cast
<ResultItem*>(fileItem->
child
(j,
0
));
if
(!child) {
continue
;
}
//
Check if this error should be hidden
const
bool
hide = child->
hidden
||
isErrorItemHidden
(child->
errorItem
);
if
(!hide) {
showFile =
true
;
mVisibleErrors
=
true
;
}
//
Hide/show accordingly
setRowHidden
(j, fileItem->
index
(), hide);
}
//
Show the file if any of it's errors are visible
setRowHidden
(i,
QModelIndex
(), !showFile);
}
sortByColumn
(
header
()->
sortIndicatorSection
(),
header
()->
sortIndicatorOrder
());
}
bool
ResultsTree::isErrorItemHidden
(
const
QSharedPointer<ErrorItem>& errorItem)
const
{
//
Check if this error should be hidden
if
(
mHiddenMessageId
.
contains
(errorItem->
errorId
))
return
true
;
bool
hide;
if
(
mReportType
== ReportType::normal)
hide = !
mShowSeverities
.
isShown
(errorItem->
severity
);
else
hide = errorItem->
classification
.
isEmpty
() || !
mShowSeverities
.
isShown
(
getSeverityFromClassification
(errorItem->
classification
));
//
If specified, filter on summary, message, filename, and id
if
(!hide && !
mFilter
.
isEmpty
())
hide = !errorItem->
filterMatch
(
mFilter
);
//
Tool filter
if
(!hide) {
if
(errorItem->
isClangResult
())
hide = !
mShowClang
;
else
hide = !
mShowCppcheck
;
}
return
hide;
}
ResultItem *
ResultsTree::ensureFileItem
(
const
QSharedPointer<ErrorItem>& errorItem,
bool
hide)
{
QString name =
QDir::toNativeSeparators
(
stripPath
(errorItem->
getFile
(),
false
));
//
Since item has path with native separators we must use path with
//
native separators to find it.
ResultItem *fileItem =
findFileItem
(name);
if
(fileItem) {
if
(!hide)
setRowHidden
(fileItem->
row
(),
QModelIndex
(), hide);
return
fileItem;
}
//
Ensure shown path is with native separators
fileItem =
createFilenameItem
(errorItem, ResultItem::Type::file, errorItem->
getMainLocIndex
());
fileItem->
setIcon
(
QIcon
(
"
:images/text-x-generic.png
"
));
mModel
->
appendRow
(fileItem);
setRowHidden
(fileItem->
row
(),
QModelIndex
(), hide);
return
fileItem;
}
void
ResultsTree::contextMenuEvent
(QContextMenuEvent * e)
{
QModelIndex index =
indexAt
(e->
pos
());
if
(index.
isValid
()) {
bool
multipleSelection =
false
;
mSelectionModel
=
selectionModel
();
if
(
mSelectionModel
->
selectedRows
().
count
() >
1
)
multipleSelection =
true
;
mContextItem
=
dynamic_cast
<ResultItem*>(
mModel
->
itemFromIndex
(index));
//
Create a new context menu
QMenu
menu
(
this
);
//
Create a signal mapper so we don't have to store data to class
//
member variables
QSignalMapper signalMapper;
if
(
mContextItem
&&
mApplications
->
getApplicationCount
() >
0
&&
mContextItem
->
parent
()) {
//
Create an action for the application
int
defaultApplicationIndex =
mApplications
->
getDefaultApplication
();
defaultApplicationIndex =
std::max
(defaultApplicationIndex,
0
);
const
Application& app =
mApplications
->
getApplication
(defaultApplicationIndex);
auto
*start =
new
QAction
(app.
getName
(), &menu);
if
(multipleSelection)
start->
setDisabled
(
true
);
//
Add it to context menu
menu.
addAction
(start);
//
Connect the signal to signal mapper
connect
(start, &QAction::triggered, &signalMapper, QOverload<>::
of
(&QSignalMapper::map));
//
Add a new mapping
signalMapper.
setMapping
(start, defaultApplicationIndex);
connect
(&signalMapper,
SIGNAL
(
mappedInt
(
int
)),
this
,
SLOT
(
context
(
int
)));
}
//
Add popup menuitems
if
(
mContextItem
) {
if
(
mApplications
->
getApplicationCount
() >
0
) {
menu.
addSeparator
();
}
int
selectedFiles =
0
;
int
selectedResults =
0
;
for
(
auto
row :
mSelectionModel
->
selectedRows
()) {
auto
*item =
mModel
->
itemFromIndex
(row);
if
(!item->
parent
())
selectedFiles++;
else
if
(!item->
parent
()->
parent
())
selectedResults++;
}
//
Create an action for the application
auto
*recheckAction =
new
QAction
(
tr
(
"
Recheck %1 file(s)
"
).
arg
(selectedFiles), &menu);
auto
*copyAction =
new
QAction
(
tr
(
"
Copy
"
), &menu);
auto
*hide =
new
QAction
(
tr
(
"
Hide %1 result(s)
"
).
arg
(selectedResults), &menu);
auto
*hideallid =
new
QAction
(
tr
(
"
Hide all with id
"
), &menu);
auto
*opencontainingfolder =
new
QAction
(
tr
(
"
Open containing folder
"
), &menu);
if
(selectedFiles ==
0
||
mThread
->
isChecking
() ||
mResultsSource
== ResultsSource::Log)
recheckAction->
setDisabled
(
true
);
if
(selectedResults ==
0
)
hide->
setDisabled
(
true
);
if
(selectedResults ==
0
|| multipleSelection)
hideallid->
setDisabled
(
true
);
if
(multipleSelection ||
mResultsSource
== ResultsSource::Log)
opencontainingfolder->
setDisabled
(
true
);
menu.
addAction
(recheckAction);
menu.
addSeparator
();
menu.
addAction
(copyAction);
menu.
addSeparator
();
menu.
addAction
(hide);
menu.
addAction
(hideallid);
auto
*suppress =
new
QAction
(
tr
(
"
Suppress selected id(s)
"
), &menu);
{
if
(selectedResults ==
0
||
ErrorLogger::isCriticalErrorId
(
mContextItem
->
errorItem
->
errorId
.
toStdString
()))
suppress->
setDisabled
(
true
);
}
menu.
addAction
(suppress);
connect
(suppress, &QAction::triggered,
this
, &ResultsTree::suppressSelectedIds);
menu.
addSeparator
();
menu.
addAction
(opencontainingfolder);
connect
(recheckAction, &QAction::triggered,
this
, &ResultsTree::recheckSelectedFiles);
connect
(copyAction, &QAction::triggered,
this
, &ResultsTree::copy);
connect
(hide, &QAction::triggered,
this
, &ResultsTree::hideResult);
connect
(hideallid, &QAction::triggered,
this
, &ResultsTree::hideAllIdResult);
connect
(opencontainingfolder, &QAction::triggered,
this
, &ResultsTree::openContainingFolder);
const
ProjectFile *currentProject =
ProjectFile::getActiveProject
();
if
(currentProject && !currentProject->
getTags
().
isEmpty
()) {
menu.
addSeparator
();
QMenu *tagMenu = menu.
addMenu
(
tr
(
"
Tag
"
));
{
auto
*action =
new
QAction
(
tr
(
"
No tag
"
), tagMenu);
tagMenu->
addAction
(action);
connect
(action, &QAction::triggered, [
this
]() {
tagSelectedItems
(
QString
());
});
}
for
(
const
QString& tagstr : currentProject->
getTags
()) {
auto
*action =
new
QAction
(tagstr, tagMenu);
tagMenu->
addAction
(action);
connect
(action, &QAction::triggered, [tagstr,
this
]() {
tagSelectedItems
(tagstr);
});
}
}
}
//
Start the menu
menu.
exec
(e->
globalPos
());
index =
indexAt
(e->
pos
());
if
(index.
isValid
())
mContextItem
=
dynamic_cast
<ResultItem*>(
mModel
->
itemFromIndex
(index));
}
}
void
ResultsTree::startApplication
(
const
ResultItem *target,
int
application)
{
//
If there are no applications specified, tell the user about it
if
(
mApplications
->
getApplicationCount
() ==
0
) {
QMessageBox
msg
(QMessageBox::Critical,
"
Cppcheck
"
,
tr
(
"
No editor application configured.
\n\n
"
"
Configure the editor application for Cppcheck in preferences/Applications.
"
),
QMessageBox::Ok,
this
);
msg.
exec
();
return
;
}
if
(application == -
1
)
application =
mApplications
->
getDefaultApplication
();
if
(application == -
1
) {
QMessageBox
msg
(QMessageBox::Critical,
"
Cppcheck
"
,
tr
(
"
No default editor application selected.
\n\n
"
"
Please select the default editor application in preferences/Applications.
"
),
QMessageBox::Ok,
this
);
msg.
exec
();
return
;
}
if
(target && application >=
0
&& application <
mApplications
->
getApplicationCount
() && target->
parent
()) {
const
auto
& errorPathItem = target->
getErrorPathItem
();
//
Replace (file) with filename
QString file =
QDir::toNativeSeparators
(errorPathItem.
file
);
qDebug
() <<
"
Opening file:
"
<< file;
const
QFileInfo
info
(file);
if
(!info.
exists
()) {
if
(info.
isAbsolute
()) {
QMessageBox
msgbox
(
this
);
msgbox.
setWindowTitle
(
"
Cppcheck
"
);
msgbox.
setText
(
tr
(
"
Could not find the file!
"
));
msgbox.
setIcon
(QMessageBox::Critical);
msgbox.
exec
();
}
else
{
QDir
checkdir
(
mCheckPath
);
if
(checkdir.
isAbsolute
() && checkdir.
exists
()) {
file =
mCheckPath
+
"
/
"
+ file;
}
else
{
QString dir =
askFileDir
(file);
dir +=
'
/
'
;
file = dir + file;
}
}
}
if
(file.
indexOf
(
"
"
) > -
1
) {
file.
insert
(
0
,
"
\"
"
);
file.
append
(
"
\"
"
);
}
const
Application& app =
mApplications
->
getApplication
(application);
QString params = app.
getParameters
();
params.
replace
(
"
(file)
"
, file, Qt::CaseInsensitive);
params.
replace
(
"
(line)
"
,
QString::number
(errorPathItem.
line
), Qt::CaseInsensitive);
params.
replace
(
"
(message)
"
, target->
errorItem
->
message
, Qt::CaseInsensitive);
params.
replace
(
"
(severity)
"
,
severityToTranslatedString
(target->
errorItem
->
severity
), Qt::CaseInsensitive);
QString program = app.
getPath
();
//
In Windows we must surround paths including spaces with quotation marks.
#
ifdef
Q_OS_WIN
if
(program.
indexOf
(
"
"
) > -
1
) {
if
(!program.
startsWith
(
'
"
'
) && !program.
endsWith
(
'
"
'
)) {
program.
insert
(
0
,
"
\"
"
);
program.
append
(
"
\"
"
);
}
}
#
endif
//
Q_OS_WIN
const
bool
success =
QProcess::startDetached
(program,
QProcess::splitCommand
(params));
if
(!success) {
QString text =
tr
(
"
Could not start %1
\n\n
Please check the application path and parameters are correct.
"
).
arg
(program);
QMessageBox
msgbox
(
this
);
msgbox.
setWindowTitle
(
"
Cppcheck
"
);
msgbox.
setText
(text);
msgbox.
setIcon
(QMessageBox::Critical);
msgbox.
exec
();
}
}
}
QString
ResultsTree::askFileDir
(
const
QString &file)
{
QString text =
tr
(
"
Could not find file:
"
) +
'
\n
'
+ file +
'
\n
'
;
QString title;
if
(file.
indexOf
(
'
/
'
)) {
QString folderName = file.
mid
(
0
, file.
indexOf
(
'
/
'
));
text +=
tr
(
"
Please select the folder '%1'
"
).
arg
(folderName);
title =
tr
(
"
Select Directory '%1'
"
).
arg
(folderName);
}
else
{
text +=
tr
(
"
Please select the directory where file is located.
"
);
title =
tr
(
"
Select Directory
"
);
}
QMessageBox
msgbox
(
this
);
msgbox.
setWindowTitle
(
"
Cppcheck
"
);
msgbox.
setText
(text);
msgbox.
setIcon
(QMessageBox::Warning);
msgbox.
exec
();
QString dir =
QFileDialog::getExistingDirectory
(
this
, title,
getPath
(
SETTINGS_LAST_SOURCE_PATH
),
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
if
(dir.
isEmpty
())
return
QString
();
//
User selected root path
if
(
QFileInfo::exists
(dir +
'
/
'
+ file))
mCheckPath
= dir;
//
user selected checked folder
else
if
(file.
indexOf
(
'
/
'
) >
0
) {
dir +=
'
/
'
;
QString folderName = file.
mid
(
0
, file.
indexOf
(
'
/
'
));
if
(dir.
indexOf
(
'
/
'
+ folderName +
'
/
'
))
dir = dir.
mid
(
0
, dir.
lastIndexOf
(
'
/
'
+ folderName +
'
/
'
));
if
(
QFileInfo::exists
(dir +
'
/
'
+ file))
mCheckPath
= dir;
}
//
Otherwise; return
else
return
QString
();
setPath
(
SETTINGS_LAST_SOURCE_PATH
,
mCheckPath
);
return
mCheckPath
;
}
void
ResultsTree::copy
()
{
if
(!
mSelectionModel
)
return
;
QString text;
for
(
const
QModelIndex& index :
mSelectionModel
->
selectedRows
()) {
const
auto
*item =
dynamic_cast
<ResultItem*>(
mModel
->
itemFromIndex
(index));
if
(!item)
continue
;
if
(item->
getType
() == ResultItem::Type::file)
text += item->
text
() +
'
\n
'
;
else
if
(item->
getType
() == ResultItem::Type::message)
text += item->
errorItem
->
toString
() +
'
\n
'
;
else
if
(item->
getType
() == ResultItem::Type::note) {
const
auto
e = item->
getErrorPathItem
();
text += e.
file
+
"
:
"
+
QString::number
(e.
line
) +
"
:
"
+
QString::number
(e.
column
) +
"
:note:
"
+ e.
info
+
'
\n
'
;
}
}
QClipboard *clipboard =
QApplication::clipboard
();
clipboard->
setText
(text);
}
void
ResultsTree::hideResult
()
{
if
(!
mSelectionModel
)
return
;
bool
hide =
false
;
for
(
const
QModelIndex& index :
mSelectionModel
->
selectedRows
()) {
auto
*item =
dynamic_cast
<ResultItem*>(
mModel
->
itemFromIndex
(index));
if
(item && item->
getType
() == ResultItem::Type::message)
hide = item->
hidden
=
true
;
}
if
(hide) {
refreshTree
();
emit
resultsHidden
(
true
);
}
}
void
ResultsTree::recheckSelectedFiles
()
{
if
(!
mSelectionModel
)
return
;
QStringList selectedItems;
for
(
const
QModelIndex& index :
mSelectionModel
->
selectedRows
()) {
const
auto
*item =
dynamic_cast
<ResultItem*>(
mModel
->
itemFromIndex
(index));
while
(item->
parent
())
item =
dynamic_cast
<
const
ResultItem*>(item->
parent
());
const
auto
e = item->
getErrorPathItem
();
const
QString currentFile = e.
file
;
if
(!currentFile.
isEmpty
()) {
QString fileNameWithCheckPath;
const
QFileInfo
curfileInfo
(currentFile);
if
(!curfileInfo.
exists
() && !
mCheckPath
.
isEmpty
() && currentFile.
indexOf
(
mCheckPath
) !=
0
)
fileNameWithCheckPath =
mCheckPath
+
"
/
"
+ currentFile;
else
fileNameWithCheckPath = currentFile;
const
QFileInfo
fileInfo
(fileNameWithCheckPath);
if
(!fileInfo.
exists
()) {
askFileDir
(currentFile);
return
;
}
if
(
Path::isHeader
(currentFile.
toStdString
())) {
if
(!item->
errorItem
->
file0
.
isEmpty
() && !selectedItems.
contains
(item->
errorItem
->
file0
)) {
selectedItems << ((!
mCheckPath
.
isEmpty
() && (item->
errorItem
->
file0
.
indexOf
(
mCheckPath
) !=
0
)) ? (
mCheckPath
+
"
/
"
+ item->
errorItem
->
file0
) : item->
errorItem
->
file0
);
if
(!selectedItems.
contains
(fileNameWithCheckPath))
selectedItems << fileNameWithCheckPath;
}
}
else
if
(!selectedItems.
contains
(fileNameWithCheckPath))
selectedItems << fileNameWithCheckPath;
}
}
emit
checkSelected
(
std::move
(selectedItems));
}
void
ResultsTree::hideAllIdResult
()
{
if
(!
mContextItem
||
mContextItem
->
getType
() == ResultItem::Type::file)
return
;
mHiddenMessageId
.
append
(
mContextItem
->
errorItem
->
errorId
);
refreshTree
();
emit
resultsHidden
(
true
);
}
void
ResultsTree::suppressSelectedIds
()
{
if
(!
mSelectionModel
)
return
;
QSet<QString> selectedIds;
for
(
const
QModelIndex& index :
mSelectionModel
->
selectedRows
()) {
const
auto
*item =
dynamic_cast
<ResultItem*>(
mModel
->
itemFromIndex
(index));
if
(!item || item->
getType
() == ResultItem::Type::file || !item->
errorItem
)
continue
;
selectedIds << item->
errorItem
->
errorId
;
}
//
delete all errors with selected message Ids
for
(
int
i =
0
; i <
mModel
->
rowCount
(); i++) {
QStandardItem *
const
file =
mModel
->
item
(i,
0
);
for
(
int
j =
0
; j < file->
rowCount
();) {
const
auto
*errorItem =
dynamic_cast
<ResultItem*>(file->
child
(j,
0
));
if
(errorItem && errorItem->
errorItem
&& selectedIds.
contains
(errorItem->
errorItem
->
errorId
)) {
file->
removeRow
(j);
}
else
{
j++;
}
}
if
(file->
rowCount
() ==
0
)
mModel
->
removeRow
(file->
row
());
}
if
(!selectedIds.
isEmpty
()) {
refreshTree
();
//
If all visible warnings was suppressed then the file item should be hidden
emit
suppressIds
(selectedIds.
values
());
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL