CPP: Macro Shielding

Is there a way to avoid macro names (identifiers) in the c (cpp) pre-processor?

I want to conditionally highlight some web code (html, css ...) with readable macro names.

Example for conditional css file:

/*root*/ some rootcode #if height==480 /* height 480 */ .page { line-height:23px; } #elif height<480 /* height < 480 */ .page { line-height:46px; } #endif 

Call

 cpp -P -D height=480 -oout.css css.ccss 

results in (after deleting newline)

 some rootcode .page { line-480:23px; } 

but "line- 480 " is incorrect.

Is there a way to avoid "height" in the code without changing the name of the macro or its substring?

+4
source share
2 answers

I played with Lucian Grigore's idea to identify invalid file names, and I found a (almost) general solution to this problem:

including "define.file" at the beginning of the conventions and "undefine.file" after this condition.

Thus, the problem boils down to two macro names that must remain: DEFINEFILE and UNDEFINEFILE. But these two macros can be encrypted with their own hash code or with a random name to avoid using these names in conditional text.

"define.file":

 #define height 480 #define os 1 

"undefine.file"

 #undef height #undef os 

"conditionalcss.ccss"

 /*root*/ some rootcode #include __DEFINEFILENAMEPATH__ #if height==480 #include __UNDEFINEFILENAMEPATH__ /* height 480 */ .page { line-height:23px; } #include __DEFINEFILENAMEPATH__ #elif height<480 #include __UNDEFINEFILENAMEPATH__ /* height > 480 */ .page { line-height:46px; } #include __DEFINEFILENAMEPATH__ #endif #if os==1 #include __UNDEFINEFILENAMEPATH__ os is windows (if 1 refers to windows) and height also undefined i hope #endif 

Finally, cppcall with parametric definition and file definition:

 cpp -P -D __DEFINEFILENAMEPATH__="\"define.file\"" -D __UNDEFINEFILENAMEPATH__="\"undefine.file\"" -oout.css css.ccss 

With this idea, the desired "out.css" looks like this:

 some rootcode .page { line-height:23px; } os is windows (if 1 refers to windows) and height also undefined i hope 

This solution has only the drawbacks of the two macros and the possible poor quality due to multiple imports.

I hope this helps other people solve their problems.

Greetz adreamus

0
source

You can:

1) define a macro:

 #undef height 

2) rename the macro using standard stubs:

 #define HEIGHT 

3) Use protection before processing the file:

 #if height==480 #define HEIGHT_480 #undef height #endif #if height>480 #define HEIGHT_OVER_480 #undef height #endif /*root*/ some rootcode #if HEIGHT_480 /* height 480 */ .page { line-height:23px; } #elif HEIGHT_OVER_480 /* height < 480 */ .page { line-height:46px; } #endif 

The first will lose information after uncertainty. The second is impractical if the macro is widely used.

The third option is the best IMO option. I saw how it was used in production code, where it was needed.

+2
source

Source: https://habr.com/ru/post/1384081/


All Articles