<div dir="ltr">Hi, all.<br><br>I am writing a tool to find references in C++ program based on libclang 3.2 python binding.<br>The parsed program include a lot of std libraries, so the parsing speed is slow, and I try to use TranslationUnit.save/load function to speed it up.<br>
<br>Use the following C++ code as example:<br><br>#include "std_all.h"<br><br>class Test<br>{<br>public:<br>     void foo();<br>};<br><br>I can parse it and travese it with the cursor returned by TranslationUnit.from_source.<br>
But when I load back through an ast file(5.2M) and try to travese it, i.e. walk the whole ast, the python program crashed in libclang. <br>Here's the crash output:<br><br>....<br>python: /home/users/Downloads/llvm-3.2.src/tools/clang/include/clang/Basic/IdentifierTable.h:168: void clang::IdentifierInfo::RevertTokenIDToIdentifier(): Assertion `TokenID != tok::identifier && "Already at tok::identifier"' failed.<br>
<br>Aborted (core dumped)<br><br><br>The following is the backtrace of the crash:<br><br>#0  0x0000003bc1632885 in raise () from /lib64/libc.so.6<br>#1  0x0000003bc1634065 in abort () from /lib64/libc.so.6<br>#2  0x0000003bc162b9fe in __assert_fail_base () from /lib64/libc.so.6<br>
#3  0x0000003bc162bac0 in __assert_fail () from /lib64/libc.so.6<br>#4  0x00007ffe098da880 in clang::IdentifierInfo::RevertTokenIDToIdentifier() ()<br>   from /usr/local/lib/libclang.so<br>#5  0x00007ffe098bac7b in clang::serialization::reader::ASTIdentifierLookupTrait::ReadData(std::pair<char const*, unsigned int> const&, unsigned char const*, unsigned int) () from /usr/local/lib/libclang.so<br>
#6  0x00007ffe098e1d09 in clang::OnDiskChainedHashTable<clang::serialization::reader::ASTIdentifierLookupTrait>::iterator::operator*() const ()<br>   from /usr/local/lib/libclang.so<br>#7  0x00007ffe098be540 in (anonymous namespace)::IdentifierLookupVisitor::visit(clang::serialization::ModuleFile&, void*) () from /usr/local/lib/libclang.so<br>
#8  0x00007ffe099ac9eb in clang::serialization::ModuleManager::visit(bool (*)(clang::serialization::ModuleFile&, void*), void*) ()<br>   from /usr/local/lib/libclang.so<br>#9  0x00007ffe098d40fe in clang::ASTReader::get(char const*, char const*) ()<br>
   from /usr/local/lib/libclang.so<br>#10 0x00007ffe098dceff in clang::ASTReader::get(llvm::StringRef) ()<br>   from /usr/local/lib/libclang.so<br>#11 0x00007ffe0951c314 in clang::IdentifierTable::get(llvm::StringRef) ()<br>
   from /usr/local/lib/libclang.so<br>#12 0x00007ffe098d55a7 in clang::ASTReader::DecodeIdentifierInfo(unsigned int)<br>    () from /usr/local/lib/libclang.so<br>#13 0x00007ffe098dcf67 in clang::ASTReader::GetIdentifierInfo(clang::serialization::ModuleFile&, llvm::SmallVector<unsigned long, 64u> const&, unsigned int&)<br>
    () from /usr/local/lib/libclang.so<br>#14 0x00007ffe098d5da3 in clang::ASTReader::ReadDeclarationName(clang::serialization::ModuleFile&, llvm::SmallVector<unsigned long, 64u> const&, unsigned int&)<br>
    () from /usr/local/lib/libclang.so<br>#15 0x00007ffe0990cbbc in clang::ASTDeclReader::VisitNamedDecl(clang::NamedDecl*) () from /usr/local/lib/libclang.so<br>#16 0x00007ffe099131c2 in clang::ASTDeclReader::VisitTemplateDecl(clang::TemplateDecl*) () from /usr/local/lib/libclang.so<br>
