【发布时间】:2019-07-29 08:35:38
【问题描述】:
我在一个独立的 Java 应用程序中使用 Eclipse CDT 作为 C/C++ 头文件的解析器,其想法是从头文件中代码生成 JNI/JNA 绑定。
下载了 JAR 文件(很难找到,这个库没有 maven!)并解析了一个简单的头文件,它工作得很好。
但是,当我尝试真正的头文件时,结果有些随机:返回的 AST 没有孩子,只有一些预处理器声明存在,最奇怪的是只检测到两个 cmets。
这里是删减代码:
final FileContent content = FileContent.createForExternalFileLocation("C:/VulkanSDK/1.1.101.0/Include/vulkan/vulkan.h");
final Map<String, String> definedMacros = new HashMap<>();
definedMacros.put("__cplusplus", "1");
final String[] includePaths = new String[0];
final IScannerInfo info = new ScannerInfo(definedMacros, includePaths);
final IncludeFileContentProvider emptyIncludes = IncludeFileContentProvider.getEmptyFilesProvider();
final IIndex index = EmptyCIndex.INSTANCE;
final int options = 0;
final IParserLogService log = new DefaultLogService();
final IASTTranslationUnit unit = GPPLanguage.getDefault().getASTTranslationUnit(content, info, emptyIncludes, index, options, log);
System.out.println("len="+unit.getChildren().length);
我要解析的头文件是针对 Vulkan 图形库的,它非常大,所以这里是 link,这是文件的前几位:
#ifndef VULKAN_H_
#define VULKAN_H_ 1
#ifdef __cplusplus
extern "C" {
#endif
/*
** Copyright (c) 2015-2017 The Khronos Group Inc.
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
/*
** This header is generated from the Khronos Vulkan XML API Registry.
**
*/
#define VK_VERSION_1_0 1
#include "vk_platform.h"
getComments() 返回的两个 cmets 是文件顶部的第一个大注释块,奇怪的是:
// VULKAN_H_
即似乎已经决定预处理器语句是注释!?
虽然解析器接受了一个日志回调,但它并没有告诉我任何信息,也没有报告任何问题。我怀疑我对 CDT 和预处理器语句有一些不理解的地方,但我找不到任何类似的问题被发布(CDT 是一个非常小众的工具)。
我已经修改了代码中我能想到的所有内容,包括各种选项参数,但我每次都得到相同的结果。正如我所说,解析其他更简单的头文件就可以了。
有什么想法吗?建议?
【问题讨论】:
-
我的建议是:A) 如果 eclipse-cdt 的人有论坛或邮件列表并在那里提问,那么只有当利基人监视相应的标签时,SO 才对利基有好处 B) 小心用于其他工具
-
@GhostCat 面向 C/C++ 开发人员的 Eclipse IDE 每天从 eclipse.org 下载大约 1,500 次,这还不包括许多基于 Eclipse CDT 的应用程序和其他分发渠道。我不会称其为利基市场。使用 CDT 解析器有什么问题?
-
@howlger 是我(OP)使用了术语niche - 实际上Eclipse/CDT 绝对不是小众,但是在Java 应用程序中使用CDT 工具中的底层C/C++ 解析器是小众.
-
@stridecolossus 我也对此表示怀疑(例如,参见this GitHub project 或this question)。您是否使用 AST 访问者来查找 cmets?你得到的完整 AST 是多少?
-
症状听起来可能是解析器认为包含保护宏
VULKAN_H_已经定义,因此除了包含保护本身之外的所有文件内容都被跳过。
标签: java eclipse eclipse-cdt