Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find identifier through Clang API but Clang can find it when used

Tags:

c++

clang

I'm looking up an identifier in a C++ source file parsed by Clang. It's a function that takes five arguments which is declared right there in the source file (not a header). When I attempt to call it with one argument, Clang gives an appropriate error- even the full text of the declaration of the function. But when I try to look it up with the API, Clang insists that it does not exist.

Here's the relevant code:

llvm::LLVMContext c;
clang::CompilerInstance CI;
llvm::Module m("", c);
clang::EmitLLVMOnlyAction emit(&c);
emit.setLinkModule(&m);

std::string errors;
llvm::raw_string_ostream error_stream(errors);
clang::DiagnosticOptions diagopts;
clang::TextDiagnosticPrinter printer(error_stream, &diagopts);
llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagids(new clang::DiagnosticIDs);
clang::DiagnosticsEngine engine(diagids, &diagopts, &printer, false);
CI.setDiagnostics(&engine);

clang::TargetOptions target;
target.Triple = llvm::sys::getDefaultTargetTriple();
CI.setTarget(clang::TargetInfo::CreateTargetInfo(engine, &target));

CI.getLangOpts().CPlusPlus0x = true;
CI.getLangOpts().CPlusPlus = true;

clang::FrontendInputFile f("main.cpp", clang::InputKind::IK_CXX, true);
emit.BeginSourceFile(CI, f);

emit.Execute();
auto sema = CI.takeSema();
auto ast = &CI.getASTContext();
CI.resetAndLeakASTContext();
emit.EndSourceFile();

emit.takeModule();
auto identinfo = CI.getPreprocessor().getIdentifierInfo("WriteConsoleW");

auto sloc = CI.getSourceManager().getLocForEndOfFile(CI.getSourceManager().translateFile(CI.getFileManager().getFile("main.cpp")));
clang::LookupResult lr(*sema, clang::DeclarationName(identinfo), sloc, clang::Sema::LookupNameKind::LookupOrdinaryName);
auto result = sema->LookupName(lr, sema->TUScope);

I have verified that identinfo is not NULL, and that sloc is also not zero.

Why can't Clang find my name?

Edit: I have succeeded in performing qualified name lookup into the global scope. Unfortunately, this is not quite the same as performing unqualified name lookup- for example, no ADL. Clang still insists that an unqualified function name does not exist.

Edit: The qualified code is similar but refactored to avoid dependencies on the Frontend library, as it has extremely questionable ownership semantics and the action doesn't do what I need anyway.

clang::CompilerInstance ci;
clang::FileSystemOptions fso;
clang::FileManager fm(fso);

std::string errors;
llvm::raw_string_ostream error_stream(errors);
clang::DiagnosticOptions diagopts;

llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagids(new clang::DiagnosticIDs);
clang::DiagnosticsEngine engine(diagids, &diagopts, new clang::TextDiagnosticPrinter(error_stream, &diagopts), false);

clang::SourceManager sm(engine, fm);

clang::LangOptions langopts;
langopts.CPlusPlus = true;
langopts.CPlusPlus0x = true;

clang::TargetOptions target;
target.Triple = llvm::sys::getDefaultTargetTriple();
auto targetinfo = clang::TargetInfo::CreateTargetInfo(engine, &target);

auto headeropts = llvm::IntrusiveRefCntPtr<clang::HeaderSearchOptions>(new clang::HeaderSearchOptions());
clang::HeaderSearch hs(headeropts, fm, engine, langopts, targetinfo);

auto x = llvm::IntrusiveRefCntPtr<clang::PreprocessorOptions>(new clang::PreprocessorOptions());
clang::Preprocessor p(x, engine, langopts, targetinfo, sm, hs, ci);

clang::ASTContext astcon(langopts, sm, targetinfo, p.getIdentifierTable(), p.getSelectorTable(), p.getBuiltinInfo(), 1000);
clang::CodeGenOptions codegenopts;
clang::CodeGen::CodeGenModule codegen(astcon, codegenopts, m, llvm::DataLayout(&m), engine);
CodeGenConsumer consumer(&codegen);
clang::Sema sema(p, astcon, consumer, clang::TranslationUnitKind::TU_Prefix);

sm.createMainFileID(fm.getFile(filepath));
engine.getClient()->BeginSourceFile(langopts, &p);
clang::ParseAST(sema);
codegen.Release();
engine.getClient()->EndSourceFile();
    /*
for (auto it = astcon.getTranslationUnitDecl()->decls_begin(); it != astcon.getTranslationUnitDecl()->decls_end(); ++it) {
    if (auto named = llvm::dyn_cast<clang::NamedDecl>(*it)) {
        std::cout << named->getNameAsString() << "\n";
    }
}*/
clang::LookupResult lr(sema, clang::DeclarationName(p.getIdentifierInfo("f")), sm.getLocForEndOfFile(sm.translateFile(fm.getFile(filepath))), clang::Sema::LookupNameKind::LookupOrdinaryName);
auto result = sema.LookupQualifiedName(lr, astcon.getTranslationUnitDecl());
like image 425
Puppy Avatar asked Dec 22 '12 00:12

Puppy


1 Answers

Apparently, this is just known to be broken right now. I looked through the LookupName implementation and it flat out does not handle this case.

I will have to live with qualified lookup for now.

like image 138
Puppy Avatar answered Nov 15 '22 12:11

Puppy