macro to control conditional compilation based on date

R

richardlang

Anyone out there ever come across a preprocessor macro that compares an
argument value against the predefined __DATE__ macro in order to
control conditional compilation based on date.

Something along the lines of...

# define DateLaterThan(x) ...

that could be used for things like

#if DateLaterThan("Jun 15 2006")
#error The following debug code can be deleted now
#end


Would be pretty handy to be able to leave a forceful reminder in souce
code to prompt cleanup of ugly quick fixes, tempory diagnostics etc.
etc. rather than passive TODO/FIXME type comment tags.
 
R

Rod Pemberton

Anyone out there ever come across a preprocessor macro that compares an
argument value against the predefined __DATE__ macro in order to
control conditional compilation based on date.

(First post was yesterday and also to comp.lang.c.moderated , so it'll
probably show up here a week from now... ;)

Nope.

But, you "might" be able to do something similar using assert(), strcmp(),
localtime(), strftime(), and __DATE__.


Rod Pemberton
 
V

Vladimir S. Oka

Anyone out there ever come across a preprocessor macro that compares
an argument value against the predefined __DATE__ macro in order to
control conditional compilation based on date.

Something along the lines of...

# define DateLaterThan(x) ...

that could be used for things like

#if DateLaterThan("Jun 15 2006")
#error The following debug code can be deleted now
#end


Would be pretty handy to be able to leave a forceful reminder in souce
code to prompt cleanup of ugly quick fixes, tempory diagnostics etc.
etc. rather than passive TODO/FIXME type comment tags.

__DATE__ is not part of standard C, and `DateLaterThan` certainly isn't
either. I'd expect that some compilers may implement the former, but
I'd be surprised if that were the case for the latter.

All this is off-topic here (only standard C is discussed). You're much
better of asking in your particular compiler's group. Note, however,
that if you change compilers sometime down the line, you're neat
solution is likely to stop working.
 
V

Vladimir S. Oka

Anyone out there ever come across a preprocessor macro that compares an
argument value against the predefined __DATE__ macro in order to
control conditional compilation based on date.

Something along the lines of...

# define DateLaterThan(x) ...

that could be used for things like

#if DateLaterThan("Jun 15 2006")
#error The following debug code can be deleted now
#end


Would be pretty handy to be able to leave a forceful reminder in souce
code to prompt cleanup of ugly quick fixes, tempory diagnostics etc.
etc. rather than passive TODO/FIXME type comment tags.

__DATE__ is not available in standard C. Even if your compiler provides
it, and you can use it in the way you want, it's not going to be
portable. Switching to a different compiler is almost certain to break
it. If you still want to pursue this avenue, it's best asking in the
group that discusses your particular compiler.

An idea: why don't you mandate that TODO/FIXMEs are #ifdef-ed in a
manner similar to:

#if FIXME
#error "This needs fixing NOW!"
#else
/* your temporary fix */
#endif

Then, on normal days you #define (or pass as an option to the compiler)
FIXME as 0, and on special occassions as 1. If you pass it through the
compiler command line, you may also play with passing time-dependant
value and comparing against it. This is still likely to be
non-portable, though.
 
E

Eric Sosman

Anyone out there ever come across a preprocessor macro that compares an
argument value against the predefined __DATE__ macro in order to
control conditional compilation based on date.

Something along the lines of...

# define DateLaterThan(x) ...

that could be used for things like

#if DateLaterThan("Jun 15 2006")
#error The following debug code can be deleted now
#end

Would be pretty handy to be able to leave a forceful reminder in souce
code to prompt cleanup of ugly quick fixes, tempory diagnostics etc.
etc. rather than passive TODO/FIXME type comment tags.

The unaided preprocessor can't do it, because even though
it generates the literal corresponding to __DATE__ it's unable
to look at the contents of the literal (the literal doesn't
become a string until long after the preprocessor finishes,
and besides the preprocessor has no access to the memory that
holds the string).

Although the preprocessor won't do what you want, the
compiler itself can give some help. You could use Martin
Dickopp's trick

http://groups.google.com/group/comp.lang.c/msg/22b053811e98f490

to derive values from the __DATE__ string and a similar
string you supply, and then you could use the two values
in a relational expression in a context that causes a
compile-time error if they're not in the correct relation,
such as declaring an array whose dimension is invalid if
__DATE__ is too late.

