GithubHelp home page GithubHelp logo

Comments (2)

kelteseth avatar kelteseth commented on August 20, 2024

Something similar to this script, that I used for ScreenPlay:

import clang.cindex
from clang.cindex import CompilationDatabase
from pathlib import Path

def find_methods_with_locations(node, methods, source_file):
    """
    Recursively find all the method declarations in the node, capturing their names and start locations,
    but only if they are declared in the specified source file.
    """
    # Check if the node location is in the main file
    if node.location.file and Path(node.location.file.name) == source_file:
        if node.kind == clang.cindex.CursorKind.CXX_METHOD:
            # Access the extent directly, not via get_extent()
            start_location = node.extent.start
            methods.append((node.spelling, start_location.line))
    for child_node in node.get_children():
        find_methods_with_locations(child_node, methods, source_file)

def parse_cpp_methods(file_path: Path, compilation_database_path: str, source_file: Path):
    """
    Parse the C++ file using compilation commands from the specified compilation database and return a list of all method names along with their start locations.
    """
    # Load the compilation database
    compdb = CompilationDatabase.fromDirectory(compilation_database_path)
    
    # Retrieve the compilation commands for the file
    compile_commands = compdb.getCompileCommands(str(file_path))

    file_args = []
    for command in compile_commands:
        for argument in command.arguments:
            file_args.append(argument)
    file_args = file_args[3:-3] # Skip the compiler command itself

    # Initialize Clang index and parse the file
    index = clang.cindex.Index.create()
    translation_unit = index.parse(str(file_path), args=file_args)

    methods_with_locations = []
    find_methods_with_locations(translation_unit.cursor, methods_with_locations, source_file)
    return methods_with_locations


def main():
    clang.cindex.Config.set_library_file('C:/Program Files/LLVM/bin/libclang.dll')
    file_path = Path("C:/Code/Cpp/ScreenPlay/ScreenPlay/ScreenPlay/src/create.cpp")
    # Path to compile_commands.json
    compilation_database_path = "C:/Code/Cpp/ScreenPlay/build_ScreenPlay_Clang_Debug"
  
    print(f"Processing File: ", file_path.is_file(), file_path)
    methods_with_locations = parse_cpp_methods(file_path.absolute(), compilation_database_path, file_path)
    print("Found methods with locations:", methods_with_locations)


main()

from arewemodulesyet.

ChuanqiXu9 avatar ChuanqiXu9 commented on August 20, 2024

Now we have https://github.com/ChuanqiXu9/clang-modules-converter

from arewemodulesyet.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.