diff --git a/CMakeLists.txt b/CMakeLists.txt index 40eff0e..b6d37b8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ option(CPPAST_ENABLE_ASSERTIONS "whether or not to enable internal assertions fo option(CPPAST_ENABLE_PRECONDITION_CHECKS "whether or not to enable precondition checks" ON) option(CPPAST_BUILD_TEST "whether or not to build the tests" ON) +option(CPPAST_BUILD_EXAMPLE "whether or not to build the examples" ON) option(CPPAST_BUILD_TOOL "whether or not to build the tool" ON) option(BUILD_TESTING "build test" OFF) # The ctest variable for building tests @@ -24,6 +25,12 @@ else() set(build_test OFF) endif() +if(${CPPAST_BUILD_EXAMPLE} OR (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)) + set(build_example ON) +else() + set(build_example OFF) +endif() + if(${CPPAST_BUILD_TOOL} OR (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)) set(build_tool ON) else() @@ -43,6 +50,9 @@ add_subdirectory(src) if(${build_test}) add_subdirectory(test) endif() +if(${build_example}) + add_subdirectory(example) +endif() if(${build_tool}) add_subdirectory(tool) endif() diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt new file mode 100644 index 0000000..4b5f547 --- /dev/null +++ b/example/CMakeLists.txt @@ -0,0 +1,10 @@ +# Copyright (C) 2017 Jonathan Müller +# This file is subject to the license terms in the LICENSE file +# found in the top-level directory of this distribution. + +function(_cppast_example name) + add_executable(cppast_example_${name} ${name}.cpp) + target_link_libraries(cppast_example_${name} PUBLIC cppast) +endfunction() + +_cppast_example(ast_printer) diff --git a/example/ast_printer.cpp b/example/ast_printer.cpp new file mode 100644 index 0000000..5346563 --- /dev/null +++ b/example/ast_printer.cpp @@ -0,0 +1,65 @@ +// Copyright (C) 2017 Jonathan Müller +// 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. + +#include + +#include // libclang_compilation_database, libclang_parser, etc. +#include // visit() + +int main(int argc, char* argv[]) +{ + if (argc != 2) + { + std::cerr << "usage: " << argv[0] << " \n"; + return 1; + } + else + { + cppast::libclang_compilation_database database(argv[1]); // the compilation database + cppast::cpp_entity_index index; // only needed for cross referencing in the AST + + // simple_file_parser allows parsing multiple files and stores the results for us + cppast::simple_file_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()) // for each file in the project + { + 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'; + + return true; // continue with visit + }); + } + } +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c45d7bb..26cded2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -103,6 +103,7 @@ set(libclang_source libclang/variable_parser.cpp) add_library(cppast ${detail_header} ${header} ${source} ${libclang_source}) +set_target_properties(cppast PROPERTIES CXX_STANDARD 11) target_include_directories(cppast PUBLIC ../include) target_link_libraries(cppast PUBLIC type_safe _cppast_tiny_process _cppast_libclang) target_compile_definitions(cppast PUBLIC diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 281a4b1..6c75862 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -36,7 +36,6 @@ set(tests add_executable(cppast_test test.cpp test_parser.hpp ${tests}) target_include_directories(cppast_test PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) target_link_libraries(cppast_test PUBLIC cppast) -set_target_properties(cppast_test PROPERTIES CXX_STANDARD 11) enable_testing() add_test(NAME test COMMAND cppast_test) diff --git a/tool/CMakeLists.txt b/tool/CMakeLists.txt index 8f3aaf0..75661da 100644 --- a/tool/CMakeLists.txt +++ b/tool/CMakeLists.txt @@ -4,5 +4,4 @@ add_executable(cppast_tool main.cpp) target_link_libraries(cppast_tool PUBLIC cppast cxxopts) -set_target_properties(cppast_tool PROPERTIES CXX_STANDARD 11 - OUTPUT_NAME cppast) +set_target_properties(cppast_tool PROPERTIES OUTPUT_NAME cppast)