However, I'd suggest you seek other avenues. A long time
ago I, too, longed for such a construct -- I had just tracked
down and fixed a performance problem caused by a "temporary"
hack that had remained in the code through two major product
versions and into the development phase of a third. I thought
that if the compiler could have alerted us that the code needed
attention, much grief would have been avoided. After pondering
for a considerable time, though, I came to the conclusion that
the compiler's job is to translate the code it's given, not to
act as a software engineering methodology enforcer. There are
other tools to handle the latter function, and they're capable
of things that are completely outside the compiler's purview.

Also, __DATE__ is rather inflexible. You'll put in some
sort of hack and then estimate how long it'll be before you
simply must come back and do a better job, but schedules for
software development are notoriously elastic: your estimate of
the deadline is not going to be reliable. Yes, you can always
respond to the compiler's complaint by postponing the drop-dead
date, but that's just make-work -- and since Murphy never sleeps,
you can be sure the drop-dead will happen on the very night of
the Giant Rebuild before the Big Demonstration ...

If you must insert time bombs in the compilation, I'd suggest
you base them on things like product version numbers, "build for
release" flags, and the like: they are sensitive to the status
of the development rather than to the passage of time. I once
needed to hack around an error in the vendor-supplied printf()
by substituting my own version, so I had something like

#include <stdio.h>
#ifdef PLATFORM_X
#ifdef DEBUG_BUILD
/* Beta versions of printf() et al. print
* floating-point values at one-quarter
* actual magnitude. Use our own substitutes
* until Vendor X fixes this.
*/
#undef printf
#undef fprintf
#undef sprintf
#define printf fake_printf
#define fprintf fake_fprintf
#define sprintf fake_sprintf
#else
#error "Do we still need this hack?"
#endif
#endif

(The reality, of course, was more elaborate.) IMHO this sort of
thing makes more sense than date comparisons -- but it's still no
substitute for self-discipline.
 
D

Douglas A. Gwyn

#if DateLaterThan("Jun 15 2006")
#error The following debug code can be deleted now
#end
Would be pretty handy to be able to leave a forceful reminder in souce
code to prompt cleanup of ugly quick fixes, tempory diagnostics etc.
etc. rather than passive TODO/FIXME type comment tags.

It's pretty rare that one actually wants code to become
uncompilable at a particular date. If you think there
is a need for this, it is indicative of a software
development management problem, which needs to be
addressed in some other way.
 
R

Rod Pemberton

Anyone out there ever come across a preprocessor macro that compares an
argument value against the predefined __DATE__ macro in order to
control conditional compilation based on date.

Something along the lines of...

# define DateLaterThan(x) ...

that could be used for things like

#if DateLaterThan("Jun 15 2006")
#error The following debug code can be deleted now
#end

Nope.

But, you "might" be able to do something similar using assert(), strcmp(),
localtime(), strftime(), and __DATE__.


Rod Pemberton
 
W

WillerZ

Anyone out there ever come across a preprocessor macro that compares an
argument value against the predefined __DATE__ macro in order to
control conditional compilation based on date.

Something along the lines of...

# define DateLaterThan(x) ...

that could be used for things like

#if DateLaterThan("Jun 15 2006")
#error The following debug code can be deleted now
#end

No such macro can possibly be written within the constraints of the
standard C preprocessor.
 
M

Marc Thrun

Vladimir said:
__DATE__ is not part of standard C, and `DateLaterThan` certainly isn't
either. I'd expect that some compilers may implement the former, but
I'd be surprised if that were the case for the latter.

All this is off-topic here (only standard C is discussed). You're much
better of asking in your particular compiler's group. Note, however,
that if you change compilers sometime down the line, you're neat
solution is likely to stop working.

__DATE__ certainly is part of C99, quoting from 6.10.8p1:

__DATE__ The date of translation of the preprocessing translation

unit: 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. If
the date of translation is not available, an
implementation-defined valid date shall be supplied.
 
M

Micah Cowan

Marc Thrun said:
__DATE__ certainly is part of C99, quoting from 6.10.8p1:

__DATE__ The date of translation of the preprocessing translation
unit: 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. If
the date of translation is not available, an
implementation-defined valid date shall be supplied.

It's in my draft copy of C89, as well. Guaranteed to have a suitable
value, even when the date is not available.
 
V

Vladimir S. Oka

Micah Cowan opined:
It's in my draft copy of C89, as well. Guaranteed to have a suitable
value, even when the date is not available.

It's my silly assumption that the Standard would actually have the two
underscores consecutive. As it turns out, they've split them with a
blank to emphasise there's two. That, of course, foils any attempt to
search for `__DATE__`, as it's `_ _DATE_ _`.

