2014-08-30 11 views
7

Ho un colpo di testa C++ chiamato class.h che voglio analizzare:Impossibile forzare un oggetto clang CompilerInstance per analizzare un colpo di testa da file C++

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; 
} 

Io uso Clang un'istanza compilatore con un AST consumatore. Tutto il mio codice funziona bene con il file sorgente c. Ma non riesco a configurare/forzare il langage che deve essere utilizzato da CompilerInstance. ecco il codice che uso:

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

Quando ho verificare ciò, il parser genera errori come questo:

error: unknown type name 'class' 

E il test

m_ci->getLangOpts.CPlusPlus == 0 

vale così sembra che LangOptions non è applicato a CompilerInstance.

risposta

11

Dopo alcuni test e un sacco di ricerche, ho capito come farlo. Per impostare le opzioni di lingua di un oggetto CompilerInstance, basta fare questo:

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

La cosa importante è per configurare le LangOpts prima di creare o ricreare il preprocessore con CompilerInstance::createPreprocessor()

Problemi correlati