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
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Cppcheck 1.46</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"></HEAD
><BODY
CLASS="book"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="BOOK"
><A
NAME="AEN1"
></A
><DIV
CLASS="TITLEPAGE"
><H1
CLASS="title"
><A
NAME="AEN2"
>Cppcheck 1.46</A
></H1
><HR></DIV
><DIV
CLASS="TOC"
><DL
><DT
><B
>Table of Contents</B
></DT
><DT
>1. <A
HREF="#AEN5"
>Introduction</A
></DT
><DT
>2. <A
HREF="#AEN19"
>Getting started</A
></DT
><DD
><DL
><DT
>2.1. <A
HREF="#AEN21"
>First test</A
></DT
><DT
>2.2. <A
HREF="#AEN30"
>Checking all files in a folder</A
></DT
><DT
>2.3. <A
HREF="#AEN36"
>Excluding a file or folder from checking</A
></DT
><DT
>2.4. <A
HREF="#AEN44"
>Severities</A
></DT
><DT
>2.5. <A
HREF="#AEN65"
>Enable messages</A
></DT
><DD
><DL
><DT
>2.5.1. <A
HREF="#AEN70"
>Stylistic issues</A
></DT
><DT
>2.5.2. <A
HREF="#AEN84"
>Unused functions</A
></DT
><DT
>2.5.3. <A
HREF="#AEN88"
>Enable all checks</A
></DT
></DL
></DD
><DT
>2.6. <A
HREF="#AEN93"
>Saving results in file</A
></DT
><DT
>2.7. <A
HREF="#AEN97"
>Multithreaded checking</A
></DT
></DL
></DD
><DT
>3. <A
HREF="#AEN101"
>Preprocessor configurations</A
></DT
><DT
>4. <A
HREF="#AEN109"
>XML output</A
></DT
><DT
>5. <A
HREF="#AEN143"
>Reformatting the output</A
></DT
><DT
>6. <A
HREF="#AEN159"
>Suppressions</A
></DT
><DT
>7. <A
HREF="#AEN172"
>Leaks</A
></DT
><DD
><DL
><DT
>7.1. <A
HREF="#AEN175"
>Userdefined allocation/deallocation functions</A
></DT
></DL
></DD
><DT
>8. <A
HREF="#AEN192"
>Exception safety</A
></DT
><DT
>9. <A
HREF="#AEN212"
>Html report</A
></DT
><DT
>10. <A
HREF="#AEN224"
>Graphical user interface</A
></DT
><DD
><DL
><DT
>10.1. <A
HREF="#AEN226"
>Introduction</A
></DT
><DT
>10.2. <A
HREF="#AEN230"
>Check source code</A
></DT
><DT
>10.3. <A
HREF="#AEN234"
>Inspecting results</A
></DT
><DT
>10.4. <A
HREF="#AEN242"
>Settings</A
></DT
><DT
>10.5. <A
HREF="#AEN249"
>Project files</A
></DT
></DL
></DD
></DL
></DIV
><DIV
CLASS="chapter"
><HR><H1
><A
NAME="AEN5"
></A
>Chapter 1. Introduction</H1
><P
>Cppcheck is an analysis tool for C/C++ code. Unlike C/C++ compilers
and many other analysis tools, it doesn't detect syntax errors. Cppcheck
only detects the types of bugs that the compilers normally fail to detect.
The goal is no false positives.</P
><P
>Supported code and platforms:</P
><P
></P
><UL
><LI
><P
>You can check non-standard code that includes various compiler
extensions, inline assembly code, etc.</P
></LI
><LI
><P
>Cppcheck should be compilable by any C++ compiler that handles
the latest C++ standard.</P
></LI
><LI
><P
>Cppcheck should work on any platform that has sufficient cpu and
memory.</P
></LI
></UL
><P
>Accuracy</P
><P
>Please understand that there are limits of Cppcheck. Cppcheck is
rarely wrong about reported errors. But there are many bugs that it
doesn't detect.</P
><P
>You will find more bugs in your software by testing your software
carefully, than by using Cppcheck. You will find more bugs in your
software by instrumenting your software, than by using Cppcheck. But
Cppcheck can still detect some of the bugs that you miss when testing and
instrumenting your software.</P
></DIV
><DIV
CLASS="chapter"
><HR><H1
><A
NAME="AEN19"
></A
>Chapter 2. Getting started</H1
><DIV
CLASS="section"
><H2
CLASS="section"
><A
NAME="AEN21"
>2.1. First test</A
></H2
><P
>Here is a simple code</P
><PRE
CLASS="programlisting"
>int main()
{
char a[10];
a[10] = 0;
return 0;
}</PRE
><P
>If you save that into <TT
CLASS="filename"
>file1.c</TT
> and
execute:</P
><PRE
CLASS="programlisting"
>cppcheck file1.c</PRE
><P
>The output from cppcheck will then be:</P
><PRE
CLASS="programlisting"
>Checking file1.c...
[file1.c:4]: (error) Array 'a[10]' index 10 out of bounds</PRE
></DIV
><DIV
CLASS="section"
><HR><H2
CLASS="section"
><A
NAME="AEN30"
>2.2. Checking all files in a folder</A
></H2
><P
>Normally a program has many sourcefiles. And you want to check
them all. Cppcheck can check all sourcefiles in a directory:</P
><PRE
CLASS="programlisting"
>cppcheck path</PRE
><P
>If "path" is a folder then cppcheck will check all sourcefiles in
this folder.</P
><PRE
CLASS="programlisting"
>Checking path/file1.cpp...
1/2 files checked 50% done
Checking path/file2.cpp...
2/2 files checked 100% done</PRE
></DIV
><DIV
CLASS="section"
><HR><H2
CLASS="section"
><A
NAME="AEN36"
>2.3. Excluding a file or folder from checking</A
></H2
><P
>There is no command to exclude a file or folder from checking. But
you can exclude a file or folder by being more careful when including
files and folders in the checking.</P
><P
>Imagine for example that the folder "src" contain the folders "a",
"b" and "c". To exclude "c" this command can be used:</P
><PRE
CLASS="programlisting"
>cppcheck src/a src/b</PRE
><P
>All files under "src/a" and "src/b" are then checked.</P
><P
>The flag <TT
CLASS="literal"
>--file-list</TT
> might also be
useful.</P
></DIV
><DIV
CLASS="section"
><HR><H2
CLASS="section"
><A
NAME="AEN44"
>2.4. Severities</A
></H2
><P
>The possible severities for messages are:</P
><P
></P
><DIV
CLASS="variablelist"
><DL
><DT
>error</DT
><DD
><P
>used when bugs are found</P
></DD
><DT
>warning</DT
><DD
><P
>suggestions about defensive programming to prevent
bugs</P
></DD
><DT
>style</DT
><DD
><P
>stylistic issues related to code cleanup (unused functions,
redundant code, constness, and such)</P
></DD
><DT
>performance</DT
><DD
><P
>suggestions for making the code faster</P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="section"
><HR><H2
CLASS="section"
><A
NAME="AEN65"
>2.5. Enable messages</A
></H2
><P
>By default only <TT
CLASS="literal"
>error</TT
> messages are shown.
Through the <TT
CLASS="literal"
>--enable</TT
> command more checks can be
enabled.</P
><DIV
CLASS="section"
><HR><H3
CLASS="section"
><A
NAME="AEN70"
>2.5.1. Stylistic issues</A
></H3
><P
>With <TT
CLASS="literal"
>--enable=style</TT
> you enable most
<TT
CLASS="literal"
>warning</TT
>, <TT
CLASS="literal"
>style</TT
> and
<TT
CLASS="literal"
>performance</TT
> messages.</P
><P
>Here is a simple code example:</P
><PRE
CLASS="programlisting"
>void f(int x)
{
int i;
if (x == 0)
{
i = 0;
}
}</PRE
><P
>There are no bugs in that code so Cppcheck won't report anything
by default. To enable the stylistic messages, use the --enable=style
command:</P
><PRE
CLASS="programlisting"
>cppcheck --enable=style file3.c</PRE
><P
>The output from Cppcheck is now:</P
><P
><PRE
CLASS="programlisting"
>Checking file3.c...
[file3.c:3]: (style) Variable 'i' is assigned a value that is never used
[file3.c:3]: (style) The scope of the variable i can be reduced</PRE
></P
></DIV
><DIV
CLASS="section"
><HR><H3
CLASS="section"
><A
NAME="AEN84"
>2.5.2. Unused functions</A
></H3
><P
>This check will try to find unused functions. It is best to use
this when the whole program is checked, so that all usages is seen by
cppcheck.</P
><PRE
CLASS="programlisting"
>cppcheck --enable=unusedFunction path</PRE
></DIV
><DIV
CLASS="section"
><HR><H3
CLASS="section"
><A
NAME="AEN88"
>2.5.3. Enable all checks</A
></H3
><P
>To enable all checks your can use the
<TT
CLASS="literal"
>--enable=all</TT
> flag:</P
><PRE
CLASS="programlisting"
>cppcheck --enable=all path</PRE
></DIV
></DIV
><DIV
CLASS="section"
><HR><H2
CLASS="section"
><A
NAME="AEN93"
>2.6. Saving results in file</A
></H2
><P
>Many times you will want to save the results in a file. You can
use the normal shell redirection for piping error output to a
file.</P
><PRE
CLASS="programlisting"
>cppcheck file1.c 2> err.txt</PRE
></DIV
><DIV
CLASS="section"
><HR><H2
CLASS="section"
><A
NAME="AEN97"
>2.7. Multithreaded checking</A
></H2
><P
>To use 4 threads to check the files in a folder:</P
><PRE
CLASS="programlisting"
>cppcheck -j 4 path</PRE
></DIV
></DIV
><DIV
CLASS="chapter"
><HR><H1
><A
NAME="AEN101"
></A
>Chapter 3. Preprocessor configurations</H1
><P
>By default Cppcheck will check all preprocessor configurations
(except those that have #error in them). This is the recommended
behaviour.</P
><P
>But if you want to manually limit the checking you can do so with
<TT
CLASS="literal"
>-D</TT
>.</P
><P
>Beware that only the macros, which are given here and the macros
defined in source files and known header files are considered. That
excludes all the macros defined in some system header files, which are by
default not examined by cppcheck.</P
><P
>The usage: if you, for example, want to limit the checking so the
only configuration to check should be "DEBUG=1;__cplusplus" then something
like this can be used:</P
><PRE
CLASS="programlisting"
>cppcheck -DDEBUG=1 -D__cplusplus path</PRE
></DIV
><DIV
CLASS="chapter"
><HR><H1
><A
NAME="AEN109"
></A
>Chapter 4. XML output</H1
><P
>Cppcheck can generate the output in XML format.</P
><P
>Use the --xml flag when you execute cppcheck:</P
><PRE
CLASS="programlisting"
>cppcheck --xml file1.cpp</PRE
><P
>The xml format is:</P
><PRE
CLASS="programlisting"
><?xml version="1.0"?>
<results>
<error file="file1.cpp" line="123" id="someError"
severity="error" msg="some error text"/>
</results></PRE
><P
>Attributes:</P
><P
></P
><DIV
CLASS="variablelist"
><DL
><DT
>file</DT
><DD
><P
>filename. Both relative and absolute paths are possible</P
></DD
><DT
>line</DT
><DD
><P
>a number</P
></DD
><DT
>id</DT
><DD
><P
>id of error. These are always valid symbolnames.</P
></DD
><DT
>severity</DT
><DD
><P
>either <TT
CLASS="literal"
>error</TT
> or <TT
CLASS="literal"
>style</TT
>.
<TT
CLASS="literal"
>warning</TT
> and <TT
CLASS="literal"
>performance</TT
> are
saved as <TT
CLASS="literal"
>style</TT
>.</P
></DD
><DT
>msg</DT
><DD
><P
>the error message</P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="chapter"
><HR><H1
><A
NAME="AEN143"
></A
>Chapter 5. Reformatting the output</H1
><P
>If you want to reformat the output so it looks different you can use
templates.</P
><P
>To get Visual Studio compatible output you can use "--template
vs":</P
><PRE
CLASS="programlisting"
>cppcheck --template vs gui/test.cpp</PRE
><P
>This output will look like this:</P
><PRE
CLASS="programlisting"
>Checking gui/test.cpp...
gui/test.cpp(31): error: Memory leak: b
gui/test.cpp(16): error: Mismatching allocation and deallocation: k</PRE
><P
>To get gcc compatible output you can use "--template gcc":</P
><PRE
CLASS="programlisting"
>cppcheck --template gcc gui/test.cpp</PRE
><P
>The output will look like this:</P
><PRE
CLASS="programlisting"
>Checking gui/test.cpp...
gui/test.cpp:31: error: Memory leak: b
gui/test.cpp:16: error: Mismatching allocation and deallocation: k</PRE
><P
>You can write your own pattern (for example a comma-separated
format):</P
><PRE
CLASS="programlisting"
>cppcheck --template "{file},{line},{severity},{id},{message}" gui/test.cpp</PRE
><P
>The output will look like this:</P
><PRE
CLASS="programlisting"
>Checking gui/test.cpp...
gui/test.cpp,31,error,memleak,Memory leak: b
gui/test.cpp,16,error,mismatchAllocDealloc,Mismatching allocation and deallocation: k</PRE
><P
></P
></DIV
><DIV
CLASS="chapter"
><HR><H1
><A
NAME="AEN159"
></A
>Chapter 6. Suppressions</H1
><P
>If you want to filter out certain errors you can suppress these.
First you need to create a suppressions file. The format is:</P
><PRE
CLASS="programlisting"
>[error id]:[filename]:[line]
[error id]:[filename2]
[error id]</PRE
><P
>The <TT
CLASS="literal"
>error id</TT
> is the id that you want to suppress.
The easiest way to get it is to use the <TT
CLASS="literal"
>--xml</TT
> command
line flag. Copy and paste the <TT
CLASS="literal"
>id</TT
> string from the xml
output.</P
><P
>Here is an example:</P
><PRE
CLASS="programlisting"
>memleak:file1.cpp
exceptNew:file1.cpp
uninitvar</PRE
><P
>You can then use the suppressions file:</P
><PRE
CLASS="programlisting"
>cppcheck --suppressions suppressions.txt src/</PRE
><P
></P
></DIV
>
<DIV
CLASS="chapter"
><HR><H1
><A
NAME="AEN192"
></A
>Chapter 7. Exception safety</H1
><P
>Cppcheck has a few checks that ensure that you don't break the basic
guarantee of exception safety. It doesn't have any checks for the strong
guarantee yet.</P
><P
>Example:</P
><PRE
CLASS="programlisting"
>Fred::Fred() : a(new int[20]), b(new int[20])
{
}</PRE
><P
>By default cppcheck will not detect any problems in that
code.</P
><P
>To enable the exception safety checking you can use
<TT
CLASS="literal"
>--enable</TT
>:</P
><PRE
CLASS="programlisting"
>cppcheck --enable=exceptNew --enable=exceptRealloc fred.cpp</PRE
><P
>The output will be:</P
><PRE
CLASS="programlisting"
>[fred.cpp:3]: (style) Upon exception there is memory leak: a</PRE
><P
>If an exception occurs when <TT
CLASS="literal"
>b</TT
> is allocated,
<TT
CLASS="literal"
>a</TT
> will leak.</P
><P
>Here is another example:</P
><PRE
CLASS="programlisting"
>int *p;
int a(int sz)
{
delete [] p;
if (sz <= 0)
throw std::runtime_error("size <= 0");
p = new int[sz];
}</PRE
><P
>Check that with Cppcheck:</P
><PRE
CLASS="programlisting"
>cppcheck --enable=exceptNew --enable=exceptRealloc except2.cpp</PRE
><P
>The output from Cppcheck is:</P
><PRE
CLASS="programlisting"
>[except2.cpp:7]: (error) Throwing exception in invalid state, p points at deallocated memory</PRE
></DIV
><DIV
CLASS="chapter"
><HR><H1
><A
NAME="AEN212"
></A
>Chapter 8. Html report</H1
><P
>You can convert the xml output from cppcheck into a html report.
You'll need python and the pygments module
(<FONT
COLOR="RED"
>http://pygments.org/</FONT
>) for this to work. In the Cppcheck source
tree there is a folder "htmlreport" that contains a script that transforms
a Cppcheck xml file into html output.</P
><P
>This command generates the help screen:</P
><P
><PRE
CLASS="programlisting"
>htmlreport/cppcheck-htmlreport -h</PRE
></P
><P
>The output screen says:</P
><P
><PRE
CLASS="programlisting"
>Usage: cppcheck-htmlreport [options]
Options:
-h, --help show this help message and exit
--file=FILE The cppcheck xml output file to read defects from.
Default is reading from stdin.
--report-dir=REPORT_DIR
The directory where the html report content is written.
--source-dir=SOURCE_DIR
Base directory where source code files can be found.</PRE
></P
><P
>An example usage:</P
><PRE
CLASS="programlisting"
>./cppcheck gui/test.cpp --xml 2> err.xml
htmlreport/cppcheck-htmlreport --file=err.xml --report-dir=test1 --source-dir=.</PRE
></DIV
><DIV
CLASS="chapter"
><HR><H1
><A
NAME="AEN224"
></A
>Chapter 9. Graphical user interface</H1
><DIV
CLASS="section"
><H2
CLASS="section"
><A
NAME="AEN226"
>9.1. Introduction</A
></H2
><P
>A Cppcheck GUI is available.</P
><P
>The main screen is shown immediately when the GUI is
started.</P
></DIV
><DIV
CLASS="section"
><HR><H2
CLASS="section"
><A
NAME="AEN230"
>9.2. Check source code</A
></H2
><P
>Use the <TT
CLASS="literal"
>Check</TT
> menu.</P
></DIV
><DIV
CLASS="section"
><HR><H2
CLASS="section"
><A
NAME="AEN234"
>9.3. Inspecting results</A
></H2
><P
>The results are shown in a list.</P
><P
>You can show/hide certain types of messages through the
<TT
CLASS="literal"
>View</TT
> menu.</P
><P
>Results can be saved to an xml file that can later be opened. See
<TT
CLASS="literal"
>Save results to file</TT
> and <TT
CLASS="literal"
>Open
XML</TT
>.</P
></DIV
><DIV
CLASS="section"
><HR><H2
CLASS="section"
><A
NAME="AEN242"
>9.4. Settings</A
></H2
><P
>The language can be changed at any time by using the
<TT
CLASS="literal"
>Language</TT
> menu.</P
><P
>More settings are available in
<TT
CLASS="literal"
>Edit</TT
>><TT
CLASS="literal"
>Preferences</TT
>.</P
></DIV
><DIV
CLASS="section"
><HR><H2
CLASS="section"
><A
NAME="AEN249"
>9.5. Project files</A
></H2
><P
>The project files are used to store project specific settings.
These settings are:</P
><P
></P
><UL
><LI
><P
>include folders</P
></LI
><LI
><P
>preprocessor defines</P
></LI
></UL
><P
>It isn't recommended to provide the paths to the standard C/C++
headers - Cppcheck has internal knowledge about ANSI C/C++ and it isn't
recommended that this known functionality is redefined. But feel free to
try it.</P
><P
>As you can read in chapter 3 in this manual the default is that
Cppcheck checks all configurations. So only provide preprocessor defines
if you want to limit the checking.</P
></DIV
></DIV
></DIV
></BODY
></HTML
>
|