OSDN Git Service

libunwind related patch from David Mosberger
[pf3gnuchains/gcc-fork.git] / gcc / doc / cppinternals.texi
index ea8b13e..3f3d9af 100644 (file)
@@ -16,7 +16,7 @@
 @ifinfo
 This file documents the internals of the GNU C Preprocessor.
 
-Copyright 2000, 2001 Free Software Foundation, Inc.
+Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
 
 Permission is granted to make and distribute verbatim copies of
 this manual provided the copyright notice and this permission notice
@@ -41,13 +41,13 @@ into another language, under the above conditions for modified versions.
 @titlepage
 @c @finalout
 @title Cpplib Internals
-@subtitle Last revised September 2001
+@subtitle Last revised January 2002
 @subtitle for GCC version 3.1
 @author Neil Booth
 @page
 @vskip 0pt plus 1filll
 @c man begin COPYRIGHT
-Copyright @copyright{} 2000, 2001
+Copyright @copyright{} 2000, 2001, 2002
 Free Software Foundation, Inc.
 
 Permission is granted to make and distribute verbatim copies of
@@ -68,10 +68,10 @@ into another language, under the above conditions for modified versions.
 
 @node Top
 @top
-@chapter Cpplib---the core of the GNU C Preprocessor
+@chapter Cpplib---the GNU C Preprocessor
 
 The GNU C preprocessor in GCC 3.x has been completely rewritten.  It is
-now implemented as a library, cpplib, so it can be easily shared between
+now implemented as a library, @dfn{cpplib}, so it can be easily shared between
 a stand-alone preprocessor, and a preprocessor integrated with the C,
 C++ and Objective-C front ends.  It is also available for use by other
 programs, though this is not recommended as its exposed interface has
@@ -121,7 +121,7 @@ bare minimum necessary, and then to keep it there.  This makes clear
 exactly what external clients are entitled to assume, and allows us to
 change internals in the future without worrying whether library clients
 are perhaps relying on some kind of undocumented implementation-specific
-behaviour.
+behavior.
 
 @node Lexer
 @unnumbered The Lexer
@@ -158,23 +158,23 @@ will be removed, so I'll not discuss it further here.
 
 The job of @code{_cpp_lex_direct} is simply to lex a token.  It is not
 responsible for issues like directive handling, returning lookahead
-tokens directly, multiple-include optimisation, or conditional block
+tokens directly, multiple-include optimization, or conditional block
 skipping.  It necessarily has a minor r@^ole to play in memory
 management of lexed lines.  I discuss these issues in a separate section
 (@pxref{Lexing a line}).
 
 The lexer places the token it lexes into storage pointed to by the
-variable @var{cur_token}, and then increments it.  This variable is
+variable @code{cur_token}, and then increments it.  This variable is
 important for correct diagnostic positioning.  Unless a specific line
 and column are passed to the diagnostic routines, they will examine the
-@var{line} and @var{col} values of the token just before the location
-that @var{cur_token} points to, and use that location to report the
+@code{line} and @code{col} values of the token just before the location
+that @code{cur_token} points to, and use that location to report the
 diagnostic.
 
 The lexer does not consider whitespace to be a token in its own right.
 If whitespace (other than a new line) precedes a token, it sets the
 @code{PREV_WHITE} bit in the token's flags.  Each token has its
-@var{line} and @var{col} variables set to the line and column of the
+@code{line} and @code{col} variables set to the line and column of the
 first character of the token.  This line number is the line number in
 the translation unit, and can be converted to a source (file, line) pair
 using the line map code.
@@ -193,7 +193,7 @@ New lines are treated specially; exactly how the lexer handles them is
 context-dependent.  The C standard mandates that directives are
 terminated by the first unescaped newline character, even if it appears
 in the middle of a macro expansion.  Therefore, if the state variable
-@var{in_directive} is set, the lexer returns a @code{CPP_EOF} token,
+@code{in_directive} is set, the lexer returns a @code{CPP_EOF} token,
 which is normally used to indicate end-of-file, to indicate
 end-of-directive.  In a directive a @code{CPP_EOF} token never means
 end-of-file.  Conveniently, if the caller was @code{collect_args}, it
