Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rm main from student file #1

Open
Jelleas opened this issue Jan 31, 2024 · 2 comments
Open

rm main from student file #1

Jelleas opened this issue Jan 31, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@Jelleas
Copy link
Contributor

Jelleas commented Jan 31, 2024

pycparser kan met een beetje "masseren" de C opdrachten parsen. Onderstaande vind de main functie in een C bestand.

import check50.internal
from pycparser import c_ast, parse_file, c_generator

def get_main(filename: str) -> c_ast.FuncDef | None:
    main = None
    class FuncDefVisitor(c_ast.NodeVisitor):
        def visit_FuncDef(self, node):
            if node.decl.name == "main":
                nonlocal main
                main = node

    fake_libc_include = check50.internal.check_dir.parent / "fake_libc_include"

    ast = parse_file(
        filename, 
        use_cpp=True,
        cpp_path='clang',
        cpp_args=[
            '-E', # preprocess using clang
            '-nostdinc', # strictly use fake libc
            '-D__attribute__(x)=', # pycparser does not support __attribute__, so delete
            f'-I{fake_libc_include}' # include fake libc (just typedefs and headers)
        ]
    )

    visitor = FuncDefVisitor()
    visitor.visit(ast)

    return main

Bijvoorbeeld

>>> print(get_main("functions.c").coord)
functions.c:8:5

fake_libc_include is nodig om libc te spoofen. In bovenstaande voorbeeld staat deze map direct naast de checks, vandaar check50.internal.check_dir.parent / "fake_libc_include"

@Jelleas Jelleas added the enhancement New feature or request label Jan 31, 2024
@Jelleas
Copy link
Contributor Author

Jelleas commented Jan 31, 2024

Coord van laatste karakter is niet beschikbaar, alleen het begin van iedere ast-node. Dat maakt einde van de main functie lastig.

@Jelleas
Copy link
Contributor Author

Jelleas commented Jan 31, 2024

Wegschrijven via c_generator.CGenerator bijvoorbeeld met dit voorbeeld is niet zo handig. Dat schrijft ook gespoofde/gefakete includes mee.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant