如何使用 clang 从源代码解析 C++ 'struct' 定义
*注意:*另见我们升级版的文章,如何使用 clang 和 boost::json 将 C++ 结构体字段导出为 JSON。
clang 库提供了一种相对简单的方法来解析 C/C++ 代码并提取有关 struct 定义的信息。
以下代码演示了如何解析以下 struct 定义:
parse_struct.cpp
typedef struct {
double a; /* That weird parameter */
double b; /* Another weird parameter */
} myParameters;完整源代码
parse_struct_full.cpp
#include <clang-c/Index.h>
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
void extractStructFields(CXCursor cursor) {
CXCursorKind kind = clang_getCursorKind(cursor);
if (kind == CXCursor_StructDecl) {
CXString structName = clang_getCursorSpelling(cursor);
std::cout << "Struct: " << clang_getCString(structName) << std::endl;
clang_disposeString(structName);
clang_visitChildren(cursor, [](CXCursor c, CXCursor parent, CXClientData client_data) {
CXCursorKind kind = clang_getCursorKind(c);
if (kind == CXCursor_FieldDecl) {
CXString fieldName = clang_getCursorSpelling(c);
CXType fieldType = clang_getCursorType(c);
CXString typeName = clang_getTypeSpelling(fieldType);
std::cout << " Field: " << clang_getCString(fieldName) << " (Type: " << clang_getCString(typeName) << ")" << std::endl;
clang_disposeString(fieldName);
clang_disposeString(typeName);
}
return CXChildVisit_Continue;
}, nullptr);
}
}
int main() {
CXIndex index = clang_createIndex(0, 0);
const char *code = R"(
typedef struct {
double a; /* That weird parameter */
double b; /* Another weird parameter */
} myParameters;
)";
CXUnsavedFile unsavedFile = {"test.cpp", code, (unsigned long)strlen(code)};
CXTranslationUnit unit = clang_parseTranslationUnit(index, "test.cpp", nullptr, 0, &unsavedFile, 1, CXTranslationUnit_None);
if (unit == nullptr) {
std::cerr << "Failed to parse translation unit." << std::endl;
return 1;
}
CXCursor cursor = clang_getTranslationUnitCursor(unit);
clang_visitChildren(cursor, [](CXCursor c, CXCursor parent, CXClientData client_data) {
extractStructFields(c);
return CXChildVisit_Continue;
}, nullptr);
clang_disposeTranslationUnit(unit);
clang_disposeIndex(index);
return 0;
}如何编译
在 Ubuntu 上,使用以下命令安装所需的库
install_libclang.sh
sudo apt -y install libclang-19-dev并使用以下命令编译代码
build_parse_struct.sh
g++ parse_struct.cpp -o parse_struct -std=c++17 -I/usr/lib/llvm-19/include -L/usr/lib/llvm-19/lib -lclang示例输出
使用 ./parse_struct 运行
parse_struct_output.txt
Struct: myParameters
Field: a (Type: double)
Field: b (Type: double)使用 cmake 的替代构建
你可能需要调整版本(19.1.1)和其他参数以使其在你的设置中正常工作。
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(parse_structs)
# C++17 Standard festlegen
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# LLVM/Clang-Komponenten finden
find_package(LLVM 19.1.1 REQUIRED CONFIG)
find_package(Clang REQUIRED CONFIG)
# Include-Verzeichnisse hinzufügen
include_directories(${LLVM_INCLUDE_DIRS})
include_directories(${CLANG_INCLUDE_DIRS})
# Executable erstellen
add_executable(parse_structs parse_structs.cpp)
# LLVM-Definitionen hinzufügen
target_compile_definitions(parse_structs PUBLIC ${LLVM_DEFINITIONS})
# Gegen libclang linken
target_link_libraries(parse_structs PUBLIC libclang)Check out similar posts by category:
C/C++, Clang, Source Introspection
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow