Parsing with libclang; unable to parse certain tokens (Python in Windows)

不想你离开。 提交于 2019-12-01 08:33:39
Andrew Walker

By default, libclang doesn't add the compiler system include path.

Always make sure you've checked the diagnostics - like compiler error messages, they tend to indicate how to resolve any issues. In this case, it would have been a reasonably obvious there was an include issue:

<Diagnostic severity 4, location <SourceLocation file 'test.cpp', line 3, column 10>, spelling "'iostream' file not found">

If you make sure libclang adds those paths, it should start working.

This question includes an approach to solving this problem. This seems to be a recurring theme on Stackoverflow, so I wrote ccsyspath to help find those paths on OSX, Linux and Windows. Simplifying your code slightly:

import clang.cindex
clang.cindex.Config.set_library_file('C:/Program Files (x86)/LLVM/bin/libclang.dll')
import ccsyspath

index = clang.cindex.Index.create()

args    = '-x c++ --std=c++11'.split()
syspath = ccsyspath.system_include_paths('clang++')
incargs = [ b'-I' + inc for inc in syspath ]
args    = args + incargs

trans_unit = index.parse('test.cpp', args=args)
for node in trans_unit.cursor.walk_preorder():
    if node.location.file is None:
        continue
    if node.location.file.name != 'test.cpp':
        continue
    if node.kind.is_declaration():
        print(node.kind, node.location)

Where my args end up being:

['-x',
 'c++',
 '--std=c++11',
 '-IC:\\Program Files (x86)\\LLVM\\bin\\..\\lib\\clang\\3.8.0\\include',
 '-IC:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\include',
 '-IC:\\Program Files (x86)\\Windows Kits\\8.1\\include\\shared',
 '-IC:\\Program Files (x86)\\Windows Kits\\8.1\\include\\um',
 '-IC:\\Program Files (x86)\\Windows Kits\\8.1\\include\\winrt']

and the output is:

(CursorKind.USING_DIRECTIVE, <SourceLocation file 'test.cpp', line 10, column 17>)
(CursorKind.FUNCTION_DECL, <SourceLocation file 'test.cpp', line 12, column 6>)
(CursorKind.PARM_DECL, <SourceLocation file 'test.cpp', line 12, column 35>)
(CursorKind.PARM_DECL, <SourceLocation file 'test.cpp', line 12, column 54>)
(CursorKind.VAR_DECL, <SourceLocation file 'test.cpp', line 15, column 14>)
(CursorKind.FUNCTION_DECL, <SourceLocation file 'test.cpp', line 21, column 8>)
(CursorKind.PARM_DECL, <SourceLocation file 'test.cpp', line 21, column 37>)
(CursorKind.VAR_DECL, <SourceLocation file 'test.cpp', line 24, column 14>)
(CursorKind.VAR_DECL, <SourceLocation file 'test.cpp', line 25, column 40>)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!