linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Damien Lespiau <damien.lespiau@gmail.com>
To: linux-sparse@vger.kernel.org
Cc: Damien Lespiau <damien.lespiau@gmail.com>
Subject: [PATCH] __DATE__ & __TIME expansion
Date: Sun, 20 May 2007 23:59:46 +0200	[thread overview]
Message-ID: <11796983861111-git-send-email-damien.lespiau@gmail.com> (raw)
In-Reply-To: <11796983862564-git-send-email-damien.lespiau@gmail.com>

Makes __DATE__expand to a character string literal of
the form "Mmm dd yyyy" where the names of the months
are the same as those generated by the asctime function,
and the first character of dd is a space character if the
value is less than 10.
Makes __TIME__ expand to a a character string literal of
the form "hh:mm:ss" as in the time generated by the
asctime function.

So says C99.

Signed-off-by: Damien Lespiau <damien.lespiau@gmail.com>
---
 ident-list.h                        |    2 ++
 lib.c                               |    2 --
 pre-process.c                       |   11 +++++++++++
 validation/preprocessor-date-time.c |   22 ++++++++++++++++++++++
 4 files changed, 35 insertions(+), 2 deletions(-)
 create mode 100644 validation/preprocessor-date-time.c

diff --git a/ident-list.h b/ident-list.h
index 7633a2f..dad22ef 100644
--- a/ident-list.h
+++ b/ident-list.h
@@ -75,6 +75,8 @@ __IDENT(pragma_ident, "__pragma__", 0);
 __IDENT(__VA_ARGS___ident, "__VA_ARGS__", 0);
 __IDENT(__LINE___ident, "__LINE__", 0);
 __IDENT(__FILE___ident, "__FILE__", 0);
+__IDENT(__DATE___ident, "__DATE__", 0);
+__IDENT(__TIME___ident, "__TIME__", 0);
 __IDENT(__func___ident, "__func__", 0);
 __IDENT(__FUNCTION___ident, "__FUNCTION__", 0);
 __IDENT(__PRETTY_FUNCTION___ident, "__PRETTY_FUNCTION__", 0);
diff --git a/lib.c b/lib.c
index 2162007..b678990 100644
--- a/lib.c
+++ b/lib.c
@@ -602,8 +602,6 @@ void create_builtin_stream(void)
 
 	/* FIXME! We need to do these as special magic macros at expansion time! */
 	add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
-	add_pre_buffer("#define __DATE__ \"??? ?? ????\"\n");
-	add_pre_buffer("#define __TIME__ \"??:??:??\"\n");
 
 	if (optimize)
 		add_pre_buffer("#define __OPTIMIZE__ 1\n");
diff --git a/pre-process.c b/pre-process.c
index afae77a..eb291f2 100644
--- a/pre-process.c
+++ b/pre-process.c
@@ -18,6 +18,7 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <limits.h>
+#include <time.h>
 
 #include "pre-process.h"
 #include "lib.h"
@@ -141,6 +142,8 @@ static int expand_one_symbol(struct token **list)
 {
 	struct token *token = *list;
 	struct symbol *sym;
+	static char buffer[12]; /* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */
+	time_t t;
 
 	if (token->pos.noexpand)
 		return 1;
@@ -154,6 +157,14 @@ static int expand_one_symbol(struct token **list)
 		replace_with_integer(token, token->pos.line);
 	} else if (token->ident == &__FILE___ident) {
 		replace_with_string(token, stream_name(token->pos.stream));
+	} else if (token->ident == &__DATE___ident) {
+		time(&t);
+		strftime(buffer, 12, "%b %e %Y", localtime(&t));
+		replace_with_string(token, buffer);
+	} else if (token->ident == &__TIME___ident) {
+		time(&t);
+		strftime(buffer, 9, "%T", localtime(&t));
+		replace_with_string(token, buffer);
 	}
 	return 1;
 }
diff --git a/validation/preprocessor-date-time.c b/validation/preprocessor-date-time.c
new file mode 100644
index 0000000..18d8bdb
--- /dev/null
+++ b/validation/preprocessor-date-time.c
@@ -0,0 +1,22 @@
+/*
+ * __DATE__ & __TIME__ macros handling test.
+ */
+
+/*
+ * Testing it against GCC preprocessor in the same second gives:
+ *
+ * $ gcc -E validation/preprocessor-date-time.c 
+ * # 1 "validation/preprocessor-date-time.c"
+ * # 1 "<built-in>"
+ * # 1 "<command line>"
+ * # 1 "validation/preprocessor-date-time.c"
+ * # 11 "validation/preprocessor-date-time.c"
+ * "May 20 2007" "20:19:00"
+ * $ ./cgcc -E validation/preprocessor-date-time.c 
+ * 
+ * "May 20 2007" "20:19:00"
+ * $
+*/
+
+__DATE__ __TIME__
+
-- 
1.5.2.rc3.87.g404fd

  reply	other threads:[~2007-05-20 21:59 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-05-20 21:59 [PATCH] Beautify all & install Makefile targets Damien Lespiau
2007-05-20 21:59 ` Damien Lespiau [this message]
2007-05-20 22:50   ` [PATCH] __DATE__ & __TIME expansion Neil Booth
2007-05-20 23:02     ` Josh Triplett
2007-05-20 23:49       ` Damien Lespiau
2007-05-21  0:06         ` Derek M Jones
2007-05-20 22:52   ` Josh Triplett
2007-05-21  0:35 ` [PATCH] Beautify all & install Makefile targets Josh Triplett
2007-05-22 18:28   ` Damien Lespiau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=11796983861111-git-send-email-damien.lespiau@gmail.com \
    --to=damien.lespiau@gmail.com \
    --cc=linux-sparse@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).