def get_clang_TranslationUnit(path="t.cpp", in_args=[], in_str="", options=0): """ Get the TranslationalUnit for the input fule listed: Parameters :: - path: The path of the file to parse (not read if in_str is set) - in_args: additional arguments to pass to clang - in_str: input string to parse instead of the file path. - options: clang.cindex.Options Returns :: - A TranslationalUnits """ # Make sure we are parsing as std c++11 args = '-x c++ --std=c++11'.split() # Add the include files for the standard library. syspath = ccsyspath.system_include_paths('clang++') incargs = [b'-I' + inc for inc in syspath] # turn args into a list of args (in_args may contain more includes) args = args + incargs + in_args # Create a clang index to parse into index = cl.Index.create() unsaved_files = None # If we are parsing from a string instead if in_str: unsaved_files = [(path, in_str)] return index.parse(path, args=args, options=options, unsaved_files=unsaved_files)
def get_args(self): import ccsyspath syspath = ccsyspath.system_include_paths('clang++') incargs = [b'-I' + inc for inc in syspath] args = '-x c++'.split() + incargs logging.debug( '[INFO]: Count of dependency args by syspath: {0}.'.format( len(args))) return args
def parse_ast(self, program: str, imports: list = None, thread_nr: int = 0): os.makedirs('temp', exist_ok=True) # Create temp file path for each trhead for clang to save in memory contents temp_file_path = os.path.join('temp', f'tmp{thread_nr}.cpp') # Set arguments and add compiler system include paths (with ccsyspath) args = '-x c++ --std=c++20'.split() syspath = ccsyspath.system_include_paths('clang') incargs = [b'-I' + inc for inc in syspath] args = args + incargs if imports is None: program, imports = extract_imports(program) else: imports = [ ele for ele in imports[1:-1].split("'") if ele != '' and ele != ', ' ] # Preprocess the program, expand the macros preprocessed_program = self.preprocess_program(program, temp_file_path, imports) # print(preprocessed_program) # Parse the program to a clang AST tu = self.index.parse(temp_file_path, unsaved_files=[(temp_file_path, preprocessed_program)], args=args, options=0) # Retrieve only the cursor items (children) that contain the program code (no import code) cursor_items = self.get_cursor_items(tu.cursor, temp_file_path) # Create a root node root_node = Node(self.tokenizers['RES'].get_token('root'), is_reserved=True) # for cursor_item in cursor_items: # for c in cursor_item.walk_preorder(): # print(f'spelling: {c.spelling}, kind: {c.kind.name}, type spelling: {c.type.spelling}, return type: {c.type.get_result().spelling}, type kind: {c.type.kind}') # Parse each cursor item for cursor_item in cursor_items: self.parse_item(cursor_item, root_node, program) shutil.rmtree('temp') # Return the root node filled with children to form the AST return root_node
def main(): syspath = ccsyspath.system_include_paths('clang++') incargs = [b'-I' + inc for inc in syspath] args = '-x c++'.split() + incargs source_code = [r'placeholder'] for path_item in source_code: for file_item in filter_file_by_suffixation(path_item, '.c', '.cxx', '.cc', '.c++', '.cpp'): index = clang.cindex.Index.create() tu = index.parse(file_item, args=args) root = tu.cursor _find_bare_threads(file_item, root, 0)
def parse(path): index = clang.cindex.Index.create() # Clang can't parse files with missing definitions, add static library definition or not? args = ['-x', 'c++', '-std=c++11', '-fparse-all-comments', '-DIGL_STATIC_LIBRARY'] args.append('-I/usr/include/eigen3/') # TODO Properly add all needed includes syspath = ccsyspath.system_include_paths('clang++') # Add the system libraries incargs = [(b'-I' + inc).decode("utf-8") for inc in syspath] args.extend(incargs) tu = index.parse(path, args) objects = {"functions": [], "enums": [], "namespaces": [], "classes": [], "structs": []} traverse(tu.cursor, path, objects) return objects
def parse(path): index = clang.cindex.Index.create() # Clang can't parse files with missing definitions, add static library definition or not? args = ['-x', 'c++', '-std=c++11', '-fparse-all-comments', '-DIGL_STATIC_LIBRARY'] args.append('-I/usr/include/eigen3/') # TODO Properly add all needed includes syspath = ccsyspath.system_include_paths('clang++') # Add the system libraries incargs = [(b'-I' + inc).decode("utf-8") for inc in syspath] args.extend(incargs) tu = index.parse(path, args) objects = {"functions": [], "enums": [], "namespaces": [], "classes": [], "structs": []} traverse(tu.cursor, path, objects) return objects
def parse(src): """Parse a string of source code and return the translation unit """ # Find the clang compiler clang = os.path.join(os.environ['LLVM_HOME'], 'bin', 'clang++') # Find the system paths for the compiler syspath = ccsyspath.system_include_paths(clang) syspath = [ p.decode('utf-8') for p in syspath ] # Build up an appropriate set of compiler flags args = '-x c++ --std=c++11'.split() args += llvm_config('--cppflags') args += [ '-I' + inc for inc in syspath ] # Parse the source passed in return glud.parse_string(src, name='input.cpp', args=args)
def parse(src): """Parse a string of source code and return the translation unit """ # Find the clang compiler clang = os.path.join(os.environ['LLVM_HOME'], 'bin', 'clang++') # Find the system paths for the compiler syspath = ccsyspath.system_include_paths(clang) syspath = [p.decode('utf-8') for p in syspath] # Build up an appropriate set of compiler flags args = '-x c++ --std=c++11'.split() args += llvm_config('--cppflags') args += ['-I' + inc for inc in syspath] # Parse the source passed in return glud.parse_string(src, name='input.cpp', args=args)
libclang_path = '/Library/Developer/CommandLineTools/usr/lib/libclang.dylib' pipe = os.popen('mdfind -name "libclang.dylib"').readlines() if not pipe: raise Exception("Please install clang with libclang.dylib") elif platform.system() == "Linux": # IS_LINUX slash = '/' libclang_path = '/usr/lib/llvm-7.0/lib/libclang-7.0.so.1' if not os.path.exists(libclang_path): raise Exception("Please install clang with libclang.so") if Config.loaded == True: pass else: Config.set_library_file(libclang_path) syspath = ccsyspath.system_include_paths('clang++') incargs = [b'-I' + inc for inc in syspath] args = '-x c++'.split() + incargs parser = OptionParser() parser.add_option("-s", "--source_dir", action="store", dest="source_dir", help="read input data from source directory") parser.add_option("-t", "--target_file", action="store", dest="target_file", help="parse data to target file") (options, _) = parser.parse_args() INPUT_SOURCE_ORIGIN = [] INPUT_SOURCE_ORIGIN.append(options.source_dir) _VAR_DECL = [] # record variable offsets _DECL_REF_EXPR = [] # record variable reference offsets _NODE_DICT = {} # record node: domain _GLOBAL_VAR_COUNT = 0 # records the number of global variables _BARE_THREAD_COUNT = 0 # records the number of bare thread
def translation_units(compile_commands: cindex.CompilationDatabase, cache_path: Optional[str]) -> Iterator[typing.Union[cindex.TranslationUnit, SerializedTU]]: '''Returns an iterator over a translation unit for each file in the compilation database.''' compile_command: cindex.CompileCommand for compile_command in compile_commands.getAllCompileCommands(): if cache_path: full_path = os.path.join( compile_command.directory, compile_command.filename, ) serialized_tu = read_tu( cache_path, full_path, ) modified_time = os.path.getmtime(full_path) if serialized_tu.serialization_time >= modified_time: log(LogLevel.INFO, f'Using cached analysis for {full_path}') yield serialized_tu continue log( LogLevel.INFO, f'parsing {compile_command.filename}' ) try: if 'lua' in compile_command.filename: continue os.chdir(compile_command.directory) translation_unit = cindex.TranslationUnit.from_source( os.path.join(compile_command.directory, compile_command.filename), args=[arg for arg in compile_command.arguments if arg != compile_command.filename] + ['-I' + inc.decode() for inc in ccsyspath.system_include_paths('clang')], ) for diag in translation_unit.diagnostics: log( LogLevel.WARNING, f'Parsing: {compile_command.filename}: {diag}' ) yield translation_unit except cindex.TranslationUnitLoadError: log( LogLevel.WARNING, f'could not parse {os.path.join(compile_command.directory, compile_command.filename)}', )
def add_required_include_paths(extra_include_paths=[], compiler_path='clang'): include_paths = ccsyspath.system_include_paths( 'clang') + extra_include_paths for p in include_paths: compiler_args.extend(['-I', p])
# # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import os import sys import argparse import logging from collections import defaultdict tob_path = os.environ['TRAILOFBITS_LIBRARIES'] cc_path = tob_path + "/llvm/bin/clang" try: import ccsyspath syspath = ccsyspath.system_include_paths(cc_path) print(syspath) except ImportError: syspath = list() SUPPORTED_ARCH = ["x86", "amd64"] SUPPORTED_LIBRARY_TYPE = ["c", "cpp"] ARCH_NAME = "" ABI_LIBRARY_TYPE = "c" logging.basicConfig(filename="debug.log", level=logging.DEBUG) cc_pragma = """
def test_c_compiler(self): lines = ccsyspath.system_include_paths('clang', cpp=False) self.assertEqual(list, type(lines)) self.assertTrue(len(lines) > 0)