[英]clang::HeaderSearch search path ignored
我正在尝试让clang::CompilerInstance
解析包含包含的源文件,但是我不知道如何获取它才能真正找到包含的标头。 这是我的设置:
std::unique_ptr<clang::CompilerInstance> ci(new clang::CompilerInstance());
ci->createDiagnostics();
LLVMInitializeARMTarget();
LLVMInitializeARMTargetMC();
LLVMInitializeARMAsmPrinter();
LLVMInitializeARMAsmParser();
std::shared_ptr<clang::TargetOptions> options(new clang::TargetOptions);
options->Triple = "arm-v7m-unknown-none-eabi";
options->CPU = "cortex-m3";
clang::TargetInfo *targetInfo = clang::TargetInfo::CreateTargetInfo(ci->getDiagnostics(), options);
ci->setTarget(targetInfo);
ci->createFileManager();
ci->createSourceManager(ci->getFileManager());
NSURL *sysrootURL = [[[NSBundle mainBundle] resourceURL] URLByAppendingPathComponent:@"Compiler/basalt"];
NSURL *includeURL = [sysrootURL URLByAppendingPathComponent:@"include"];
ci->createPreprocessor(clang::TranslationUnitKind::TU_Complete);
ci->getPreprocessorOpts().UsePredefines = false;
// Header searcher
llvm::IntrusiveRefCntPtr<clang::HeaderSearchOptions> hso(new clang::HeaderSearchOptions());
hso->UseBuiltinIncludes = false;
hso->UseStandardSystemIncludes = false;
hso->UseStandardCXXIncludes = false;
hso->Sysroot = [[includeURL path] UTF8String];
clang::HeaderSearch headerSearch(hso, ci->getSourceManager(), ci->getDiagnostics(), ci->getLangOpts(), targetInfo);
headerSearch.AddSearchPath(clang::DirectoryLookup(ci->getFileManager().getDirectory([[includeURL path] UTF8String]), clang::SrcMgr::C_System, false), true);
clang::InitializePreprocessor(ci->getPreprocessor(), ci->getPreprocessorOpts(), ci->getFrontendOpts());
// Main file
const clang::FileEntry *file = ci->getFileManager().getFile([[[_url URLByAppendingPathComponent:@"src/main.c"] path] UTF8String]);
ci->getSourceManager().setMainFileID(ci->getSourceManager().createFileID(file, clang::SourceLocation(), clang::SrcMgr::C_User));
ci->getPreprocessor().EnterMainSourceFile();
ci->getDiagnosticClient().BeginSourceFile(ci->getLangOpts(), &ci->getPreprocessor());
clang::Token tok;
do {
ci->getPreprocessor().Lex(tok);
if(ci->getDiagnostics().hasErrorOccurred())
break;
ci->getPreprocessor().DumpToken(tok);
std::cerr << std::endl;
} while(tok.isNot(clang::tok::eof));
ci->getDiagnosticClient().EndSourceFile();
路径绝对是100%正确的,并一遍又一遍地检查它们。 一切工作直到我抛出的源代码包含#include <foobar.h>
类的东西,在这种情况下,即使foobar.h确实存在,它也会error 'foobar.h' file not found
失败。 。 我觉得我在这里确实缺少一些明显的东西。 任何指向正确方向的指针?
首先,使用CompilerInstance删除-所有权语义非常糟糕,几乎无法使用(除非它们在3.6中使用unique_ptr
修复)。 自己简单地制作组件会更容易。
其次,是的,您必须自己做。 这是我自己使用Clang的项目的逐字摘要:
clang::HeaderSearch hs(/*params*/);
// WHY AM I DOING THIS MYSELF CLANG
// CAN'T YOU READ YOUR OWN CONSTRUCTOR PARAMETERS AND OPTIONS STRUCTS?
std::vector<clang::DirectoryLookup> lookups;
for (auto entry : opts.HeaderSearchOptions->UserEntries) {
auto lookup = clang::DirectoryLookup(FileManager.getDirectory(entry.Path), clang::SrcMgr::CharacteristicKind::C_System, false);
if (!lookup.getDir())
throw SpecificError<ClangCouldNotInterpretPath>(a, where, "Clang could not interpret path " + entry.Path);
lookups.push_back(lookup);
}
hs.SetSearchPaths(lookups, 0, 0, true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.