Restructured cx namespace

This commit is contained in:
Bartek Kryza
2021-02-15 23:59:20 +01:00
parent a5f85a3d8e
commit b70d726662
7 changed files with 131 additions and 122 deletions

View File

@@ -0,0 +1,88 @@
#include "compilation_database.h"
#include <filesystem>
#include <spdlog/fmt/fmt.h>
#include <spdlog/spdlog.h>
namespace clanguml {
namespace cx {
compilation_database::compilation_database(CXCompilationDatabase &&d)
: m_database{std::move(d)}
, m_index{clang_createIndex(0, 1)}
{
}
compilation_database::~compilation_database()
{
clang_CompilationDatabase_dispose(m_database);
}
compilation_database compilation_database::from_directory(
const std::string &dir)
{
CXCompilationDatabase_Error error;
auto path = std::filesystem::path{dir};
CXCompilationDatabase cdb =
clang_CompilationDatabase_fromDirectory(path.c_str(), &error);
if (error != CXCompilationDatabase_Error::CXCompilationDatabase_NoError) {
throw std::runtime_error(fmt::format(
"Cannot load compilation database database from: {}", dir));
}
return compilation_database{std::move(cdb)};
}
CXTranslationUnit compilation_database::parse_translation_unit(
const std::string &path)
{
const auto p = std::filesystem::canonical(path);
CXCompileCommands compile_commands =
clang_CompilationDatabase_getCompileCommands(m_database, p.c_str());
unsigned int compile_commands_count =
clang_CompileCommands_getSize(compile_commands);
int i;
// for (i = 0; i < compile_commands_count; i++) {
CXCompileCommand compile_command =
clang_CompileCommands_getCommand(compile_commands, 0);
auto cc_filename = clang_CompileCommand_getFilename(compile_command);
spdlog::debug(
"Processing compile command file: {}", clang_getCString(cc_filename));
auto num_args = clang_CompileCommand_getNumArgs(compile_command);
char **arguments = NULL;
if (num_args) {
int j;
arguments = (char **)malloc(sizeof(char *) * num_args);
for (j = 0; j < num_args; ++j) {
CXString arg = clang_CompileCommand_getArg(compile_command, j);
spdlog::debug("Processing argument: {}", clang_getCString(arg));
arguments[j] = strdup(clang_getCString(arg));
clang_disposeString(arg);
}
}
CXTranslationUnit tu = clang_parseTranslationUnit(m_index, nullptr,
(const char *const *)arguments, num_args, NULL, 0,
CXTranslationUnit_None);
if (num_args) {
int j;
for (j = 0; j < num_args; ++j) {
free(arguments[j]);
}
free(arguments);
}
//}
return tu;
}
CXCompilationDatabase &compilation_database::db() { return m_database; }
}
}

View File

@@ -0,0 +1,32 @@
#pragma once
#include <clang-c/CXCompilationDatabase.h>
#include <clang-c/Index.h>
#include <functional>
#include <memory>
#include <string>
namespace clanguml {
namespace cx {
class compilation_database {
public:
compilation_database(CXCompilationDatabase &&d);
~compilation_database();
CXCompilationDatabase &db();
CXIndex &index();
CXTranslationUnit parse_translation_unit(
const std::string &path);
static compilation_database from_directory(const std::string &dir);
private:
CXCompilationDatabase m_database;
CXIndex m_index;
};
}
}

128
src/cx/cursor.h Normal file
View File

@@ -0,0 +1,128 @@
#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));
}
std::string fully_qualified() const
{
std::list<std::string> res;
cursor iterator{m_cursor};
while (iterator.kind() != CXCursor_TranslationUnit) {
auto name = iterator.spelling();
if (!name.empty())
res.push_front(iterator.spelling());
iterator = iterator.semantic_parent();
}
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; }
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_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()); }
CXVisibilityKind visibitity() const
{
return clang_getCursorVisibility(m_cursor);
}
CXAvailabilityKind availability() const
{
return clang_getCursorAvailability(m_cursor);
}
std::string usr() const { return to_string(clang_getCursorUSR(m_cursor)); }
const CXCursor &get() const { return m_cursor; }
private:
CXCursor m_cursor;
};
}
}

145
src/cx/type.h Normal file
View File

@@ -0,0 +1,145 @@
#pragma once
#include <clang-c/CXCompilationDatabase.h>
#include <clang-c/Index.h>
#include <spdlog/spdlog.h>
namespace clanguml {
namespace cx {
namespace detail {
std::string to_string(CXString &&cxs)
{
std::string r{clang_getCString(cxs)};
clang_disposeString(cxs);
return r;
}
}
using detail::to_string;
class type {
public:
type(CXType &&t)
: m_type{std::move(t)}
{
}
~type() = default;
std::string spelling() const
{
return to_string(clang_getTypeSpelling(m_type));
}
bool operator==(const type &b) const
{
return clang_equalTypes(m_type, b.get());
}
type canonical() const { return {clang_getCanonicalType(m_type)}; }
bool is_const_qualified() const
{
return clang_isConstQualifiedType(m_type);
}
bool is_volatile_qualified() const
{
return clang_isVolatileQualifiedType(m_type);
}
bool is_restricted_qualified() const
{
return clang_isRestrictQualifiedType(m_type);
}
std::string typedef_name() const
{
return to_string(clang_getTypedefName(m_type));
}
type pointee_type() const { return {clang_getPointeeType(m_type)}; }
/*
*cursor type_declaration() const
*{
* return {clang_getTypeDeclaration(m_type)};
*}
*/
/*
*std::string type_kind_spelling() const
*{
* return clang_getTypeKindSpelling(kind());
*}
*/
CXCallingConv calling_convention() const
{
return clang_getFunctionTypeCallingConv(m_type);
}
type result_type() const { return clang_getResultType(m_type); }
int exception_specification_type() const
{
return clang_getExceptionSpecificationType(m_type);
}
int argument_type_count() const { return clang_getNumArgTypes(m_type); }
type argument_type(int i) const { return {clang_getArgType(m_type, i)}; }
bool is_function_variadic() const
{
return clang_isFunctionTypeVariadic(m_type);
}
bool is_pod() const { return clang_isPODType(m_type); }
type element_type() const { return clang_getElementType(m_type); }
long long element_count() const { return clang_getNumElements(m_type); }
type array_element_type() const
{
return clang_getArrayElementType(m_type);
}
type named_type() const { return clang_Type_getNamedType(m_type); }
CXTypeNullabilityKind nullability() const
{
return clang_Type_getNullability(m_type);
}
type class_type() const { return clang_Type_getClassType(m_type); }
long long size_of() const { return clang_Type_getSizeOf(m_type); }
type modified_type() const { return clang_Type_getModifiedType(m_type); }
type value_type() const { return clang_Type_getValueType(m_type); }
int template_arguments_count() const
{
return clang_Type_getNumTemplateArguments(m_type);
}
type template_argument_type(int i) const
{
return clang_Type_getTemplateArgumentAsType(m_type, i);
}
const CXType &get() const { return m_type; }
CXRefQualifierKind cxxref_qualifier() const
{
return clang_Type_getCXXRefQualifier(m_type);
}
private:
CXType m_type;
};
}
}