This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] Ensure source_date_epoch is always initialised
- From: James Clarke <jrtc27 at jrtc27 dot com>
- To: gcc-patches at gcc dot gnu dot org
- Date: Thu, 19 May 2016 01:02:28 +0100
- Subject: [PATCH] Ensure source_date_epoch is always initialised
- Authentication-results: sourceware.org; auth=none
gcc/c-family
PR preprocessor/71183
* c-common.c (get_source_date_epoch): Move to libcpp/init.c.
* c-common.h (get_source_date_epoch): Remove definition, as it
is now internal to libcpp/init.c.
* c-lex.c (c_lex_with_flags): Remove source_date_epoch
initialization, as this is now done by libcpp.
gcc/testsuite/
PR preprocessor/71183
* gcc.dg/cpp/special/date-time.c: New testcase.
* gcc.dg/cpp/special/date-time.exp: New file. Sets the
SOURCE_DATE_EPOCH environment variable for date-time.c.
libcpp/
PR preprocessor/71183
* include/cpplib.h (cpp_init_source_date_epoch): Remove
definition, as it is now internal to init.c.
* init.c (cpp_create_reader): Initialize source_date_epoch.
(get_source_date_epoch): Moved from gcc/c-family/c-common.c, and
uses cpp_error instead of fatal_error.
(cpp_init_source_date_epoch): Drop source_date_epoch argument
and call get_source_date_epoch to get the value.
---
gcc/c-family/c-common.c | 33 --------------------
gcc/c-family/c-common.h | 5 ---
gcc/c-family/c-lex.c | 3 --
gcc/testsuite/gcc.dg/cpp/special/date-time.c | 5 +++
gcc/testsuite/gcc.dg/cpp/special/date-time.exp | 35 +++++++++++++++++++++
libcpp/include/cpplib.h | 3 --
libcpp/init.c | 43 ++++++++++++++++++++++++--
7 files changed, 80 insertions(+), 47 deletions(-)
diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
index 146e805..83f38dd 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -12791,39 +12791,6 @@ valid_array_size_p (location_t loc, tree type, tree name)
return true;
}
-/* Read SOURCE_DATE_EPOCH from environment to have a deterministic
- timestamp to replace embedded current dates to get reproducible
- results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
-time_t
-get_source_date_epoch ()
-{
- char *source_date_epoch;
- long long epoch;
- char *endptr;
-
- source_date_epoch = getenv ("SOURCE_DATE_EPOCH");
- if (!source_date_epoch)
- return (time_t) -1;
-
- errno = 0;
- epoch = strtoll (source_date_epoch, &endptr, 10);
- if ((errno == ERANGE && (epoch == LLONG_MAX || epoch == LLONG_MIN))
- || (errno != 0 && epoch == 0))
- fatal_error (UNKNOWN_LOCATION, "environment variable $SOURCE_DATE_EPOCH: "
- "strtoll: %s\n", xstrerror(errno));
- if (endptr == source_date_epoch)
- fatal_error (UNKNOWN_LOCATION, "environment variable $SOURCE_DATE_EPOCH: "
- "no digits were found: %s\n", endptr);
- if (*endptr != '\0')
- fatal_error (UNKNOWN_LOCATION, "environment variable $SOURCE_DATE_EPOCH: "
- "trailing garbage: %s\n", endptr);
- if (epoch < 0)
- fatal_error (UNKNOWN_LOCATION, "environment variable $SOURCE_DATE_EPOCH: "
- "value must be nonnegative: %lld \n", epoch);
-
- return (time_t) epoch;
-}
-
/* Check and possibly warn if two declarations have contradictory
attributes, such as always_inline vs. noinline. */
diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h
index 0ee9f56..63fd2b9 100644
--- a/gcc/c-family/c-common.h
+++ b/gcc/c-family/c-common.h
@@ -1482,9 +1482,4 @@ extern bool valid_array_size_p (location_t, tree, tree);
extern bool cilk_ignorable_spawn_rhs_op (tree);
extern bool cilk_recognize_spawn (tree, tree *);
-/* Read SOURCE_DATE_EPOCH from environment to have a deterministic
- timestamp to replace embedded current dates to get reproducible
- results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
-extern time_t get_source_date_epoch (void);
-
#endif /* ! GCC_C_COMMON_H */
diff --git a/gcc/c-family/c-lex.c b/gcc/c-family/c-lex.c
index 38a428d..5bab8d1 100644
--- a/gcc/c-family/c-lex.c
+++ b/gcc/c-family/c-lex.c
@@ -389,9 +389,6 @@ c_lex_with_flags (tree *value, location_t *loc, unsigned char *cpp_flags,
enum cpp_ttype type;
unsigned char add_flags = 0;
enum overflow_type overflow = OT_NONE;
- time_t source_date_epoch = get_source_date_epoch ();
-
- cpp_init_source_date_epoch (parse_in, source_date_epoch);
timevar_push (TV_CPP);
retry:
diff --git a/gcc/testsuite/gcc.dg/cpp/special/date-time.c b/gcc/testsuite/gcc.dg/cpp/special/date-time.c
new file mode 100644
index 0000000..3304b75
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/special/date-time.c
@@ -0,0 +1,5 @@
+/* { dg-do preprocess } */
+__DATE__
+__TIME__
+/* { dg-final { scan-file date-time.i "\"Jul 4 1978\"" } } */
+/* { dg-final { scan-file date-time.i "\"21:24:16\"" } } */
diff --git a/gcc/testsuite/gcc.dg/cpp/special/date-time.exp b/gcc/testsuite/gcc.dg/cpp/special/date-time.exp
new file mode 100644
index 0000000..3c43143
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/special/date-time.exp
@@ -0,0 +1,35 @@
+# Copyright (C) 1997-2016 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GCC; see the file COPYING3. If not see
+# <http://www.gnu.org/licenses/>.
+
+# GCC testsuite that uses the `dg.exp' driver.
+
+load_lib gcc-dg.exp
+
+# If a testcase doesn't have special options, use these.
+global DEFAULT_CFLAGS
+if ![info exists DEFAULT_CFLAGS] then {
+ set DEFAULT_CFLAGS " -ansi -pedantic-errors"
+}
+
+setenv SOURCE_DATE_EPOCH 268435456
+
+# Initialize `dg'.
+dg-init
+
+dg-runtest $srcdir/$subdir/date-time.c "" $DEFAULT_CFLAGS
+
+# All done.
+dg-finish
diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
index 4998b3a..35b0375 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -784,9 +784,6 @@ extern void cpp_init_special_builtins (cpp_reader *);
/* Set up built-ins like __FILE__. */
extern void cpp_init_builtins (cpp_reader *, int);
-/* Initialize the source_date_epoch value. */
-extern void cpp_init_source_date_epoch (cpp_reader *, time_t);
-
/* This is called after options have been parsed, and partially
processed. */
extern void cpp_post_options (cpp_reader *);
diff --git a/libcpp/init.c b/libcpp/init.c
index f5ff85b..9594ae6 100644
--- a/libcpp/init.c
+++ b/libcpp/init.c
@@ -36,6 +36,7 @@ along with this program; see the file COPYING3. If not see
static void init_library (void);
static void mark_named_operators (cpp_reader *, int);
+static void cpp_init_source_date_epoch (cpp_reader *);
static void read_original_filename (cpp_reader *);
static void read_original_directory (cpp_reader *);
static void post_options (cpp_reader *);
@@ -267,6 +268,9 @@ cpp_create_reader (enum c_lang lang, cpp_hash_table *table,
_cpp_init_hashtable (pfile, table);
+ /* Initialize the source_date_epoch value. */
+ cpp_init_source_date_epoch (pfile);
+
return pfile;
}
@@ -533,11 +537,44 @@ cpp_init_builtins (cpp_reader *pfile, int hosted)
_cpp_define_builtin (pfile, "__OBJC__ 1");
}
+/* Read SOURCE_DATE_EPOCH from environment to have a deterministic
+ timestamp to replace embedded current dates to get reproducible
+ results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
+static time_t
+get_source_date_epoch (cpp_reader *pfile)
+{
+ char *source_date_epoch;
+ long long epoch;
+ char *endptr;
+
+ source_date_epoch = getenv ("SOURCE_DATE_EPOCH");
+ if (!source_date_epoch)
+ return (time_t) -1;
+
+ errno = 0;
+ epoch = strtoll (source_date_epoch, &endptr, 10);
+ if ((errno == ERANGE && (epoch == LLONG_MAX || epoch == LLONG_MIN))
+ || (errno != 0 && epoch == 0))
+ cpp_error (pfile, CPP_DL_FATAL, "environment variable $SOURCE_DATE_EPOCH: "
+ "strtoll: %s\n", xstrerror(errno));
+ if (endptr == source_date_epoch)
+ cpp_error (pfile, CPP_DL_FATAL, "environment variable $SOURCE_DATE_EPOCH: "
+ "no digits were found: %s\n", endptr);
+ if (*endptr != '\0')
+ cpp_error (pfile, CPP_DL_FATAL, "environment variable $SOURCE_DATE_EPOCH: "
+ "trailing garbage: %s\n", endptr);
+ if (epoch < 0)
+ cpp_error (pfile, CPP_DL_FATAL, "environment variable $SOURCE_DATE_EPOCH: "
+ "value must be nonnegative: %lld \n", epoch);
+
+ return (time_t) epoch;
+}
+
/* Initialize the source_date_epoch value. */
-void
-cpp_init_source_date_epoch (cpp_reader *pfile, time_t source_date_epoch)
+static void
+cpp_init_source_date_epoch (cpp_reader *pfile)
{
- pfile->source_date_epoch = source_date_epoch;
+ pfile->source_date_epoch = get_source_date_epoch (pfile);
}
/* Sanity-checks are dependent on command-line options, so it is
--
2.8.1.369.geae769a