This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: Minor doc updates.
- From: Zack Weinberg <zack at codesourcery dot com>
- To: Phil Edwards <phil at jaj dot com>
- Cc: Geoffrey Keating <geoffk at redhat dot com>, gcc-patches at gcc dot gnu dot org
- Date: Thu, 1 Aug 2002 12:19:16 -0700
- Subject: Re: Minor doc updates.
- References: <200207292348.g6TNmjW02378@pride.sfbay.redhat.com> <20020730155056.GB22501@codesourcery.com> <20020801132855.A17805@disaster.basement.lan>
On Thu, Aug 01, 2002 at 01:28:55PM -0400, Phil Edwards wrote:
> On Tue, Jul 30, 2002 at 08:50:56AM -0700, Zack Weinberg wrote:
> > We ought to be checking the return values here. It is vanishingly
> > unlikely that time() will fail, but localtime() might not be able to
> > find its timezone database, and then we'll try to dereference a null
> > pointer.
> >
> > "??:??:??" and "??? ?? ????" seem like reasonable failure-mode strings
> > to return. Anyone have a better idea?
>
> Those seem quite reasonable, as long as they're put into a FAQ or a manual.
> (Something that can be found in an online search, at least.)
What do you think of this patch? I documented the strings, and I have
GCC issue a warning when time/localtime fail. I've done cursory
testing by LD_PRELOAD-ing a fake time() that always fails:
$ cat test.c
__DATE__
__TIME__
__DATE__
__TIME__
$ LD_PRELOAD=./time-fails.so ./cc1 -quiet -E -P test.c
test.c:1:1: warning: could not determine date and time: Bad address
"??? ?? ????"
"??:??:??"
"??? ?? ????"
"??:??:??"
* cppmacro.c (_cpp_builtin_macro_text) [BT_TIME, BT_DATE]:
Check for failing time()/localtime(), issue a warning, and
make __TIME__ and __DATE__ expand to fallback strings.
* doc/cpp.texi, doc/extend.texi: Document behavior of __DATE__
and __TIME__ when the date and time cannot be determined.
===================================================================
Index: cppmacro.c
--- cppmacro.c 26 Jul 2002 16:29:31 -0000 1.119
+++ cppmacro.c 1 Aug 2002 19:12:29 -0000
@@ -207,17 +207,37 @@ _cpp_builtin_macro_text (pfile, node)
storage. We only do this once, and don't generate them
at init time, because time() and localtime() are very
slow on some systems. */
- time_t tt = time (NULL);
- struct tm *tb = localtime (&tt);
+ time_t tt;
+ struct tm *tb = NULL;
- pfile->date = _cpp_unaligned_alloc (pfile,
- sizeof ("\"Oct 11 1347\""));
- sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
- monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
+ /* (time_t) -1 is a legitimate value for "number of seconds
+ since the Epoch", so we have to do a little dance to
+ distinguish that from a genuine error. */
+ errno = 0;
+ tt = time(NULL);
+ if (tt != (time_t)-1 || errno == 0)
+ tb = localtime (&tt);
- pfile->time = _cpp_unaligned_alloc (pfile, sizeof ("\"12:34:56\""));
- sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
- tb->tm_hour, tb->tm_min, tb->tm_sec);
+ if (tb)
+ {
+ pfile->date = _cpp_unaligned_alloc (pfile,
+ sizeof ("\"Oct 11 1347\""));
+ sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
+ monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
+
+ pfile->time = _cpp_unaligned_alloc (pfile,
+ sizeof ("\"12:34:56\""));
+ sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
+ tb->tm_hour, tb->tm_min, tb->tm_sec);
+ }
+ else
+ {
+ cpp_errno (pfile, DL_WARNING,
+ "could not determine date and time");
+
+ pfile->date = U"\"??? ?? ????\"";
+ pfile->time = U"\"??:??:??\"";
+ }
}
if (node->value.builtin == BT_DATE)
===================================================================
Index: doc/cpp.texi
--- doc/cpp.texi 17 Jul 2002 21:31:42 -0000 1.38
+++ doc/cpp.texi 1 Aug 2002 19:12:30 -0000
@@ -1780,10 +1780,16 @@ the preprocessor is being run. The stri
characters and looks like @code{@w{"Feb 12 1996"}}. If the day of the
month is less than 10, it is padded with a space on the left.
+If GCC cannot determine the current date, it will emit a warning message
+(once per compilation) and __DATE__ will expand to @code{@w{"??? ?? ????"}}.
+
@item __TIME__
This macro expands to a string constant that describes the time at
which the preprocessor is being run. The string constant contains
eight characters and looks like @code{"23:59:01"}.
+
+If GCC cannot determine the current time, it will emit a warning message
+(once per compilation) and __TIME__ will expand to @code{@w{"??:??:??"}}.
@item __STDC__
In normal operation, this macro expands to the constant 1, to signify
===================================================================
Index: doc/extend.texi
--- doc/extend.texi 31 Jul 2002 00:11:31 -0000 1.92
+++ doc/extend.texi 1 Aug 2002 19:12:32 -0000
@@ -366,7 +366,9 @@ directive (6.10.6).}
@cite{The definitions for @code{__DATE__} and @code{__TIME__} when
respectively, the date and time of translation are not available (6.10.8).}
-GCC assumes that the date and time is always available.
+If the date and time are not available, @code{__DATE__} expands to
+@code{@w{"??? ?? ????"}} and @code{__TIME__} expands to
+@code{@w{"??:??:??"}}.
@end itemize