Skip to main content

RG3 is a C/C++ analyzer framework

Project description

RG3

RG3 - is a backend & frontend for processing & analyzing C++ code. It provides information about types into Python frontend to future codegen stage. We've using LLVM as our backend to prepare code.

See our GitHub for more details.

Install

Make sure that your system has clang (any version):

  • macOS: you need to install XCode (tested on 15.x but should work everywhere)
  • Window: you need to install clang 17.x or later and add it into PATH
  • Linux: gcc & g++ at least 13 version (temporary limitation, planned to fix at 0.0.4)
  • Other platforms & archs: Contact us in our GitHub.

It's a better way to use RG3 inside virtualenv:

python3 -m venv venv
source ./venv/bin/activate
pip install rg3py

Usage:

Sample code analyze code from buffer:

from rg3py import CodeAnalyzer, CppStandard, CppCompilerIssueKind, CppTypeKind, CppClass, FunctionArgument
from typing import List


def prepare_args(args: List[FunctionArgument]) -> str:
    return ','.join(f'{arg.type_info.get_name()} {arg.name}' for arg in args)


analyzer: CodeAnalyzer = CodeAnalyzer.make()
analyzer.set_code("""
    namespace my::cool::name_space {
        /**
        * @runtime
        **/
        enum class ECoolEnum : int {
            CE_FIRST_ENTRY = 0,
            CE_ANOTHER_ENTRY = 0xFFEE,
            CE_DUMMY = 256
        };

        /// @runtime
        struct MyGeniusStruct
        {};

        struct SomeThirdPartyStruct
        {
            float fProperty = 42.f;
            MyGeniusStruct sGenius {};

            static bool IsGeniusDesc(bool bCanReplace) const;
        };

        template <typename T> struct ThirdPartyRegistrator {};
        template <> struct 
            __attribute__((annotate("RG3_RegisterRuntime")))
            __attribute__((annotate("RG3_RegisterField[fProperty:Property]")))
            __attribute__((annotate("RG3_RegisterField[sGenius:GeniusData]")))
            __attribute__((annotate("RG3_RegisterFunction[IsGeniusDesc]")))
        ThirdPartyRegistrator<SomeThirdPartyStruct>
        {
            using Type = SomeThirdPartyStruct;
        };
    }
    """)

analyzer.set_cpp_standard(CppStandard.CXX_17)
analyzer.analyze()

for t in analyzer.types:
    print(f"We have a type {t.pretty_name} ({t.kind})")

    if t.kind == CppTypeKind.TK_STRUCT_OR_CLASS:
        as_class: CppClass = t
        for prop in as_class.properties:
            print(f"\tProperty {prop.name} (aka {prop.alias}) of type {prop.type_info.get_name()}")

        for func in as_class.functions:
            args_as_str: str = prepare_args(func.arguments)
            print(f"\tFunction {'static' if func.is_static else ''} {func.return_type.get_name()} {func.name}({args_as_str}){' const' if func.is_const else ''}")

expected output is

We have a type my::cool::name_space::ECoolEnum (TK_ENUM)
We have a type my::cool::name_space::MyGeniusStruct (TK_STRUCT_OR_CLASS)
We have a type my::cool::name_space::SomeThirdPartyStruct (TK_STRUCT_OR_CLASS)
	Property fProperty (aka Property) of type float
	Property sGenius (aka GeniusData) of type MyGeniusStruct
	Function static bool IsGeniusDesc(bool bCanReplace)

Features

  • Supported Windows (x86_64), Linux (x86_64) and macOS (x86_64 and ARM64)
  • Supported C++03, 11, 14, 17, 20, 23 (26 in theory, need to migrate to next LLVM)
  • Supported threads in analysis on native side (see Tests/PyIntegration/test.py test: test_analyzer_context_sample for example)
  • Statically linked, no external dependencies (except Clang instance on machine)
  • Special macro definitions to hide unnecessary code
  • Template specializations reporting
  • Anonymous registration without changes in third party code

Current limitations

Project focused on work around C/C++ headers (C++ especially). Feel free to fork project & add support of anything what you want :)

Third Party libraries

  • LLVM 16.0.4 - our main backend of C++ analysis
  • Boost 1.81.0 - python support & process launcher
  • FMT - string formatter
  • googletest - for internal unit testing
  • pytest - for python side unit testing

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

rg3py-0.0.17-py3-none-win_amd64.whl (8.0 MB view details)

Uploaded Python 3 Windows x86-64

rg3py-0.0.17-py3-none-manylinux_2_28_x86_64.whl (15.2 MB view details)

Uploaded Python 3 manylinux: glibc 2.28+ x86-64

rg3py-0.0.17-py3-none-macosx_14_0_universal2.whl (23.5 MB view details)

Uploaded Python 3 macOS 14.0+ universal2 (ARM64, x86-64)

File details

Details for the file rg3py-0.0.17-py3-none-win_amd64.whl.

File metadata

  • Download URL: rg3py-0.0.17-py3-none-win_amd64.whl
  • Upload date:
  • Size: 8.0 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.3

File hashes

Hashes for rg3py-0.0.17-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 feea537c9083a06a4457d7c3d086f829fe511dc577a585c7c4d64c28b3dfb789
MD5 7bb7da88ccf48366ca637a44812949f4
BLAKE2b-256 1e115841aeb6ee1ce870cac17ceab4e59a4b1030d3bb741d7b6cc49644d7848e

See more details on using hashes here.

File details

Details for the file rg3py-0.0.17-py3-none-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for rg3py-0.0.17-py3-none-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1548b83b9b3b1677dd22921e125f8e29406478ec83c4932b33432f35bc9b64d1
MD5 77b9a01a05b901a2261662c7fa7ab718
BLAKE2b-256 31f2970ba2bcae595a2928147201eb3e962a5bba8aee29df5ea31c1d0ab0c73a

See more details on using hashes here.

File details

Details for the file rg3py-0.0.17-py3-none-macosx_14_0_universal2.whl.

File metadata

File hashes

Hashes for rg3py-0.0.17-py3-none-macosx_14_0_universal2.whl
Algorithm Hash digest
SHA256 5769cf0642928df3afd93fb7fc8a56e8c2b88942f5fe695699c6c712462adb9d
MD5 14936dd71e6546dcb5444707cec00f53
BLAKE2b-256 bad04a81eb8085c7f49e860187f8b65556d3a667fbd9738b094ead2856c7353e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page