From 54b6c86e5bd690a305c0e4fab7dcd861c3d4ddcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Fri, 10 Feb 2017 19:25:02 +0100 Subject: [PATCH] Add cpp_macro_definition --- include/cppast/cpp_entity_kind.hpp | 2 + include/cppast/cpp_preprocessor.hpp | 64 +++++++++++++++++++++++++++++ src/cpp_entity_kind.cpp | 5 +++ src/cpp_preprocessor.cpp | 14 +++++++ src/visitor.cpp | 1 + 5 files changed, 86 insertions(+) create mode 100644 include/cppast/cpp_preprocessor.hpp create mode 100644 src/cpp_preprocessor.cpp diff --git a/include/cppast/cpp_entity_kind.hpp b/include/cppast/cpp_entity_kind.hpp index 2eef2e8..24f4fa0 100644 --- a/include/cppast/cpp_entity_kind.hpp +++ b/include/cppast/cpp_entity_kind.hpp @@ -14,6 +14,8 @@ namespace cppast { file_t, + macro_definition_t, + language_linkage_t, namespace_t, diff --git a/include/cppast/cpp_preprocessor.hpp b/include/cppast/cpp_preprocessor.hpp new file mode 100644 index 0000000..5322320 --- /dev/null +++ b/include/cppast/cpp_preprocessor.hpp @@ -0,0 +1,64 @@ +// 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. + +#ifndef CPPAST_CPP_PREPROCESSOR_HPP_INCLUDED +#define CPPAST_CPP_PREPROCESSOR_HPP_INCLUDED + +#include + +#include + +namespace cppast +{ + /// A [cppast::cpp_entity]() modelling a macro definition. + class cpp_macro_definition final : public cpp_entity + { + public: + /// \returns A newly built macro definition. + /// \notes It is not meant to be registered in the [cppast::cpp_entity_index](), + /// as no other [cppast::cpp_entity]() can refer to it. + static std::unique_ptr build( + std::string name, type_safe::optional parameters, std::string replacement) + { + return std::unique_ptr( + new cpp_macro_definition(std::move(name), std::move(parameters), + std::move(replacement))); + } + + /// \returns The replacement text of the macro. + const std::string& replacement() const noexcept + { + return replacement_; + } + + /// \returns Whether or not it is a function like macro. + bool is_function_like() const noexcept + { + return parameters_.has_value(); + } + + /// \returns The parameters of the macro, as the string spelled out in the source code. + /// \notes It has none if it is not a function like macro. + const type_safe::optional& parameters() const noexcept + { + return parameters_; + } + + private: + cpp_entity_kind do_get_entity_kind() const noexcept override; + + cpp_macro_definition(std::string name, type_safe::optional parameters, + std::string replacement) + : cpp_entity(std::move(name)), + parameters_(std::move(parameters)), + replacement_(std::move(replacement)) + { + } + + type_safe::optional parameters_; + std::string replacement_; + }; +} // namespace cppast + +#endif // CPPAST_CPP_PREPROCESSOR_HPP_INCLUDED diff --git a/src/cpp_entity_kind.cpp b/src/cpp_entity_kind.cpp index 91aeab4..1bbfad8 100644 --- a/src/cpp_entity_kind.cpp +++ b/src/cpp_entity_kind.cpp @@ -13,6 +13,9 @@ const char* cppast::to_string(cpp_entity_kind kind) noexcept case cpp_entity_kind::file_t: return "file"; + case cpp_entity_kind::macro_definition_t: + return "macro definition"; + case cpp_entity_kind::language_linkage_t: return "language linkage"; @@ -97,6 +100,7 @@ bool cppast::is_type(cpp_entity_kind kind) noexcept return true; case cpp_entity_kind::file_t: + case cpp_entity_kind::macro_definition_t: case cpp_entity_kind::language_linkage_t: case cpp_entity_kind::namespace_t: case cpp_entity_kind::namespace_alias_t: @@ -143,6 +147,7 @@ bool cppast::is_template(cpp_entity_kind kind) noexcept return true; case cpp_entity_kind::file_t: + case cpp_entity_kind::macro_definition_t: case cpp_entity_kind::language_linkage_t: case cpp_entity_kind::namespace_t: case cpp_entity_kind::namespace_alias_t: diff --git a/src/cpp_preprocessor.cpp b/src/cpp_preprocessor.cpp new file mode 100644 index 0000000..3c353d5 --- /dev/null +++ b/src/cpp_preprocessor.cpp @@ -0,0 +1,14 @@ +// 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 + +#include + +using namespace cppast; + +cpp_entity_kind cpp_macro_definition::do_get_entity_kind() const noexcept +{ + return cpp_entity_kind::macro_definition_t; +} diff --git a/src/visitor.cpp b/src/visitor.cpp index 73f9ddd..b33d543 100644 --- a/src/visitor.cpp +++ b/src/visitor.cpp @@ -77,6 +77,7 @@ bool detail::visit(const cpp_entity& e, detail::visitor_callback_t cb, void* fun case cpp_entity_kind::class_template_specialization_t: return handle_container(e, cb, functor); + case cpp_entity_kind::macro_definition_t: case cpp_entity_kind::namespace_alias_t: case cpp_entity_kind::using_directive_t: case cpp_entity_kind::using_declaration_t: