clang::HeaderSearch search path ignored

826 views Asked by At

I'm trying to get clang::CompilerInstance to parse a source file which contains an include, but I can't figure out how to get it to actually find the included header. Here is my set up:

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();

The paths are definitely 100% correct and checked them over and over again. It all works up until the point the source code I throw at it contains something like #include <foobar.h>, in which case it will fail with error 'foobar.h' file not found, even though foobar.h is definitely there. I feel like I'm missing something really obvious here. Any pointers into the right direction?

1

There are 1 answers

3
Puppy On BEST ANSWER

Firstly, drop using CompilerInstance- the ownership semantics are so bad it's practically unusable (unless they fixed that in 3.6 with unique_ptr). It's easier to simply make the components yourself.

Secondly, yes, you have to do it yourself. Here's a verbatim excerpt from my own project that uses 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);