@@ -203,14 +203,14 @@ error about an unterminated macro argument list.
 The C standard also specifies that a new line in the middle of the
 arguments to a macro is treated as whitespace.  This white space is
 important in case the macro argument is stringified.  The state variable
-@var{parsing_args} is non-zero when the preprocessor is collecting the
+@code{parsing_args} is nonzero when the preprocessor is collecting the
 arguments to a macro call.  It is set to 1 when looking for the opening
 parenthesis to a function-like macro, and 2 when collecting the actual
 arguments up to the closing parenthesis, since these two cases need to
 be distinguished sometimes.  One such time is here: the lexer sets the
 @code{PREV_WHITE} flag of a token if it meets a new line when
-@var{parsing_args} is set to 2.  It doesn't set it if it meets a new
-line when @var{parsing_args} is 1, since then code like
+@code{parsing_args} is set to 2.  It doesn't set it if it meets a new
+line when @code{parsing_args} is 1, since then code like
 
 @smallexample
 #define foo() bar
@@ -295,7 +295,7 @@ whether a diagnostic is appropriate.  Since we change state on a
 per-token basis, and don't lex whole lines at a time, this is not a
 problem.
 
-Another place where state flags are used to change behaviour is whilst
+Another place where state flags are used to change behavior is whilst
 lexing header names.  Normally, a @samp{<} would be lexed as a single
 token.  After a @code{#include} directive, though, it should be lexed as
 a single token as far as the nearest @samp{>} character.  Note that we
@@ -331,7 +331,7 @@ Occasionally the preprocessor wants to be able to peek ahead in the
 token stream.  For example, after the name of a function-like macro, it
 wants to check the next token to see if it is an opening parenthesis.
 Another example is that, after reading the first few tokens of a
-@code{#pragma} directive and not recognising it as a registered pragma,
+@code{#pragma} directive and not recognizing it as a registered pragma,
 it wants to backtrack and allow the user-defined handler for unknown
 pragmas to access the full @code{#pragma} token stream.  The stand-alone
 preprocessor wants to be able to test the current token with the
@@ -373,7 +373,7 @@ the pointers to the tokens of its expansion that we return will always
 remain valid.  However, macros are a little trickier than that, since
 they give rise to three sources of fresh tokens.  They are the built-in
 macros like @code{__LINE__}, and the @samp{#} and @samp{##} operators
-for stringifcation and token pasting.  I handled this by allocating
+for stringification and token pasting.  I handled this by allocating
 space for these tokens from the lexer's token run chain.  This means
 they automatically receive the same lifetime guarantees as lexed tokens,
 and we don't need to concern ourselves with freeing them.
@@ -383,7 +383,7 @@ issues, but not all.  The opening parenthesis after a function-like
 macro name might lie on a different line, and the front ends definitely
 want the ability to look ahead past the end of the current line.  So
 cpplib only moves back to the start of the token run at the end of a
-line if the variable @var{keep_tokens} is zero.  Line-buffering is
+line if the variable @code{keep_tokens} is zero.  Line-buffering is
 quite natural for the preprocessor, and as a result the only time cpplib
 needs to increment this variable is whilst looking for the opening
 parenthesis to, and reading the arguments of, a function-like macro.  In
@@ -426,7 +426,7 @@ time, each identifier falls into exactly one of three categories:
 
 These have been declared to be macros, either on the command line or
 with @code{#define}.  A few, such as @code{__TIME__} are built-ins
-entered in the hash table during initialisation.  The hash node for a
+entered in the hash table during initialization.  The hash node for a
 normal macro points to a structure with more information about the
 macro, such as whether it is function-like, how many arguments it takes,
 and its expansion.  Built-in macros are flagged as special, and instead
@@ -467,8 +467,172 @@ enum stored in its hash node, so that directive lookup is also O(1).
 
 @node Macro Expansion
 @unnumbered Macro Expansion Algorithm
+@cindex macro expansion
+
+Macro expansion is a tricky operation, fraught with nasty corner cases
+and situations that render what you thought was a nifty way to
+optimize the preprocessor's expansion algorithm wrong in quite subtle
+ways.
+
+I strongly recommend you have a good grasp of how the C and C++
+standards require macros to be expanded before diving into this
+section, let alone the code!.  If you don't have a clear mental
+picture of how things like nested macro expansion, stringification and
+token pasting are supposed to work, damage to your sanity can quickly
+result.
+
+@section Internal representation of macros
+@cindex macro representation (internal)
+
+The preprocessor stores macro expansions in tokenized form.  This
+saves repeated lexing passes during expansion, at the cost of a small
+increase in memory consumption on average.  The tokens are stored
+contiguously in memory, so a pointer to the first one and a token
+count is all you need to get the replacement list of a macro.
+
+If the macro is a function-like macro the preprocessor also stores its
+parameters, in the form of an ordered list of pointers to the hash
+table entry of each parameter's identifier.  Further, in the macro's
+stored expansion each occurrence of a parameter is replaced with a
+special token of type @code{CPP_MACRO_ARG}.  Each such token holds the
+index of the parameter it represents in the parameter list, which
+allows rapid replacement of parameters with their arguments during
+expansion.  Despite this optimization it is still necessary to store
+the original parameters to the macro, both for dumping with e.g.,
+@option{-dD}, and to warn about non-trivial macro redefinitions when
+the parameter names have changed.
+
+@section Macro expansion overview
+The preprocessor maintains a @dfn{context stack}, implemented as a
+linked list of @code{cpp_context} structures, which together represent
+the macro expansion state at any one time.  The @code{struct
+cpp_reader} member variable @code{context} points to the current top
+of this stack.  The top normally holds the unexpanded replacement list
+of the innermost macro under expansion, except when cpplib is about to
+pre-expand an argument, in which case it holds that argument's
+unexpanded tokens.
+
+When there are no macros under expansion, cpplib is in @dfn{base
+context}.  All contexts other than the base context contain a
+contiguous list of tokens delimited by a starting and ending token.
+When not in base context, cpplib obtains the next token from the list
+of the top context.  If there are no tokens left in the list, it pops
+that context off the stack, and subsequent ones if necessary, until an
+unexhausted context is found or it returns to base context.  In base
+context, cpplib reads tokens directly from the lexer.
+
+If it encounters an identifier that is both a macro and enabled for
+expansion, cpplib prepares to push a new context for that macro on the
+stack by calling the routine @code{enter_macro_context}.  When this
+routine returns, the new context will contain the unexpanded tokens of
+the replacement list of that macro.  In the case of function-like
+macros, @code{enter_macro_context} also replaces any parameters in the
+replacement list, stored as @code{CPP_MACRO_ARG} tokens, with the
+appropriate macro argument.  If the standard requires that the
+parameter be replaced with its expanded argument, the argument will
+have been fully macro expanded first.
+
+@code{enter_macro_context} also handles special macros like
+@code{__LINE__}.  Although these macros expand to a single token which
+cannot contain any further macros, for reasons of token spacing
+(@pxref{Token Spacing}) and simplicity of implementation, cpplib
+handles these special macros by pushing a context containing just that
+one token.
+
+The final thing that @code{enter_macro_context} does before returning
+is to mark the macro disabled for expansion (except for special macros
+like @code{__TIME__}).  The macro is re-enabled when its context is
+later popped from the context stack, as described above.  This strict
+ordering ensures that a macro is disabled whilst its expansion is
+being scanned, but that it is @emph{not} disabled whilst any arguments
+to it are being expanded.
+
+@section Scanning the replacement list for macros to expand
+The C standard states that, after any parameters have been replaced
+with their possibly-expanded arguments, the replacement list is
+scanned for nested macros.  Further, any identifiers in the
+replacement list that are not expanded during this scan are never
+again eligible for expansion in the future, if the reason they were
+not expanded is that the macro in question was disabled.
+
+Clearly this latter condition can only apply to tokens resulting from
+argument pre-expansion.  Other tokens never have an opportunity to be
+re-tested for expansion.  It is possible for identifiers that are
+function-like macros to not expand initially but to expand during a
+later scan.  This occurs when the identifier is the last token of an
+argument (and therefore originally followed by a comma or a closing
+parenthesis in its macro's argument list), and when it replaces its
+parameter in the macro's replacement list, the subsequent token
+happens to be an opening parenthesis (itself possibly the first token
+of an argument).
+
+It is important to note that when cpplib reads the last token of a
+given context, that context still remains on the stack.  Only when
+looking for the @emph{next} token do we pop it off the stack and drop
+to a lower context.  This makes backing up by one token easy, but more
+importantly ensures that the macro corresponding to the current
+context is still disabled when we are considering the last token of
+its replacement list for expansion (or indeed expanding it).  As an
+example, which illustrates many of the points above, consider
 
-@c TODO
+@smallexample
+#define foo(x) bar x
+foo(foo) (2)
+@end smallexample
+
+@noindent which fully expands to @samp{bar foo (2)}.  During pre-expansion
+of the argument, @samp{foo} does not expand even though the macro is
+enabled, since it has no following parenthesis [pre-expansion of an
+argument only uses tokens from that argument; it cannot take tokens
+from whatever follows the macro invocation].  This still leaves the
+argument token @samp{foo} eligible for future expansion.  Then, when
+re-scanning after argument replacement, the token @samp{foo} is
+rejected for expansion, and marked ineligible for future expansion,
+since the macro is now disabled.  It is disabled because the
+replacement list @samp{bar foo} of the macro is still on the context
+stack.
+
+If instead the algorithm looked for an opening parenthesis first and
+then tested whether the macro were disabled it would be subtly wrong.
+In the example above, the replacement list of @samp{foo} would be
+popped in the process of finding the parenthesis, re-enabling
+@samp{foo} and expanding it a second time.
+
+@section Looking for a function-like macro's opening parenthesis
+Function-like macros only expand when immediately followed by a
+parenthesis.  To do this cpplib needs to temporarily disable macros
+and read the next token.  Unfortunately, because of spacing issues
+(@pxref{Token Spacing}), there can be fake padding tokens in-between,
+and if the next real token is not a parenthesis cpplib needs to be
+able to back up that one token as well as retain the information in
+any intervening padding tokens.
+
+Backing up more than one token when macros are involved is not
+permitted by cpplib, because in general it might involve issues like
+restoring popped contexts onto the context stack, which are too hard.
+Instead, searching for the parenthesis is handled by a special
+function, @code{funlike_invocation_p}, which remembers padding
+information as it reads tokens.  If the next real token is not an
+opening parenthesis, it backs up that one token, and then pushes an
+extra context just containing the padding information if necessary.
+
+@section Marking tokens ineligible for future expansion
+As discussed above, cpplib needs a way of marking tokens as
+unexpandable.  Since the tokens cpplib handles are read-only once they
+have been lexed, it instead makes a copy of the token and adds the
+flag @code{NO_EXPAND} to the copy.
+
+For efficiency and to simplify memory management by avoiding having to
+remember to free these tokens, they are allocated as temporary tokens
+from the lexer's current token run (@pxref{Lexing a line}) using the
+function @code{_cpp_temp_token}.  The tokens are then re-used once the
+current line of tokens has been read in.
+
+This might sound unsafe.  However, tokens runs are not re-used at the
+end of a line if it happens to be in the middle of a macro argument
+list, and cpplib only wants to back-up more than one lexer token in
+situations where no macro expansion is involved, so the optimization
+is safe.
 
 @node Token Spacing
 @unnumbered Token Spacing
@@ -479,8 +643,8 @@ enum stored in its hash node, so that directive lookup is also O(1).
 First, let's look at an issue that only concerns the stand-alone
 preprocessor: we want to guarantee that re-reading its preprocessed
 output results in an identical token stream.  Without taking special
-measures, this might not be the case because of macro substitution.  For
-example:
+measures, this might not be the case because of macro substitution.
+For example:
 
 @smallexample
 #define PLUS +
@@ -498,12 +662,13 @@ both for aesthetic reasons and because it causes problems for people who
 still try to abuse the preprocessor for things like Fortran source and
 Makefiles.
 
-For now, just notice that the only places we need to be careful about
-@dfn{paste avoidance} are when tokens are added (or removed) from the
-original token stream.  This only occurs because of macro expansion, but
-care is needed in many places: before @strong{and} after each macro
-replacement, each argument replacement, and additionally each token
-created by the @samp{#} and @samp{##} operators.
+For now, just notice that when tokens are added (or removed, as shown by
+the @code{EMPTY} example) from the original lexed token stream, we need
+to check for accidental token pasting.  We call this @dfn{paste
+avoidance}.  Token addition and removal can only occur because of macro
+expansion, but accidental pasting can occur in many places: both before
+and after each macro replacement, each argument replacement, and
+additionally each token created by the @samp{#} and @samp{##} operators.
 
 Let's look at how the preprocessor gets whitespace output correct
 normally.  The @code{cpp_token} structure contains a flags byte, and one
@@ -512,7 +677,7 @@ indicates that the token was preceded by whitespace of some form other
 than a new line.  The stand-alone preprocessor can use this flag to
 decide whether to insert a space between tokens in the output.
 
-Now consider the following:
+Now consider the result of the following macro expansion:
 
 @smallexample
 #define add(x, y, z) x + y +z;
@@ -524,20 +689,21 @@ The interesting thing here is that the tokens @samp{1} and @samp{2} are
 output with a preceding space, and @samp{3} is output without a
 preceding space, but when lexed none of these tokens had that property.
 Careful consideration reveals that @samp{1} gets its preceding
-whitespace from the space preceding @samp{add} in the macro
-@emph{invocation}, @samp{2} gets its whitespace from the space preceding
-the parameter @samp{y} in the macro @emph{replacement list}, and
-@samp{3} has no preceding space because parameter @samp{z} has none in
-the replacement list.
+whitespace from the space preceding @samp{add} in the macro invocation,
+@emph{not} replacement list.  @samp{2} gets its whitespace from the
+space preceding the parameter @samp{y} in the macro replacement list,
+and @samp{3} has no preceding space because parameter @samp{z} has none
+in the replacement list.
 
 Once lexed, tokens are effectively fixed and cannot be altered, since
 pointers to them might be held in many places, in particular by
 in-progress macro expansions.  So instead of modifying the two tokens
 above, the preprocessor inserts a special token, which I call a
-@dfn{padding token}, into the token stream in front of every macro
-expansion and expanded macro argument, to indicate that the subsequent
-token should assume its @code{PREV_WHITE} flag from a different
-@dfn{source token}.  In the above example, the source tokens are
+@dfn{padding token}, into the token stream to indicate that spacing of
+the subsequent token is special.  The preprocessor inserts padding
+tokens in front of every macro expansion and expanded macro argument.
+These point to a @dfn{source token} from which the subsequent real token
+should inherit its spacing.  In the above example, the source tokens are
 @samp{add} in the macro invocation, and @samp{y} and @samp{z} in the
 macro replacement list, respectively.
 
@@ -551,10 +717,14 @@ a macro's first replacement token expands straight into another macro.
         @expansion{} [baz]
 @end smallexample
 
-Here, two padding tokens with sources @samp{foo} between the brackets,
-and @samp{bar} from foo's replacement list, are generated.  Clearly the
-first padding token is the one that matters.  But what if we happen to
-leave a macro expansion?  Adjusting the above example slightly:
+Here, two padding tokens are generated with sources the @samp{foo} token
+between the brackets, and the @samp{bar} token from foo's replacement
+list, respectively.  Clearly the first padding token is the one we
+should use, so our output code should contain a rule that the first
+padding token in a sequence is the one that matters.
+
+But what if we happen to leave a macro expansion?  Adjusting the above
+example slightly:
 
 @smallexample
 #define foo bar
@@ -564,64 +734,133 @@ leave a macro expansion?  Adjusting the above example slightly:
         @expansion{} [ baz] ;
 @end smallexample
 
-As shown, now there should be a space before baz and the semicolon.  Our
-initial algorithm fails for the former, because we would see three
-padding tokens, one per macro invocation, followed by @samp{baz}, which
-would have inherit its spacing from the original source, @samp{foo},
-which has no leading space.  Note that it is vital that cpplib get
-spacing correct in these examples, since any of these macro expansions
-could be stringified, where spacing matters.
-
-So, I have demonstrated that not just entering macro and argument
-expansions, but leaving them requires special handling too.  So cpplib
-inserts a padding token with a @code{NULL} source token when leaving
-macro expansions and after each replaced argument in a macro's
-replacement list.  It also inserts appropriate padding tokens on either
-side of tokens created by the @samp{#} and @samp{##} operators.
-
-Now we can see the relationship with paste avoidance: we have to be
-careful about paste avoidance in exactly the same locations we take care
-to get white space correct.  This makes implementation of paste
-avoidance easy: wherever the stand-alone preprocessor is fixing up
-spacing because of padding tokens, and it turns out that no space is
-needed, it has to take the extra step to check that a space is not
-needed after all to avoid an accidental paste.  The function
-@code{cpp_avoid_paste} advises whether a space is required between two
-consecutive tokens.  To avoid excessive spacing, it tries hard to only
-require a space if one is likely to be necessary, but for reasons of
-efficiency it is slightly conservative and might recommend a space where
-one is not strictly needed.
+As shown, now there should be a space before @samp{baz} and the
+semicolon in the output.
+
+The rules we decided above fail for @samp{baz}: we generate three
+padding tokens, one per macro invocation, before the token @samp{baz}.
+We would then have it take its spacing from the first of these, which
+carries source token @samp{foo} with no leading space.
+
+It is vital that cpplib get spacing correct in these examples since any
+of these macro expansions could be stringified, where spacing matters.
+
+So, this demonstrates that not just entering macro and argument
+expansions, but leaving them requires special handling too.  I made
+cpplib insert a padding token with a @code{NULL} source token when
+leaving macro expansions, as well as after each replaced argument in a
+macro's replacement list.  It also inserts appropriate padding tokens on
+either side of tokens created by the @samp{#} and @samp{##} operators.
+I expanded the rule so that, if we see a padding token with a
+@code{NULL} source token, @emph{and} that source token has no leading
+space, then we behave as if we have seen no padding tokens at all.  A
+quick check shows this rule will then get the above example correct as
+well.
+
+Now a relationship with paste avoidance is apparent: we have to be
+careful about paste avoidance in exactly the same locations we have
+padding tokens in order to get white space correct.  This makes
+implementation of paste avoidance easy: wherever the stand-alone
+preprocessor is fixing up spacing because of padding tokens, and it
+turns out that no space is needed, it has to take the extra step to
+check that a space is not needed after all to avoid an accidental paste.
+The function @code{cpp_avoid_paste} advises whether a space is required
+between two consecutive tokens.  To avoid excessive spacing, it tries
+hard to only require a space if one is likely to be necessary, but for
+reasons of efficiency it is slightly conservative and might recommend a
+space where one is not strictly needed.
 
 @node Line Numbering
 @unnumbered Line numbering
 @cindex line numbers
 
-The preprocessor takes great care to ensure it keeps track of both the
-position of a token in the source file, for diagnostic purposes, and
-where it should appear in the output file, because using CPP for other
-languages like assembler requires this.  The two positions may differ
-for the following reasons:
+@section Just which line number anyway?
+
+There are three reasonable requirements a cpplib client might have for
+the line number of a token passed to it:
 
 @itemize @bullet
 @item
-Escaped newlines are deleted, so lines spliced in this way are joined to
-form a single logical line.
+The source line it was lexed on.
+@item
+The line it is output on.  This can be different to the line it was
+lexed on if, for example, there are intervening escaped newlines or
+C-style comments.  For example:
+
+@smallexample
+foo /* A long
+comment */ bar \
+baz
+@result{}
+foo bar baz
+@end smallexample
 
 @item
-A macro expansion replaces the tokens that form its invocation, but any
-newlines appearing in the macro's arguments are interpreted as a single
-space, with the result that the macro's replacement appears in full on
-the same line that the macro name appeared in the source file.  This is
-particularly important for stringification of arguments---newlines
-embedded in the arguments must appear in the string as spaces.
+If the token results from a macro expansion, the line of the macro name,
+or possibly the line of the closing parenthesis in the case of
+function-like macro expansion.
 @end itemize
 
-The source file location is maintained in the @code{lineno} member of the
-@code{cpp_buffer} structure, and the column number inferred from the
-current position in the buffer relative to the @code{line_base} buffer
-variable, which is updated with every newline whether escaped or not.
-
-@c FINISH THIS
+The @code{cpp_token} structure contains @code{line} and @code{col}
+members.  The lexer fills these in with the line and column of the first
+character of the token.  Consequently, but maybe unexpectedly, a token
+from the replacement list of a macro expansion carries the location of
+the token within the @code{#define} directive, because cpplib expands a
+macro by returning pointers to the tokens in its replacement list.  The
+current implementation of cpplib assigns tokens created from built-in
+macros and the @samp{#} and @samp{##} operators the location of the most
+recently lexed token.  This is a because they are allocated from the
+lexer's token runs, and because of the way the diagnostic routines infer
+the appropriate location to report.
+
+The diagnostic routines in cpplib display the location of the most
+recently @emph{lexed} token, unless they are passed a specific line and
+column to report.  For diagnostics regarding tokens that arise from
+macro expansions, it might also be helpful for the user to see the
+original location in the macro definition that the token came from.
+Since that is exactly the information each token carries, such an
+enhancement could be made relatively easily in future.
+
+The stand-alone preprocessor faces a similar problem when determining
+the correct line to output the token on: the position attached to a
+token is fairly useless if the token came from a macro expansion.  All
+tokens on a logical line should be output on its first physical line, so
+the token's reported location is also wrong if it is part of a physical
+line other than the first.
+
+To solve these issues, cpplib provides a callback that is generated
+whenever it lexes a preprocessing token that starts a new logical line
+other than a directive.  It passes this token (which may be a
+@code{CPP_EOF} token indicating the end of the translation unit) to the
+callback routine, which can then use the line and column of this token
+to produce correct output.
+
+@section Representation of line numbers
+
+As mentioned above, cpplib stores with each token the line number that
+it was lexed on.  In fact, this number is not the number of the line in
+the source file, but instead bears more resemblance to the number of the
+line in the translation unit.
+
+The preprocessor maintains a monotonic increasing line count, which is
+incremented at every new line character (and also at the end of any
+buffer that does not end in a new line).  Since a line number of zero is
+useful to indicate certain special states and conditions, this variable
+starts counting from one.
+
+This variable therefore uniquely enumerates each line in the translation
+unit.  With some simple infrastructure, it is straight forward to map
+from this to the original source file and line number pair, saving space
+whenever line number information needs to be saved.  The code the
+implements this mapping lies in the files @file{line-map.c} and
+@file{line-map.h}.
+
+Command-line macros and assertions are implemented by pushing a buffer
+containing the right hand side of an equivalent @code{#define} or
+@code{#assert} directive.  Some built-in macros are handled similarly.
+Since these are all processed before the first line of the main input
+file, it will typically have an assigned line closer to twenty than to
+one.
 
 @node Guard Macros
 @unnumbered The Multiple-Include Optimization
@@ -641,7 +880,7 @@ Header files are often of the form
 @noindent
 to prevent the compiler from processing them more than once.  The
 preprocessor notices such header files, so that if the header file
-appears in a subsequent @code{#include} directive and @var{FOO} is
+appears in a subsequent @code{#include} directive and @code{FOO} is
 defined, then it is ignored and it doesn't preprocess or even re-open
 the file a second time.  This is referred to as the @dfn{multiple
 include optimization}.
@@ -665,15 +904,15 @@ the @dfn{null directive} (a line containing nothing other than a single
 @item
 The opening directive must be of the form
 
-@display
+@smallexample
 #ifndef FOO
-@end display
+@end smallexample
 
 or
 
-@display
+@smallexample
 #if !defined FOO     [equivalently, #if !defined(FOO)]
-@end display
+@end smallexample
 
 @item
 In the second form above, the tokens forming the @code{#if} expression
@@ -689,15 +928,15 @@ of interest to a subsequent pass.
 @end enumerate
 
 First, when pushing a new file on the buffer stack,
-@code{_stack_include_file} sets the controlling macro @var{mi_cmacro} to
-@code{NULL}, and sets @var{mi_valid} to @code{true}.  This indicates
+@code{_stack_include_file} sets the controlling macro @code{mi_cmacro} to
+@code{NULL}, and sets @code{mi_valid} to @code{true}.  This indicates
 that the preprocessor has not yet encountered anything that would
 invalidate the multiple-include optimization.  As described in the next
 few paragraphs, these two variables having these values effectively
 indicates top-of-file.
 
 When about to return a token that is not part of a directive,
-@code{_cpp_lex_token} sets @var{mi_valid} to @code{false}.  This
+@code{_cpp_lex_token} sets @code{mi_valid} to @code{false}.  This
 enforces the constraint that tokens outside the controlling conditional
 block invalidate the optimization.
 
@@ -711,24 +950,24 @@ and we're at top-of-file (as described above).  If an @code{#elif} or
 @code{#else} directive is encountered, the controlling macro for that
 block is cleared to @code{NULL}.  Otherwise, it survives until the
 @code{#endif} closing the block, upon which @code{do_endif} sets
-@var{mi_valid} to true and stores the controlling macro in
-@var{mi_cmacro}.
+@code{mi_valid} to true and stores the controlling macro in
+@code{mi_cmacro}.
 
-@code{_cpp_handle_directive} clears @var{mi_valid} when processing any
+@code{_cpp_handle_directive} clears @code{mi_valid} when processing any
 directive other than an opening conditional and the null directive.
 With this, and requiring top-of-file to record a controlling macro, and
 no @code{#else} or @code{#elif} for it to survive and be copied to
-@var{mi_cmacro} by @code{do_endif}, we have enforced the absence of
+@code{mi_cmacro} by @code{do_endif}, we have enforced the absence of
 directives outside the main conditional block for the optimization to be
 on.
 
-Note that whilst we are inside the conditional block, @var{mi_valid} is
+Note that whilst we are inside the conditional block, @code{mi_valid} is
 likely to be reset to @code{false}, but this does not matter since the
 the closing @code{#endif} restores it to @code{true} if appropriate.
 
 Finally, since @code{_cpp_lex_direct} pops the file off the buffer stack
 at @code{EOF} without returning a token, if the @code{#endif} directive
-was not followed by any tokens, @var{mi_valid} is @code{true} and
+was not followed by any tokens, @code{mi_valid} is @code{true} and
 @code{_cpp_pop_file_buffer} remembers the controlling macro associated
 with the file.  Subsequent calls to @code{stack_include_file} result in
 no buffer being pushed if the controlling macro is defined, effecting
@@ -736,17 +975,17 @@ the optimization.
 
 A quick word on how we handle the
 
-@display
+@smallexample
 #if !defined FOO
-@end display
+@end smallexample
 
 @noindent
 case.  @code{_cpp_parse_expr} and @code{parse_defined} take steps to see
 whether the three stages @samp{!}, @samp{defined-expression} and
 @samp{end-of-directive} occur in order in a @code{#if} expression.  If
 so, they return the guard macro to @code{do_if} in the variable
-@var{mi_ind_cmacro}, and otherwise set it to @code{NULL}.
-@code{enter_macro_context} sets @var{mi_valid} to false, so if a macro
+@code{mi_ind_cmacro}, and otherwise set it to @code{NULL}.
+@code{enter_macro_context} sets @code{mi_valid} to false, so if a macro
 was expanded whilst parsing any part of the expression, then the
 top-of-file test in @code{push_conditional} fails and the optimization
 is turned off.
@@ -807,7 +1046,7 @@ the basename @samp{foo.h} as the current directory.
 
 Enough information is stored in the splay tree that CPP can immediately
 tell whether it can skip the header file because of the multiple include
-optimisation, whether the file didn't exist or couldn't be opened for
+optimization, whether the file didn't exist or couldn't be opened for
 some reason, or whether the header was flagged not to be re-used, as it
 is with the obsolete @code{#import} directive.