gpt4 book ai didi

compiler-construction - 使用 clang-c 索引文件不会命中我的 ppIncludedFile 回调

转载 作者:行者123 更新时间:2023-12-01 02:35:08 25 4
gpt4 key购买 nike

我正在使用下面的 Objective-C 类来索引一个 Objective-C 文件。我试过解析同时使用 #include 的文件和 #import , 带有尖括号和带引号的头文件。我的 ppIncludedFile 在任何情况下都不会回调被击中。当我获得其中定义的符号的索引回调时,Clang 显然包含了这些文件。但关于整体结构,我得到enteredMainFile ,然后我得到 startedTranslationUnit对于主文件也是如此。但我从来没有发现它在寻找什么头文件。

根据文档:

/** * \brief Called when a file gets #included/#imported. */
CXIdxClientFile (*ppIncludedFile)(CXClientData client_data, const CXIdxIncludedFileInfo *);



这不会发生。这是一个错误,还是我需要做些什么来启用这个回调? libclang的版本我使用的是来自 svn trunk r156259。
//
// FZAClassParser.m
// ObjectiveBrowser
//
// Created by Graham Lee on 07/05/2012.
// Copyright (c) 2012 Fuzzy Aliens Ltd.. All rights reserved.
//

#import "FZAClassParser.h"
#import "FZAClassParserDelegate.h"

int abortQuery(CXClientData client_data, void *reserved);
void diagnostic(CXClientData client_data,
CXDiagnosticSet diagnostic_set, void *reserved);
CXIdxClientFile enteredMainFile(CXClientData client_data,
CXFile mainFile, void *reserved);
CXIdxClientFile ppIncludedFile(CXClientData client_data,
const CXIdxIncludedFileInfo *included_file);
CXIdxClientASTFile importedASTFile(CXClientData client_data,
const CXIdxImportedASTFileInfo *imported_ast);
CXIdxClientContainer startedTranslationUnit(CXClientData client_data,
void *reserved);
void indexDeclaration(CXClientData client_data,
const CXIdxDeclInfo *declaration);
void indexEntityReference(CXClientData client_data,
const CXIdxEntityRefInfo *entity_reference);

static IndexerCallbacks indexerCallbacks = {
.abortQuery = abortQuery,
.diagnostic = diagnostic,
.enteredMainFile = enteredMainFile,
.ppIncludedFile = ppIncludedFile,
.importedASTFile = importedASTFile,
.startedTranslationUnit = startedTranslationUnit,
.indexDeclaration = indexDeclaration,
.indexEntityReference = indexEntityReference
};

@interface FZAClassParser ()

- (void)realParse;

@end

@implementation FZAClassParser
{
NSString *sourceFile;
NSOperationQueue *queue;
}

@synthesize delegate;

- (id)initWithSourceFile:(NSString *)implementation {
if ((self = [super init])) {
if(![[NSFileManager defaultManager] fileExistsAtPath: implementation]) {
return nil;
}
sourceFile = [implementation copy];
queue = [[NSOperationQueue alloc] init];
}
return self;
}

- (void)parse {
__weak id parser = self;
[queue addOperationWithBlock: ^{ [parser realParse]; }];
}

- (void)realParse {
#pragma warning Pass errors back to the app
@autoreleasepool {
CXIndex index = clang_createIndex(1, 1);
if (!index) {
NSLog(@"fail: couldn't create translation unit");
return;
}
CXTranslationUnit translationUnit = clang_parseTranslationUnit(index, [sourceFile fileSystemRepresentation], NULL, 0, NULL, 0, CXTranslationUnit_None);
if (!translationUnit) {
NSLog(@"fail: couldn't compile %@", sourceFile);
return;
}
CXIndexAction action = clang_IndexAction_create(index);
if ([self.delegate respondsToSelector: @selector(classParser:willBeginParsingFile:)]) {
[self.delegate classParser: self willBeginParsingFile: sourceFile];
}
int indexResult = clang_indexTranslationUnit(action,
(__bridge CXClientData)self,
&indexerCallbacks,
sizeof(indexerCallbacks),
CXIndexOpt_SuppressWarnings,
translationUnit);
if ([self.delegate respondsToSelector: @selector(classParser:didFinishParsingFile:)]) {
[self.delegate classParser: self didFinishParsingFile: sourceFile];
}
clang_IndexAction_dispose(action);
clang_disposeTranslationUnit(translationUnit);
clang_disposeIndex(index);
(void) indexResult;
}
}

@end

int abortQuery(CXClientData client_data, void *reserved) {
@autoreleasepool {
FZAClassParser *parser = (__bridge FZAClassParser *)client_data;
if ([parser.delegate respondsToSelector: @selector(classParserShouldAbort:)]) {
return [parser.delegate classParserShouldAbort: parser];
}
return 0;
}
}

void diagnostic(CXClientData client_data,
CXDiagnosticSet diagnostic_set, void *reserved) {
@autoreleasepool {
FZAClassParser *parser = (__bridge FZAClassParser *)client_data;
if ([parser.delegate respondsToSelector: @selector(classParser:foundDiagnostics:)]) {
[parser.delegate classParser: parser foundDiagnostics: diagnostic_set];
}
}
}

CXIdxClientFile enteredMainFile(CXClientData client_data,
CXFile mainFile, void *reserved) {
@autoreleasepool {
FZAClassParser *parser = (__bridge FZAClassParser *)client_data;
if ([parser.delegate respondsToSelector: @selector(classParser:enteredMainFile:)]) {
return [parser.delegate classParser: parser enteredMainFile: mainFile];
}
return NULL;
}
}

CXIdxClientFile ppIncludedFile(CXClientData client_data,
const CXIdxIncludedFileInfo *included_file) {
@autoreleasepool {
FZAClassParser *parser = (__bridge FZAClassParser *)client_data;
if ([parser.delegate respondsToSelector: @selector(classParser:includedFile:)]) {
return [parser.delegate classParser: parser includedFile: included_file];
}
return NULL;
}
}

CXIdxClientASTFile importedASTFile(CXClientData client_data,
const CXIdxImportedASTFileInfo *imported_ast) {
@autoreleasepool {
FZAClassParser *parser = (__bridge FZAClassParser *)client_data;
if ([parser.delegate respondsToSelector: @selector(classParser:importedPCH:)]) {
return [parser.delegate classParser: parser importedPCH: imported_ast];
}
return NULL;
}
}

CXIdxClientContainer startedTranslationUnit(CXClientData client_data,
void *reserved) {
@autoreleasepool {
FZAClassParser *parser = (__bridge FZAClassParser *)client_data;
if ([parser.delegate respondsToSelector: @selector(classParserStartedTranslationUnit:)]) {
return [parser.delegate classParserStartedTranslationUnit: parser];
}
return NULL;
}
}

void indexDeclaration(CXClientData client_data,
const CXIdxDeclInfo *declaration) {
@autoreleasepool {
FZAClassParser *parser = (__bridge FZAClassParser *)client_data;
if ([parser.delegate respondsToSelector: @selector(classParser:foundDeclaration:)]) {
[parser.delegate classParser: parser foundDeclaration: declaration];
}
}
}

void indexEntityReference(CXClientData client_data,
const CXIdxEntityRefInfo *entity_reference) {
@autoreleasepool {
FZAClassParser *parser = (__bridge FZAClassParser *)client_data;
if ([parser.delegate respondsToSelector: @selector(classParser:foundEntityReference:)]) {
[parser.delegate classParser: parser foundEntityReference: entity_reference];
}
}
}

最佳答案

处理在#include时间可能不会被记录,因为那里有很多信息(在一般情况下可能不需要)。有一个 createPreporcessingRecord如果未启用该选项,则不会生成数据的功能。

Clang 文档描述了 CXTranslationUnit_DetailedPreprocessingRecord您可以将其用作标志而不是 CXTranslationUnit_None这可能有帮助。

关于compiler-construction - 使用 clang-c 索引文件不会命中我的 ppIncludedFile 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10567497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com