preprocessor


Test for empty macro definition


I've got a set of debug macros in tracing.hh. Whether it generates code and output is controlled by a macro flag in the real source code:
// File: foo.cc
#define TRACING 0
#include "tracing.hh"
// Away we go . . .
TRACEF("debug message");
The flag TRACING should have a value; I usually toggle between 0 and 1.
Within tracing.h,
#ifdef TRACING will tell me that tracing was defined.
#if TRACING controls the definition of functional macros like TRACEF()
But what if TRACING has no value? Then #if TRACING produces an error:
In file included from foo.c:3:
tracing.hh:73:12: error: #if with no expression
How can I test if TRACING is defined but has no value?
With Matti's suggestion and some more poking, I think the issue is this: if TRACING has no value, we need a valid preprocessor expression in the test #if .... The Gnu cpp manual
says it has to evaluate to an integer expression, so we need an expression that is valid even if one of the arguments is missing. What I finally hit on is:
#if (TRACING + 0)
# . . .
If TRACING has a numerical value (as in #define TRACING 2 \n), cpp has a valid expression, and we haven't changed the value.
If TRACING has no value (as in #define TRACING \n), the preprocessor evaluates #if (+0) to false
The only case this doesn't handle is
If TRACING has a token value (i.e., ON). The cpp manual says "Identifiers that are not macros . . . are all considered to be the number zero," which evaluates to false. In this case, however, it would make more sense to consider this a true value. The only tokens that do the right thing are the boolean literals true and false.
Would
#if defined(TRACING) && !TRACING
do the trick?

Related Links

Compiling with ocamlbuild and camlp5
How not to output comments using the -C operation in mcpp
C++ Builder File Version not correct
How do you a preprocess statement for #include
Does the preprocessor pass environment variables?
YAML preprocessor / macro processor
Pre-Processing using m4
Is there a practical reason for “#if defined(X) && (X != 0)”?
How to check for presence of a directory in Inno Setup preprocessor?
What are analogs of “#ifdef”, “#ifndef”, “#else”, “#elif”, “#define”, “#undef” in D programming lnaguage?
Image pre-processing in OCR
which is more important, number of variables or subexpressions?
What is the purpose of using #define to define a constant with no value?
Name of file during compile time in Progress 4gl
Triple conditional check with ifdef in M4
How to process map data from OpenStreetMap with Java?

Categories

HOME
google-app-engine
c++11
google-spreadsheet
rsa
replace
opendj
aws-cli
android-json
crystal-lang
fhir
alamofire
emoji
line
filechannel
angular-dart
gruntfile
mvc-mini-profiler
easyphp
git-extensions
mapstruct
sse
enterprise-miner
sencha-touch
lilypond
breadth-first-search
rubymotion
visual-studio-extensions
resourcebundle
jboss6.x
game-theory
fragment-backstack
progress-bar
spotipy
exchangewebservices
failover
finagle
xbox
playstation-portable
pop3
odroid
plaintext
glm-math
apollostack
apache-spark-dataset
x++
term
chai-as-promised
facebook-pixel
google-cse
zenhub
cup
mathcad
jtree
smartfoxserver
pure-css
hadoop-2.7.2
winusb
data-protection
concept-insights
dynatree
xvim
include-guards
apache-commons-daemon
public-html
ejb-2.x
execve
sip-server
crowd
big-ip
template-deduction
codeplex
high-resolution
scriptaculous
mt4j
diem
device-width
joomla3.1
fitbounds
denied
screen-size
uipagecontrol
massive
object-database
drupal-fivestar
zen
skype4java
google-wave
int64

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App