I apologise. I should have looked more carefully (I normally don't use
those macros).
 
C

CBFalconer

Vladimir S. Oka said:
.... snip ...

It's my silly assumption that the Standard would actually have the
two underscores consecutive. As it turns out, they've split them
with a blank to emphasise there's two. That, of course, foils any
attempt to search for `__DATE__`, as it's `_ _DATE_ _`.

Look again. At least in N1124.pdf that is not so. However your
fonts may be displaying the underline as something that shows blank
space between instances.
 
M

Micah Cowan

CBFalconer said:
Look again. At least in N1124.pdf that is not so. However your
fonts may be displaying the underline as something that shows blank
space between instances.

No, it is so. Try doing a search of __DATE__ on that page. At least,
xpdf won't find it.

<OT>
I don't think they actually "split them with a blank" per se. I'm not
going to bother actually looking into the nuts and bolts, but unless
they specifically tagged the content, or otherwise gave specific clues
about the nature of the textual content, there's usually not a clean
mapping from PDF content to plain text (which is necessary for
performing searches). Often, a heuristic is applied to try to figure
out where spaces are (since they pretty much never result from an
actual space character). Often, actual location is ignored, and what
gets looked at is what text appears atomically in a single
text-writing operation.
</OT>

Interestingly enough, a search for "_" (the underscore) in xpdf on
that page only seems to find the first character of each of those
predefined macros in 6.10.7.
 
V

Vladimir S. Oka

Micah Cowan opined:
No, it is so. Try doing a search of __DATE__ on that page. At least,
xpdf won't find it.

Nor will Acrobat Reader for Linux (v7.0.1 on SUSE 10.0).
<OT>
I don't think they actually "split them with a blank" per se. I'm not
going to bother actually looking into the nuts and bolts, but unless

I can't be bother either, but at least now I know what to search for
when I want to find a double underscore in it.
 
C

CBFalconer

Micah said:
No, it is so. Try doing a search of __DATE__ on that page. At least,
xpdf won't find it.

Exactly what I did - I loaded n1124.pdf into acroread, and
searched. I suspect your use of 'page' implies something
different.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
A

Al Balmer

Micah Cowan opined:


Nor will Acrobat Reader for Linux (v7.0.1 on SUSE 10.0).


I can't be bother either, but at least now I know what to search for
when I want to find a double underscore in it.

This is strange. My copy of the C99 pdf, as interpreted by Acrobat
5.1.0, shows a leading double underscore, and a trailing underscore,
space, underscore for __FILE__, __LINE__, and __func__ in their first
occurrence, 7.2.1.1. Copy and paste into a hex editor confirms this.
 
P

pete

Al said:
My copy of the C99 pdf, as interpreted by Acrobat
5.1.0, shows a leading double underscore, and a trailing underscore,
space, underscore for __FILE__, __LINE__, and __func__ in their first
occurrence, 7.2.1.1. Copy and paste into a hex editor confirms this.

Same here.
This is my copy and paste of C99 from acrobat into my newsreader:
__FILE_ _ and __LINE_ _


This is copied and pasted from the text version of n869:
__FILE__ and __LINE__

I like text files.
 
V

Vladimir S. Oka

pete opined:
Same here.
This is my copy and paste of C99 from acrobat into my newsreader:
__FILE_ _ and __LINE_ _


This is copied and pasted from the text version of n869:
__FILE__ and __LINE__

I like text files.

Copy/paste from my N1124.pdf as read by Adobe Reader 7.0.1 for Linux:

_ _DATE_ _

Weird...

--
Receiving a million dollars tax free will make you feel better than
being flat broke and having a stomach ache.
-- Dolph Sharp

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
 
M

Mark McIntyre

occurrence, 7.2.1.1. Copy and paste into a hex editor confirms this.

7.2.1 mentions "__FILE_ _ and __LINE_ _" but visually, it appears as
"_ _FILE_ _ and _ _LINE_ _"

FWIW 6.10.8 of the ISO Standard has
"_ _DATE_ _ " in both visual and searchable values.

I reckon its an artefact of the justifying typsetter engine in
Acrobat..







Mark McIntyre
 
M

Micah Cowan

CBFalconer said:
Exactly what I did - I loaded n1124.pdf into acroread, and
searched. I suspect your use of 'page' implies something
different.

Nope. Must be differences in our readers. I'm using xpdf, you're using
acroread.

But Vlad seems to be using acroread (7) as well. Maybe they mucked
that up since your version?

Huh. Wierd.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top