Check for concatenated define in C preprocessor
NickName:Jul3k Ask DateTime:2016-09-01T21:09:14

Check for concatenated define in C preprocessor

I access registers by concatenated defines unsing the function GETREG

#define NUMBER 1 //changes

#define REG1 register.N1
#define REG2 register.N2
#define REG8 register.N8

#define GETREG_(N) REG ## N
#define GETREG(N) GETREG_(N)

Sometimes the registers for that NUMBER are not defined so i want to make sure the macro correctly expands before inserting it in the code so i tried to do:

#define NUMBER 5

#ifdef GETREG(NUMBER)
GETREG(NUMBER) = 0
#endif

However this always seems to evaluate as true and the compiler prints

Warning:extra tokens at end of #ifdef directive


Background Story:

In my projects I create libraries to interface with a HAL to abstract the hardware level. Often from one project to another the codebasis in the HAL stays exactly the same but just the location of pins changes. For that reason i would like to use macro-expansion to access the pins. The following macro does the job for me at adressing the analog features of the pin:

#define ANSEL_(Pin)  _ANS ## Pin
#define ANSEL(...) ANSEL_(__VA_ARGS__)

that way i can turn on or off analog features by:

#define PIN_RX A0
ANSEL(PIN_RX)= 0;

and other registers by similar macros. The problem i am facing now is that some pins for example do not have analog features (e.g. Pin A5). Because of that i would like to test if the define in the library esists. I was tryin to do this by:

#ifdef ANSEL(PIN_RX)
ANSEL(PIN_RX)= 0;
#endif

However this simple approach is not working.

The microcontroller (PIC33) lib: Edit: Shipped from the manufactorer

#define ANSELA ANSELA
extern volatile unsigned int  ANSELA __attribute__((__sfr__));
typedef struct tagANSELABITS {
  unsigned ANSA0:1;
  unsigned ANSA1:1;
  unsigned ANSA2:1;
  unsigned ANSA3:1;
  unsigned ANSA4:1;
  unsigned :4;
  unsigned ANSA9:1;
} ANSELABITS;
extern volatile ANSELABITS ANSELAbits __attribute__((__sfr__));

/* ANSELA */
#define _ANSA0 ANSELAbits.ANSA0
#define _ANSA1 ANSELAbits.ANSA1
#define _ANSA2 ANSELAbits.ANSA2
#define _ANSA3 ANSELAbits.ANSA3
#define _ANSA4 ANSELAbits.ANSA4
#define _ANSA9 ANSELAbits.ANSA9

Copyright Notice:Content Author:「Jul3k」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/39272406/check-for-concatenated-define-in-c-preprocessor

Answers
John Bollinger 2016-09-01T13:52:31

You have the wrong expectation for the behavior of the #ifdef directive. That directive is equivalent to #if defined, where defined is a preprocessing operator in that context. Although the condition of an overall #if directive is evaluated only after the line is fully macro-expanded, the defined operator operates on the next preprocessing token, which must have the form of an identifier, before macro expansion:\n\n\n Prior to evaluation, macro invocations in the list of preprocessing tokens that will become the controlling constant expression are replaced (except for those macro names modified by the defined unary operator) [...]\n\n\n[C2011, 6.10.1/4]\n\nThus, your\n\n#ifdef GETREG(NUMBER)\n\n\ntests whether GETREG is a defined macro, and the (NUMBER) constitutes a sequence of unexpected extra tokens.\n\nThe defined operator therefore cannot serve your purpose. It is conceivable that there is a way to use a more general #if directive to achieve your aim, but I'm not seeing it at the moment. Instead, I'm inclined to suggest letting the compiler find such errors for you, instead of expecting the preprocessor to do it. If compile time is too late, then perhaps you need to look to a build configuration system such as the Autotools or CMake could help you create.",


More about “Check for concatenated define in C preprocessor” related questions

Check for concatenated define in C preprocessor

I access registers by concatenated defines unsing the function GETREG #define NUMBER 1 //changes #define REG1 register.N1 #define REG2 register.N2 #define REG8 register.N8 #define GETREG_(N) REG...

Show Detail

Preprocessor directive #define specific to my machine

I have a Visual Studio 2013 project, using the VC++ 2010 Platform Toolkit. I know I could add a #define preprocessor directive by means of PROJECT > Properties > Configuration Properties > C/C++ >

Show Detail

C preprocessor #if expression

I am a bit confused on the type of expression we can use with the #IF preprocessor in the C language. I tried the following code, and it isn't working. Please explain and provide examples for expre...

Show Detail

effective C Preprocessor macros without #define

I want use Logger class for logging In C++, this class has static and use like : auto logger = Logger::GetInstance(); logger->Log(message, FileName, Line) for filename and Line number I can us...

Show Detail

#define preprocessor in c#

I am coming from a C++ background, trying to learn C#. I find it really annoying, that every time I wish to print something to the console I have to type something like Console.WriteLine("whatever...

Show Detail

Define a preprocessor macro with preprocessor comands

I'm using often a syntax like #ifdef __cplusplus extern "C" #endif void myCFunc(); so I tried to make a macro to have a syntax like CFUNC(void myCFunc()); I'm not relly sure if it's

Show Detail

configure: error: C preprocessor fails sanity check

I am compiling several libraries on Ubuntu 12.04 x86_64. First I compiled the libraries with GCC 4.7.2 and it went all well. Then I tryed to recompile them with Inte Composer 2013 u2. Fot that purp...

Show Detail

C recursive preprocessor define

I have incorporated libiniparser library in to my Android NDK application. One problem this library write logs directly to stdout/stderr. I did not want to heavily modify the code so I wrote a ma...

Show Detail

How to define Preprocessor Directive Global in c#

My Question is how to define Preprocessor Directive Global at one place for all C# classes in website.

Show Detail

When to use the preprocessor to define functions in C?

When should I use the C preprocessor to define a function? Here's a quick example: #define isUnderscore(ch) ((ch) == '_') Over this: // just to make the bool more clear. typedef enum { false...

Show Detail