Added basic variadic template class support

This commit is contained in:
Bartek Kryza
2021-03-13 18:22:14 +01:00
parent 86945b11d4
commit ca9927ecc9
7 changed files with 109 additions and 3 deletions

View File

@@ -234,8 +234,32 @@ public:
return clang_getSpecializedCursorTemplate(m_cursor);
}
CXTranslationUnit translation_unit() const
{
return clang_Cursor_getTranslationUnit(m_cursor);
}
std::string usr() const { return to_string(clang_getCursorUSR(m_cursor)); }
CXSourceRange extent() const { return clang_getCursorExtent(m_cursor); }
std::vector<std::string> tokenize() const
{
auto range = extent();
std::vector<std::string> res;
CXToken *toks;
unsigned toks_count{0};
auto tu = translation_unit();
clang_tokenize(tu, range, &toks, &toks_count);
for (int i = 0; i < toks_count; i++) {
res.push_back(to_string(clang_getTokenSpelling(tu, toks[i])));
}
return res;
}
const CXCursor &get() const { return m_cursor; }
private:

View File

@@ -110,6 +110,7 @@ struct class_template {
std::string name;
std::string type;
std::string default_value;
bool is_variadic{false};
};
class class_ : public element {

View File

@@ -330,12 +330,16 @@ static enum CXChildVisitResult class_visitor(
ret = CXChildVisit_Continue;
break;
case CXCursor_TemplateTypeParameter: {
spdlog::info("Found template type parameter: {}: {}",
cursor.spelling(), cursor.type());
const auto &tokens = cursor.tokenize();
spdlog::info("Found template type parameter: {}: {}, [{}]", cursor,
cursor.type(), fmt::join(tokens, ", "));
class_template ct;
ct.type = "";
ct.name = cursor.spelling();
ct.default_value = "";
ct.is_variadic = tokens.size() > 2 && tokens[1] == "...";
ct.name = cursor.spelling();
if (ct.is_variadic)
ct.name += "...";
ctx->element.templates.emplace_back(std::move(ct));
ret = CXChildVisit_Continue;

12
tests/t00012/.clanguml Normal file
View File

@@ -0,0 +1,12 @@
compilation_database_dir: ..
output_directory: puml
diagrams:
t00012_class:
type: class
glob:
- ../../tests/t00012/t00012.cc
using_namespace:
- clanguml::t00012
include:
namespaces:
- clanguml::t00012

13
tests/t00012/t00012.cc Normal file
View File

@@ -0,0 +1,13 @@
#include <algorithm>
#include <numeric>
#include <variant>
namespace clanguml {
namespace t00012 {
template <typename T, typename... Ts> class A {
T value;
std::variant<Ts...> values;
};
}
}

51
tests/t00012/test_case.h Normal file
View File

@@ -0,0 +1,51 @@
/**
* tests/t00012/test_case.cc
*
* Copyright (c) 2021 Bartek Kryza <bkryza@gmail.com>
*
* 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.
*/
TEST_CASE("Test t00012", "[unit-test]")
{
spdlog::set_level(spdlog::level::debug);
auto [config, db] = load_config("t00012");
auto diagram = config.diagrams["t00012_class"];
REQUIRE(diagram->name == "t00012_class");
REQUIRE(diagram->include.namespaces.size() == 1);
REQUIRE_THAT(diagram->include.namespaces,
VectorContains(std::string{"clanguml::t00012"}));
REQUIRE(diagram->exclude.namespaces.size() == 0);
REQUIRE(diagram->should_include("clanguml::t00012::A"));
REQUIRE(diagram->should_include("clanguml::t00012::B"));
auto model = generate_class_diagram(db, diagram);
REQUIRE(model.name == "t00012_class");
auto puml = generate_class_puml(diagram, model);
AliasMatcher _A(puml);
REQUIRE_THAT(puml, StartsWith("@startuml"));
REQUIRE_THAT(puml, EndsWith("@enduml\n"));
REQUIRE_THAT(puml, IsClassTemplate("A", "T, Ts..."));
save_puml(
"./" + config.output_directory + "/" + diagram->name + ".puml", puml);
}

View File

@@ -126,6 +126,7 @@ using clanguml::test::matchers::Static;
#include "t00009/test_case.h"
#include "t00010/test_case.h"
#include "t00011/test_case.h"
#include "t00012/test_case.h"
//
// Sequence diagram tests