I have a C ++ header called class.h that I want to parse :
class MyClass { public: Class() {} ~Class() {} bool isTrue() const; bool isFalse() const; private: bool m_attrib; }; bool MyClass::isTrue() const { return true; } bool MyClass::isFalse() const { return false; }
I am using clang instance compiler with user AST. All my code works well with the source c file. But I cannot configure / force the use of langage, which should use CompilerInstance. Here is the code I'm using:
m_ci = new clang::CompilerInstance(); clang::CompilerInvocation *invocation = new clang::CompilerInvocation; clang::LangOptions langOpts; langOpts.CPlusPlus = 1; invocation->setLangDefaults(langOpts, langage); m_ci->setInvocation(invocation); m_ci->createDiagnostics(); llvm::IntrusiveRefCntPtr<clang::TargetOptions> pto( new clang::TargetOptions()); pto->Triple = llvm::sys::getDefaultTargetTriple(); clang::TargetInfo *pti = clang::TargetInfo::CreateTargetInfo(m_ci->getDiagnostics(), pto.getPtr()); m_ci->setTarget(pti); m_ci->createFileManager(); m_ci->createSourceManager(ci->getFileManager()); m_ci->createPreprocessor(); m_hso = llvm::IntrusiveRefCntPtr<clang::HeaderSearchOptions>(new clang::HeaderSearchOptions()); m_hso->AddPath( pathName.c_str(), clang::frontend::Angled, false, false); const clang::FileEntry *pFile = m_ci->getFileManager().getFile(fileName.c_str()); m_ci->getSourceManager().createMainFileID(pFile); clang::InitializePreprocessor(m_ci->getPreprocessor(), m_ci->getPreprocessorOpts(), *m_hso, m_ci->getFrontendOpts()); m_ci->createASTContext(); m_headerElements = new HeaderElements(); m_ci->setASTConsumer(m_headerElements); m_ci->getDiagnosticClient().BeginSourceFile(m_ci->getLangOpts(), &m_ci->getPreprocessor()); clang::ParseAST(m_ci->getPreprocessor(), m_headerElements, m_ci->getASTContext()); m_ci->getDiagnosticClient().EndSourceFile();
When I check this, the parser produces the following errors:
error: unknown type name 'class'
And test
m_ci->getLangOpts.CPlusPlus == 0
true, therefore it seems that LangOptions does not apply to CompilerInstance.
c ++ clang llvm-clang
cedlemo
source share