From d7c8c14cdad08c6eb3b235bffcb462fa542accb4 Mon Sep 17 00:00:00 2001 From: Bartek Kryza Date: Tue, 13 Dec 2022 23:18:37 +0100 Subject: [PATCH] Added initial support for skip decorator in sequence diagrams --- .../plantuml/sequence_diagram_generator.cc | 2 +- .../visitor/translation_unit_visitor.cc | 11 ++++- tests/t20025/.clang-uml | 14 ++++++ tests/t20025/t20025.cc | 45 ++++++++++++++++++ tests/t20025/test_case.h | 46 +++++++++++++++++++ tests/test_cases.cc | 1 + tests/test_cases.yaml | 3 ++ 7 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 tests/t20025/.clang-uml create mode 100644 tests/t20025/t20025.cc create mode 100644 tests/t20025/test_case.h diff --git a/src/sequence_diagram/generators/plantuml/sequence_diagram_generator.cc b/src/sequence_diagram/generators/plantuml/sequence_diagram_generator.cc index 15dc5e3e..ba6938f2 100644 --- a/src/sequence_diagram/generators/plantuml/sequence_diagram_generator.cc +++ b/src/sequence_diagram/generators/plantuml/sequence_diagram_generator.cc @@ -127,7 +127,7 @@ void generator::generate_activity(const activity &a, std::ostream &ostr, if (m.type() == message_t::kCall) { const auto &to = m_model.get_participant(m.to()); - if (!to) + if (!to || to.value().skip()) continue; visited.push_back(m.from()); diff --git a/src/sequence_diagram/visitor/translation_unit_visitor.cc b/src/sequence_diagram/visitor/translation_unit_visitor.cc index 7dc5b9e4..3a3747e2 100644 --- a/src/sequence_diagram/visitor/translation_unit_visitor.cc +++ b/src/sequence_diagram/visitor/translation_unit_visitor.cc @@ -298,6 +298,8 @@ bool translation_unit_visitor::VisitCXXMethodDecl(clang::CXXMethodDecl *m) param->getType(), m->getASTContext(), false)))); } + process_comment(*m, *m_ptr); + set_source_location(*m, *m_ptr); const auto method_full_name = m_ptr->full_name(false); @@ -368,6 +370,8 @@ bool translation_unit_visitor::VisitFunctionDecl(clang::FunctionDecl *f) } set_unique_id(f->getID(), f_ptr->id()); + process_comment(*f, *f_ptr); + set_source_location(*f, *f_ptr); // TODO: Handle overloaded functions with different arguments @@ -400,6 +404,8 @@ bool translation_unit_visitor::VisitFunctionDecl(clang::FunctionDecl *f) } set_unique_id(f->getID(), f_ptr->id()); + process_comment(*f, *f_ptr); + set_source_location(*f, *f_ptr); // TODO: Handle overloaded functions with different arguments @@ -441,6 +447,8 @@ bool translation_unit_visitor::VisitFunctionTemplateDecl( f_ptr->is_void( function_template->getAsFunction()->getReturnType()->isVoidType()); + process_comment(*function_template, *f_ptr); + set_source_location(*function_template, *f_ptr); context().update(function_template); @@ -811,8 +819,7 @@ bool translation_unit_visitor::VisitCallExpr(clang::CallExpr *expr) return true; // Skip casts, moves and such - if (expr->isCallToStdMove()) - return true; + if (expr->isCallToStdMove()) return true; if (expr->isImplicitCXXThis()) return true; diff --git a/tests/t20025/.clang-uml b/tests/t20025/.clang-uml new file mode 100644 index 00000000..1598fcf3 --- /dev/null +++ b/tests/t20025/.clang-uml @@ -0,0 +1,14 @@ +compilation_database_dir: .. +output_directory: puml +diagrams: + t20025_sequence: + type: sequence + glob: + - ../../tests/t20025/t20025.cc + include: + namespaces: + - clanguml::t20025 + using_namespace: + - clanguml::t20025 + start_from: + - function: "clanguml::t20025::tmain()" \ No newline at end of file diff --git a/tests/t20025/t20025.cc b/tests/t20025/t20025.cc new file mode 100644 index 00000000..6764e123 --- /dev/null +++ b/tests/t20025/t20025.cc @@ -0,0 +1,45 @@ +namespace clanguml { +namespace t20025 { + +int add(int x, int y) { return x + y; } + +/// Add 2 numbers +/// +/// \param x +/// \param y +/// \return +/// \uml{skip} +int add2(int x, int y) { return x + x + y + y; } + +struct A { + int a() + { + /// TODO: this doesn't work yet... + /// \uml{skip} + a2(); + + return a1(); + } + + /// \uml{skip} + int a1() { return 1; } + + void a2() { } +}; + +int tmain() +{ + A a; + + int result{}; + + result = a.a(); + + result += add(1, 2); + + result += add2(2, 4); + + return result; +} +} +} \ No newline at end of file diff --git a/tests/t20025/test_case.h b/tests/t20025/test_case.h new file mode 100644 index 00000000..2d6c55f7 --- /dev/null +++ b/tests/t20025/test_case.h @@ -0,0 +1,46 @@ +/** + * tests/t20025/test_case.h + * + * Copyright (c) 2021-2022 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. + */ + +TEST_CASE("t20025", "[test-case][sequence]") +{ + auto [config, db] = load_config("t20025"); + + auto diagram = config.diagrams["t20025_sequence"]; + + REQUIRE(diagram->name == "t20025_sequence"); + + auto model = generate_sequence_diagram(*db, diagram); + + REQUIRE(model->name() == "t20025_sequence"); + + auto puml = generate_sequence_puml(diagram, *model); + AliasMatcher _A(puml); + + REQUIRE_THAT(puml, StartsWith("@startuml")); + REQUIRE_THAT(puml, EndsWith("@enduml\n")); + + // Check if all calls exist + REQUIRE_THAT(puml, HasCall(_A("tmain()"), _A("A"), "a()")); + REQUIRE_THAT(puml, !HasCall(_A("A"), _A("A"), "a1()")); + // REQUIRE_THAT(puml, !HasCall(_A("A"), _A("A"), "a2()")); + REQUIRE_THAT(puml, HasCall(_A("tmain()"), _A("add(int,int)"), "")); + REQUIRE_THAT(puml, !HasCall(_A("tmain()"), _A("add2(int,int)"), "")); + + save_puml( + "./" + config.output_directory() + "/" + diagram->name + ".puml", puml); +} \ No newline at end of file diff --git a/tests/test_cases.cc b/tests/test_cases.cc index ce6cf013..539291ed 100644 --- a/tests/test_cases.cc +++ b/tests/test_cases.cc @@ -271,6 +271,7 @@ using namespace clanguml::test::matchers; #include "t20022/test_case.h" #include "t20023/test_case.h" #include "t20024/test_case.h" +#include "t20025/test_case.h" /// /// Package diagram tests diff --git a/tests/test_cases.yaml b/tests/test_cases.yaml index 8b328373..60d2ea5b 100644 --- a/tests/test_cases.yaml +++ b/tests/test_cases.yaml @@ -220,6 +220,9 @@ test_cases: - name: t20024 title: Switch statement sequence diagram test case description: + - name: t20025 + title: Skip decorator sequence diagram test case + description: Package diagrams: - name: t30001 title: Basic package diagram test case