Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/iohannrabeson/cpp2json

Export meta-informations about your C++ using Clang
https://github.com/iohannrabeson/cpp2json

c-plus-plus clang code-generation cpp14 json llvm reflection

Last synced: 2 months ago
JSON representation

Export meta-informations about your C++ using Clang

Awesome Lists containing this project

README

        

# Cpp2Json [![Build Status](https://travis-ci.com/IohannRabeson/cpp2json.svg?token=oSgYDG8ZHmxB1gxGNZxP&branch=master)](https://travis-ci.com/IohannRabeson/cpp2json) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/456265ad762c40d5aca25cb649589c69)](https://www.codacy.com/app/IohannRabeson/cpp2json?utm_source=github.com&utm_medium=referral&utm_content=IohannRabeson/cpp2json&utm_campaign=Badge_Grade)

![general_usage](https://docs.google.com/drawings/d/e/2PACX-1vSvz2O3s9Oica9CyYAUq42n7U62ygswZS-CPsdTIPUDFLky-4Ff0zx-U-kBqJSubkmiBsJ9GsByTDsl/pub?w=1149&h=542)

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:
```bash
mkdir builds && cd builds
cmake .. -G Ninja -DCMAKE_PREFIX_PATH=
ninja
```
With brew you can print the path using:
```bash
brew --prefix llvm
```
```bash
cmake .. -G Ninja -DCMAKE_PREFIX_PATH="$(brew --prefix llvm)"
```

## Example
File test_example.hpp:
```c++
#ifndef CPP2JSON_TEST_EXAMPLE_HPP
#define CPP2JSON_TEST_EXAMPLE_HPP
#include

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

#endif //CPP2JSON_TEST_EXAMPLE_HPP
```

Run cpp2json on it:
```bash
$> cpp2json test_example.hpp --pretty --
```

Will give you the following JSON output:
```json
{
"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
```bash
USAGE: cpp2json [options] [... ]

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= - Specify the content for exclude annotation
-extra-arg= - Additional argument to append to the compiler command line
-extra-arg-before= - Additional argument to prepend to the compiler command line
-output= - Specify output JSON filename.
You can also send the output to the stdout using - instead of a filename
-p= - 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] [... ] -- -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.