aa26a3f648
From-SVN: r81001 |
||
---|---|---|
.. | ||
ada | ||
config | ||
cp | ||
doc | ||
f | ||
fixinc | ||
ginclude | ||
java | ||
objc | ||
po | ||
testsuite | ||
treelang | ||
.cvsignore | ||
ABOUT-GCC-NLS | ||
aclocal.m4 | ||
alias.c | ||
alloc-pool.c | ||
alloc-pool.h | ||
attribs.c | ||
basic-block.h | ||
bb-reorder.c | ||
bitmap.c | ||
bitmap.h | ||
bt-load.c | ||
builtin-attrs.def | ||
builtin-types.def | ||
builtins.c | ||
builtins.def | ||
c-aux-info.c | ||
c-common.c | ||
c-common.def | ||
c-common.h | ||
c-config-lang.in | ||
c-convert.c | ||
c-cppbuiltin.c | ||
c-decl.c | ||
c-dump.c | ||
c-errors.c | ||
c-format.c | ||
c-incpath.c | ||
c-incpath.h | ||
c-lang.c | ||
c-lex.c | ||
c-objc-common.c | ||
c-opts.c | ||
c-parse.in | ||
c-pch.c | ||
c-ppoutput.c | ||
c-pragma.c | ||
c-pragma.h | ||
c-pretty-print.c | ||
c-pretty-print.h | ||
c-semantics.c | ||
c-tree.h | ||
c-typeck.c | ||
c.opt | ||
caller-save.c | ||
calls.c | ||
cfg.c | ||
cfganal.c | ||
cfgbuild.c | ||
cfgcleanup.c | ||
cfghooks.c | ||
cfghooks.h | ||
cfglayout.c | ||
cfglayout.h | ||
cfgloop.c | ||
cfgloop.h | ||
cfgloopanal.c | ||
cfgloopmanip.c | ||
cfgrtl.c | ||
cgraph.c | ||
cgraph.h | ||
cgraphunit.c | ||
ChangeLog | ||
ChangeLog.0 | ||
ChangeLog.1 | ||
ChangeLog.2 | ||
ChangeLog.3 | ||
ChangeLog.4 | ||
ChangeLog.5 | ||
ChangeLog.6 | ||
ChangeLog.7 | ||
ChangeLog.8 | ||
ChangeLog.9 | ||
ChangeLog.10 | ||
ChangeLog.lib | ||
collect2.c | ||
collect2.h | ||
combine.c | ||
common.opt | ||
conditions.h | ||
config.build | ||
config.gcc | ||
config.host | ||
config.in | ||
configure | ||
configure.ac | ||
conflict.c | ||
convert.c | ||
convert.h | ||
COPYING | ||
COPYING.LIB | ||
coretypes.h | ||
coverage.c | ||
coverage.h | ||
cppcharset.c | ||
cppdefault.c | ||
cppdefault.h | ||
cpperror.c | ||
cppexp.c | ||
cppfiles.c | ||
cpphash.c | ||
cpphash.h | ||
cppinit.c | ||
cpplex.c | ||
cpplib.c | ||
cpplib.h | ||
cppmacro.c | ||
cpppch.c | ||
cppspec.c | ||
cpptrad.c | ||
cppucnid.h | ||
cppucnid.pl | ||
cppucnid.tab | ||
crtstuff.c | ||
cse.c | ||
cselib.c | ||
cselib.h | ||
cstamp-h.in | ||
dbxout.c | ||
dbxout.h | ||
debug.c | ||
debug.h | ||
defaults.h | ||
df.c | ||
df.h | ||
diagnostic.c | ||
diagnostic.def | ||
diagnostic.h | ||
dojump.c | ||
dominance.c | ||
dummy-conditions.c | ||
dwarf2.h | ||
dwarf2asm.c | ||
dwarf2asm.h | ||
dwarf2out.c | ||
dwarf2out.h | ||
dwarf.h | ||
emit-rtl.c | ||
errors.c | ||
errors.h | ||
et-forest.c | ||
et-forest.h | ||
except.c | ||
except.h | ||
explow.c | ||
expmed.c | ||
expr.c | ||
expr.h | ||
final.c | ||
fix-header.c | ||
fixproto | ||
flags.h | ||
flow.c | ||
fold-const.c | ||
fp-test.c | ||
FSFChangeLog | ||
FSFChangeLog.10 | ||
FSFChangeLog.11 | ||
function.c | ||
function.h | ||
gbl-ctors.h | ||
gcc.c | ||
gcc.h | ||
gccbug.in | ||
gccspec.c | ||
gcov-dump.c | ||
gcov-io.c | ||
gcov-io.h | ||
gcov-iov.c | ||
gcov.c | ||
gcse.c | ||
gdbinit.in | ||
gen-protos.c | ||
genattr.c | ||
genattrtab.c | ||
genattrtab.h | ||
genautomata.c | ||
gencheck.c | ||
gencodes.c | ||
genconditions.c | ||
genconfig.c | ||
genconstants.c | ||
genemit.c | ||
genextract.c | ||
genflags.c | ||
gengenrtl.c | ||
gengtype-lex.l | ||
gengtype-yacc.y | ||
gengtype.c | ||
gengtype.h | ||
genmodes.c | ||
genmultilib | ||
genopinit.c | ||
genoutput.c | ||
genpeep.c | ||
genpreds.c | ||
genrecog.c | ||
gensupport.c | ||
gensupport.h | ||
ggc-common.c | ||
ggc-none.c | ||
ggc-page.c | ||
ggc-zone.c | ||
ggc.h | ||
glimits.h | ||
global.c | ||
gmon.c | ||
graph.c | ||
graph.h | ||
gstab.h | ||
gsyms.h | ||
gsyslimits.h | ||
gthr-aix.h | ||
gthr-dce.h | ||
gthr-gnat.c | ||
gthr-gnat.h | ||
gthr-posix.c | ||
gthr-posix.h | ||
gthr-rtems.h | ||
gthr-single.h | ||
gthr-solaris.h | ||
gthr-vxworks.h | ||
gthr-win32.h | ||
gthr.h | ||
haifa-sched.c | ||
hard-reg-set.h | ||
hashtable.c | ||
hashtable.h | ||
hooks.c | ||
hooks.h | ||
host-default.c | ||
hosthooks-def.h | ||
hosthooks.h | ||
hwint.h | ||
ifcvt.c | ||
input.h | ||
insn-addr.h | ||
integrate.c | ||
integrate.h | ||
intl.c | ||
intl.h | ||
jump.c | ||
langhooks-def.h | ||
langhooks.c | ||
langhooks.h | ||
LANGUAGES | ||
lcm.c | ||
libada-mk.in | ||
libfuncs.h | ||
libgcc2.c | ||
libgcc2.h | ||
libgcc-darwin.ver | ||
libgcc-std.ver | ||
libgcov.c | ||
limitx.h | ||
limity.h | ||
line-map.c | ||
line-map.h | ||
lists.c | ||
local-alloc.c | ||
longlong.h | ||
loop-doloop.c | ||
loop-init.c | ||
loop-iv.c | ||
loop-unroll.c | ||
loop-unswitch.c | ||
loop.c | ||
loop.h | ||
machmode.def | ||
machmode.h | ||
main.c | ||
Makefile.in | ||
mips-tdump.c | ||
mips-tfile.c | ||
mkconfig.sh | ||
mkdeps.c | ||
mkdeps.h | ||
mkheaders.in | ||
mklibgcc.in | ||
mkmap-flat.awk | ||
mkmap-symver.awk | ||
mode-classes.def | ||
ONEWS | ||
optabs.c | ||
optabs.h | ||
opts.c | ||
opts.h | ||
opts.sh | ||
output.h | ||
params.c | ||
params.def | ||
params.h | ||
passes.c | ||
postreload.c | ||
predict.c | ||
predict.def | ||
predict.h | ||
prefix.c | ||
prefix.h | ||
pretty-print.c | ||
pretty-print.h | ||
print-rtl.c | ||
print-tree.c | ||
profile.c | ||
protoize.c | ||
ra-build.c | ||
ra-colorize.c | ||
ra-debug.c | ||
ra-rewrite.c | ||
ra.c | ||
ra.h | ||
read-rtl.c | ||
README-fixinc | ||
README.Portability | ||
real.c | ||
real.h | ||
recog.c | ||
recog.h | ||
reg-stack.c | ||
regclass.c | ||
regmove.c | ||
regrename.c | ||
regs.h | ||
reload1.c | ||
reload.c | ||
reload.h | ||
reorg.c | ||
resource.c | ||
resource.h | ||
rtl-error.c | ||
rtl.c | ||
rtl.def | ||
rtl.h | ||
rtlanal.c | ||
sbitmap.c | ||
sbitmap.h | ||
scan-decls.c | ||
scan-types.sh | ||
scan.c | ||
scan.h | ||
sched-deps.c | ||
sched-ebb.c | ||
sched-int.h | ||
sched-rgn.c | ||
sched-vis.c | ||
sdbout.c | ||
sdbout.h | ||
SERVICE | ||
sibcall.c | ||
simplify-rtx.c | ||
sort-protos | ||
sreal.c | ||
sreal.h | ||
stab.def | ||
stack.h | ||
statistics.h | ||
stmt.c | ||
stor-layout.c | ||
stringpool.c | ||
stub-objc.c | ||
sys-protos.h | ||
sys-types.h | ||
system.h | ||
target-def.h | ||
target.h | ||
targhooks.c | ||
targhooks.h | ||
timevar.c | ||
timevar.def | ||
timevar.h | ||
tlink.c | ||
toplev.c | ||
toplev.h | ||
tracer.c | ||
tree-dump.c | ||
tree-dump.h | ||
tree-inline.c | ||
tree-inline.h | ||
tree-optimize.c | ||
tree.c | ||
tree.def | ||
tree.h | ||
tsystem.h | ||
typeclass.h | ||
unroll.c | ||
unwind-c.c | ||
unwind-dw2-fde-darwin.c | ||
unwind-dw2-fde-glibc.c | ||
unwind-dw2-fde.c | ||
unwind-dw2-fde.h | ||
unwind-dw2.c | ||
unwind-pe.h | ||
unwind-sjlj.c | ||
unwind.h | ||
unwind.inc | ||
value-prof.c | ||
value-prof.h | ||
var-tracking.c | ||
varasm.c | ||
varray.c | ||
varray.h | ||
version.c | ||
version.h | ||
vmsdbg.h | ||
vmsdbgout.c | ||
web.c | ||
xcoff.h | ||
xcoffout.c | ||
xcoffout.h |
Copyright (C) 2000, 2003 Free Software Foundation, Inc. This file is intended to contain a few notes about writing C code within GCC so that it compiles without error on the full range of compilers GCC needs to be able to compile on. The problem is that many ISO-standard constructs are not accepted by either old or buggy compilers, and we keep getting bitten by them. This knowledge until know has been sparsely spread around, so I thought I'd collect it in one useful place. Please add and correct any problems as you come across them. I'm going to start from a base of the ISO C90 standard, since that is probably what most people code to naturally. Obviously using constructs introduced after that is not a good idea. For the complete coding style conventions used in GCC, please read http://gcc.gnu.org/codingconventions.html String literals --------------- Irix6 "cc -n32" and OSF4 "cc" have problems with constant string initializers with parens around it, e.g. const char string[] = ("A string"); This is unfortunate since this is what the GNU gettext macro N_ produces. You need to find a different way to code it. Some compilers like MSVC++ have fairly low limits on the maximum length of a string literal; 509 is the lowest we've come across. You may need to break up a long printf statement into many smaller ones. Empty macro arguments --------------------- ISO C (6.8.3 in the 1990 standard) specifies the following: If (before argument substitution) any argument consists of no preprocessing tokens, the behavior is undefined. This was relaxed by ISO C99, but some older compilers emit an error, so code like #define foo(x, y) x y foo (bar, ) needs to be coded in some other way. free and realloc ---------------- Some implementations crash upon attempts to free or realloc the null pointer. Thus if mem might be null, you need to write if (mem) free (mem); Trigraphs --------- You weren't going to use them anyway, but some otherwise ISO C compliant compilers do not accept trigraphs. Suffixes on Integer Constants ----------------------------- You should never use a 'l' suffix on integer constants ('L' is fine), since it can easily be confused with the number '1'. Common Coding Pitfalls ====================== errno ----- errno might be declared as a macro. Implicit int ------------ In C, the 'int' keyword can often be omitted from type declarations. For instance, you can write unsigned variable; as shorthand for unsigned int variable; There are several places where this can cause trouble. First, suppose 'variable' is a long; then you might think (unsigned) variable would convert it to unsigned long. It does not. It converts to unsigned int. This mostly causes problems on 64-bit platforms, where long and int are not the same size. Second, if you write a function definition with no return type at all: operate (int a, int b) { ... } that function is expected to return int, *not* void. GCC will warn about this. Implicit function declarations always have return type int. So if you correct the above definition to void operate (int a, int b) ... but operate() is called above its definition, you will get an error about a "type mismatch with previous implicit declaration". The cure is to prototype all functions at the top of the file, or in an appropriate header. Char vs unsigned char vs int ---------------------------- In C, unqualified 'char' may be either signed or unsigned; it is the implementation's choice. When you are processing 7-bit ASCII, it does not matter. But when your program must handle arbitrary binary data, or fully 8-bit character sets, you have a problem. The most obvious issue is if you have a look-up table indexed by characters. For instance, the character '\341' in ISO Latin 1 is SMALL LETTER A WITH ACUTE ACCENT. In the proper locale, isalpha('\341') will be true. But if you read '\341' from a file and store it in a plain char, isalpha(c) may look up character 225, or it may look up character -31. And the ctype table has no entry at offset -31, so your program will crash. (If you're lucky.) It is wise to use unsigned char everywhere you possibly can. This avoids all these problems. Unfortunately, the routines in <string.h> take plain char arguments, so you have to remember to cast them back and forth - or avoid the use of strxxx() functions, which is probably a good idea anyway. Another common mistake is to use either char or unsigned char to receive the result of getc() or related stdio functions. They may return EOF, which is outside the range of values representable by char. If you use char, some legal character value may be confused with EOF, such as '\377' (SMALL LETTER Y WITH UMLAUT, in Latin-1). The correct choice is int. A more subtle version of the same mistake might look like this: unsigned char pushback[NPUSHBACK]; int pbidx; #define unget(c) (assert(pbidx < NPUSHBACK), pushback[pbidx++] = (c)) #define get(c) (pbidx ? pushback[--pbidx] : getchar()) ... unget(EOF); which will mysteriously turn a pushed-back EOF into a SMALL LETTER Y WITH UMLAUT. Other common pitfalls --------------------- o Expecting 'plain' char to be either sign or unsigned extending. o Shifting an item by a negative amount or by greater than or equal to the number of bits in a type (expecting shifts by 32 to be sensible has caused quite a number of bugs at least in the early days). o Expecting ints shifted right to be sign extended. o Modifying the same value twice within one sequence point. o Host vs. target floating point representation, including emitting NaNs and Infinities in a form that the assembler handles. o qsort being an unstable sort function (unstable in the sense that multiple items that sort the same may be sorted in different orders by different qsort functions). o Passing incorrect types to fprintf and friends. o Adding a function declaration for a module declared in another file to a .c file instead of to a .h file.