From 0b88656cc2ae77977ef0c36b57090c8a8130e6b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Sat, 21 Jan 2017 22:59:38 +0100 Subject: [PATCH] Add full_name() function --- include/cppast/cpp_entity.hpp | 17 +++++++++++++++++ include/cppast/cpp_enum.hpp | 4 ++++ include/cppast/cpp_namespace.hpp | 6 ++++++ src/cpp_entity.cpp | 19 +++++++++++++++++++ src/cpp_enum.cpp | 5 +++++ 5 files changed, 51 insertions(+) create mode 100644 src/cpp_entity.cpp diff --git a/include/cppast/cpp_entity.hpp b/include/cppast/cpp_entity.hpp index bb005ac..d6db60d 100644 --- a/include/cppast/cpp_entity.hpp +++ b/include/cppast/cpp_entity.hpp @@ -35,6 +35,13 @@ namespace cppast return name_; } + /// \returns The name of the new scope created by the entity, + /// if there is any. + type_safe::optional scope_name() const + { + return do_get_scope_name(); + } + /// \returns A [ts::optional_ref]() to the parent entity in the AST. type_safe::optional_ref parent() const noexcept { @@ -51,6 +58,13 @@ namespace cppast /// \returns The type of the entity. virtual cpp_entity_type do_get_entity_type() const noexcept = 0; + /// \returns The name of the new scope created by the entity, if any. + /// By default, there is no scope created. + virtual type_safe::optional do_get_scope_name() const + { + return type_safe::nullopt; + } + void on_insert(const cpp_entity& parent) noexcept { parent_ = parent; @@ -63,6 +77,9 @@ namespace cppast friend struct detail::intrusive_list_access; friend detail::intrusive_list_node; }; + + /// \returns The full name of the [cppast::cpp_entity](), with all scopes. + std::string full_name(const cpp_entity& e); } // namespace cppast #endif // CPPAST_CPP_ENTITY_HPP_INCLUDED diff --git a/include/cppast/cpp_enum.hpp b/include/cppast/cpp_enum.hpp index cdea2d6..944def9 100644 --- a/include/cppast/cpp_enum.hpp +++ b/include/cppast/cpp_enum.hpp @@ -106,6 +106,10 @@ namespace cppast cpp_entity_type do_get_entity_type() const noexcept override; + /// \returns If the enum is scoped, the name of the enum, + /// otherwise [ts::nullopt](). + type_safe::optional do_get_scope_name() const override; + std::unique_ptr type_; bool scoped_; }; diff --git a/include/cppast/cpp_namespace.hpp b/include/cppast/cpp_namespace.hpp index e92f251..021f378 100644 --- a/include/cppast/cpp_namespace.hpp +++ b/include/cppast/cpp_namespace.hpp @@ -61,6 +61,12 @@ namespace cppast cpp_entity_type do_get_entity_type() const noexcept override; + /// \returns The name of the namespace. + type_safe::optional do_get_scope_name() const override + { + return name(); + } + bool inline_; }; diff --git a/src/cpp_entity.cpp b/src/cpp_entity.cpp new file mode 100644 index 0000000..4950ad2 --- /dev/null +++ b/src/cpp_entity.cpp @@ -0,0 +1,19 @@ +// 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. + +#include + +using namespace cppast; + +std::string cppast::full_name(const cpp_entity& e) +{ + std::string scopes; + + for (auto cur = e.parent(); cur; cur = cur.value().parent()) + // prepend each scope, if there is any + type_safe::with(cur.value().scope_name(), + [&](const std::string& cur_scope) { scopes = cur_scope + "::" + scopes; }); + + return scopes + e.name(); +} diff --git a/src/cpp_enum.cpp b/src/cpp_enum.cpp index 2e84cea..54c3f70 100644 --- a/src/cpp_enum.cpp +++ b/src/cpp_enum.cpp @@ -17,3 +17,8 @@ cpp_entity_type cpp_enum::do_get_entity_type() const noexcept { return cpp_entity_type::enum_t; } + +type_safe::optional cpp_enum::do_get_scope_name() const +{ + return scoped_ ? type_safe::make_optional(name()) : type_safe::nullopt; +}