xref: /csrg-svn/old/cpp/README (revision 16236)
17981Srrh#
2*16236Smckusick# @(#)README 1.3 03/24/84
37981Srrh#
47983SrrhAugust 30, 1982
57983SrrhFixed by Kurt Shoens, UCB
67983SrrhIf the "#line n name" occurs, then all future references
77983Srrhto the current file are generated in terms of "name", instead
87983Srrhof the name of file given to cpp in its command argument
97983Srrh
107981SrrhAugust 25, 1978
117981Srrh
127981SrrhFiles in this directory form the C preprocessor, which handles '#include'
137981Srrhfiles and macro definition and expansion for the C compiler.
147981SrrhThis new version was written by John F. Reiser and is from 5 to 12
157981Srrhtimes faster (on UNIX systems) than the old.
167981Srrh
177981SrrhTo create the executable file 'cpp' in the current directory:
187981Srrh	make
197981Srrh
207981SrrhTo install the preprocessor 'cpp' so it will be used by the C compiler:
217981Srrh	: safety first: backup the existing version
227981Srrh	cp /lib/cpp /lib/ocpp
237981Srrh	: install the new version
247981Srrh	cp cpp /lib/cpp
257981Srrh
267981SrrhInvocation
27*16236Smckusick	cpp [-CEPRM] [-Dname] ... [-Dname=def] ... [-Idirectory] ...
287981Srrh		[-Uname] ... [<infile>] [<outfile>]
297981Srrh
307981Srrh	If there are two non-flag arguments then the first is the name of the
317981Srrh	input file and the second is the name of the output file.  If there is
327981Srrh	one non-flag argument then it is the name of the input file and the
337981Srrh	output is written on the standard output.  If there are no non-flag
347981Srrh	arguments then the input is taken from the standard input and the output
357981Srrh	is written on the standard output.  Flag arguments are:
367981Srrh
377981Srrh		-C	retain comments in output
387981Srrh		-Dname	define name as "1"
397981Srrh		-Dname=def	define name as def
407981Srrh		-E	ignored
417981Srrh		-Idirectory	add directory to search list for #include files
42*16236Smckusick		-M	generate Makefile dependencies (-C and -M ignored)
437981Srrh		-P	don't insert lines "# 12 \"foo.c\"" into output
447981Srrh		-R	allow recursive macros
457981Srrh		-Uname	undefine name
467981Srrh
477981SrrhDocumentation clarifications:
487981Srrh	Symbols defined on the command line by "-Dfoo" are defined as "1",
497981Srrh		i.e., as if they had been defined by "#define foo 1" or "-Dfoo=1".
507981Srrh	The directory search order for #include files is
517981Srrh		1) the directory of the file which contains the #include request
527981Srrh		   (e.g. #include is relative to the file being scanned when
537981Srrh		   the request is made)
547981Srrh		2) the directories specified by -I, in left-to-right order
557981Srrh		3) the standard directory(s) (which for UNIX is /usr/include)
567981Srrh	An unescaped linefeed (the single character "\n") terminates a
577981Srrh		character constant or quoted string.
587981Srrh	An escaped linefeed (the two-character sequence "\\\n") may be
597981Srrh		used in the body of a '#define' statement to continue
607981Srrh		the definition onto the next line.  The escaped linefeed is
617981Srrh		not included in the macro body.
627981Srrh	Comments are uniformly removed (except if the argument -C is specified).
637981Srrh		They are also ignored, except that a comment terminates a token.
647981Srrh		Thus "foo/* la di da */bar" may expand 'foo' and 'bar' but
657981Srrh		will never expand 'foobar'.  If neither 'foo' nor 'bar' is a
667981Srrh		macro then the output is "foobar", even if 'foobar'
677981Srrh		is defined as something else.  The file
687981Srrh			#define foo(a,b)b/**/a
697981Srrh			foo(1,2)
707981Srrh		produces "21" because the comment causes a break which enables
717981Srrh		the recognition of 'b' and 'a' as formals in the string "b/**/a".
727981Srrh	Macro formal parameters are recognized in '#define' bodies even inside
737981Srrh		character constants and quoted strings.  The output from
747981Srrh			#define foo(a) '\a'
757981Srrh			foo(bar)
767981Srrh		is the seven characters " '\\bar'".  Macro names are not recognized
777981Srrh		inside character constants or quoted strings during the regular scan.
787981Srrh		Thus
797981Srrh			#define foo bar
807981Srrh			printf("foo");
817981Srrh		does not expand 'foo' in the second line, because it is inside
827981Srrh		a quoted string which is not part of a '#define' macro definition.
837981Srrh	Macros are not expanded while processing a '#define' or '#undef'.
847981Srrh		Thus
857981Srrh			#define foo bletch
867981Srrh			#define bar foo
877981Srrh			#undef foo
887981Srrh			bar
897981Srrh		produces "foo".  The token appearing immediately after a
907981Srrh		'#ifdef' or '#ifndef' is not expanded (of course!).
917981Srrh	Macros are not expanded during the scan which determines the actual
927981Srrh		parameters to another macro call.  Thus
937981Srrh			#define foo(a,b)b a
947981Srrh			#define bar hi
957981Srrh			foo(bar,
967981Srrh			#define bar bye
977981Srrh			)
987981Srrh		produces " bye" (and warns about the redefinition of 'bar').
997981Srrh
1007981SrrhThere are some differences between the new and the old preprocessor.
1017981SrrhBugs fixed:
1027981Srrh	"1.e4" is recognized as a floating-point number, rather than as an
1037981Srrh		opportunity to expand the possible macro name "e4".
1047981Srrh	Any kind and amount of white space (space, tab, linefeed, vertical tab,
1057981Srrh		formfeed, carriage return) is allowed between a macro name and
1067981Srrh		the left parenthesis which introduces its actual parameters.
1077981Srrh	The comma operator is legal in preprocessor '#if' statements.
1087981Srrh	Macros with parameters are legal in preprocessor '#if' statements.
1097981Srrh	Single-character character constants are legal in preprocessor '#if' statements.
1107981Srrh	Linefeeds are put out in the proper place when a multiline comment
1117981Srrh		is not passed through to the output.
1127981Srrh	The following example expands to "# # #" :
1137981Srrh		#define foo #
1147981Srrh		foo foo foo
1157981Srrh	If the -R flag is not specified then the invocation of some recursive
1167981Srrh		macros is trapped and the recursion forcibly terminated with an
1177981Srrh		error message.  The recursions that are trapped are the ones
1187981Srrh		in which the nesting level is non-decreasing from some point on.
1197981Srrh		In particular,
1207981Srrh			#define a a
1217981Srrh			a
1227981Srrh		will be detected.  (Use "#undef a" if that is what you want.)
1237981Srrh		The recursion
1247981Srrh			#define a c b
1257981Srrh			#define b c a
1267981Srrh			#define c foo
1277981Srrh			a
1287981Srrh		will not be detected because the nesting level decreases after
1297981Srrh		each expansion of "c".
1307981Srrh	The -R flag specifically allows recursive macros and recursion will
1317981Srrh		be strictly obeyed (to the extent that space is available).
1327981Srrh		Assuming that -R is specified:
1337981Srrh			#define a a
1347981Srrh			a
1357981Srrh		causes an infinite loop with very little output.  The tail recursion
1367981Srrh			#define a <b
1377981Srrh			#define b >a
1387981Srrh			a
1397981Srrh		causes the string "<>" to be output infinitely many times.  The
1407981Srrh		non-tail recursion
1417981Srrh			#define a b>
1427981Srrh			#define b a<
1437981Srrh			a
1447981Srrh		complains "too much pushback", dumps the pushback, and continues
1457981Srrh		(again, infinitely).
1467981Srrh
1477981SrrhStylistic choice:
1487981Srrh	Nothing (not even linefeeds) is output while a false '#if', '#ifdef',
1497981Srrh		or '#ifndef' is in effect.  Thus when all conditions become true
1507981Srrh		a line of the form "# 12345 \"foo.c\"" is output (unless -P).
1517981Srrh	Error and warning messages always appear on standard error (file
1527981Srrh		descriptor 2).
1537981Srrh	Mismatch between the number of formals and actuals in a macro call
1547981Srrh		produces only a warning, and not an error.  Excess actuals
1557981Srrh		are ignored; missing actuals are turned into null strings.
1567981Srrh
1577981SrrhIncompatibility:
1587981Srrh	The virgule '/' in "a=/*b" is interpreted as the first character of
1597981Srrh		the pair "/*" which introduces a comment, rather than as the
1607981Srrh		second character of the divide-and-replace operator "=/".
1617981Srrh		This incompatibility reflects the recent change in the C language
1627981Srrh		which made "a/=*b" the legal way to write such a statement
1637981Srrh		if the meaning "a=a/ *b" is intended.
164