libcpp: Complete the __COUNTER__ support with upstream implementation.

We brought an original __COUNTER__ implementation in r228474, however, it
was missing documentation and it had a different behaviour for precompiled
headers with respect to the upstream version. Since the upstream version
is under the same license as GCC4.2, bring the missing pieces to reduce
differences against upstream.

Optained from:	GCC pre-4.3 (rev. 125041 ; GPLv2)
This commit is contained in:
pfg 2016-08-30 19:39:33 +00:00
parent 4463403064
commit a687854894
5 changed files with 62 additions and 2 deletions

View File

@ -1912,6 +1912,13 @@ underscores.
@table @code
@item __COUNTER__
This macro expands to sequential integral values starting from 0. In
conjuction with the @code{##} operator, this provides a convenient means to
generate unique identifiers. Care must be taken to ensure that
@code{__COUNTER__} is not expanded prior to inclusion of precompiled headers
which use it. Otherwise, the precompiled headers will not be used.
@item __GNUC__
@itemx __GNUC_MINOR__
@itemx __GNUC_PATCHLEVEL__

View File

@ -12,6 +12,17 @@
PR preprocessor/14331
* lex.c (_cpp_get_fresh_line): Don't warn if no newline at EOF.
2007-05-24 Ollie Wild <aaw@google.com> (r125041)
* macro.c (_cpp_builtin_macro_text): Handle BT_COUNTER.
* pch.c (cpp_write_pch_deps): Save __COUNTER__ state.
(cpp_write_pch_state): Save __COUNTER__ state.
(cpp_valid_state): Check valid __COUNTER__ state.
(cpp_read_state): Read new __COUNTER__ state.
* include/cpplib.h (enum builtin_type): Add BT_COUNTER enumerator.
* init.c (builtin_array): Add __COUNTER__/BT_COUNTER.
* internal.h (struct cpp_reader): Add counter member.
2007-05-21 Ian Lance Taylor <iant@google.com> (r124929)
* internal.h (struct cpp_reader): Add new fields:

View File

@ -458,7 +458,8 @@ struct cpp_reader
of precompiled headers. */
struct cpp_savedstate *savedstate;
unsigned int nextcounter;
/* Next value of __COUNTER__ macro. */
unsigned int counter;
};
/* Character classes. Based on the more primitive macros in safe-ctype.h.

View File

@ -268,7 +268,7 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive)
cpp_error (pfile, CPP_DL_ERROR,
"__COUNTER__ expanded inside directive with -fdirectives-only");
number = pfile->nextcounter++;
number = pfile->counter++;
break;
}

View File

@ -337,6 +337,14 @@ cpp_write_pch_deps (cpp_reader *r, FILE *f)
/* Free the saved state. */
free (ss);
r->savedstate = NULL;
/* Save the next value of __COUNTER__. */
if (fwrite (&r->counter, sizeof (r->counter), 1, f) != 1)
{
cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
return -1;
}
return 0;
}
@ -361,6 +369,15 @@ cpp_write_pch_state (cpp_reader *r, FILE *f)
return -1;
}
/* Save the next __COUNTER__ value. When we include a precompiled header,
we need to start at the offset we would have if the header had been
included normally. */
if (fwrite (&r->counter, sizeof (r->counter), 1, f) != 1)
{
cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
return -1;
}
return 0;
}
@ -423,6 +440,7 @@ cpp_valid_state (cpp_reader *r, const char *name, int fd)
struct ht_node_list nl = { 0, 0, 0 };
unsigned char *first, *last;
unsigned int i;
unsigned int counter;
/* Read in the list of identifiers that must be defined
Check that they are defined in the same way. */
@ -524,7 +542,23 @@ cpp_valid_state (cpp_reader *r, const char *name, int fd)
}
free(nl.defs);
nl.defs = NULL;
free (undeftab);
undeftab = NULL;
/* Read in the next value of __COUNTER__.
Check that (a) __COUNTER__ was not used in the pch or (b) __COUNTER__
has not been used in this translation unit. */
if (read (fd, &counter, sizeof (counter)) != sizeof (counter))
goto error;
if (counter && r->counter)
{
if (CPP_OPTION (r, warn_invalid_pch))
cpp_error (r, CPP_DL_WARNING_SYSHDR,
"%s: not used because `__COUNTER__' is invalid",
name);
goto fail;
}
/* We win! */
return 0;
@ -631,6 +665,7 @@ cpp_read_state (cpp_reader *r, const char *name, FILE *f,
{
size_t i;
struct lexer_state old_state;
unsigned int counter;
/* Restore spec_nodes, which will be full of references to the old
hashtable entries and so will now be invalid. */
@ -690,6 +725,12 @@ cpp_read_state (cpp_reader *r, const char *name, FILE *f,
if (! _cpp_read_file_entries (r, f))
goto error;
if (fread (&counter, sizeof (counter), 1, f) != 1)
goto error;
if (!r->counter)
r->counter = counter;
return 0;
error: