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 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 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 __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, 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 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 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
#!/usr/bin/env python # -*- coding: utf-8 -*- # usage: $ python sample.py sample.cpp import clang.cindex from clang.cindex import Index from clang.cindex import Config Config.set_compatibility_check(False) def print_method_area(node): if (node.kind.name == 'FUNCTION_DECL'): print("file : %s" % node.extent.start.file) print("function : %s" % node.displayname) print(" from line:%s column:%s" % (node.extent.start.line, node.extent.start.column)) print(" to line:%s column:%s" % (node.extent.end.line, node.extent.end.column)) for child in node.get_children(): print_method_area(child) index = Index.create() tu = index.parse("sample.cpp") print_method_area(tu.cursor)
#!/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)