def init(): global inited global script_dir_path if not inited: from clang.cindex import Config Config.set_library_path(script_dir_path) inited = True
def main(args): clang_args = None args_split = [i for i, arg in enumerate(args) if arg == "--"] if args_split: args, clang_args = args[:args_split[0]], args[args_split[0] + 1:] default_config = os.path.dirname(args[0]) + "/gmock.conf" parser = OptionParser(usage="usage: %prog [options] files...") parser.add_option("-c", "--config", dest="config", default=default_config, help="config FILE (default='gmock.conf')", metavar="FILE") parser.add_option("-d", "--dir", dest="path", default=".", help="dir for generated mocks (default='.')", metavar="DIR") parser.add_option("-e", "--expr", dest="expr", default="", help="limit to interfaces within expression (default='')", metavar="LIMIT") parser.add_option("-l", "--libclang", dest="libclang", default=None, help="path to libclang.so (default=None)", metavar="LIBCLANG") (options, args) = parser.parse_args(args) if len(args) == 1: parser.error("at least one file has to be given") config = {} with open(options.config, 'r') as file: exec(file.read(), config) if options.libclang: Config.set_library_file(options.libclang) return mock_generator( files = args[1:], args = clang_args, expr = options.expr, path = options.path, mock_file_hpp = config['mock_file_hpp'], file_template_hpp = config['file_template_hpp'], mock_file_cpp = config['mock_file_cpp'], file_template_cpp = config['file_template_cpp'] ).generate()
def _run_checker(checker, argv): global clang_library_file if argv.execution_mode == 'full': files = core.get_files(argv.location[0]) else: files = core.get_files(argv.location[0], checker.get_pattern_hint()) if not files: cnf = 'Could not find any problem related to ' cnf += checker.get_problem_type().lower() sys.stderr.write(cnf + '\n') else: print(__current_wip(checker, files)) visitor = Visitor(checker) if clang_library_file == '': clang_libraries = glob.glob('/usr/lib*/libclang.so*') reverse_list = list(reversed(clang_libraries)) if reverse_list: clang_library_file = reverse_list[0] Config.set_library_file(clang_library_file) index = Index.create() for c_file in files: args = ["-ferror-limit=9999"] + _include_paths() root = index.parse( c_file, args=args, options=TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD) ReportBlocker.blocked_lines = [] visitor.visit(root.cursor, c_file)
def update_function(self): """ Obtain File-Function mapping through Python bindings for Clang and load into database. """ # load libclang.so if not Config.loaded: Config.set_library_path("/usr/local/lib") git_root = "hana" function_map = dict() for node in os.listdir(git_root): curr_path = os.path.join(git_root, node) if os.path.isdir(curr_path): print(curr_path) # update functions by directory headers = self.header_path(curr_path) if headers: function_map.update(self.multi_process(headers)) # insert documents documents = [] for key in sorted(function_map.keys()): data = dict() data["function"] = key data["component"] = function_map[key] documents.append(data) with MongoConnection(self.host, self.port) as mongo: collection = mongo.connection["kdetector"]["function"] collection.drop() collection.insert_many(documents) print( f"\x1b[32mSuccessfully updated File-Function mapping ({len(documents)}).\x1b[0m" )
def __init__(self, lang, header, api_headers, check_all, args): # TODO: Avoid hard-coding paths and args in general. Config.set_library_file('libclang-6.0.so.1') index = Index.create() self.is_cpp = True if lang == 'c++' else False self.clang_args = ['-x', lang] self.translation_unit = index.parse(header, args + self.clang_args, options=1) for diag in self.translation_unit.diagnostics: msg = '\033[91mERROR : {} at {} line {}, column {}\033[00m' print( msg.format(diag.spelling, diag.location.file, diag.location.line, diag.location.column)) self.api_headers = api_headers self.check_all = check_all self.enum_constant_decls = [] self.function_decls = [] self.var_decls = [] self.macro_defs = [] self.record_decls = [] self.namespaces = []
def sync_configure_path(libclangPath): mutex2 = threading.Lock() if Config.loaded == False: mutex2.acquire() if Config.loaded == False: Config.set_library_file(libclangPath) mutex2.release()
def init_clang(): global initialized, ix if not initialized: conda_prefix = Path(getenv('CONDA_PREFIX')) Config.set_library_file(conda_prefix / 'lib' / 'libclang.so') # Monkeypatch clang monkeypatch_cursor('is_virtual', 'clang_isVirtualBase', [Cursor], c_uint) monkeypatch_cursor('is_inline', 'clang_Cursor_isFunctionInlined', [Cursor], c_uint) monkeypatch_cursor('get_specialization', 'clang_getSpecializedCursorTemplate', [Cursor], Cursor) monkeypatch_cursor('get_template_kind', 'clang_getTemplateCursorKind', [Cursor], c_uint) monkeypatch_cursor('get_num_overloaded_decl', 'clang_getNumOverloadedDecls', [Cursor], c_uint) initialized = True ix = Index.create()
def main(): import sys from clang.cindex import Index, Config from io import open # TODO: Don't hard code the clang library path Config.set_library_path("/usr/lib/llvm-3.3/lib") if len(sys.argv) == 1 or len(sys.argv) > 3: usage() sys.exit(1) cppFile = str() overwrite = False if "-o" in sys.argv: overwrite = True cppFile = sys.argv[len(sys.argv) - 1] index = Index.create() transUnit = index.parse(cppFile) docBlocks = get_doc_comments(transUnit.cursor, cppFile) source = source_with_doc_blocks(cppFile, docBlocks) if overwrite: with open(cppFile, "w") as file: file.write(unicode(source)) else: sys.stdout.write(unicode(source))
def run(self): Config.set_library_file(self.libClangPath) index = Index.create() tree = index.parse(self.path, args=self.args) self.walker.startWalk(tree.cursor, self.generator)
def main(args): clang_args = None args_split = [i for i, arg in enumerate(args) if arg == "--"] if args_split: args, clang_args = args[:args_split[0]], args[args_split[0] + 1:] default_config = os.path.dirname(args[0]) + "/gmock.conf" parser = OptionParser(usage="usage: %prog [options] files...") parser.add_option("-c", "--config", dest="config", default=default_config, help="config FILE (default='gmock.conf')", metavar="FILE") parser.add_option("-d", "--dir", dest="path", default=".", help="dir for generated mocks (default='.')", metavar="DIR") parser.add_option( "-e", "--expr", dest="expr", default="", help="limit to interfaces within expression (default='')", metavar="LIMIT") parser.add_option("-l", "--libclang", dest="libclang", default=None, help="path to libclang.so (default=None)", metavar="LIBCLANG") (options, args) = parser.parse_args(args) if len(args) == 1: parser.error("at least one file has to be given") config = {} with open(options.config, 'r') as file: exec(file.read(), config) if options.libclang: Config.set_library_file(options.libclang) return mock_generator( files=args[1:], args=clang_args, expr=options.expr, path=options.path, mock_file_hpp=config['mock_file_hpp'], file_template_hpp=config['file_template_hpp'], mock_file_cpp=config['mock_file_cpp'], file_template_cpp=config['file_template_cpp']).generate()
def main(): Config.set_compatibility_check(False) # NOTE: for darwin Config.set_library_path("/usr/local/opt/llvm/lib") parser = argparse.ArgumentParser() parser.add_argument('--style', type=str, help='coding rule (only google is supported)') parser.add_argument('filepath') args, extra_args = parser.parse_known_args() clang_func_range_parser = ClangFuncRangeParser(args.filepath) clang_func_range_parser.print_all()
def clang_setup(force=False): """Try to find and configure libclang location on RTD.""" if 'READTHEDOCS' in os.environ or force: try: result = subprocess.run(['llvm-config', '--libdir'], check=True, capture_output=True, encoding='utf-8') libdir = result.stdout.strip() # For some reason there is no plain libclang.so symlink on RTD. from clang.cindex import Config Config.set_library_file(os.path.join(libdir, 'libclang.so.1')) except Exception as e: print(e)
def __init__(self, filename, llvm_libdir=None, llvm_libfile=None): self.prototypes = [] self.functions = {} self.filename = filename if llvm_libdir is not None: Config.set_library_path(llvm_libdir) if llvm_libfile is not None: Config.set_library_file(llvm_libfile) self._parse()
def __init__(self, library_path, project_root, n_workers=None): if n_workers is None: n_workers = (multiprocessing.cpu_count() * 3) / 2 self.clang_library_path = library_path if not Config.loaded: Config.set_library_path(self.clang_library_path) Config.set_compatibility_check(False) self.builtin_header_path = getBuiltinHeaderPath( self.clang_library_path) if self.builtin_header_path is None: raise Exception("Cannot find clang includes") project_root = os.path.abspath(project_root) curr_path = project_root self.compile_commands_path = None while curr_path: compile_commands_path = os.path.join(curr_path, 'compile_commands.json') if os.path.exists(compile_commands_path): self.compile_commands_path = compile_commands_path self.index_db_path = os.path.join(curr_path, '.ctrlk-index') self.project_root = curr_path break elif curr_path == '/': break curr_path = os.path.dirname(curr_path) if self.compile_commands_path is None: raise Exception("Could not find a 'compile_commands.json' file in the " +\ "directory hierarchy from '%s'" % (project_root)) self._compilation_db = None self._compilation_db_modtime = 0 self._leveldb_connection = None indexer.start(self.leveldb_connection, n_workers) self.current_file_tus = {} self.current_file_expire = {} self.current_file_scopes = {} self.c_parse_queue = [] self.c_parse_lock = threading.Lock() self.c_parse_cond = threading.Condition(self.c_parse_lock) threading.Thread(target=ParseCurrentFileThread, args=(self, )).start()
def __init__(self, clang_path, args, cache_folder): self._clang_path = 'clang++' if clang_path is not None: Config.set_library_path(clang_path) self._clang_path = os.path.normpath( os.path.join(clang_path, "clang++")) syspath = ccsyspaths_improved.system_include_paths(self._clang_path) incargs = [b'-I' + inc for inc in syspath] args = args + incargs self._args = args self._parsed_classes = {} self._cache_folder = cache_folder
def main(): global is_including_other_file global source_file_path parser = argparse.ArgumentParser( description='Convert C++ header file to rust source file.') parser.add_argument('src', help='C++ header file path.') parser.add_argument('dst', help='Output file path.') parser.add_argument('--CLANG_LIBRARY_PATH', help='Clang library path.') parser.add_argument('-i', '--include', action='store_true', help='Include included file') args = parser.parse_args() print('src=' + args.src) print('dst=' + args.dst) print('CLANG_LIBRARY_PATH=' + str(args.CLANG_LIBRARY_PATH)) print('include=' + str(args.include)) source_file_path = args.src is_including_other_file = args.include if args.CLANG_LIBRARY_PATH: Config.set_library_path(args.CLANG_LIBRARY_PATH) else: CLANG_LIBRARY_PATH = 'CLANG_LIBRARY_PATH' if CLANG_LIBRARY_PATH in os.environ: clang_library_path = os.environ[CLANG_LIBRARY_PATH] Config.set_library_path(clang_library_path) index = Index.create() tu = index.parse(args.src, options=TranslationUnit.PARSE_SKIP_FUNCTION_BODIES) file_test = open(args.dst + '.txt', 'w') dump(tu.cursor, file_test) file_test.close() converted = convert(tu.cursor, Converted()) file = open(args.dst, 'w') file.writelines(converted.enums) file.writelines(converted.unions) file.writelines(converted.structs) file.writelines(converted.functions) file.close()
def __init__(self, library_path, project_root, n_workers=None): if n_workers is None: n_workers = (multiprocessing.cpu_count() * 3) / 2 self.clang_library_path = library_path if not Config.loaded: Config.set_library_path(self.clang_library_path) Config.set_compatibility_check(False) self.builtin_header_path = getBuiltinHeaderPath(self.clang_library_path) if self.builtin_header_path is None: raise Exception("Cannot find clang includes") project_root = os.path.abspath(project_root) curr_path = project_root self.compile_commands_path = None while curr_path: compile_commands_path = os.path.join(curr_path, 'compile_commands.json') if os.path.exists(compile_commands_path): self.compile_commands_path = compile_commands_path self.index_db_path = os.path.join(curr_path, '.ctrlk-index') self.project_root = curr_path break elif curr_path == '/': break curr_path = os.path.dirname(curr_path) if self.compile_commands_path is None: raise Exception("Could not find a 'compile_commands.json' file in the " +\ "directory hierarchy from '%s'" % (project_root)) self._compilation_db = None self._compilation_db_modtime = 0 self._leveldb_connection = None indexer.start(self.leveldb_connection, n_workers) self.current_file_tus = {} self.current_file_expire = {} self.current_file_scopes = {} self.c_parse_queue = [] self.c_parse_lock = threading.Lock() self.c_parse_cond = threading.Condition(self.c_parse_lock) threading.Thread(target=ParseCurrentFileThread, args=(self,)).start()
def main(prog_path, libclang_path, api_header, pch_dst, *libclang_args): Config.set_library_file(libclang_path) index = Index.create(excludeDecls=True) # We should really want to use a compilation database here, except that it's only supported by makefiles... tu = index.parse(api_header, args=libclang_args, options=TranslationUnit.PARSE_INCOMPLETE | TranslationUnit.PARSE_SKIP_FUNCTION_BODIES) errors = diagnose_errors(list_diagnostics(tu)) if errors: raise errors else: tu.save( pch_dst ) # We'll need this in a minute, and there's no way to just get it in RAM
def __init__(self, hw_file_name, json_file_name, llvm_libdir, llvm_libfile): #llvmのファイルをセット if llvm_libfile != None: Config.set_library_file(llvm_libfile) if llvm_libdir != None: Config.set_library_path(llvm_libdir) #clangにソースコードをぶちこむ self.index = clang.cindex.Index.create() self.tree = self.index.parse(hw_file_name) self.hw_file_name = hw_file_name self.json_file_name = json_file_name #抽出するデータたち self.func_name = "" self.func_decl = "" self.return_type = "" self.parm_decls = [] self.parm_types = [] self.parm_suffixs = [] self.parm_data_numbers = [] self.parm_interfaces = [] self.parm_bundles = [] self.parm_directions = [] self.parm_slave_bundles_noduplication = [] self.bundle_port_dic = {} self.use_hp_ports = False self.func_find_flag = False self.func_find_flag_once = False #Json/C解析 self.__analyzeJson() self.__extractParameter() #関数名の成形 self.func_name_u = self.func_name.upper() self.func_name_l = self.func_name.lower() self.func_name_ul = (self.func_name[0]).upper() + ( self.func_name[1:]).lower()
def try_clang_cindex(): from clang.cindex import Index, Config, TranslationUnit Config.set_library_file(f"/usr/lib/llvm-9/lib/libclang.so") filename = "/tmp/clang_cindex_tmp_src.cc" with open(filename, "w") as f: f.write(code) index = Index.create() t_start = time.time() tu = TranslationUnit.from_source( filename=filename, index=index, args=[ f"-I/usr/include/eigen3", ], ) dt = time.time() - t_start return dt
def find_clang_posix() -> None: # Find the clang library. On some distros, the clang library is not called `libclang.so`, for # example on Ubuntu, it is `libclang.so.1`, making cindex unable to find it. proc: subprocess.CompletedProcess = subprocess.run( ['llvm-config', '--libdir'], stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE, encoding='utf-8') if proc.returncode != 0: raise RuntimeError('Could not find LLVM path') clang_libs: List[str] = glob.glob( os.path.join(proc.stdout.strip(), 'libclang.so*')) if len(clang_libs) == 0: raise RuntimeError( 'Could not find libclang.so, make sure Clang is installed') Config.set_library_file(clang_libs[0])
def api_init_thread(libraryPath): global g_api global g_builtin_header_path global parsingState global parsingCurrentState global updateProcess global updateFileProcess Config.set_library_path(libraryPath) Config.set_compatibility_check(False) parsingState = "Initializing" parsingCurrentState = "Initializing" if not try_initialize(libraryPath): server_path = ctrlk_server.get_absolute_path() with open('/tmp/ctrlk_server_stdout', 'a') as server_stdout: with open('/tmp/ctrlk_server_stderr', 'a') as server_stderr: subprocess.Popen(['python', server_path, '--port', str(client_api.DEFAULT_PORT), '--suicide-seconds', '3600'],\ stdout=server_stdout, stderr=server_stderr) for i in range(30): if try_initialize(libraryPath): break time.sleep(0.1) else: parsingState = "Failed to initialize" pass if g_api is not None: g_builtin_header_path = g_api.get_builtin_header_path() parsingCurrentState = "Ready to parse" if updateProcess == None: updateProcess = threading.Thread(target=UpdateCppIndexThread) updateProcess.daemon = True updateProcess.start() updateFileProcess = threading.Thread(target=ParseCurrentFileThread) updateFileProcess.daemon = True updateFileProcess.start()
def main(prog_path, libclang_path, api_header, pch_dst, api_casts_dst, namespace_filter, function_filter, namespace_dst, *libclang_args): accept_from = set(namespace_filter.split(" ")) valid_function_prefixes = set(function_filter.split(" ")) Config.set_library_file(libclang_path) index = Index.create(excludeDecls=True) # We should really want to use a compilation database here, except that it's only supported by makefiles... tu = TranslationUnit.from_ast_file(pch_dst, index) filt = FFIFilter(lambda s: s[0] in accept_from, lambda x: any([x.displayname.startswith(prefix) for prefix in valid_function_prefixes]), solve_template_base_config(index, pch_dst)) code_gen = CodeGen(prog_path, pre_hook = lambda: ("namespace %s {" % (namespace_dst,), 4), post_hook = lambda indent: "}") with open(api_casts_dst, 'w') as out_handle: from os.path import abspath out_handle.write(code_gen(api_header, filt.exposed_types, filt.emit_table_for_TU(tu.cursor)))
def main(filename): """Main function""" functions = [] tree = [] Config.set_library_file( "/Library/Developer/CommandLineTools/usr/lib/libclang.dylib") index = Index.create() ast = index.parse(None, [filename]) if not ast: print("Unable to parse file") sys.exit(1) # Step 1: Find all the C/ObjC functions functions = [x for x in ast.cursor.get_children() if is_function(x)] # Step 1a: Add in ObjC class methods for item in ast.cursor.get_children(): if is_implementation(item): functions += [x for x in item.get_children() if is_function(x)] # Step 2: Filter out all of the functions that are Lua->C calls functions = [x for x in functions if not is_lua_call(x)] # Step 3: Recursively walk the remaining functions tree = [build_tree(x, False) for x in functions] # Step 4: Filter the functions down to those which contain Lua calls tree = [x for x in tree if contains_lua_calls(x)] for thing in tree: stacktxt = "NO STACKGUARD" hasstackentry = "_lua_stackguard_entry" in thing['tokens'] hasstackexit = "_lua_stackguard_exit" in thing['tokens'] if hasstackentry and hasstackexit: continue if hasstackentry and not hasstackexit: stacktxt = "STACK ENTRY, BUT NO EXIT" if hasstackexit and not hasstackentry: stacktxt = "STACK EXIT, BUT NO ENTRY (THIS IS A CRASH)" print(u"%s :: %s :: %s" % (filename, thing['spelling'], stacktxt))
def __init__(self): homebrew_lib_path = '/usr/local/Cellar/llvm/6.0.1/lib/' xcode_lib_path = '/Applications/Xcode.app/Contents/Developer/Toolchains' \ '/XcodeDefault.xctoolchain/usr/lib/' if os.path.exists(homebrew_lib_path): Config.set_library_path(homebrew_lib_path) elif os.path.exists(xcode_lib_path): Config.set_library_path(xcode_lib_path) else: raise Exception('Path for libclang lib not found') self.index: Index = Index.create() self.translation_unit: TranslationUnit = self.index.parse( path=None, args=[ '/Users/pierre/src/bitcoin/src/wallet/wallet.h', '/Users/pierre/src/bitcoin/src/wallet/wallet.cpp' ] )
def initClangComplete(clang_complete_flags, clang_compilation_database, library_path): global index debug = int(vim.eval("g:clang_debug")) == 1 quiet = int(vim.eval("g:clang_quiet")) == 1 if library_path: if os.path.isdir(library_path): Config.set_library_path(library_path) else: Config.set_library_file(library_path) Config.set_compatibility_check(False) try: index = Index.create() except Exception as e: if quiet: return 0 if library_path: suggestion = "Are you sure '%s' contains libclang?" % library_path else: suggestion = "Consider setting g:clang_library_path." if debug: exception_msg = str(e) else: exception_msg = '' print('''Loading libclang failed, completion won't be available. %s %s ''' % (suggestion, exception_msg)) return 0 global builtinHeaderPath builtinHeaderPath = None if not canFindBuiltinHeaders(index): builtinHeaderPath = getBuiltinHeaderPath(library_path) if not builtinHeaderPath: print("WARNING: libclang can not find the builtin includes.") print(" This will cause slow code completion.") print(" Please report the problem.") global translationUnits translationUnits = dict() global complete_flags complete_flags = int(clang_complete_flags) global compilation_database if clang_compilation_database != '': compilation_database = CompilationDatabase.fromDirectory( clang_compilation_database) else: compilation_database = None global libclangLock libclangLock = threading.Lock() return 1
def init(): conf = Config() # here we use the libclang.dylib from the vim plugin -- YouCompleteMe # path = "/Users/lono/.config/nvim/plugged/YouCompleteMe/third_party/ycmd" # path = "/Applications/Xcode.app/Contents/Frameworks" path = "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib" # path = "/Library/Developer/CommandLineTools/usr/lib/" # path = "/Applications/Xcode8.app/Contents/Frameworks" Config.set_library_path(path) conf.set_library_path(path) try: conf.get_cindex_library() except LibclangError as e: print "Error: " + str(e)
def main(): from clang.cindex import Index, Config from pprint import pprint from optparse import OptionParser, OptionGroup global opts parser = OptionParser("usage: %prog [options] {filename} [clang-args*]") parser.add_option("", "--show-ids", dest="showIDs", help="Compute cursor IDs (very slow)", action="store_true", default=False) parser.add_option("", "--max-depth", dest="maxDepth", help="Limit cursor expansion to depth N", metavar="N", type=int, default=None) parser.disable_interspersed_args() from pathlib import Path as path file = path('../src/simple.cpp') #file = path('/home/mpercossi/.conan/data/range-v3/0.11.0/_/_/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9/include/range/v3/view/single.hpp') all_args = ['--show-ids', str(file.absolute()), '-stdlib=libc++', '-std=c++20', '-L/usr/lib/llvm-11/lib', '-I/usr/lib/llvm-11/include', '-std=c++20'] + EXTRA_ARGS.split(' ') print(f"{' '.join(all_args)}") (opts, args) = parser.parse_args(['--show-ids', str(file.absolute()), '-stdlib=libc++', '-std=c++20', '-L/usr/lib/llvm-11/lib', '-I/usr/lib/llvm-11/include', '-std=c++20'] + EXTRA_ARGS.split(' ')) if len(args) == 0: parser.error('invalid number arguments') Config.set_library_file('/usr/lib/llvm-11/lib/libclang-11.so') index = Index.create() tu = index.parse(None, args) if not tu: parser.error("unable to load input") #pprint(('diags', [get_diag_info(d) for d in tu.diagnostics])) pprint(('nodes', get_defns(tu.cursor)))
def init_clang(path=None): global initialized, ix if not initialized: conda_prefix = Path(getenv('CONDA_PREFIX', '')) if path: pass elif platform.startswith('win'): path = conda_prefix / 'Library' / 'bin' / 'libclang.dll' elif platform.startswith('linux') or platform.startswith('freebsd'): path = conda_prefix / 'lib' / 'libclang.so' elif platform.startswith('darwin'): path = conda_prefix / 'lib' / 'libclang.dylib' Config.set_library_file(path) # Monkeypatch clang monkeypatch_cursor('is_virtual', 'clang_isVirtualBase', [Cursor], c_uint) monkeypatch_cursor('is_inline', 'clang_Cursor_isFunctionInlined', [Cursor], c_uint) monkeypatch_cursor('get_specialization', 'clang_getSpecializedCursorTemplate', [Cursor], Cursor) monkeypatch_cursor('get_template_kind', 'clang_getTemplateCursorKind', [Cursor], c_uint) monkeypatch_cursor('get_num_overloaded_decl', 'clang_getNumOverloadedDecls', [Cursor], c_uint) initialized = True ix = Index.create()
def initClangComplete(clang_complete_flags, clang_compilation_database, library_path): global index debug = int(vim.eval("g:clang_debug")) == 1 quiet = int(vim.eval("g:clang_quiet")) == 1 if library_path: if os.path.isdir(library_path): Config.set_library_path(library_path) else: Config.set_library_file(library_path) Config.set_compatibility_check(False) try: index = Index.create() except Exception as e: if quiet: return 0 if library_path: suggestion = "Are you sure '%s' contains libclang?" % library_path else: suggestion = "Consider setting g:clang_library_path." if debug: exception_msg = str(e) else: exception_msg = '' print('''Loading libclang failed, completion won't be available. %s %s ''' % (suggestion, exception_msg)) return 0 global builtinHeaderPath builtinHeaderPath = None if not canFindBuiltinHeaders(index): builtinHeaderPath = getBuiltinHeaderPath(library_path) if not builtinHeaderPath: print("WARNING: libclang can not find the builtin includes.") print(" This will cause slow code completion.") print(" Please report the problem.") global translationUnits translationUnits = dict() global complete_flags complete_flags = int(clang_complete_flags) global compilation_database if clang_compilation_database != '': compilation_database = CompilationDatabase.fromDirectory(clang_compilation_database) else: compilation_database = None global libclangLock libclangLock = threading.Lock() return 1
def initialiseClangDoxygen(): conf = Config() if vim.eval("exists(\"g:clang_doxygen_libclang_library_path\")") != "0": Config.set_library_path(vim.eval("g:clang_doxygen_libclang_library_path")) conf.set_library_path(vim.eval("g:clang_doxygen_libclang_library_path")) try: conf.get_cindex_library() except LibclangError as e: print "Error: " + str(e) return vim.command("let g:initialised_clang_doxygen = 1")
def __init__(self, headName): index = clang.cindex.Index.create() self.Conf = Config() self.tu = index.parse(headName, args=['-std=c++11']) self.className = [] # class name stack self.classType = [] # class type stack: 'class','template','struct' self.headName = headName.split('/')[-1].split('.')[0] self.nameSpace = [] # namespace stack self.classDepth = 0 # the size of class stack self.waitPyx = '' # Nested class or struct or enum waiting to write to .pyx self.waitPxd = '' # Nested class or struct or enum waiting to write to .pxd self.typedefMap = {} # map a typedef name to its type self.constructCode = {} # map a class name to its constructors self.constructDict = { } # map a class name to the number of its constructor self.abstractMap = { } # map a class name to whether it is abstract or not self.construct = False # is now traversing constructor or not self.inClassEnum = False # is now traversing nested enum or not self.waitPxdChanged = False
def __init__(self, src, flags, input_stream, output_stream): self.src = src self.flags = flags self.input_stream = input_stream self.output_stream = output_stream self.index = Index.create() self.translation_unit = None self.completion_cache = None self.cached_contents = None conf = Config() self.custom_clang_lib = conf.lib self._register_custom_clang_functions() # Cache the libclang version. cxstr = self.custom_clang_lib.clang_getClangVersion() version = self.custom_clang_lib.clang_getCString(cxstr) if version.startswith(Server.CLANG_VERSION_PREFIX): version = version[len(Server.CLANG_VERSION_PREFIX):] else: version = '3.7.0' self.clang_version = LooseVersion(version)
def init(self): clang_complete_flags = self.vim.eval('g:clang_complete_lib_flags') library_path = self.vim.eval('g:clang_library_path') clang_compilation_database = self.vim.eval( 'g:clang_compilation_database') if library_path: if os.path.isdir(library_path): Config.set_library_path(library_path) else: Config.set_library_file(library_path) Config.set_compatibility_check(False) try: self.index = Index.create() except Exception as e: if library_path: suggestion = "Are you sure '%s' contains libclang?" % library_path else: suggestion = "Consider setting g:clang_library_path." logger.exception( "Loading libclang failed, completion won't be available. %s %s ", suggestion, exception_msg) return 0 if not self.canFindBuiltinHeaders(self.index): self.builtinHeaderPath = self.getBuiltinHeaderPath(library_path) if not self.builtinHeaderPath: logger.warn("libclang find builtin header path failed: %s", self.builtinHeaderPath) self.complete_flags = int(clang_complete_flags) if clang_compilation_database != '': self.compilation_database = CompilationDatabase.fromDirectory( clang_compilation_database) else: self.compilation_database = None return 1
A simple command line tool for dumping a Graphviz description (dot) that describes include dependencies. Heavily inspired by cindex-includes.py disitributed with the clang source code. """ import ctypes.util import logging import sys import os from clang.cindex import Index, Config for libname in ['clang', 'clang-4.0', 'clang-3.9', 'clang-3.8', 'clang-3.7']: filename = ctypes.util.find_library(libname) if filename: Config.set_library_file(filename) break logger = logging.getLogger('incdeps') def emit_node(out, uid, label): out.write(' "%s" [label="%s"];\n' % (uid, label)) def emit_cluster(out, children, cluster_path='', parent_path=''): prefix = ' ' * (cluster_path.count(os.path.sep) - 1) nodes = list() clusters = dict()
import asciitree from clang.cindex import Config, Index, TranslationUnit from ConfigParser import ConfigParser from path import path if not Config.loaded: _path_ = path(__file__) while len(_path_) > 0 and not str(_path_.name) == 'src': _path_ = _path_.parent _path_ = _path_.parent configparser = ConfigParser() configparser.read(_path_/'metainfo.ini') config = dict(configparser.items('libclang')) if 'path' in config: Config.set_library_path(config['path']) elif 'file' in config: Config.set_library_file(config['file']) else: raise IOError('cannot find libclang path or file') class AST(object): """ """ def __init__(self, *translation_units): """ """ if any(not isinstance(translation_unit, TranslationUnit) for translation_unit in translation_units): raise TypeError('`translation_units` parameter') self._translation_units = translation_units
#!/usr/bin/env python import sys, os; from os import environ as env LIBCLANG_PATH = "./libclang-py" if env.has_key('TM_BUNDLE_SUPPORT'): LIBCLANG_PATH = env['TM_BUNDLE_SUPPORT']+"/libclang-py" sys.path.append(LIBCLANG_PATH) from clang.cindex import Index, Cursor, CursorKind, Config, SourceLocation as Location Config.set_library_path('/opt/llvm/head/lib') # PROJECT_DIR = '/Users/bholt/dev/test' DEBUG = False class MateIndex: def __init__(self): if env.has_key('TM_PROJECT_DIRECTORY'): self.project_dir = env['TM_PROJECT_DIRECTORY'] else: self.project_dir = "." self.index = Index.create() self.type_kinds = set([CursorKind.STRUCT_DECL, CursorKind.CLASS_DECL, CursorKind.CLASS_TEMPLATE,CursorKind.TYPEDEF_DECL]) def find_types(self, node=None, types=set()): global DEBUG if not node: node = self.tu.cursor
return Run(cmd)[0].strip() NACL_CONFIG = os.path.join(NACL_SDK_ROOT, 'tools', 'nacl_config.py') PNACL_CLANG = RunNaClConfig('-t', 'pnacl', '--tool', 'clang') PNACL_ROOT = os.path.dirname(os.path.dirname(PNACL_CLANG)) # PNACL_LIB = os.path.join(PNACL_ROOT, GetHostDir(), 'lib') PNACL_LIB = os.path.join(PNACL_ROOT, 'lib') sys.path.append(PYTHON_BINDINGS_DIR) import clang.cindex from clang.cindex import Index, CursorKind, TypeKind, TranslationUnit, Config from clang.cindex import TokenKind Config.set_library_path(PNACL_LIB) class Error(Exception): pass class RunError(Error): def __init__(self, msg, stdout, stderr): Error.__init__(self, msg) self.stdout = stdout self.stderr = stderr def CreateTranslationUnit(args, detailed=False): new_args = GetIndexParseArgs(args)
g = ObjectMetadataGenerator(obj) generators.append(g) all_attrs += [a.name for a in attrs] self.data = { 'impls': [g.implementation() for g in generators], 'headers': [x for x in [g.header() for g in generators] if len(x) > 0], 'object_names': [o.name for o in h.objects], 'attrs': sorted(all_attrs) } if __name__ == '__main__': parser = OptionParser() parser.add_option('--clang-lib', default='/usr/lib/llvm-6.0/lib/libclang.so.1') (options, args) = parser.parse_args() Config.set_library_file(options.clang_lib) h = TAIHeader(args[0]) g = TAIMetadataGenerator(h) with open('taimetadata.h', 'w') as f: f.write(g.header()) with open('taimetadata.c', 'w') as f: f.write(g.implementation())
def main(argv=None): """ Takes a set of C++ header files and generate a JSON output file describing the objects found in them. This output is intended to support more convenient access to a set of cppyy-supported bindings. Examples: INC=/usr/include QT5=$INC/x86_64-linux-gnu/qt5 KF5=$INC/KF5 INCDIRS="\\\\-I$KF5/KConfigCore;\\\\-I$QT5/QtXml;\\\\-I$QT5/QtCore" STDDIRS="\\\\-I$Qt5/mkspecs/linux-g++-64\\\\;-I$KF5;\\\\-I$QT5" FLAGS="\\\\-fvisibility=hidden;\\\-D__PIC__;\\\\-Wno-macro-redefined;\\\\-std=c++14" cppyy-generator --flags "$FLAGS;$INCDIRS;$STDDIRS" KF5/Config/Config.map $INC/KF5/KConfigCore/* """ if argv is None: argv = sys.argv parser = argparse.ArgumentParser(epilog=inspect.getdoc(main), formatter_class=HelpFormatter) parser.add_argument("-v", "--verbose", action="store_true", default=False, help=_("Enable verbose output")) parser.add_argument("--flags", default="", help=_("Semicolon-separated C++ compile flags to use, escape leading - or -- with \\")) parser.add_argument("--libclang", help=_("libclang library to use for parsing")) parser.add_argument("output", help=_("Output filename to write")) parser.add_argument("sources", nargs="+", help=_("C++ headers to process")) try: args = parser.parse_args(argv[1:]) if args.verbose: logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)s %(levelname)s: %(message)s') else: logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') flags = [] for f in args.flags.lstrip().split(";"): if f.startswith("\\-\\-"): flags.append("--" + f[4:]) elif f.startswith("\\-"): flags.append("-" + f[2:]) elif f: flags.append(f) # # Load the given libclang. # if args.libclang: Config.set_library_file(args.libclang) lib = Config().lib import ctypes from clang.cindex import Type items = [ ("clang_Type_getNumTemplateArguments", [Type], ctypes.c_size_t), ] for item in items: func = getattr(lib, item[0]) if len(item) >= 2: func.argtypes = item[1] if len(item) >= 3: func.restype = item[2] if len(item) == 4: func.errcheck = item[3] # # Generate! # g = CppyyGenerator(flags, verbose=args.verbose) mapping = g.create_mapping(args.sources) with open(args.output, "w") as f: json.dump(mapping, f, indent=1, sort_keys=True) return 0 except Exception as e: tbk = traceback.format_exc() print(tbk) return 1
#!/usr/bin/env python """ Usage: call with <filename> <typename> """ import sys import clang.cindex def find_typerefs(node, typename): """ Find all references to the type named 'typename' """ if node.kind.is_reference(): ref_node = clang.cindex.Cursor(node) if ref_node.spelling == typename: print 'Found %s [line=%s, col=%s]' % ( typename, node.location.line, node.location.column) # Recurse for children of this node for c in node.get_children(): find_typerefs(c, typename) from ctypes.util import find_library from clang.cindex import Config Config.set_library_file(find_library('clang')) index = clang.cindex.Index.create() tu = index.parse(sys.argv[1]) print 'Translation unit:', tu.spelling find_typerefs(tu.cursor, sys.argv[2])
unsaved_contents == self.cached_contents): return translation_unit options = 0 # There are no reparse options available in libclang yet. translation_unit.reparse(self._make_files(unsaved_contents), options) self.cached_contents = unsaved_contents if self.completion_cache is not None: self.completion_cache.invalidate() return translation_unit def _register_custom_clang_functions(self): # Extend the Clang C bindings with the additional required functions. for item in Server.CUSTOM_CLANG_FUNCTIONS: func = getattr(self.custom_clang_lib, item[0]) func.argtypes = item[1] func.restype = item[2] if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('src', metavar='<path>', type=str, help='Full path of source file to analyze.') parser.add_argument('flags', metavar='<flags>', type=str, nargs='*', help='Extra flags to pass to Clang.') parser.add_argument('--libclang-file', help='Path to libclang dynamic library') args = parser.parse_args() if args.libclang_file: Config.set_library_file(args.libclang_file) set_up_logging(args.src) Server(args.src, args.flags, sys.stdin, sys.stdout).run()
from clang.cindex import Config Config.set_library_path("/usr/i386-linux-cgc/lib")
def __init__(self, libclang_path, srcdir): if Config.library_file != libclang_path: Config.set_library_file(libclang_path) self.srcdir = srcdir
if args.table: call(["cp", os.path.join(os.path.dirname(__file__), 'util', 'table_util.hh'), args.util_path]) else: call(["cp", os.path.join(os.path.dirname(__file__), 'util', 'util.hh'), args.util_path]) def generate_type_erased_interface(args): type_erasure.detail_generator.write_file(get_data(args)) type_erasure.interface_generator.write_file(get_data(args)) def format_generated_files(args): # format files abs_path = os.path.join(os.getcwd(), args.detail_folder, args.detail_file) type_erasure.util.clang_format(abs_path) abs_path = os.path.join(os.getcwd(), args.interface_file) type_erasure.util.clang_format(abs_path) if not args.header_only: abs_path = os.path.join(os.getcwd(), type_erasure.interface_generator.get_source_filename(args.interface_file)) type_erasure.util.clang_format(abs_path) if __name__ == "__main__": parser = create_parser() args = parser.parse_args() if args.clang_path: Config.set_library_path(args.clang_path) copy_utility_file(args) generate_type_erased_interface(args) format_generated_files(args)
import os from clang.cindex import Config if 'CLANG_LIBRARY_PATH' in os.environ: Config.set_library_path(os.environ['CLANG_LIBRARY_PATH']) import ctypes import gc import unittest from clang.cindex import AvailabilityKind from clang.cindex import CursorKind from clang.cindex import TemplateArgumentKind from clang.cindex import TranslationUnit from clang.cindex import TypeKind from .util import get_cursor from .util import get_cursors from .util import get_tu kInput = """\ struct s0 { int a; int b; }; struct s1; void f0(int a0, int a1) { int l0, l1; if (a0)
# sdef file emitted by the sdef tool. # import sys import struct import re from itertools import chain from clang.cindex import TranslationUnit from clang.cindex import CursorKind from clang.cindex import Config from clang.cindex import TypeKind Config.set_library_path("/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib") keywords = ['class', 'deinit', 'enum', 'extension', 'func', 'import', 'init', 'internal', 'let', 'operator', 'private', 'protocol', 'public', 'static', 'struct', 'subscript', 'typealias', 'var', 'break', 'case', 'continue', 'default', 'do', 'else', 'fallthrough', 'for', 'if', 'in', 'return', 'switch', 'where', 'while', 'as', 'dynamicType', 'false', 'is', 'nil', 'self', 'Self', 'super', 'true', 'associativity', 'convenience', 'dynamic', 'didSet', 'final', 'get', 'infix', 'inout', 'lazy', 'left', 'mutating', 'none', 'nonmutating', 'optional', 'override', 'postfix', 'precedence', 'prefix', 'Protocol', 'required', 'right', 'set', 'Type', 'unowned', 'weak', 'willSet'] type_dict = {'BOOL': 'Bool', 'double': 'Double', 'long': 'Int64', 'int': 'Int', 'NSInteger': 'Int', 'NSString': 'String',
import sys, os import ctypes import clang.cindex from clang.cindex import Index, Config, CursorKind, TypeKind, TranslationUnit Config.set_library_path('/usr/lib/llvm-6.0/lib/') Config.set_library_file('/usr/lib/llvm-6.0/lib/libclang.so.1') REAL_NUMBER_TYPES = set({TypeKind.DOUBLE, TypeKind.FLOAT, TypeKind.FLOAT128}) INTEGER_TYPES = set({ TypeKind.INT, TypeKind.INT128, TypeKind.LONG, TypeKind.LONGDOUBLE, TypeKind.LONGLONG, TypeKind.UCHAR, TypeKind.CHAR32 }) NUMERIC_TYPES = REAL_NUMBER_TYPES | INTEGER_TYPES BUILTIN_C_TYPES = { TypeKind.BOOL: ctypes.c_bool, TypeKind.CHAR_S: ctypes.c_char, TypeKind.CHAR_U: ctypes.c_char, TypeKind.DOUBLE: ctypes.c_double, TypeKind.FLOAT: ctypes.c_float, TypeKind.INT: ctypes.c_int, TypeKind.LONG: ctypes.c_long, TypeKind.LONGDOUBLE: ctypes.c_longdouble, TypeKind.LONGLONG: ctypes.c_longlong, TypeKind.SCHAR: ctypes.c_char, TypeKind.SHORT: ctypes.c_short, TypeKind.UCHAR: ctypes.c_char, TypeKind.UINT: ctypes.c_uint, TypeKind.ULONG: ctypes.c_ulong, TypeKind.ULONGLONG: ctypes.c_ulonglong, TypeKind.USHORT: ctypes.c_ushort, TypeKind.WCHAR: ctypes.c_wchar
#!/usr/bin/env python3 import re import sys from os import makedirs from os.path import join, basename, splitext from importlib import import_module from collections import OrderedDict from clang.cindex import Config, TranslationUnit, CursorKind from common import * from builders import BuildersWriter from module import ModuleHeaderWriter, ModuleWriter from datatype import DataTypeDeclWriter, DataTypeWriter from ozfunc import OzFunction Config.set_compatibility_check(False) #def is_blacklisted(name): # return any(regex.match(name) for regex in BLACKLISTED) #------------------------------------------------------------------------------- def collect_nodes(basename, constants): functions = {} types = OrderedDict() # order is important, otherwise the builders will refer to non-existing types. clang_args = [arg.encode('utf-8') for arg in constants.PKG_CONFIG_RES] include_paths = [arg[2:] for arg in constants.PKG_CONFIG_RES if arg[:2] == '-I'] tu_name = join(C_FILES, basename + C_EXT)