Update copyright notices

This commit is contained in:
Jonathan Müller 2022-02-07 20:43:22 +01:00
commit 62e571a4d6
133 changed files with 936 additions and 1103 deletions

View file

@ -1,5 +1,5 @@
# Copyright (C) 2017-2018 Jonathan Müller <jonathanmueller.dev@gmail.com>
# This file is subject to the license terms in the LICENSE file
# Copyright (C) 2017-2022 Jonathan Müller and cppast contributors
# SPDX-License-Identifier: MIT
# found in the top-level directory of this distribution.
function(_cppast_example name)

View file

@ -1,6 +1,5 @@
// 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.
// Copyright (C) 2017-2022 Jonathan Müller and cppast contributors
// SPDX-License-Identifier: MIT
/// \file
/// This is a very primitive version of the cppast tool.

View file

@ -1,6 +1,5 @@
// 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.
// Copyright (C) 2017-2022 Jonathan Müller and cppast contributors
// SPDX-License-Identifier: MIT
/// \file
/// Generate equality comparisons.
@ -80,48 +79,46 @@ void generate_op_non_equal(std::ostream& out, const cppast::cpp_class& c)
// generate comparison operators for all classes in the file
void generate_comparison(const cppast::cpp_file& file)
{
cppast::visit(file,
[](const cppast::cpp_entity& e) {
// only visit non-templated class definitions that have the attribute set
return (!cppast::is_templated(e)
&& e.kind() == cppast::cpp_entity_kind::class_t
&& cppast::is_definition(e)
&& cppast::has_attribute(e, "generate::comparison"))
// or all namespaces
|| e.kind() == cppast::cpp_entity_kind::namespace_t;
},
[](const cppast::cpp_entity& e, const cppast::visitor_info& info) {
if (e.kind() == cppast::cpp_entity_kind::class_t && !info.is_old_entity())
{
// it is a new class
auto& class_ = static_cast<const cppast::cpp_class&>(e);
auto& attribute
= cppast::has_attribute(e, "generate::comparison").value();
cppast::visit(
file,
[](const cppast::cpp_entity& e) {
// only visit non-templated class definitions that have the attribute set
return (!cppast::is_templated(e) && e.kind() == cppast::cpp_entity_kind::class_t
&& cppast::is_definition(e) && cppast::has_attribute(e, "generate::comparison"))
// or all namespaces
|| e.kind() == cppast::cpp_entity_kind::namespace_t;
},
[](const cppast::cpp_entity& e, const cppast::visitor_info& info) {
if (e.kind() == cppast::cpp_entity_kind::class_t && !info.is_old_entity())
{
// it is a new class
auto& class_ = static_cast<const cppast::cpp_class&>(e);
auto& attribute = cppast::has_attribute(e, "generate::comparison").value();
// generate requested operators
if (attribute.arguments())
{
if (has_token(attribute.arguments().value(), "=="))
generate_op_equal(std::cout, class_);
if (has_token(attribute.arguments().value(), "!="))
generate_op_non_equal(std::cout, class_);
}
else
{
generate_op_equal(std::cout, class_);
generate_op_non_equal(std::cout, class_);
}
}
else if (e.kind() == cppast::cpp_entity_kind::namespace_t)
{
if (info.event == cppast::visitor_info::container_entity_enter)
// open namespace
std::cout << "namespace " << e.name() << " {\n\n";
else // if (info.event == cppast::visitor_info::container_entity_exit)
// close namespace
std::cout << "}\n";
}
});
// generate requested operators
if (attribute.arguments())
{
if (has_token(attribute.arguments().value(), "=="))
generate_op_equal(std::cout, class_);
if (has_token(attribute.arguments().value(), "!="))
generate_op_non_equal(std::cout, class_);
}
else
{
generate_op_equal(std::cout, class_);
generate_op_non_equal(std::cout, class_);
}
}
else if (e.kind() == cppast::cpp_entity_kind::namespace_t)
{
if (info.event == cppast::visitor_info::container_entity_enter)
// open namespace
std::cout << "namespace " << e.name() << " {\n\n";
else // if (info.event == cppast::visitor_info::container_entity_exit)
// close namespace
std::cout << "}\n";
}
});
}
int main(int argc, char* argv[])

View file

@ -1,6 +1,5 @@
// 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.
// Copyright (C) 2017-2022 Jonathan Müller and cppast contributors
// SPDX-License-Identifier: MIT
/// \file
/// A primitive documentation generator.
@ -120,39 +119,39 @@ std::string generate_synopsis(const cppast::cpp_entity& e)
void generate_documentation(const cppast::cpp_file& file)
{
// visit each entity
cppast::visit(file,
[](const cppast::cpp_entity& e, cppast::cpp_access_specifier_kind access) {
if (is_excluded_documentation(e, access))
// exclude this and all children
return cppast::visit_filter::exclude_and_children;
else if (cppast::is_templated(e) || cppast::is_friended(e))
// continue on with children for a dummy entity
return cppast::visit_filter::exclude;
else
return cppast::visit_filter::include;
},
[](const cppast::cpp_entity& e, const cppast::visitor_info& info) {
if (info.is_old_entity())
// already done
return;
cppast::visit(
file,
[](const cppast::cpp_entity& e, cppast::cpp_access_specifier_kind access) {
if (is_excluded_documentation(e, access))
// exclude this and all children
return cppast::visit_filter::exclude_and_children;
else if (cppast::is_templated(e) || cppast::is_friended(e))
// continue on with children for a dummy entity
return cppast::visit_filter::exclude;
else
return cppast::visit_filter::include;
},
[](const cppast::cpp_entity& e, const cppast::visitor_info& info) {
if (info.is_old_entity())
// already done
return;
// print name
std::cout << "## " << cppast::to_string(e.kind()) << " '" << e.name()
<< "'\n";
std::cout << '\n';
// print name
std::cout << "## " << cppast::to_string(e.kind()) << " '" << e.name() << "'\n";
std::cout << '\n';
// print synopsis
std::cout << "```\n";
std::cout << generate_synopsis(e);
std::cout << "```\n\n";
// print synopsis
std::cout << "```\n";
std::cout << generate_synopsis(e);
std::cout << "```\n\n";
// print documentation comment
if (e.comment())
std::cout << e.comment().value() << '\n';
// print documentation comment
if (e.comment())
std::cout << e.comment().value() << '\n';
// print separator
std::cout << "\n---\n\n";
});
// print separator
std::cout << "\n---\n\n";
});
std::cout << "\n\n";
}

View file

