C Checker Reference Manual

January 1998

next section previous section current document TenDRA home page document index


8.1 - Introduction
8.2 - Enumerations controlling switch statements
8.3 - Incomplete structures and unions
8.4 - Variable shadowing
8.5 - Floating point equality

8 Common Errors


8.1 Introduction

Tchk is capable of performing a number of checks for common programming mistakes. This chapter describes these checks and controlling pragmas.


8.2 Enumerations controlling switch statements

Enumerations are commonly used as control expressions in switch statements. When case labels for some of the enumeration constant belonging to the enumeration type do not exist and there is no default label, the switch statement has no effect for certain possible values of the control expression. Checks to detect such switch statements are controlled by:

	#pragma TenDRA enum switch analysis status
where status is on (raise an error), warning (produce a warning), or off (the default mode when no errors are produced).


8.3 Incomplete structures and unions

ISO C allows for structures or unions to be declared but not defined, provided they are not used in a context where it is necessary to know the complete structure. For example:

	struct tag *p;
is allowed, despite the fact that struct tag is incomplete. The TenDRA C checker has an option to detect such incomplete structures or unions, controlled by:

	#pragma TenDRA complete struct/union analysis status
where status is on to give an error as an incomplete structure or union is detected, warning to give a warning, or off to disable the check.

The check can also be controlled by passing the command-line option -X:complete_struct=state to tchk, where state is check, warn or dont.

The only place where the checker can actually detect that a structure or union is incomplete is at the end of the source file. This is because it is possible to complete a structure after it has been used. For example, in:

	struct tag *p;
	struct tag {
		int a;
		int b;
	};
struct tag is complete despite the fact that it was incomplete in the definition of p.


8.4 Variable shadowing

It is quite legal in C to have a variable in an inner scope, with the same name as a variable in an outer scope. These variables are distinct and whilst in the inner scope, the declaration in the outer scope is not visible - it is "shadowed" by the local variable of the same name. Confusion can arise if this was not what the programmer intended. The checker can therefore be configured to detect shadowing in three cases: a local variable shadowing a global variable; a local variable shadowing a local variable with a wider scope and a local variable shadowing a typedef name, by using:

	#pragma TenDRA variable hiding analysis status
If status is on an error is raised when a local variable that shadows another variable is declared, if warning is used the error is replaced by a warning and the off option restores the default behaviour (shadowing is permitted and no errors are produced).


8.5 Floating point equality

Due to the rounding errors that occur in the handling of floating point values, comparison for equality between two floating point values is a hazardous and unpredictable operation. Tests for equality of two floating point numbers are controlled by:

	#pragma TenDRA floating equality permit
where permit is allow, warning or disallow. By default the check is switched off.


Part of the TenDRA Web.
Crown Copyright © 1998.