#17 0x00007ffe099133ea in clang::ASTDeclReader::VisitRedeclarableTemplateDecl(clang::RedeclarableTemplateDecl*) () from /usr/local/lib/libclang.so<br>#18 0x00007ffe09913492 in clang::ASTDeclReader::VisitClassTemplateDecl(clang::ClassTemplateDecl*) () from /usr/local/lib/libclang.so<br>
#19 0x00007ffe09924f40 in clang::DeclVisitor<clang::ASTDeclReader, void>::Visit(clang::Decl*) () from /usr/local/lib/libclang.so<br>#20 0x00007ffe0990c3e7 in clang::ASTDeclReader::Visit(clang::Decl*) ()<br>   from /usr/local/lib/libclang.so<br>
#21 0x00007ffe0991d848 in clang::ASTReader::ReadDeclRecord(unsigned int) ()<br>   from /usr/local/lib/libclang.so<br>#22 0x00007ffe098d1277 in clang::ASTReader::GetDecl(unsigned int) ()<br>   from /usr/local/lib/libclang.so<br>
#23 0x00007ffe098dce57 in clang::ASTReader::GetLocalDecl(clang::serialization::ModuleFile&, unsigned int) () from /usr/local/lib/libclang.so<br>#24 0x00007ffe098d171e in (anonymous namespace)::FindExternalLexicalDeclsVisitor::visit(clang::serialization::ModuleFile&, bool, void*) ()<br>
   from /usr/local/lib/libclang.so<br>#25 0x00007ffe099acd52 in visitDepthFirst(clang::serialization::ModuleFile&, bool (*)(clang::serialization::ModuleFile&, bool, void*), void*, llvm::SmallPtrSet<clang::serialization::ModuleFile*, 4u>&) () from /usr/local/lib/libclang.so<br>
#26 0x00007ffe099acde8 in clang::serialization::ModuleManager::visitDepthFirst(bool (*)(clang::serialization::ModuleFile&, bool, void*), void*) ()<br>   from /usr/local/lib/libclang.so<br>#27 0x00007ffe098d17e8 in clang::ASTReader::FindExternalLexicalDecls(clang::DeclContext const*, bool (*)(clang::Decl::Kind), llvm::SmallVectorImpl<clang::Decl*>&) () from /usr/local/lib/libclang.so<br>
#28 0x00007ffe09e6849a in clang::ExternalASTSource::FindExternalLexicalDecls(clang::DeclContext const*, llvm::SmallVectorImpl<clang::Decl*>&) ()<br>   from /usr/local/lib/libclang.so<br>#29 0x00007ffe09e666c2 in clang::DeclContext::LoadLexicalDeclsFromExternalStorage() const () from /usr/local/lib/libclang.so<br>
#30 0x00007ffe09e66a88 in clang::DeclContext::decls_begin() const ()<br>   from /usr/local/lib/libclang.so<br>#31 0x00007ffe0938216d in clang::cxcursor::CursorVisitor::VisitDeclContext(clang::DeclContext*) () from /usr/local/lib/libclang.so<br>
#32 0x00007ffe093846bf in clang::cxcursor::CursorVisitor::VisitNamespaceDecl(clang::NamespaceDecl*) () from /usr/local/lib/libclang.so<br>#33 0x00007ffe093ac330 in clang::DeclVisitor<clang::cxcursor::CursorVisitor, bool>::Visit(clang::Decl*) () from /usr/local/lib/libclang.so<br>
#34 0x00007ffe09381a05 in clang::cxcursor::CursorVisitor::VisitChildren(CXCursor) () from /usr/local/lib/libclang.so<br>#35 0x00007ffe0938ca66 in clang_visitChildren ()<br>   from /usr/local/lib/libclang.so<br>#36 0x00007ffe0ba29dac in ffi_call_unix64 () from /usr/lib64/libffi.so.5<br>
#37 0x00007ffe0ba29b34 in ffi_call () from /usr/lib64/libffi.so.5<br>#38 0x00007ffe0bc3d004 in _CallProc ()<br>   from /usr/lib64/python2.6/lib-dynload/_ctypes.so<br>#39 0x00007ffe0bc36312 in ?? ()<br>   from /usr/lib64/python2.6/lib-dynload/_ctypes.so<br>
#40 0x0000003bd2e44043 in PyObject_Call () from /usr/lib64/libpython2.6.so.1.0<br>#41 0x0000003bd2eddd84 in PyEval_EvalFrameEx ()<br><br>Note that for small program, e.g. a cpp file including only a simple function definition, loading the saved ast file will not crash. I think this crash will only happen for big source file.<br>
<br>Is this a bug in clang 3.2? Has it been fixed in latest code? Or anyone can explain this?<br><br><br>-----------------<br clear="all"><div><div dir="ltr">Best regards,<br>Yaolong Huang(Curtis)<br><br>Briontech China<br>
<br>Blog: <a href="http://airekans.github.com">http://airekans.github.com</a></div></div>
</div>