@ -1,6 +1,5 @@
// 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.
// Copyright (C) 2017-2022 Jonathan Müller and cppast contributors
// SPDX-License-Identifier: MIT
/// \file
/// Generates enum category functions.
@ -55,68 +54,67 @@ const cppast::cpp_enum& get_enum(const cppast::cpp_entity_index& index,
// generates the function definitions
void generate_enum_category(const cppast::cpp_entity_index& index, const cppast::cpp_file& file)
{
cppast::visit(file,
[](const cppast::cpp_entity& e) {
// only visit function declarations that have the attribute set
return (e.kind() == cppast::cpp_entity_kind::function_t
&& !cppast::is_definition(e)
&& cppast::has_attribute(e, "generate::enum_category"))
// or all namespaces
|| e.kind() == cppast::cpp_entity_kind::namespace_t;
},
[&](const cppast::cpp_entity& e, const cppast::visitor_info& info) {
if (e.kind() == cppast::cpp_entity_kind::function_t)
{
// a new function, generate implementation
assert(info.is_new_entity());
cppast::visit(
file,
[](const cppast::cpp_entity& e) {
// only visit function declarations that have the attribute set
return (e.kind() == cppast::cpp_entity_kind::function_t && !cppast::is_definition(e)
&& cppast::has_attribute(e, "generate::enum_category"))
// or all namespaces
|| e.kind() == cppast::cpp_entity_kind::namespace_t;
},
[&](const cppast::cpp_entity& e, const cppast::visitor_info& info) {
if (e.kind() == cppast::cpp_entity_kind::function_t)
{
// a new function, generate implementation
assert(info.is_new_entity());
auto category = cppast::has_attribute(e, "generate::enum_category")
.value()
.arguments()
.value()
.as_string();
auto category = cppast::has_attribute(e, "generate::enum_category")
.value()
.arguments()
.value()
.as_string();
auto& func = static_cast<const cppast::cpp_function&>(e);
// return type must be bool
assert(func.return_type().kind() == cppast::cpp_type_kind::builtin_t
&& static_cast<const cppast::cpp_builtin_type&>(func.return_type())
.builtin_type_kind()
== cppast::cpp_bool);
auto& func = static_cast<const cppast::cpp_function&>(e);
// return type must be bool
assert(func.return_type().kind() == cppast::cpp_type_kind::builtin_t
&& static_cast<const cppast::cpp_builtin_type&>(func.return_type())
.builtin_type_kind()
== cppast::cpp_bool);
// single parameter...
assert(std::next(func.parameters().begin()) == func.parameters().end());
auto& param = *func.parameters().begin();
auto& enum_ = get_enum(index, param);
// single parameter...
assert(std::next(func.parameters().begin()) == func.parameters().end());
auto& param = *func.parameters().begin();
auto& enum_ = get_enum(index, param);
// generate function definition
std::cout << "inline bool " << func.name() << "("
<< cppast::to_string(param.type()) << " e) {\n";
// generate function definition
std::cout << "inline bool " << func.name() << "(" << cppast::to_string(param.type())
<< " e) {\n";
// generate switch
std::cout << " switch (e) {\n";
for (const auto& enumerator : enum_)
{
std::cout << " case " << enum_.name() << "::" << enumerator.name()
<< ":\n";
if (is_category(enumerator, category))
std::cout << " return true;\n";
else
std::cout << " return false;\n";
}
std::cout << " }\n";
// generate switch
std::cout << " switch (e) {\n";
for (const auto& enumerator : enum_)
{
std::cout << " case " << enum_.name() << "::" << enumerator.name() << ":\n";
if (is_category(enumerator, category))
std::cout << " return true;\n";
else
std::cout << " return false;\n";
}
std::cout << " }\n";
std::cout << "}\n\n";
}
else if (e.kind() == cppast::cpp_entity_kind::namespace_t)
{
if (info.event == cppast::visitor_info::container_entity_enter)
// open namespace
std::cout << "namespace " << e.name() << " {\n\n";
else // if (info.event == cppast::visitor_info::container_entity_exit)
// close namespace
std::cout << "}\n";
}
});
std::cout << "}\n\n";
}
else if (e.kind() == cppast::cpp_entity_kind::namespace_t)
{
if (info.event == cppast::visitor_info::container_entity_enter)
// open namespace
std::cout << "namespace " << e.name() << " {\n\n";
else // if (info.event == cppast::visitor_info::container_entity_exit)
// close namespace
std::cout << "}\n";
}
});
}
int main(int argc, char* argv[])

View file

@ -1,6 +1,5 @@
// 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.
// Copyright (C) 2017-2022 Jonathan Müller and cppast contributors
// SPDX-License-Identifier: MIT
/// \file
/// Generates enum `to_string()` code.
@ -17,55 +16,51 @@
void generate_to_string(const cppast::cpp_file& file)
{
cppast::visit(file,
[](const cppast::cpp_entity& e) {
// only visit enum definitions that have the attribute set
return (e.kind() == cppast::cpp_entity_kind::enum_t
&& cppast::is_definition(e)
&& cppast::has_attribute(e, "generate::to_string"))
// or all namespaces
|| e.kind() == cppast::cpp_entity_kind::namespace_t;
},
[](const cppast::cpp_entity& e, const cppast::visitor_info& info) {
if (e.kind() == cppast::cpp_entity_kind::enum_t && !info.is_old_entity())
{
// a new enum, generate to string function
auto& enum_ = static_cast<const cppast::cpp_enum&>(e);
cppast::visit(
file,
[](const cppast::cpp_entity& e) {
// only visit enum definitions that have the attribute set
return (e.kind() == cppast::cpp_entity_kind::enum_t && cppast::is_definition(e)
&& cppast::has_attribute(e, "generate::to_string"))
// or all namespaces
|| e.kind() == cppast::cpp_entity_kind::namespace_t;
},
[](const cppast::cpp_entity& e, const cppast::visitor_info& info) {
if (e.kind() == cppast::cpp_entity_kind::enum_t && !info.is_old_entity())
{
// a new enum, generate to string function
auto& enum_ = static_cast<const cppast::cpp_enum&>(e);
// write function header
std::cout << "inline const char* to_string(const " << enum_.name()
<< "& e) {\n";
// write function header
std::cout << "inline const char* to_string(const " << enum_.name() << "& e) {\n";
// generate switch
std::cout << " switch (e) {\n";
for (const auto& enumerator : enum_)
{
std::cout << " case " << enum_.name() << "::" << enumerator.name()
<< ":\n";
// generate switch
std::cout << " switch (e) {\n";
for (const auto& enumerator : enum_)
{
std::cout << " case " << enum_.name() << "::" << enumerator.name() << ":\n";
// attribute can be used to override the string
if (auto attr
= cppast::has_attribute(enumerator, "generate::to_string"))
std::cout << " return "
<< attr.value().arguments().value().as_string()
<< ";\n";
else
std::cout << " return \"" << enumerator.name() << "\";\n";
}
std::cout << " }\n";
// attribute can be used to override the string
if (auto attr = cppast::has_attribute(enumerator, "generate::to_string"))
std::cout << " return " << attr.value().arguments().value().as_string()
<< ";\n";
else
std::cout << " return \"" << enumerator.name() << "\";\n";
}
std::cout << " }\n";
std::cout << "}\n\n";
}
else if (e.kind() == cppast::cpp_entity_kind::namespace_t)
{
if (info.event == cppast::visitor_info::container_entity_enter)
// open namespace
std::cout << "namespace " << e.name() << " {\n\n";
else // if (info.event == cppast::visitor_info::container_entity_exit)
// close namespace
std::cout << "}\n";
}
});
std::cout << "}\n\n";
}
else if (e.kind() == cppast::cpp_entity_kind::namespace_t)
{
if (info.event == cppast::visitor_info::container_entity_enter)
// open namespace
std::cout << "namespace " << e.name() << " {\n\n";
else // if (info.event == cppast::visitor_info::container_entity_exit)
// close namespace
std::cout << "}\n";
}
});
}
int main(int argc, char* argv[])

View file

@ -1,6 +1,5 @@
// 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.
// Copyright (C) 2017-2022 Jonathan Müller and cppast contributors
// SPDX-License-Identifier: MIT
#ifndef CPPAST_EXAMPLE_PARSER_HPP_INCLUDED
#define CPPAST_EXAMPLE_PARSER_HPP_INCLUDED
@ -13,7 +12,8 @@
// 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
int example_main(int argc, char* argv[], const cppast::cpp_entity_index& index, Callback cb)
try
{
if (argc != 2)
{

View file

@ -1,6 +1,5 @@
// 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.
// Copyright (C) 2017-2022 Jonathan Müller and cppast contributors
// SPDX-License-Identifier: MIT
/// \file
/// Serialization code generation.
@ -69,52 +68,50 @@ void generate_serialize_member(std::ostream& out, const cppast::cpp_member_varia
// generate serialization function
void generate_serialize(const cppast::cpp_file& file)
{
cppast::visit(file,
[](const cppast::cpp_entity& e) {
// only visit non-templated class definitions that have the attribute set
return (!cppast::is_templated(e)
&& e.kind() == cppast::cpp_entity_kind::class_t
&& cppast::is_definition(e)
&& cppast::has_attribute(e, "generate::serialize"))
// or all namespaces
|| e.kind() == cppast::cpp_entity_kind::namespace_t;
},
[](const cppast::cpp_entity& e, const cppast::visitor_info& info) {
if (e.kind() == cppast::cpp_entity_kind::class_t && !info.is_old_entity())
{
auto& class_ = static_cast<const cppast::cpp_class&>(e);
cppast::visit(
file,
[](const cppast::cpp_entity& e) {
// only visit non-templated class definitions that have the attribute set
return (!cppast::is_templated(e) && e.kind() == cppast::cpp_entity_kind::class_t
&& cppast::is_definition(e) && cppast::has_attribute(e, "generate::serialize"))
// or all namespaces
|| e.kind() == cppast::cpp_entity_kind::namespace_t;
},
[](const cppast::cpp_entity& e, const cppast::visitor_info& info) {
if (e.kind() == cppast::cpp_entity_kind::class_t && !info.is_old_entity())
{
auto& class_ = static_cast<const cppast::cpp_class&>(e);
std::cout << "inline void serialize(const foo::serializer& s, const "
<< class_.name() << "& obj) {\n";
std::cout << "inline void serialize(const foo::serializer& s, const "
<< class_.name() << "& obj) {\n";
// serialize base classes
for (auto& base : class_.bases())
if (!cppast::has_attribute(base, "generate::transient"))
std::cout << " serialize(s, static_cast<const " << base.name()
<< "&>(obj));\n";
// serialize base classes
for (auto& base : class_.bases())
if (!cppast::has_attribute(base, "generate::transient"))
std::cout << " serialize(s, static_cast<const " << base.name()
<< "&>(obj));\n";
// serialize member variables
for (auto& member : class_)
{
if (member.kind() == cppast::cpp_entity_kind::member_variable_t)
generate_serialize_member(std::cout,
static_cast<
const cppast::cpp_member_variable&>(
member));
}
// serialize member variables
for (auto& member : class_)
{
if (member.kind() == cppast::cpp_entity_kind::member_variable_t)
generate_serialize_member(std::cout,
static_cast<const cppast::cpp_member_variable&>(
member));
}
std::cout << "}\n\n";
}
else if (e.kind() == cppast::cpp_entity_kind::namespace_t)
{
if (info.event == cppast::visitor_info::container_entity_enter)
// open namespace
std::cout << "namespace " << e.name() << " {\n\n";
else // if (info.event == cppast::visitor_info::container_entity_exit)
// close namespace
std::cout << "}\n";
}
});
std::cout << "}\n\n";
}
else if (e.kind() == cppast::cpp_entity_kind::namespace_t)
{
if (info.event == cppast::visitor_info::container_entity_enter)
// open namespace
std::cout << "namespace " << e.name() << " {\n\n";
else // if (info.event == cppast::visitor_info::container_entity_exit)
// close namespace
std::cout << "}\n";
}
});
}
int main(int argc, char* argv[])