GCC Extensions. NXP CodeWarrior Development Studio
Annonce
Annonce
Chapter 15 C Compiler
15.3 GCC Extensions
The CodeWarrior compiler accepts many of the extensions to the C language that the
GCC (Gnu Compiler Collection) tools allow. Source code that uses these extensions does not conform to the ISO/IEC 9899-1990 C ("C90") standard.
•
•
Initializing Automatic Arrays and Structures
•
•
•
•
•
Void and Function Pointer Arithmetic
•
The __builtin_constant_p() Operator
•
Forward Declarations of Static Arrays
•
Omitted Operands in Conditional Expressions
•
The __builtin_expect() Operator
•
•
•
15.3.1 Controlling GCC Extensions
Table 15-5 shows how to turn GCC extensions on or off.
Table 15-5. Controlling GCC extensions to the C language
To control this option from here...
CodeWarrior IDE source code command line
use this setting
Enable GCC Extensions in the C/C++ Language Settings panel
#pragma gcc_extensions
-gcc_extensions
15.3.2 Initializing Automatic Arrays and Structures
CodeWarrior Development Studio for Advanced Packet Processing Build Tools Reference Manual, Rev.
10.2, 01/2016
Freescale Semiconductor, Inc.
293
GCC Extensions
When the GCC extensions setting is on, array and structure variables that are local to a function and have the automatic storage class may be initialized with values that do not
need to be constant. Listing 15-16 on page 294 shows an example.
Listing 15-16. Initializing arrays and structures with non-constant values
void f(int i)
{
int j = i * 10; /* Always OK. */
/* These initializations are only accepted when GCC extensions
* are on. */
struct { int x, y; } s = { i + 1, i + 2 };
int a[2] = { i, i + 2 };
}
15.3.3 The sizeof() Operator
When the GCC extensions setting is on, the sizeof()
operator computes the size of function and void types. In both cases, the sizeof()
operator evaluates to 1. The ISO/IEC
9899-1990 C Standard ("C90") does not specify the size of the void
type and functions.
Listing 15-17 on page 294 shows an example.
Listing 15-17. Using the sizeof() operator with void and function types
int f(int a)
{
return a * 10;
} void g(void)
{
size_t voidsize = sizeof(void); /* voidsize contains 1 */
size_t funcsize = sizeof(f); /* funcsize contains 1 */
}
15.3.4 Statements in Expressions
When the GCC extensions setting is on, expressions in function bodies may contain statements and definitions. To use a statement or declaration in an expression, enclose it within braces. The last item in the brace-enclosed expression gives the expression its value.
Listing 15-18 on page 294 shows an example.
Listing 15-18. Using statements and definitions in expressions
CodeWarrior Development Studio for Advanced Packet Processing Build Tools Reference Manual, Rev.
10.2, 01/2016
294 Freescale Semiconductor, Inc.
Chapter 15 C Compiler
#define POW2(n) ({ int i,r; for(r=1,i=n; i>0; --i) r *= 2; r;}) int main()
{
return POW2(4);
}
15.3.5 Redefining Macros
When the GCC extensions setting is on, macros may be redefined with the
#define directive without first undefining them with the
#undef
directive. Listing 15-19 on page
Listing 15-19. Redefining a macro without undefining first
#define SOCK_MAXCOLOR 100
#undef SOCK_MAXCOLOR
#define SOCK_MAXCOLOR 200 /* OK: this macro is previously undefined. */
#define SOCK_MAXCOLOR 300
15.3.6 The typeof() Operator
When the GCC extensions setting is on, the compiler recognizes the typeof()
operator.
This compile-time operator returns the type of an expression. You may use the value returned by this operator in any statement or expression where the compiler expects you to specify a type. The compiler evaluates this operator at compile time. The
__typeof()__ operator is the same as this operator.
shows an example.
Listing 15-20. Using the typeof() operator
int *ip;
/* Variables iptr and jptr have the same type. */ typeof(ip) iptr; int *jptr;
/* Variables i and j have the same type. */ typeof(*ip) i; int j;
CodeWarrior Development Studio for Advanced Packet Processing Build Tools Reference Manual, Rev.
10.2, 01/2016
Freescale Semiconductor, Inc.
295
GCC Extensions
15.3.7 Void and Function Pointer Arithmetic
The ISO/IEC 9899-1990 C Standard does not accept arithmetic expressions that use pointers to void
or functions. With GCC extensions on, the compiler accepts arithmetic manipulation of pointers to void
and functions.
15.3.8 The __builtin_constant_p() Operator
When the GCC extensions setting is on, the compiler recognizes the
__builtin_constant_p() operator. This compile-time operator takes a single argument and returns 1 if the argument is a constant expression or 0 if it is not.
15.3.9 Forward Declarations of Static Arrays
When the GCC extensions setting is on, the compiler will not issue an error when you declare a static array without specifying the number of elements in the array if you later declare the array completely.
Listing 15-21 on page 296 shows an example.
Listing 15-21. Forward declaration of an empty array
static int a[]; /* Allowed only when GCC extensions are on. */
/* ... */ static int a[10]; /* Complete declaration. */
15.3.10 Omitted Operands in Conditional Expressions
When the GCC extensions setting is on, you may skip the second expression in a conditional expression. The default value for this expression is the first expression.
Listing 15-22 on page 296 shows an example.
Listing 15-22. Using the shorter form of the conditional expression
void f(int i, int j)
{
int a = i ? i : j;
int b = i ?: j; /* Equivalent to int b = i ? i : j; */
/* Variables a and b are both assigned the same value. */
CodeWarrior Development Studio for Advanced Packet Processing Build Tools Reference Manual, Rev.
10.2, 01/2016
296 Freescale Semiconductor, Inc.
Chapter 15 C Compiler
}
15.3.11 The __builtin_expect() Operator
When the GCC extensions setting is on, the compiler recognizes the
__builtin_expect() operator. Use this compile-time operator in an if
or while
statement to specify to the compiler how to generate instructions for branch prediction.
This compile-time operator takes two arguments:
• the first argument must be an integral expression
• the second argument must be a literal value
The second argument is the most likely result of the first argument.
Listing 15-23. Example for __builtin_expect() operator
void search(int *array, int size, int key)
{
int i;
for (i = 0; i < size; ++i)
{
/* We expect to find the key rarely. */
if (__builtin_expect(array[i] == key, 0))
{
rescue(i);
}
}
}
15.3.12 Void Return Statements
When the GCC extensions setting is on, the compiler allows you to place expressions of type void
in a return
statement. Listing 15-24 on page 297 shows an example.
Listing 15-24. Returning void
void f(int a)
{
/* ... */
return; /* Always OK. */
} void g(int b)
CodeWarrior Development Studio for Advanced Packet Processing Build Tools Reference Manual, Rev.
10.2, 01/2016
Freescale Semiconductor, Inc.
297
GCC Extensions
{
/* ... */
return f(b); /* Allowed when GCC extensions are on. */
}
15.3.13 Minimum and Maximum Operators
When the GCC extensions setting is on, the compiler recognizes built-in minimum (
<?
) and maximum (
>?
) operators.
Listing 15-25. Example of minimum and maximum operators
int a = 1 <? 2; // 1 is assigned to a.
int b = 1 >? 2; // 2 is assigned to b.
15.3.14 Local Labels
When the GCC extensions setting is on, the compiler allows labels limited to a block's scope. A label declared with the
__label__
keyword is visible only within the scope of its enclosing block.
Listing 15-26 on page 298 shows an example.
Listing 15-26. Example of using local labels
void f(int i)
{
if (i >= 0)
{
__label__ again; /* First again. */
if (--i > 0)
goto again; /* Jumps to first again. */
}
else
{
__label__ again; /* Second again. */
if (++i < 0)
goto again; /* Jumps to second again. */
}
}
CodeWarrior Development Studio for Advanced Packet Processing Build Tools Reference Manual, Rev.
10.2, 01/2016
298 Freescale Semiconductor, Inc.
Download
Annonce