I'm trying to debug some boost::fusion stuff, and this would be exceedingly helpful, and I see no reason it's impossible - there's a stack of defined symbols and something analogous to a cursor/current position - has anyone built this?
You can pass your code through the Boost.Wave preprocessor that offers tracing facility: http://www.boost.org/libs/wave/doc/tracing_facility.html
Just wrap the code you want to trace in
#pragma wave trace(enable)
...
#pragma wave trace(disable)
and then run it with --traceto <trace-filename> command line argument, like
wave --traceto test.trace test.cpp
The test.trace file will have all the details of the macro expansion process of the traced part of your code.
Related
Is it possible to run a c macro function at compile time.
for example writing something in a file each time code is compiled.
Yes; No.
The macros do execute at compile time but there isn't much you can do directly with them other than mix text into the code.
Now, taking the software tools approach that unix pioneered (after all) you could conditionally generate output with #warning and then catch this with some script via a pipe.
Then that script could do stuff.
But, you probably wouldn't want to do that. Once you are running a script you could just have that script do whatever you want. Also, #error and #warning don't macro-expand the error or warning text, so using them for I/O is problematic.
This is obvious, I suppose, but how about using Ruby, Python, or the shell to script some macro processing?
Macros in C are strictly a text substitution tool. The output of the preprocessor is a preprocessed file.
So no, you can't do additional tasks like that from the preprocessor.
If you really want to perform certain tasks at compile time, that's what make is for.
I'm writing a small operating system for microcontrollers in C (not C++, so I can't use templates). It makes heavy use of some gcc features, one of the most important being the removal of unused code. The OS doesn't load anything at runtime; the user's program and the OS source are compiled together to form a single binary.
This design allows gcc to include only the OS functions that the program actually uses. So if the program never uses i2c or USB, support for those won't be included in the binary.
The problem is when I want to include optional support for those features without introducing a dependency. For example, a debug console should provide functions to debug i2c if it's being used, but including the debug console shouldn't also pull in i2c if the program isn't using it.
The methods that come to mind to achieve this aren't ideal:
Have the user explicitly enable the modules they need (using #define), and use #if to only include support for them in the debug console if enabled. I don't like this method, because currently the user doesn't have to do this, and I'd prefer to keep it that way.
Have the modules register function pointers with the debug module at startup. This isn't ideal, because it adds some runtime overhead and means the debug code is split up over several files.
Do the same as above, but using weak symbols instead of pointers. But I'm still not sure how to actually accomplish this.
Do a compile-time test in the debug code, like:
if(i2cInit is used) {
debugShowi2cStatus();
}
The last method seems ideal, but is it possible?
This seems like an interesting problem. Here's an idea, although it's not perfect:
Two-pass compile.
What you can do is first, compile the program with a flag like FINDING_DEPENDENCIES=1. Surround all the dependency checks with #ifs for this (I'm assuming you're not as concerned about adding extra ifs there.)
Then, when the compile is done (without any optional features), use nm or similar to detect the usage of functions/features in the program (such as i2cInit), and format this information into a .h file.
#ifndef FINDING_DEPENDENCIES
#include "dependency_info.h"
#endif
Now the optional dependencies are known.
This still doesn't seem like a perfect solution, but ultimately, it's mostly a chicken-and-the-egg problem. When compiling, the compiler doesn't know what symbols are going to be gc'd out. You basically need to get this information from the linker stage and feed it back to the compilation stage.
Theoretically, this might not increase build times much, especially if you used a temp file for the generated h, and then only replaced it if it was different. You'd need to use different object dirs, though.
Also this might help (pre-strip, of course):
How can I view function names and parameters contained in an ELF file?
I was wondering if it is possible, and if yes how, can I run a C preprocessor, like cpp, on a
C++ source file and only process the conditional directives #if #endif etc. I would like other
directives to stay intact in the output file.
I'm doing some analysis on C# code and there is no C# pre-processor. My idea is to run a C preprocessor on C# file and process only conditionals. This way for example, the #region directive, will stay
in the file, but cpp appears to remove #region.
You might be looking for a tool like coan:
Coan is a software engineering tool for analysing preprocessor-based configurations of C or C++ source code. Its principal use is to simplify a body of source code by eliminating any parts that are redundant with respect to a specified configuration.
It's precisely designed to process #if and #ifdef preprocessor lines, and remove code accordingly, but it has a lot of other possible uses.
The linux unifdef command does what you want:
http://linux.die.net/man/1/unifdef
Even if you're not on linux, there is source available on the web.
BTW, this is a duplicate of another question: Way to omit undefined preprocessor branches by default with unifdef?
Oh, this is the same task as I had in the past. I've tried cpp unifdef and coan tools - all of them stumbled upon special C# preprocessor things like #region. In the end I've decided to make my own one:
https://github.com/gaDZella/undefine.
The tool has a pretty simple set of options compared to the mentioned cpp tools but it is fully compatible with C# preprocessor syntax.
You can use g++ -E option to stop after preprocessing stage
-E -> stop after the preprocessing stage.The output is in the form of preprocessed source code, which is sent to the standard output
We have a code base that relies on lots of generated code generated by C macros.
If something goes wrong and there is a error or a warning, the compiler points at the line of the first macro expansion without telling more about where it went wrong inside the expanded code. I my particular case they are those /analyze warnings in Visual Studio.
Are there any tricks and tips that help find problems in complex preprocessor macros?
EDIT:
If you wonder why this code base have complex macros.
This is an emulator project where the decoding phase and execution phase is separated. For example instead of finding out during the execution of each instruction what addressing mode or operand size, etc is used, we generate a function for each combination with a DEFINE_INSTRUCTION macro which in turn generate the functions for all combinations. And chain these functions.
idea: dont ;) don't use macros that are complicated as you loose a lot of IDE support / compiler support
=> if you have such macros, refactor them into functions... maybe even inline functions
but seriously. to help you with the bad macros you're stuck with: As TripeHound said, there are flags to 'compile' C files only to the stage of preprocessed C files --
On the command line, clang -E foo.m will show you the preprocessed output.
I am writing a stateful scanner and I want to have a debugging symbol for every state change.
In my code I call a macro SETSTATE(ST_xxx) for instance, which does some nasty things, BUT I could easily also tell GCC to emit at that point a specific debugging symbol based on that name ST_xxx.
What I need to accomplish is setting a breakpoint in gdb.
I suppose it should be a #pragma or something.
If I only knew how ...
Though I might misunderstand the question,
how about making a dummy function and calling that in SETSTATE,
then setting a breakpoint in that function?
For example:
void dummy_breakpoint() {}
#define SETSTATE(st) dummy_breakpoint(); ...usual process...
Setting break dummy_breakpoint in .gdbinit might help some labor savings.
EDIT:
How about setting a watch-point in SETSTATE like the following, and
setting watch dummy_variable in .gdbinit?
char dummy_variable; /* global variable */
#define SETSTATE(st) ++ dummy_variable; ...usual process...
However, this might make the program's execution be slower if your
environment doesn't provide hardware watch-point...
If you want debugging symbols as a point of reference, you can use labels to create these (just make sure they aren't stripped out of the debug info if unreferenced), though having never used used gdb i'm not sure if it'll pick up labels like ollydbg does with obj scanning/analysis. but seeing as its breakpoints your after, why not just use a debug trap, like msvc's __debugbreak()?, something from here might be of use for the gcc variant: Is there a portable equivalent to DebugBreak()/__debugbreak?
On compile use -D ST_xxx
I use this for enabling debugging messages, using macros. It defines the constant ST_xxx with value 1.