From patchwork Thu May 19 00:02:28 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jessica Clarke X-Patchwork-Id: 623767 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3r9B92118Bz9t3h for ; Thu, 19 May 2016 10:02:54 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=XbIObc7t; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=NGRXD6KigvujsWuocdMSuUlQ1hsFPGgtCQkIxtCprtqI537kDL2wY iacJL5LCv0B3CJj/25CO2z/FPrxwfDS8WcOrSu1odM4B+41u3riJE+iFpeHbHonY rHP9F2wqngn5C0XItWl191EUMw4RcPAUDZ8oOd6RoZ79fdYr5zRqpg= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=OBIoPcVgnyElAJXDmmZKYNypaE0=; b=XbIObc7tKCW521W7KHgO jzjvNwzwrkokIwWXCLYB/f3ZT6kdsVMHBCPtXsmyiXLtlZl5xeyef11h58a8Pf6U cbVejYtFT/+8kjoT+ljr5Skx3cNAY3uuC0gndHUPCR4JdwdjEDqI0hPShnoFPCh5 prbLSs/ZKNR8N5j7q38ETI4= Received: (qmail 12236 invoked by alias); 19 May 2016 00:02:45 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 12223 invoked by uid 89); 19 May 2016 00:02:44 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy=H*r:501, 1978, *table, contradictory X-HELO: mail-wm0-f41.google.com Received: from mail-wm0-f41.google.com (HELO mail-wm0-f41.google.com) (74.125.82.41) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Thu, 19 May 2016 00:02:33 +0000 Received: by mail-wm0-f41.google.com with SMTP id a17so102125972wme.0 for ; Wed, 18 May 2016 17:02:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition:user-agent; bh=kf7xEQ9Ght+vzB5bOCt+by/n4Fs7Ds0cMZ23XCtHDeQ=; b=L8zn4nhsTvQ9KiynPZwUNeFbzMTZiNQf6eg6xca06OzxuDt6sToUC5Oa6pvEXERbEs 3/V3rnCWA//QVmtJr+kA7/0H+eiche9ab4oUkmiZJrxZejZBB0KXLko5XUajjFUo4qEZ D1zTg6GGrxB6XfSZbScwgQOTwI93jpnRu8NtY1Vq+vkMzI6/advhbPk4aSqhxBh/zDIa o5HChuQStGTZv5zaeLEjo29w/O/x1fuEyN0Sa5lP21T9KHBcutb67+pHJ17bhP/5tumS EotDNnEvoZ9v3NwSF0JeOvAJoSMgacj6vpE1b6PFpzpUHmufoHj+97QfuBFK6YyZ5Eg7 qlHw== X-Gm-Message-State: AOPr4FXA+5a+QsNGkzLShnSHgjaAimXqjYapGMHRRc46nGDqLJYWpB+dKE/ALXXAyI07mg== X-Received: by 10.194.63.226 with SMTP id j2mr10027357wjs.27.1463616150409; Wed, 18 May 2016 17:02:30 -0700 (PDT) Received: from Jamess-MacBook.local (global-184-8.nat-1.net.cam.ac.uk. [131.111.184.8]) by smtp.gmail.com with ESMTPSA id w10sm10410645wjk.18.2016.05.18.17.02.29 (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 18 May 2016 17:02:29 -0700 (PDT) Received: by Jamess-MacBook.local (Postfix, from userid 501) id 977E0378151F; Thu, 19 May 2016 01:02:28 +0100 (BST) Date: Thu, 19 May 2016 01:02:28 +0100 From: James Clarke To: gcc-patches@gcc.gnu.org Subject: [PATCH] Ensure source_date_epoch is always initialised Message-ID: <20160519000228.GA52716@Jamess-MacBook.local> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.6.1 (2016-04-27) 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 +# . + +# 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