2 |
7u83 |
1 |
<!-- Crown Copyright (c) 1998 -->
|
|
|
2 |
<HTML>
|
|
|
3 |
<HEAD>
|
|
|
4 |
<TITLE>C Checker Reference Manual: Common Errors</TITLE>
|
|
|
5 |
</HEAD>
|
|
|
6 |
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000FF" VLINK="#400080" ALINK="#FF0000">
|
|
|
7 |
<A NAME=S108>
|
|
|
8 |
<H1>C Checker Reference Manual</H1>
|
|
|
9 |
<H3>January 1998</H3>
|
|
|
10 |
<A HREF="tdfc12.html"><IMG SRC="../images/next.gif" ALT="next section"></A>
|
|
|
11 |
<A HREF="tdfc10.html"><IMG SRC="../images/prev.gif" ALT="previous section"></A>
|
|
|
12 |
<A HREF="tdfc1.html"><IMG SRC="../images/top.gif" ALT="current document"></A>
|
|
|
13 |
<A HREF="../index.html"><IMG SRC="../images/home.gif" ALT="TenDRA home page">
|
|
|
14 |
</A>
|
|
|
15 |
<IMG SRC="../images/no_index.gif" ALT="document index"><P>
|
|
|
16 |
<HR>
|
|
|
17 |
<DL>
|
|
|
18 |
<DT><A HREF="#S109"><B>8.1 </B> - Introduction</A><DD>
|
|
|
19 |
<DT><A HREF="#S110"><B>8.2 </B> - Enumerations controlling switch
|
|
|
20 |
statements</A><DD>
|
|
|
21 |
<DT><A HREF="#S111"><B>8.3 </B> - Incomplete structures and unions</A><DD>
|
|
|
22 |
<DT><A HREF="#S112"><B>8.4 </B> - Variable shadowing</A><DD>
|
|
|
23 |
<DT><A HREF="#S113"><B>8.5 </B> - Floating point equality</A><DD>
|
|
|
24 |
</DL>
|
|
|
25 |
|
|
|
26 |
<HR>
|
|
|
27 |
<H1>8 Common Errors</H1>
|
|
|
28 |
<A NAME=S109>
|
|
|
29 |
<HR><H2>8.1 Introduction</H2>
|
|
|
30 |
Tchk is capable of performing a number of checks for common programming
|
|
|
31 |
mistakes. This chapter describes these checks and controlling pragmas.<P>
|
|
|
32 |
<A NAME=S110>
|
|
|
33 |
<HR><H2>8.2 <A NAME=2>Enumerations controlling switch statements</H2>
|
|
|
34 |
Enumerations are commonly used as control expressions in switch statements.
|
|
|
35 |
When case labels for some of the enumeration constant belonging to
|
|
|
36 |
the enumeration type do not exist and there is no default label, the
|
|
|
37 |
switch statement has no effect for certain possible values of the
|
|
|
38 |
control expression. Checks to detect such switch statements are controlled
|
|
|
39 |
by:<P>
|
|
|
40 |
<PRE>
|
|
|
41 |
#pragma TenDRA enum switch analysis <EM>status</EM>
|
|
|
42 |
</PRE>
|
|
|
43 |
where <EM>status</EM> is <CODE>on</CODE> (raise an error),
|
|
|
44 |
<CODE>warning</CODE> (produce a warning), or <CODE>off</CODE> (the
|
|
|
45 |
default mode when no errors are produced).<P>
|
|
|
46 |
<A NAME=S111>
|
|
|
47 |
<HR><H2>8.3 <A NAME=4>Incomplete structures and unions</H2>
|
|
|
48 |
ISO C allows for structures or unions to be declared but not defined,
|
|
|
49 |
provided they are not used in a context where it is necessary to know
|
|
|
50 |
the complete structure. For example:<P>
|
|
|
51 |
<PRE>
|
|
|
52 |
struct tag *p;
|
|
|
53 |
</PRE>
|
|
|
54 |
is allowed, despite the fact that struct tag is incomplete. The TenDRA
|
|
|
55 |
C checker has an option to detect such incomplete structures or unions,
|
|
|
56 |
controlled by:<P>
|
|
|
57 |
<PRE>
|
|
|
58 |
#pragma TenDRA complete struct/union analysis <EM>status</EM>
|
|
|
59 |
</PRE>
|
|
|
60 |
where <CODE>status</CODE> is <CODE>on</CODE> to give an error as an
|
|
|
61 |
incomplete structure or union is detected, <CODE>warning</CODE> to
|
|
|
62 |
give a warning, or <CODE>off</CODE> to disable the check.<P>
|
|
|
63 |
The check can also be controlled by passing the command-line option
|
|
|
64 |
<CODE>-X:complete_struct=</CODE><CODE>state</CODE> to tchk, where
|
|
|
65 |
<CODE>state</CODE> is <CODE>check</CODE>, <CODE>warn</CODE> or <CODE>dont</CODE>.
|
|
|
66 |
<P>
|
|
|
67 |
The only place where the checker can actually detect that a structure
|
|
|
68 |
or union is incomplete is at the end of the source file. This is because
|
|
|
69 |
it is possible to complete a structure after it has been used. For
|
|
|
70 |
example, in:<P>
|
|
|
71 |
<PRE>
|
|
|
72 |
struct tag *p;
|
|
|
73 |
struct tag {
|
|
|
74 |
int a;
|
|
|
75 |
int b;
|
|
|
76 |
};
|
|
|
77 |
</PRE>
|
|
|
78 |
struct tag is complete despite the fact that it was incomplete in
|
|
|
79 |
the definition of p.<P>
|
|
|
80 |
<A NAME=S112>
|
|
|
81 |
<HR><H2>8.4 <A NAME=6>Variable shadowing</H2>
|
|
|
82 |
It is quite legal in C to have a variable in an inner scope, with
|
|
|
83 |
the same name as a variable in an outer scope. These variables are
|
|
|
84 |
distinct and whilst in the inner scope, the declaration in the outer
|
|
|
85 |
scope is not visible - it is "shadowed" by the local variable
|
|
|
86 |
of the same name. Confusion can arise if this was not what the programmer
|
|
|
87 |
intended. The checker can therefore be configured to detect shadowing
|
|
|
88 |
in three cases: a local variable shadowing a global variable; a local
|
|
|
89 |
variable shadowing a local variable with a wider scope and a local
|
|
|
90 |
variable shadowing a typedef name, by using:<P>
|
|
|
91 |
<PRE>
|
|
|
92 |
#pragma TenDRA variable hiding analysis <EM>status</EM>
|
|
|
93 |
</PRE>
|
|
|
94 |
If <EM>status</EM> is <CODE>on</CODE> an error is raised when a local
|
|
|
95 |
variable that shadows another variable is declared, if
|
|
|
96 |
<CODE>warning</CODE> is used the error is replaced by a warning and
|
|
|
97 |
the <CODE>off</CODE> option restores the default behaviour (shadowing
|
|
|
98 |
is permitted and no errors are produced).<P>
|
|
|
99 |
<A NAME=S113>
|
|
|
100 |
<HR><H2>8.5 <A NAME=8>Floating point equality</H2>
|
|
|
101 |
Due to the rounding errors that occur in the handling of floating
|
|
|
102 |
point values, comparison for equality between two floating point values
|
|
|
103 |
is a hazardous and unpredictable operation. Tests for equality of
|
|
|
104 |
two floating point numbers are controlled by:<P>
|
|
|
105 |
<PRE>
|
|
|
106 |
#pragma TenDRA floating equality <EM>permit</EM>
|
|
|
107 |
</PRE>
|
|
|
108 |
<EM>where</EM><CODE> </CODE><EM>permit</EM><CODE> </CODE><EM>is</EM><CODE>
|
|
|
109 |
allow</CODE>, <CODE>warning</CODE> or <CODE>disallow</CODE>. By default
|
|
|
110 |
the check is switched off.<P>
|
|
|
111 |
<P>
|
|
|
112 |
<!-- FM pgf ignored -->
|
|
|
113 |
<HR>
|
|
|
114 |
<P><I>Part of the <A HREF="../index.html">TenDRA Web</A>.<BR>Crown
|
|
|
115 |
Copyright © 1998.</I></P>
|
|
|
116 |
</BODY>
|
|
|
117 |
</HTML>
|