评估C ++头文件中的所有宏 [英] Evaluate all macros in a C++ header file
问题描述
我需要构建一个自动化系统,以分析其中包含许多#define
语句的C ++ .h文件,并使用每个#define
得出的值进行处理. .h文件有很多其他垃圾在里面除了
I have a requirement to build an automated system to parse a C++ .h file with a lot of #define
statements in it and do something with the value that each #define
works out to. The .h file has a lot of other junk in it besides the #define
statements.
的目标是创建一个键 - 值列表中,其中键是全部由
The objective is to create a key-value list, where the keys are all the keywords defined by the #define
statements and the values are the evaluations of the macros which correspond to the definitions. The #defines
define the keywords with a series of nested macros that ultimately resolve to compile-time integer constants. There are some that do not resolve to compile-time integer constants, and these must be skipped.
在h文件将随着时间的推移,这样的工具不能是一个长硬编码程序,其实例化一个变量为等于每个关键字.我有超过.h文件的内容没有控制权.唯一的保证是可以使用标准的C ++编译器来构建它,并且将添加但不会删除更多的#defines
.宏公式可能随时更改.
The .h file will evolve over time, so the tool cannot be a long hardcoded program which instantiates a variable to be equal to each keyword. I have no control over the contents of the .h file. The only guarantees are that it can be built with a standard C++ compiler, and that more #defines
will be added but never removed. The macro formulas may change at any time.
我看到这种情况的选项是:
The options I see for this are:
- 在预处理器步骤实现的部分(或钩到现有的)C ++编译器和截距的宏的值.
- 使用正则表达式来动态地建立一个源文件,该文件将消耗所有当前定义的宏,则编译和执行源文件以获得所有的宏的评价形式.不知怎的,(?)跳过不评价编译时整型常量宏. (另外,不知道是否是正则表达式表达足以捕获所有可能的多行宏定义)
这两种方法都将增加相当大的复杂性和脆弱性的生成过程为这个项目,我想避免的.有没有更好的方法来评估C ++ .h文件中的所有#define
宏?
下面是我所期待解析一个例子:
Below is an example of what I am looking to parse:
#ifndef Constants_h
#define Constants_h
namespace Foo
{
#define MAKE_CONSTANT(A, B) (A | (B << 4))
#define MAGIC_NUMBER_BASE 40
#define MAGIC_NUMBER MAGIC_NUMBER_BASE + 0x2
#define MORE_MAGIC_1 345
#define MORE_MAGIC_2 65
// Other stuff...
#define CONSTANT_1 MAKE_CONSTANT (MAGIC_NUMBER + 564, MORE_MAGIC_1 | MORE_MAGIC_2)
#define CONSTANT_2 MAKE_CONSTANT (MAGIC_NUMBER - 84, MORE_MAGIC_1 & MORE_MAGIC_2 ^ 0xA)
// etc...
#define SKIP_CONSTANT "What?"
// More CONSTANT_N mixed with more other stuff and constants which do
// not resolve to compile-time integers and must be skipped
}
#endif Constants_h
我需要得到这一点是所有的决心,编译时间整型常量该定义的名称和评估.在这种情况下,对于所示的定义将是
What I need to get out of this is the names and evaluations of all the defines which resolve to compile-time integer constants. In this case, for the defines shown it would be
MAGIC_NUMBER_BASE 40
MAGIC_NUMBER 42
MORE_MAGIC_1 345
MORE_MAGIC_2 65
CONSTANT_1 1887
CONSTANT_2 -42
这并不重要,什么格式这个输出是只要我可以用它作为键值对的列表进一步努力沿管路.
It doesn't really matter what format this output is in as long as I can work with it as a list of key-value pairs further down the pipe.
推荐答案
的一种方法可能是编写生成程序(printDefines程序),其包括如
An approach could be to write a "program generator" that generates a program (the printDefines program) comprising statements like std::cout << "MAGIC_NUMBER" << " " << (MAGIC_NUMBER_BASE + 0x2) << std::endl;
. Obviously, executing such statements will resolve the respective macros and print out their values.
g++
具有-dM -E' option. Feeding this "program generator" with such a list of #defines will generate a "printDefines.cpp" with all the required
cout`语句可以获取头文件中的宏列表.编译和执行所生成的程序printDefines然后产生最终输出.这将解决所有的宏,包括那些本身使用其他宏.
The list of macros in a header file can be obtained by g++
with an -dM -E' option. Feeding this "program generator" with such a list of #defines will generate a "printDefines.cpp" with all the required
cout`-statements. Compiling and executing the generated printDefines program then yields the final output. It will resolve all the macros, including those that by itself use other macros.
请参阅下面的shell脚本和下面的程序代码发生器一起实现这种方法:
See the following shell script and the following program generator code that together implement this approach:
脚本印刷#1的值定义语句中的 someHeaderfile.h":
Script printing the values of #define-statements in "someHeaderfile.h":
# printDefines.sh
g++ -std=c++11 -dM -E someHeaderfile.h > defines.txt
./generateDefinesCpp someHeaderfile.h defines.txt > defines.cpp
g++ -std=c++11 -o defines.o defines.cpp
./defines.o
程序生成 generateDefinesCpp" 的代码:
Code of program generator "generateDefinesCpp":
#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>
#include <cstring>
using std::cout;
using std::endl;
/*
* Argument 1: name of the headerfile to scan
* Argument 2: name of the cpp-file to generate
* Note: will crash if parameters are not provided.
*/
int main(int argc, char* argv[])
{
cout << "#include<iostream>" << endl;
cout << "#include<stdio.h>" << endl;
cout << "#include \"" << argv[1] << "\"" << endl;
cout << "int main() {" << endl;
std::ifstream headerFile(argv[2], std::ios::in);
std::string buffer;
char macroName[1000];
int macroValuePos;
while (getline(headerFile,buffer)) {
const char *bufferCStr = buffer.c_str();
if (sscanf(bufferCStr, "#define %s %n", macroName, ¯oValuePos) == 1) {
const char* macroValue = bufferCStr+macroValuePos;
if (macroName[0] != '_' && strchr(macroName, '(') == NULL && *macroValue) {
cout << "std::cout << \"" << macroName << "\" << \" \" << (" << macroValue << ") << std::endl;" << std::endl;
}
}
}
cout << "return 0; }" << endl;
return 0;
}
该方法可以被优化,使得中间文件
The approach could be optimised such that the intermediate files defines.txt
and defines.cpp
are not necessary; For demonstration purpose, however, they are helpful. When applied to your header file, the content of defines.txt
and defines.cpp
will be as follows:
defines.txt:
defines.txt:
#define CONSTANT_1 MAKE_CONSTANT (MAGIC_NUMBER + 564, MORE_MAGIC_1 | MORE_MAGIC_2)
#define CONSTANT_2 MAKE_CONSTANT (MAGIC_NUMBER - 84, MORE_MAGIC_1 & MORE_MAGIC_2 ^ 0xA)
#define Constants_h
#define MAGIC_NUMBER MAGIC_NUMBER_BASE + 0x2
#define MAGIC_NUMBER_BASE 40
#define MAKE_CONSTANT(A,B) (A | (B << 4))
#define MORE_MAGIC_1 345
#define MORE_MAGIC_2 65
#define OBJC_NEW_PROPERTIES 1
#define SKIP_CONSTANT "What?"
#define _LP64 1
#define __APPLE_CC__ 6000
#define __APPLE__ 1
#define __ATOMIC_ACQUIRE 2
#define __ATOMIC_ACQ_REL 4
...
defines.cpp:
defines.cpp:
#include<iostream>
#include<stdio.h>
#include "someHeaderfile.h"
int main() {
std::cout << "CONSTANT_1" << " " << (MAKE_CONSTANT (MAGIC_NUMBER + 564, MORE_MAGIC_1 | MORE_MAGIC_2)) << std::endl;
std::cout << "CONSTANT_2" << " " << (MAKE_CONSTANT (MAGIC_NUMBER - 84, MORE_MAGIC_1 & MORE_MAGIC_2 ^ 0xA)) << std::endl;
std::cout << "MAGIC_NUMBER" << " " << (MAGIC_NUMBER_BASE + 0x2) << std::endl;
std::cout << "MAGIC_NUMBER_BASE" << " " << (40) << std::endl;
std::cout << "MORE_MAGIC_1" << " " << (345) << std::endl;
std::cout << "MORE_MAGIC_2" << " " << (65) << std::endl;
std::cout << "OBJC_NEW_PROPERTIES" << " " << (1) << std::endl;
std::cout << "SKIP_CONSTANT" << " " << ("What?") << std::endl;
return 0; }
和执行的输出
And the output of executing defines.o
is then:
CONSTANT_1 1887
CONSTANT_2 -9
MAGIC_NUMBER 42
MAGIC_NUMBER_BASE 40
MORE_MAGIC_1 345
MORE_MAGIC_2 65
OBJC_NEW_PROPERTIES 1
SKIP_CONSTANT What?
这篇关于评估C ++头文件中的所有宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!