问题描述
我有一个名为 class.h 的C ++头文件,我想要解析:
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;
}
我使用AST消费者clang一个编译器实例。我所有的代码适用于c源文件。但我无法配置/强制CompilerInstance必须使用的语言。
这里是我使用的代码:
I use clang a compiler Instance with an AST consumer. All my code works well with c source file. But I am unable to configure/force the langage that the CompilerInstance must use.Here is the code I use :
m_ci = new clang::CompilerInstance();
/*configure the langage to use*/
clang::CompilerInvocation *invocation = new clang::CompilerInvocation;
clang::LangOptions langOpts;
/*with langage = clang::IK_CXX*/
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();
/*add some header search paths*/
m_hso = llvm::IntrusiveRefCntPtr<clang::HeaderSearchOptions>(new clang::HeaderSearchOptions());
m_hso->AddPath( pathName.c_str(),
clang::frontend::Angled,
false,
false);
/*add the source file*/
const clang::FileEntry *pFile = m_ci->getFileManager().getFile(fileName.c_str());
m_ci->getSourceManager().createMainFileID(pFile);
/*parse*/
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 test this, the parser throws errors like this:
error: unknown type name 'class'
和测试
m_ci->getLangOpts.CPlusPlus == 0
是真的,所以看起来没有对CompilerInstance应用LangOptions。
is true so it seems that the LangOptions is not applied on the CompilerInstance.
推荐答案
经过一些测试和大量的搜索,我想出了如何做到这一点。为了设置CompilerInstance对象的语言选项,你只需要这样做:
After some tests and a lot of searches, I have figured out how to do this. In order to set the Language Options of a CompilerInstance object, you just have to do this:
clang::CompilerInstance ci;
//initialize lot of stuff
ci.createDiagnostics();
ci.createFileManager();
ci.createSourceManager(m_ci.getFileManager());
std::shared_ptr<clang::TargetOptions> pto = std::make_shared<clang::TargetOptions>();
pto->Triple = llvm::sys::getDefaultTargetTriple();
clang::TargetInfo *pti = clang::TargetInfo::CreateTargetInfo(ci.getDiagnostics(), pto);
ci.setTarget(pti);
//force langage to C++
ci.getLangOpts().CPlusPlus = 1;
ci.createPreprocessor(clang::TU_Complete);
重要的是在创建或重新创建预处理器之前配置LangOpts CompilerInstance :: createPreprocessor()
The important thing is to configure the LangOpts before creating or re-creating the preprocessor with CompilerInstance::createPreprocessor()
这篇关于无法强制clang CompilerInstance对象将头解析为C ++文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!