I am trying to extract some string constants/literals from files, along with their line location. But sometimes, these are composited from macro definitions to get the final string. Something like:
do_stuff(PREFIX_STR "somename" SUFFIX_STR)
This will probably not be 100% foolproof even if I manage it, but I need some balance between complexity and reliability.
One way to get this done is to run compiler for those files with preprocessor only mode and read the result. The problem with that is that the result will look nothing like the original file. Again, the correct way to do this would be to analyze the AST, but that would be more work than this is worth.
I'd just like to tell the compiler "Do preprocessing only, but only resolve strings, ignore everything else". Any compiler will do, it does not have to be the same I use to build the code. So from a C++ file like this:
#include <vector>
#include "./my_header.h"
#define SOME_MACRO(x, y) anything could be here
#define PREFIX_STR "prefix"
void some_function()
{
do_stuff(PREFIX_STR "somename");
SOME_MACRO(PREFIX_STR "othername");
do_stuff("custom_prefix" "othername");
}
I'd like to get a file like this:
#include <vector>
#include "./my_header.h"
#define SOME_MACRO(x, y) anything could be here
void some_function()
{
do_stuff("prefixsomename");
SOME_MACRO("prefixothername");
do_stuff("custom_prefixothername");
}
I don't think I can get exactly what I want, but I'd appreciate any advice that helps me get as close as possible.