Skip to content

Commit

Permalink
Add 'cppast' package (#110)
Browse files Browse the repository at this point in the history
* Initial commit

* First try to compile

* use hunterized package

* Working package version

* Cleanup hunter cmake and add pr in doc

* Fix cppast version

* Change repo url

* Fix version number
  • Loading branch information
Bjoe authored and rbsheth committed Jan 7, 2020
1 parent 352c7c8 commit 2e17808
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 58 deletions.
1 change: 1 addition & 0 deletions cmake/configs/default.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ hunter_default_version(cmcstl2 VERSION 0.0.0-bee0705e99)
hunter_default_version(convertutf VERSION 1.0.1)
hunter_default_version(corrade VERSION 2019.01)
hunter_default_version(cpp_redis VERSION 3.5.0-h1)
hunter_default_version(cppast VERSION 0.0.0-b155d6a-p0)
hunter_default_version(cppcodec VERSION 0.2-p0)
hunter_default_version(cppfs VERSION 1.3.0)
hunter_default_version(cpr VERSION 1.3.0)
Expand Down
58 changes: 0 additions & 58 deletions cmake/find/Findtiny-process-library.cmake

This file was deleted.

24 changes: 24 additions & 0 deletions cmake/projects/cppast/hunter.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# !!! DO NOT PLACE HEADER GUARDS HERE !!!

include(hunter_add_version)
include(hunter_cacheable)
include(hunter_download)
include(hunter_pick_scheme)

hunter_add_version(
PACKAGE_NAME
cppast
VERSION
0.0.0-b155d6a-p0
URL
"https://github.com/cpp-pm/cppast/archive/v0.0.0-b155d6a-p0.tar.gz"
SHA1
aaa370f185fb5d14398622863a35b5ebe96ab4a2
)

hunter_pick_scheme(DEFAULT url_sha1_cmake)
hunter_cacheable(cppast)
hunter_download(
PACKAGE_NAME cppast
PACKAGE_INTERNAL_DEPS_ID "1"
)
21 changes: 21 additions & 0 deletions docs/packages/pkg/cppast.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.. spelling::

cppast

.. index::
single: unsorted ; cppast

.. _pkg.cppast:

cppast
======

- `Official <https://github.com/foonathan/cppast>`__
- `Hunterized <https://github.com/cpp-pm/cppast>`
- `Example <https://github.com/cpp-pm/hunter/blob/master/examples/cppast/CMakeLists.txt>`__
- Added by `Joerg-Christian Boehme <https://github.com/Bjoe>`__ (`pr-110 <https://github.com/cpp-pm/hunter/pull/110>`__)

.. literalinclude:: /../examples/cppast/CMakeLists.txt
:language: cmake
:start-after: # DOCUMENTATION_START {
:end-before: # DOCUMENTATION_END }
18 changes: 18 additions & 0 deletions examples/cppast/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (c) 2016-2018, Ruslan Baratov
# All rights reserved.

cmake_minimum_required(VERSION 3.2)

# Emulate HunterGate:
# * https://github.com/hunter-packages/gate
include("../common.cmake")

project(cppast)

# DOCUMENTATION_START {
hunter_add_package(cppast)
find_package(cppast CONFIG REQUIRED)

add_executable(boo boo.cpp)
target_link_libraries(boo PUBLIC cppast::cppast)
# DOCUMENTATION_END }
81 changes: 81 additions & 0 deletions examples/cppast/boo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (C) 2017-2019 Jonathan Müller <jonathanmueller.dev@gmail.com>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.

/// \file
/// This is a very primitive version of the cppast tool.
///
/// Given an input file it will print the AST.

#include <cppast/visitor.hpp> // visit()

#include <iostream>

#include <cppast/libclang_parser.hpp>

// reads the database directory from the command line argument
// parses all files in that directory
// and invokes the callback for each of them
template <typename Callback>
int example_main(int argc, char* argv[], const cppast::cpp_entity_index& index, Callback cb) try
{
if (argc != 2)
{
std::cerr << "usage: " << argv[0] << " <compile-commands-json-dir>\n";
return 1;
}
else
{
cppast::libclang_compilation_database database(argv[1]); // the compilation database

// simple_file_parser allows parsing multiple files and stores the results for us
cppast::simple_file_parser<cppast::libclang_parser> parser(type_safe::ref(index));
try
{
cppast::parse_database(parser, database); // parse all files in the database
}
catch (cppast::libclang_error& ex)
{
std::cerr << "fatal libclang error: " << ex.what() << '\n';
return 1;
}

if (parser.error())
// a non-fatal parse error
// error has been logged to stderr
return 1;

for (auto& file : parser.files())
cb(file);
}

return 0;
}
catch (std::exception& ex)
{
std::cerr << ex.what() << '\n';
return 1;
}

void print_ast(const cppast::cpp_file& file)
{
std::string prefix;
// visit each entity in the file
cppast::visit(file, [&](const cppast::cpp_entity& e, cppast::visitor_info info) {
if (info.event == cppast::visitor_info::container_entity_exit) // exiting an old container
prefix.pop_back();
else if (info.event == cppast::visitor_info::container_entity_enter)
// entering a new container
{
std::cout << prefix << "'" << e.name() << "' - " << cppast::to_string(e.kind()) << '\n';
prefix += "\t";
}
else // if (info.event == cppast::visitor_info::leaf_entity) // a non-container entity
std::cout << prefix << "'" << e.name() << "' - " << cppast::to_string(e.kind()) << '\n';
});
}

int main(int argc, char* argv[])
{
return example_main(argc, argv, {}, &print_ast);
}

0 comments on commit 2e17808

Please sign in to comment.