在没有预处理器的情况下展开C/C++函数宏

Inv*_*ion 7 c++ c-preprocessor

如何在C/C++文件中测试/扩展所有函数宏,而不通过预处理器运行它?例如,是否有一个程序或方法可以改变这个:

#include <iostream>
#define AAA(a) cout << "function "  << a << endl
using namespace std;
int main(){
AAA(12);
}
Run Code Online (Sandbox Code Playgroud)

进入这个?

#include <iostream>
using namespace std;
int main(){
cout << "function " << 12 << endl;
}
Run Code Online (Sandbox Code Playgroud)

我不想运行预处理器,因为文件中的所有包含使得"gcc -E <>"输出真的很难看,我只想要几个简单的宏扩展而没有所有的开销.

Kyl*_*yle -1

假设您想查看 file.c。如果它包含函数 main,则需要更改该函数的名称才能使其工作。下面的代码将文件打印到标准输出,只是稍微增加了一些丑陋。

#include "file.c"
#include <stdio.h>

#define X(...) #__VA_ARGS__

void print_file( char* s )
{
   // a loop here to move through s until some identifier is found
   // in the original file, say a comment that says: //___START___HERE___
   while(*s){
      if( *s=='{' | *s=='}' | *s=='#' ) printf("\n");
      printf("%c",*s);
      if( *s==';' ) printf("\n");
      }
}

int main(int argc, char* argv[])
{
    print_file(X(
    #include "file.c"
    ));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)