Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

Working on huge projects, inevitably there are millions of pre-processor wrapped code, s owe see

#ifdef MY_COMPLETE_MISLEADING_NAME
//some stuff
#endif

So I want to see when I compile which of these pre-processor directives are defined and which are not. I am talking about VS 2015.So I would love to be able to chose a build config ie prod/release/debug and then just let the pre-p parse the code and pull out all the values of the defines and list them, super quick as its pre compilation.

Anybody know how to do this?



What I have tried:

I can put manual code in to see the values. So I've tried creating a dummy file and doing this

bool PREP_STUPID_NAME1 = false;
#ifdef STUPID_NAME1
PREP_STUPID_NAME1 = true;
#endif

Hehe that's a point, any genius pre-processor gurus out there that could write a macro that would automatically generate the file I am talking about ? So parse all the code, stick all the defines in a file, create a bool for each one, and then repeat the above code etc


Marcus
Posted
Updated 24-Jan-17 10:04am

There is no option for the VS compiler.

But if you can install GCC/G++ (or have it already) you can use that with the -dM -E options.

See also C/C++ tip: How to list compiler predefined macros | Nadeau Software[^] for other compilers.

[EDIT]
To process all input files from a project you can create a Custom Build Rule that is executed for each source file. The tool may be a self written parser or just the GCC/G++ with optional post-processing (another self created tool for filtering and formatting). Candidates for filtering tools are regular expression command line utilities. If you know Linux a little bit, you may use grep and sed from the Native Win32 ports of some GNU utilities[^] (UnxUtils).
[/EDIT]
 
Share this answer
 
v2
Comments
Albert Holguin 24-Jan-17 16:06pm    
+5... but if OP is simply trying to find the state of a definition within a file, my solution is probably the simplest thing to do.
You can do simple pragma messages/warnings... probably simpler than spilling all the contents of every pre-processor definition in a large project.
C++
#ifdef SOMEVAR
  #pragma message("SOMEVAR is defined here")
#else
  #pragma message("SOMEVAR is NOT defined here")
#endif

Pragma message[^]
 
Share this answer
 
Comments
marcus obrien 24-Jan-17 17:47pm    
That sounds reasonable, it would be great to have script/feature in VS that would grep through the code, output all the defined pre-proc directives to a file with the pragma code above wrapped around each one, automatically, then one could compile the source and see the values of every define in the code base. I'm working on a VS sln with over 300 projects in it. Maybe I could generate a script, unless anyone has got one already ?
Albert Holguin 24-Jan-17 21:49pm    
Haven't seen one... but I've moved over to the gcc toolset a bit ago.
Stefan_Lang 26-Jan-17 3:37am    
I don't think that would be sensible, or if you had such a tool, that it would give you results that you'd consider sensible. That tool would have to scan the code, the include files included by that code, the include files included by the include files, and so on, down to the most rmeote system header file that is somehow in some obscure way connected. Even the most trivial program therefore would end up listing thousands of symbols defined in system headers that you've never heard of and that you're definitely not interested in.

What's more, most of these symbols would only be defined in a limited scope of the headers and code files. So, even if you somehow manage to spot a symbol you're interested in, you woldn't know which part of the code is aware of it, and which isn't.

In the end, you'd still need Albert's code to find out whether that symbol is actually known at the specific location in your code that you're interested in.

What you need very much depends on what you want to do with that information.

If you want to make sure that a new symbol name you're thinking of doesn't collide with an already existing one, my advice is (a) not to use #define at all, unless you absolutely cannot avoid it and (b) simply go by the general guidelines for choosing #define symbols.

Otherwise, Alberts solution seems fine to me.
marcus obrien 26-Jan-17 4:13am    
Hi, Naming clashing is by no means what I am interested in. All the code I work on is contained in namespaces, so this is not an issue I am bothered about. I think you missed the point of what I am asking, what I want is a way to tell what pre-processor defined code will be included in the current build config.
The results would be totally sensible, and exactly what I want. I do want to know every pre-processor variable's state, in order to ascertain what the compiler will build. The code in this solution is written this way due to it being a huge project with 400 projects in various languages, assembler, C, C++, C#, and scripts etc. It has to compile on 9 different platforms, and can be both native and cross compiled, so it relies heavily on code gen and platform specific code that is controlled by the pre-processor. I am asking if there is a tool that exists that would grep for all the #define, then create a fiel to dump them in using the generic macro of #ifdef <pre-proc directive> #pragma message("VAR" state is blah #else #prgma message <pre-proc dir> NOT defined #endif. I'm sure other people when they see a line of code like this #if (define MONKEY) && !(define DOG) || RELEASE || (DEBUG && FOG) || NO_TRACE && GOAT etc.
Stefan_Lang 26-Jan-17 4:59am    
Well, #define symbols completely ignore namespaces, so those will not protect you from nameclashes. Point in case: some windows header file(s?) #define the macros min and max, which clash with e. g. std::valarray::min() and std::valarray::max() (not sure if this is still the case with VS2015)

As for grepping all #defines, I already pointed out above how that would get you thousands of symbols defined in system headers in addition to those defined in your projects. I have some serious doubt that you will find that useful.

However, if your goal is to find out what code is actually included in your build, then just send your code through the preprocessor without compilation. In VS this should be possible using the /P switch - see http://stackoverflow.com/questions/277258/how-do-i-see-a-c-c-source-file-after-preprocessing-in-visual-studio for more information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900