IohannRabeson / cpp2json

Export meta-informations about your C++ using Clang

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cpp2Json Build Status Codacy Badge

general_usage

A tool to extract C++ information about enumerations, classes, methods and attributes.
All theses data are stored as JSON.

Initial idea picked up from this conference by Sergiy Migdalskiy: https://archive.org/details/GDC2014Migdalskiy

Requirements

  • Llvm 11
  • RapidJson 1.x

You might need to give the path to the llvm folder to CMake:

mkdir builds && cd builds
cmake .. -G Ninja -DCMAKE_PREFIX_PATH=<your_path_to_llvm_install>
ninja

With brew you can print the path using:

brew --prefix llvm
cmake .. -G Ninja -DCMAKE_PREFIX_PATH="$(brew --prefix llvm)"

Example

File test_example.hpp:

#ifndef CPP2JSON_TEST_EXAMPLE_HPP
#define CPP2JSON_TEST_EXAMPLE_HPP
#include <string>

struct Example01
{
    float m_float;
    std::string m_string;
    int m_int;
};

#endif //CPP2JSON_TEST_EXAMPLE_HPP

Run cpp2json on it:

$> cpp2json test_example.hpp --pretty --

Will give you the following JSON output:

{
    "classes": {
        "Example01": {
            "fields": [
                {
                    "name": "m_float",
                    "type": {
                        "key": "float",
                        "expression": "float",
                        "pointer": false,
                        "array": false,
                        "reference": false,
                        "const": false,
                        "volatile": false,
                        "literal": true,
                        "enum": false
                    }
                },
                {
                    "name": "m_string",
                    "type": {
                        "key": "std::string",
                        "expression": "std::string",
                        "pointer": false,
                        "array": false,
                        "reference": false,
                        "const": false,
                        "volatile": false,
                        "literal": false,
                        "enum": false
                    }
                },
                {
                    "name": "m_int",
                    "type": {
                        "key": "int",
                        "expression": "int",
                        "pointer": false,
                        "array": false,
                        "reference": false,
                        "const": false,
                        "volatile": false,
                        "literal": true,
                        "enum": false
                    }
                }
            ],
            "methods": [],
            "annotations": [],
            "bases": [],
            "file": "/Users/iota/projects/tools/cpp2json/sources/tests/resources/test_example.hpp"
        }
    },
    "enums": {}
}

Usage

USAGE: cpp2json [options] <source0> [... <sourceN>]

OPTIONS:

Generic Options:

  -help                                    - Display available options (-help-hidden for more)
  -help-list                               - Display list of available options (-help-list-hidden for more)
  -version                                 - Display the version of this program

Main options:

  -append                                  - Append output
  -exclude_annotation=<annotation content> - Specify the content for exclude annotation
  -extra-arg=<string>                      - Additional argument to append to the compiler command line
  -extra-arg-before=<string>               - Additional argument to prepend to the compiler command line
  -output=<filename>                       - Specify output JSON filename.
                                             You can also send the output to the stdout using - instead of a filename
  -p=<string>                              - Build path
  -pretty                                  - Output pretty JSON

Note the -- is really needed because to define where the options passed to the clang driver begins.
If cpp2json can't find standard includes (it can happen if you have moved cpp2json in another location), you should specify yourself using the correct llvm include paths using -I (after the --).

Don't forget to specify the c++ version used as clang option (e.g: -std=c++14). Example:

  cpp2json [options] <source0> [... <sourceN>] -- -std=c++14

This is required because clang assume the executable is located in a standard directory (in llvm/bin) and use that to deduce the includes paths.

About

Export meta-informations about your C++ using Clang


Languages

Language:C++ 76.3%Language:CMake 22.2%Language:Shell 1.4%