Added handling of method parameters default values
This commit is contained in:
@@ -194,6 +194,8 @@ public:
|
|||||||
return clang_CXXMethod_isDefaulted(m_cursor);
|
return clang_CXXMethod_isDefaulted(m_cursor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool is_method_parameter() const { return kind() == CXCursor_ParmDecl; }
|
||||||
|
|
||||||
CXVisibilityKind visibitity() const
|
CXVisibilityKind visibitity() const
|
||||||
{
|
{
|
||||||
return clang_getCursorVisibility(m_cursor);
|
return clang_getCursorVisibility(m_cursor);
|
||||||
@@ -266,6 +268,20 @@ public:
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string default_value() const
|
||||||
|
{
|
||||||
|
assert(is_method_parameter());
|
||||||
|
|
||||||
|
auto toks = tokenize();
|
||||||
|
std::string res;
|
||||||
|
auto it = std::find(toks.begin(), toks.end(), "=");
|
||||||
|
if (it != toks.end()) {
|
||||||
|
res = fmt::format("{}", fmt::join(it + 1, toks.end(), ""));
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<std::string> tokenize_template_parameters() const
|
std::vector<std::string> tokenize_template_parameters() const
|
||||||
{
|
{
|
||||||
auto toks = tokenize();
|
auto toks = tokenize();
|
||||||
|
|||||||
@@ -136,8 +136,9 @@ public:
|
|||||||
if (true) { // TODO: add option to disable parameter generation
|
if (true) { // TODO: add option to disable parameter generation
|
||||||
std::vector<std::string> params;
|
std::vector<std::string> params;
|
||||||
std::transform(m.parameters.begin(), m.parameters.end(),
|
std::transform(m.parameters.begin(), m.parameters.end(),
|
||||||
std::back_inserter(params),
|
std::back_inserter(params), [this](const auto &mp) {
|
||||||
[](const auto &mp) { return mp.to_string(); });
|
return mp.to_string(m_config.using_namespace);
|
||||||
|
});
|
||||||
ostr << fmt::format("{}", fmt::join(params, ", "));
|
ostr << fmt::format("{}", fmt::join(params, ", "));
|
||||||
}
|
}
|
||||||
ostr << ")";
|
ostr << ")";
|
||||||
|
|||||||
@@ -82,12 +82,15 @@ struct method_parameter {
|
|||||||
std::string name;
|
std::string name;
|
||||||
std::string default_value;
|
std::string default_value;
|
||||||
|
|
||||||
std::string to_string() const
|
std::string to_string(
|
||||||
|
const std::vector<std::string> &using_namespaces) const
|
||||||
{
|
{
|
||||||
|
using namespace clanguml::util;
|
||||||
|
auto t = ns_relative(using_namespaces, type);
|
||||||
if (default_value.empty())
|
if (default_value.empty())
|
||||||
return fmt::format("{} {}", type, name);
|
return fmt::format("{} {}", t, name);
|
||||||
|
|
||||||
return fmt::format("{} {} = {}", type, name, default_value);
|
return fmt::format("{} {} = {}", t, name, default_value);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -223,6 +223,7 @@ static enum CXChildVisitResult method_parameter_visitor(
|
|||||||
method_parameter mp;
|
method_parameter mp;
|
||||||
mp.name = cursor.spelling();
|
mp.name = cursor.spelling();
|
||||||
mp.type = cursor.type().spelling();
|
mp.type = cursor.type().spelling();
|
||||||
|
mp.default_value = cursor.default_value();
|
||||||
|
|
||||||
ctx->element.parameters.emplace_back(std::move(mp));
|
ctx->element.parameters.emplace_back(std::move(mp));
|
||||||
|
|
||||||
|
|||||||
@@ -67,6 +67,8 @@ std::string ns_relative(
|
|||||||
|
|
||||||
std::sort(namespaces_sorted.rbegin(), namespaces_sorted.rend());
|
std::sort(namespaces_sorted.rbegin(), namespaces_sorted.rend());
|
||||||
|
|
||||||
|
auto res = n;
|
||||||
|
|
||||||
for (const auto &ns : namespaces_sorted) {
|
for (const auto &ns : namespaces_sorted) {
|
||||||
if (ns.empty())
|
if (ns.empty())
|
||||||
continue;
|
continue;
|
||||||
@@ -74,15 +76,14 @@ std::string ns_relative(
|
|||||||
if (n == ns)
|
if (n == ns)
|
||||||
return split(n, "::").back();
|
return split(n, "::").back();
|
||||||
|
|
||||||
if (n.find(ns) == 0) {
|
auto ns_prefix = ns + "::";
|
||||||
if (n.size() <= ns.size() + 2)
|
auto it = res.find(ns_prefix);
|
||||||
return "";
|
while (it != std::string::npos) {
|
||||||
|
res.erase(it, ns_prefix.size());
|
||||||
return n.substr(ns.size() + 2);
|
it = res.find(ns_prefix);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return res;
|
||||||
return n;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,12 @@ public:
|
|||||||
auto double_int(const int i) { return 2 * i; }
|
auto double_int(const int i) { return 2 * i; }
|
||||||
auto sum(const double a, const double b) { return a + b; }
|
auto sum(const double a, const double b) { return a + b; }
|
||||||
|
|
||||||
|
auto default_int(int i = 12) { return i + 10; }
|
||||||
|
auto default_string(int i, std::string s = "abc")
|
||||||
|
{
|
||||||
|
return s + std::to_string(i);
|
||||||
|
}
|
||||||
|
|
||||||
int public_member;
|
int public_member;
|
||||||
static int static_int;
|
static int static_int;
|
||||||
static const int static_const_int = 1;
|
static const int static_const_int = 1;
|
||||||
|
|||||||
@@ -39,4 +39,10 @@ TEST_CASE("Test ns_relative", "[unit-test]")
|
|||||||
using namespace clanguml::util;
|
using namespace clanguml::util;
|
||||||
|
|
||||||
CHECK(ns_relative({}, "std::vector") == "std::vector");
|
CHECK(ns_relative({}, "std::vector") == "std::vector");
|
||||||
|
CHECK(ns_relative({"std"}, "std::vector") == "vector");
|
||||||
|
CHECK(ns_relative({"std"}, "const std::vector&") == "const vector&");
|
||||||
|
CHECK(ns_relative({"std", "clanguml::t0"},
|
||||||
|
"static const std::vector<clanguml::t0::a>&") ==
|
||||||
|
"static const vector<a>&");
|
||||||
|
CHECK(ns_relative({"clanguml::t0"}, "clanguml::t0") == "t0");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user