/** * src/cx/cursor.h * * Copyright (c) 2021 Bartek Kryza * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "cx/type.h" namespace clanguml { namespace cx { class cursor { public: cursor() : m_cursor{clang_getNullCursor()} { } cursor(CXCursor &&c) : m_cursor{std::move(c)} { } cursor(const CXCursor &c) : m_cursor{c} { } cursor(const cursor &c) : m_cursor{c.get()} { } ~cursor() = default; bool operator==(const cursor &b) const { return clang_equalCursors(m_cursor, b.get()); } cx::type type() const { return {clang_getCursorType(m_cursor)}; } std::string display_name() const { return to_string(clang_getCursorDisplayName(m_cursor)); } std::string spelling() const { return to_string(clang_getCursorSpelling(m_cursor)); } bool is_void() const { // Why do I have to do this like this? return spelling() == "void"; } /** * @brief Return fully qualified cursor spelling * * This method generates a fully qualified name for the cursor by * traversing the namespaces upwards. * * TODO: Add caching of this value. * * @return Fully qualified cursor spelling */ std::string fully_qualified() const { std::list res; cursor iterator{m_cursor}; if (iterator.spelling().empty()) return {}; int limit = 100; while (iterator.kind() != CXCursor_TranslationUnit) { auto name = iterator.spelling(); if (!name.empty()) res.push_front(iterator.spelling()); iterator = iterator.semantic_parent(); if (limit-- == 0) throw std::runtime_error(fmt::format( "Generating fully qualified name for '{}' failed at: '{}'", spelling(), fmt::join(res, "::"))); } return fmt::format("{}", fmt::join(res, "::")); } cursor referenced() const { return cx::cursor{clang_getCursorReferenced(m_cursor)}; } cursor semantic_parent() const { return {clang_getCursorSemanticParent(m_cursor)}; } cursor lexical_parent() const { return {clang_getCursorLexicalParent(m_cursor)}; } CXCursorKind kind() const { return m_cursor.kind; } std::string kind_spelling() const { return to_string(clang_getCursorKindSpelling(m_cursor.kind)); } bool is_definition() const { return clang_isCursorDefinition(m_cursor); } bool is_declaration() const { return clang_isDeclaration(kind()); } bool is_invalid_declaration() const { return clang_isInvalidDeclaration(m_cursor); } CXSourceLocation location() const { return clang_getCursorLocation(m_cursor); } bool is_reference() const { return clang_isReference(kind()); } bool is_expression() const { return clang_isExpression(kind()); } bool is_statement() const { return clang_isStatement(kind()); } bool is_namespace() const { return kind() == CXCursor_Namespace; } bool is_attribute() const { return clang_isAttribute(kind()); } bool has_attrs() const { return clang_Cursor_hasAttrs(m_cursor); } bool is_invalid() const { return clang_isInvalid(kind()); } bool is_translation_unit() const { return clang_isTranslationUnit(kind()); } bool is_preprocessing() const { return clang_isPreprocessing(kind()); } bool is_method_virtual() const { return clang_CXXMethod_isVirtual(m_cursor); } bool is_static() const { return clang_Cursor_getStorageClass(m_cursor) == CX_SC_Static; } bool is_method_static() const { return clang_CXXMethod_isStatic(m_cursor); } bool is_method_const() const { return clang_CXXMethod_isConst(m_cursor); } bool is_method_pure_virtual() const { return clang_CXXMethod_isPureVirtual(m_cursor); } bool is_method_defaulted() const { return clang_CXXMethod_isDefaulted(m_cursor); } CXVisibilityKind visibitity() const { return clang_getCursorVisibility(m_cursor); } CXAvailabilityKind availability() const { return clang_getCursorAvailability(m_cursor); } CX_CXXAccessSpecifier cxxaccess_specifier() const { return clang_getCXXAccessSpecifier(m_cursor); } int template_argument_count() const { return clang_Cursor_getNumTemplateArguments(m_cursor); } CXTemplateArgumentKind template_argument_kind(unsigned i) const { return clang_Cursor_getTemplateArgumentKind(m_cursor, i); } cx::type template_argument_type(unsigned i) const { return clang_Cursor_getTemplateArgumentType(m_cursor, i); } long long template_argument_value(unsigned i) const { return clang_Cursor_getTemplateArgumentValue(m_cursor, i); } std::string usr() const { return to_string(clang_getCursorUSR(m_cursor)); } const CXCursor &get() const { return m_cursor; } private: CXCursor m_cursor; }; } } template <> struct fmt::formatter { template constexpr auto parse(ParseContext &ctx) { return ctx.begin(); } template auto format(const clanguml::cx::cursor &c, FormatContext &ctx) { return fmt::format_to(ctx.out(), "(cx::cursor spelling={}, display_name={}, kind={}, " "is_expression={}, template_argument_count={})", c.spelling(), c.display_name(), c.kind_spelling(), c.is_expression(), c.template_argument_count() ); } };