Added basic class access and type specifiers generation
This commit is contained in:
@@ -137,6 +137,8 @@ public:
|
|||||||
return clang_CXXMethod_isVirtual(m_cursor);
|
return clang_CXXMethod_isVirtual(m_cursor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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_const() const { return clang_CXXMethod_isConst(m_cursor); }
|
||||||
|
|
||||||
bool is_method_pure_virtual() const
|
bool is_method_pure_virtual() const
|
||||||
|
|||||||
@@ -101,18 +101,39 @@ public:
|
|||||||
|
|
||||||
ostr << c.name << " {" << std::endl;
|
ostr << c.name << " {" << std::endl;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Process methods
|
||||||
|
//
|
||||||
for (const auto &m : c.methods) {
|
for (const auto &m : c.methods) {
|
||||||
if (m.is_pure_virtual)
|
if (m.is_pure_virtual)
|
||||||
ostr << "{abstract} ";
|
ostr << "{abstract} ";
|
||||||
|
|
||||||
ostr << to_string(m.scope) << m.type << " " << m.name + "()";
|
if (m.is_static)
|
||||||
|
ostr << "{static} ";
|
||||||
|
|
||||||
|
std::string type{};
|
||||||
|
if (m.type != "void")
|
||||||
|
type = m.type;
|
||||||
|
|
||||||
|
ostr << to_string(m.scope) << type << " " << m.name + "()";
|
||||||
|
|
||||||
|
if (m.is_const)
|
||||||
|
ostr << " const";
|
||||||
|
|
||||||
|
assert(!(m.is_pure_virtual && m.is_defaulted));
|
||||||
|
|
||||||
if (m.is_pure_virtual)
|
if (m.is_pure_virtual)
|
||||||
ostr << " = 0";
|
ostr << " = 0";
|
||||||
|
|
||||||
|
if (m.is_defaulted)
|
||||||
|
ostr << " = default";
|
||||||
|
|
||||||
ostr << std::endl;
|
ostr << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Process members
|
||||||
|
//
|
||||||
for (const auto &m : c.members) {
|
for (const auto &m : c.members) {
|
||||||
ostr << to_string(m.scope) << m.type << " " << m.name << std::endl;
|
ostr << to_string(m.scope) << m.type << " " << m.name << std::endl;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ struct class_method : public class_element {
|
|||||||
bool is_virtual{false};
|
bool is_virtual{false};
|
||||||
bool is_const{false};
|
bool is_const{false};
|
||||||
bool is_defaulted{false};
|
bool is_defaulted{false};
|
||||||
|
bool is_static{false};
|
||||||
};
|
};
|
||||||
|
|
||||||
struct class_parent {
|
struct class_parent {
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ enum CXChildVisitResult visit_if_cursor_valid(
|
|||||||
cx::cursor cursor, std::function<void(cx::cursor)> f)
|
cx::cursor cursor, std::function<void(cx::cursor)> f)
|
||||||
{
|
{
|
||||||
enum CXChildVisitResult ret = CXChildVisit_Break;
|
enum CXChildVisitResult ret = CXChildVisit_Break;
|
||||||
if (cursor.is_definition()) {
|
if (cursor.is_definition() || cursor.is_declaration()) {
|
||||||
if (!cursor.spelling().empty()) {
|
if (!cursor.spelling().empty()) {
|
||||||
f(cursor);
|
f(cursor);
|
||||||
ret = CXChildVisit_Continue;
|
ret = CXChildVisit_Continue;
|
||||||
@@ -84,21 +84,32 @@ enum CXChildVisitResult visit_if_cursor_valid(
|
|||||||
ret = CXChildVisit_Recurse;
|
ret = CXChildVisit_Recurse;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (cursor.is_declaration()) {
|
|
||||||
if (cursor.is_method_pure_virtual()) {
|
|
||||||
f(cursor);
|
|
||||||
ret = CXChildVisit_Continue;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
ret = CXChildVisit_Recurse;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
else {
|
||||||
ret = CXChildVisit_Continue;
|
ret = CXChildVisit_Continue;
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
scope_t cx_access_specifier_to_scope(CX_CXXAccessSpecifier as)
|
||||||
|
{
|
||||||
|
scope_t res = scope_t::kPublic;
|
||||||
|
switch (as) {
|
||||||
|
case CX_CXXAccessSpecifier::CX_CXXPublic:
|
||||||
|
res = scope_t::kPublic;
|
||||||
|
break;
|
||||||
|
case CX_CXXAccessSpecifier::CX_CXXPrivate:
|
||||||
|
res = scope_t::kPrivate;
|
||||||
|
break;
|
||||||
|
case CX_CXXAccessSpecifier::CX_CXXProtected:
|
||||||
|
res = scope_t::kProtected;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
static enum CXChildVisitResult enum_visitor(
|
static enum CXChildVisitResult enum_visitor(
|
||||||
CXCursor cx_cursor, CXCursor cx_parent, CXClientData client_data)
|
CXCursor cx_cursor, CXCursor cx_parent, CXClientData client_data)
|
||||||
{
|
{
|
||||||
@@ -144,6 +155,8 @@ static enum CXChildVisitResult class_visitor(
|
|||||||
c->name, cursor_name_str, cursor.kind());
|
c->name, cursor_name_str, cursor.kind());
|
||||||
|
|
||||||
enum CXChildVisitResult ret = CXChildVisit_Break;
|
enum CXChildVisitResult ret = CXChildVisit_Break;
|
||||||
|
bool is_constructor{false};
|
||||||
|
bool is_destructor{false};
|
||||||
switch (cursor.kind()) {
|
switch (cursor.kind()) {
|
||||||
case CXCursor_CXXMethod:
|
case CXCursor_CXXMethod:
|
||||||
case CXCursor_Constructor:
|
case CXCursor_Constructor:
|
||||||
@@ -157,6 +170,9 @@ static enum CXChildVisitResult class_visitor(
|
|||||||
m.is_virtual = cursor.is_method_virtual();
|
m.is_virtual = cursor.is_method_virtual();
|
||||||
m.is_const = cursor.is_method_const();
|
m.is_const = cursor.is_method_const();
|
||||||
m.is_defaulted = cursor.is_method_defaulted();
|
m.is_defaulted = cursor.is_method_defaulted();
|
||||||
|
m.is_static = cursor.is_method_static();
|
||||||
|
m.scope =
|
||||||
|
cx_access_specifier_to_scope(cursor.cxxaccess_specifier());
|
||||||
|
|
||||||
spdlog::info("Adding method {} {}::{}()", m.type, c->name,
|
spdlog::info("Adding method {} {}::{}()", m.type, c->name,
|
||||||
cursor.spelling());
|
cursor.spelling());
|
||||||
@@ -172,6 +188,8 @@ static enum CXChildVisitResult class_visitor(
|
|||||||
class_member m;
|
class_member m;
|
||||||
m.name = cursor.spelling();
|
m.name = cursor.spelling();
|
||||||
m.type = cursor.type().spelling();
|
m.type = cursor.type().spelling();
|
||||||
|
m.scope =
|
||||||
|
cx_access_specifier_to_scope(cursor.cxxaccess_specifier());
|
||||||
|
|
||||||
spdlog::info("Adding member {} {}::{}", m.type, c->name,
|
spdlog::info("Adding member {} {}::{}", m.type, c->name,
|
||||||
cursor.spelling());
|
cursor.spelling());
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ set(CLANG_UML_TEST_CASES_SRC
|
|||||||
test_cases.cc
|
test_cases.cc
|
||||||
t00001/t00001.cc
|
t00001/t00001.cc
|
||||||
t00002/t00002.cc
|
t00002/t00002.cc
|
||||||
|
t00003/t00003.cc
|
||||||
)
|
)
|
||||||
set(CLANG_UML_TEST_CASES_HEADER
|
set(CLANG_UML_TEST_CASES_HEADER
|
||||||
catch.h
|
catch.h
|
||||||
@@ -25,5 +26,6 @@ target_link_libraries(test_cases
|
|||||||
|
|
||||||
configure_file(t00001/.clanguml t00001/.clanguml COPYONLY)
|
configure_file(t00001/.clanguml t00001/.clanguml COPYONLY)
|
||||||
configure_file(t00002/.clanguml t00002/.clanguml COPYONLY)
|
configure_file(t00002/.clanguml t00002/.clanguml COPYONLY)
|
||||||
|
configure_file(t00003/.clanguml t00003/.clanguml COPYONLY)
|
||||||
|
|
||||||
add_test(NAME test_cases COMMAND test_cases)
|
add_test(NAME test_cases COMMAND test_cases)
|
||||||
|
|||||||
17
tests/t00003/.clanguml
Normal file
17
tests/t00003/.clanguml
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
compilation_database_dir: ..
|
||||||
|
output_directory: puml
|
||||||
|
diagrams:
|
||||||
|
t00003_class:
|
||||||
|
type: class
|
||||||
|
glob:
|
||||||
|
- ../../tests/t00003/t00003.cc
|
||||||
|
using_namespace:
|
||||||
|
- clanguml::t00003
|
||||||
|
include:
|
||||||
|
namespaces:
|
||||||
|
- clanguml::t00003
|
||||||
|
plantuml:
|
||||||
|
before:
|
||||||
|
- "' t00003 test class members"
|
||||||
|
after:
|
||||||
|
- 'note over "A": A class'
|
||||||
28
tests/t00003/t00003.cc
Normal file
28
tests/t00003/t00003.cc
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
namespace clanguml {
|
||||||
|
namespace t00003 {
|
||||||
|
|
||||||
|
class A {
|
||||||
|
public:
|
||||||
|
A() = default;
|
||||||
|
A(A &&) = default;
|
||||||
|
A(const A &) = default;
|
||||||
|
virtual ~A() = default;
|
||||||
|
|
||||||
|
void basic_method() {}
|
||||||
|
static int static_method() { return 0; }
|
||||||
|
void const_method() const {}
|
||||||
|
|
||||||
|
int public_member;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void protected_method() {}
|
||||||
|
|
||||||
|
int protected_member;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void private_method() {}
|
||||||
|
|
||||||
|
int private_member;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -160,3 +160,48 @@ TEST_CASE("Test t00002", "[unit-test]")
|
|||||||
save_puml(
|
save_puml(
|
||||||
"./" + config.output_directory + "/" + diagram->name + ".puml", puml);
|
"./" + config.output_directory + "/" + diagram->name + ".puml", puml);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Test t00003", "[unit-test]")
|
||||||
|
{
|
||||||
|
spdlog::set_level(spdlog::level::debug);
|
||||||
|
|
||||||
|
auto [config, db] = load_config("t00003");
|
||||||
|
|
||||||
|
auto diagram = config.diagrams["t00003_class"];
|
||||||
|
|
||||||
|
REQUIRE(diagram->name == "t00003_class");
|
||||||
|
|
||||||
|
REQUIRE(diagram->include.namespaces.size() == 1);
|
||||||
|
REQUIRE_THAT(diagram->include.namespaces,
|
||||||
|
VectorContains(std::string{"clanguml::t00003"}));
|
||||||
|
|
||||||
|
REQUIRE(diagram->exclude.namespaces.size() == 0);
|
||||||
|
|
||||||
|
REQUIRE(diagram->should_include("clanguml::t00003::A"));
|
||||||
|
|
||||||
|
auto model = generate_class_diagram(db, diagram);
|
||||||
|
|
||||||
|
REQUIRE(model.name == "t00003_class");
|
||||||
|
|
||||||
|
auto puml = generate_class_puml(diagram, model);
|
||||||
|
|
||||||
|
REQUIRE_THAT(puml, StartsWith("@startuml"));
|
||||||
|
REQUIRE_THAT(puml, EndsWith("@enduml\n"));
|
||||||
|
REQUIRE_THAT(puml, Contains("class A"));
|
||||||
|
|
||||||
|
REQUIRE_THAT(puml, Contains("+ A() = default"));
|
||||||
|
REQUIRE_THAT(puml, Contains("+ A() = default"));
|
||||||
|
REQUIRE_THAT(puml, Contains("+ A() = default"));
|
||||||
|
REQUIRE_THAT(puml, Contains("+ ~A() = default"));
|
||||||
|
REQUIRE_THAT(puml, Contains("+ basic_method()"));
|
||||||
|
REQUIRE_THAT(puml, Contains("{static} +int static_method()"));
|
||||||
|
REQUIRE_THAT(puml, Contains("+ const_method() const"));
|
||||||
|
REQUIRE_THAT(puml, Contains("# protected_method()"));
|
||||||
|
REQUIRE_THAT(puml, Contains("- private_method()"));
|
||||||
|
REQUIRE_THAT(puml, Contains("+int public_member"));
|
||||||
|
REQUIRE_THAT(puml, Contains("#int protected_member"));
|
||||||
|
REQUIRE_THAT(puml, Contains("-int private_member"));
|
||||||
|
|
||||||
|
save_puml(
|
||||||
|
"./" + config.output_directory + "/" + diagram->name + ".puml", puml);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user