उपयोग करते समय पा सकते हैं। मैं क्लैंग द्वारा पार्स किए गए सी ++ स्रोत फ़ाइल में एक पहचानकर्ता देख रहा हूं। यह एक ऐसा कार्य है जो पांच तर्क लेता है जिसे स्रोत फ़ाइल (शीर्षलेख नहीं) में ठीक से घोषित किया जाता है। जब मैं इसे एक तर्क के साथ कॉल करने का प्रयास करता हूं, तो क्लैंग एक उचित त्रुटि देता है - यहां तक कि समारोह की घोषणा का पूरा पाठ भी। लेकिन जब मैं एपीआई के साथ इसे देखने की कोशिश करता हूं, क्लैंग जोर देकर कहते हैं कि यह अस्तित्व में नहीं है।क्लैंग एपीआई के माध्यम से पहचानकर्ता नहीं ढूंढ सकता है लेकिन क्लैंग इसे
यहाँ प्रासंगिक कोड है:
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);
मैं सत्यापित किया है कि identinfo
शून्य नहीं है, और है कि sloc
भी शून्य नहीं है।
क्लैंग मेरा नाम क्यों नहीं ढूंढ सकता?
संपादित करें: मैं वैश्विक क्षेत्र में योग्य नाम लुकअप करने में सफल रहा हूं। दुर्भाग्यवश, यह अयोग्य नाम लुकअप करने के समान नहीं है- उदाहरण के लिए, कोई एडीएल नहीं। क्लैंग अभी भी जोर देकर कहते हैं कि एक अयोग्य कार्य नाम मौजूद नहीं है।
संपादित करें: योग्य कोड समान है लेकिन फ्रंटेंड लाइब्रेरी पर निर्भरताओं से बचने के लिए दोबारा प्रतिक्रिया दी गई है, क्योंकि इसमें बेहद संदिग्ध स्वामित्व अर्थशास्त्र है और कार्रवाई वैसे भी नहीं करती जो मुझे चाहिए।
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());
क्या आप योग्य खोज के साथ कोड पोस्ट कर सकते हैं? – osgx
@osgx: अनुरोध के रूप में किया गया। – Puppy
आपके कोड में इस्तेमाल किए गए एलएलवीएम + क्लैंग का संस्करण क्या है? – osgx