【发布时间】:2016-03-24 21:56:08
【问题描述】:
你有什么想法吗?我不熟悉任何代码解析器,但我知道使用 clang 是可行的。
我要解析的代码:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "InventoryItem.h"
#include "ItemView.h"
#include "InventoryScreen.generated.h"
class UGUIBase;
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FInventoryScreenEvent, UInventoryItem*, item);
UCLASS()
class FLYING_API UInventoryScreen : public UGUIBase
{
GENERATED_BODY()
public:
virtual void NativeConstruct() override;
virtual TSharedRef<SWidget> RebuildWidget() override;
public:
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Update event")
void UpdateItemView(const TArray<UInventoryItem*>& Items);
virtual void UpdateItemView_Implementation(const TArray<UInventoryItem*>& Items);
UFUNCTION()
void OnItemClicked(UInventoryItem* item);
UPROPERTY(BlueprintAssignable, Category = "Button|Event")
FInventoryScreenEvent OnClickedBy;
public:
TWeakObjectPtr<UItemView> ItemView;
};
看看像 UCLASS 和 UFUNCTION 这样的宏。使用这个宏可能会导致错误的解析。但我需要获取由 UFUNCTION 宏(函数名称和参数)修饰的所有函数。
我的无用代码(Python):
import clang.cindex
def look_header(node):
print([c.displayname for c in node.get_children()]) # Here I got 'DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam()', 'UGUIBase', 'UInventoryScreen', 'UGUIBase'
for c in node.get_children():
if c.displayname == "UInventoryScreen": # I want to get all children of this class (all functions)
print(list(c.get_children())) # What subitems in this class? I got empty list (no anything)
for cc in c.get_children():
print(cc.displayname)
index = clang.cindex.Index.create()
tu = index.parse('InventoryScreen.h')
print('Translation unit:', tu.spelling)
look_header(tu.cursor)
由于解析错误,我删除了 UCLASS() 和 FLYING_API 宏。下面的代码避免这种情况没有帮助:
#define UCLASS(...)
#define FLYING_API
python代码的输出:
Translation unit: InventoryScreen.h
['DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam()', 'UGUIBase', 'UInventoryScreen', 'UGUIBase']
[]
[]
[]
[]
[]
【问题讨论】:
-
你试过了吗?你能告诉我们你为尝试这个而编写的任何代码吗?
-
@JohnZwinck 我添加了 python 代码来发布
标签: python c++ parsing macros clang