Files
clang-uml/docs/test_cases/t20012.md
2024-01-25 10:46:41 +01:00

19 KiB

t20012 - Lambda expression call sequence diagram test case

Config

diagrams:
  t20012_sequence:
    type: sequence
    glob:
      - t20012.cc
    include:
      namespaces:
        - clanguml::t20012
    using_namespace: clanguml::t20012
    from:
      - function: "clanguml::t20012::tmain()"

Source code

File tests/t20012/t20012.cc

#include <algorithm>
#include <functional>
#include <memory>
#include <optional>
#include <utility>
#include <vector>

namespace clanguml {
namespace t20012 {
struct A {
    void a() { aa(); }

    void aa() { aaa(); }

    void aaa() { }
};

struct B {
    void b() { bb(); }

    void bb() { bbb(); }

    void bbb() { }

    void eb() { }
};

struct C {
    void c() { cc(); }

    void cc() { ccc(); }

    void ccc() { }
};

struct D {
    int add5(int arg) const { return arg + 5; }
};

class E {
    std::optional<std::shared_ptr<B>> maybe_b;
    std::shared_ptr<A> a;

public:
    template <typename F> void setup(F &&f) { f(maybe_b); }
};

template <typename F> struct R {
    R(F &&f)
        : f_{std::move(f)}
    {
    }

    void r() { f_(); }

    F f_;
};

void tmain()
{
    A a;
    B b;
    C c;

    // The activity shouldn't be marked at the lambda definition, but
    // wherever it is actually called...
    auto alambda = [&a, &b]() {
        a.a();
        b.b();
    };

    // ...like here
    alambda();

    // There should be no call to B in the sequence diagram as the blambda
    // is never called
    [[maybe_unused]] auto blambda = [&b]() { b.b(); };

    // Nested lambdas should also work
    auto clambda = [alambda, &c]() {
        c.c();
        alambda();
    };
    clambda();

    R r{[&c]() { c.c(); }};

    r.r();

    D d;

    std::vector<int> ints{0, 1, 2, 3, 4};
    std::transform(ints.begin(), ints.end(), ints.begin(),
        [&d](auto i) { return d.add5(i); });

    // TODO: Fix naming function call arguments which are lambdas
    //    E e;
    //
    //    e.setup([](auto &&arg) mutable {
    //        // We cannot know here what 'arg' might be
    //        arg.value()->eb();
    //    });
}
}
}

Generated PlantUML diagrams

t20012_sequence

Generated Mermaid diagrams

t20012_sequence

Generated JSON models

{
  "diagram_type": "sequence",
  "name": "t20012_sequence",
  "participants": [
    {
      "display_name": "tmain()",
      "id": "893699278278125827",
      "name": "tmain",
      "namespace": "clanguml::t20012",
      "source_location": {
        "column": 6,
        "file": "t20012.cc",
        "line": 59,
        "translation_unit": "t20012.cc"
      },
      "type": "function"
    },
    {
      "activities": [
        {
          "display_name": "operator()()",
          "id": "1314931307342523651",
          "name": "operator()",
          "namespace": "",
          "type": "method"
        }
      ],
      "display_name": "tmain()::(lambda t20012.cc:67:20)",
      "id": "1823127147500894672",
      "name": "tmain()::(lambda t20012.cc:67:20)",
      "namespace": "clanguml::t20012",
      "source_location": {
        "column": 20,
        "file": "t20012.cc",
        "line": 67,
        "translation_unit": "t20012.cc"
      },
      "type": "lambda"
    },
    {
      "activities": [
        {
          "display_name": "a()",
          "id": "1871432932744498976",
          "name": "a",
          "namespace": "",
          "source_location": {
            "column": 10,
            "file": "t20012.cc",
            "line": 11,
            "translation_unit": "t20012.cc"
          },
          "type": "method"
        },
        {
          "display_name": "aa()",
          "id": "1100933039353876539",
          "name": "aa",
          "namespace": "",
          "source_location": {
            "column": 10,
            "file": "t20012.cc",
            "line": 13,
            "translation_unit": "t20012.cc"
          },
          "type": "method"
        },
        {
          "display_name": "aaa()",
          "id": "941636185823691898",
          "name": "aaa",
          "namespace": "",
          "source_location": {
            "column": 10,
            "file": "t20012.cc",
            "line": 15,
            "translation_unit": "t20012.cc"
          },
          "type": "method"
        }
      ],
      "display_name": "A",
      "id": "1798184226128732119",
      "name": "A",
      "namespace": "clanguml::t20012",
      "source_location": {
        "column": 8,
        "file": "t20012.cc",
        "line": 10,
        "translation_unit": "t20012.cc"
      },
      "type": "class"
    },
    {
      "activities": [
        {
          "display_name": "b()",
          "id": "2142697410385270633",
          "name": "b",
          "namespace": "",
          "source_location": {
            "column": 10,
            "file": "t20012.cc",
            "line": 19,
            "translation_unit": "t20012.cc"
          },
          "type": "method"
        },
        {
          "display_name": "bb()",
          "id": "973718340784931313",
          "name": "bb",
          "namespace": "",
          "source_location": {
            "column": 10,
            "file": "t20012.cc",
            "line": 21,
            "translation_unit": "t20012.cc"
          },
          "type": "method"
        },
        {
          "display_name": "bbb()",
          "id": "195788529004378403",
          "name": "bbb",
          "namespace": "",
          "source_location": {
            "column": 10,
            "file": "t20012.cc",
            "line": 23,
            "translation_unit": "t20012.cc"
          },
          "type": "method"
        }
      ],
      "display_name": "B",
      "id": "1893469899260202653",
      "name": "B",
      "namespace": "clanguml::t20012",
      "source_location": {
        "column": 8,
        "file": "t20012.cc",
        "line": 18,
        "translation_unit": "t20012.cc"
      },
      "type": "class"
    },
    {
      "activities": [
        {
          "display_name": "operator()()",
          "id": "1464047298179756286",
          "name": "operator()",
          "namespace": "",
          "type": "method"
        }
      ],
      "display_name": "tmain()::(lambda t20012.cc:80:20)",
      "id": "2103332104162021186",
      "name": "tmain()::(lambda t20012.cc:80:20)",
      "namespace": "clanguml::t20012",
      "source_location": {
        "column": 20,
        "file": "t20012.cc",
        "line": 80,
        "translation_unit": "t20012.cc"
      },
      "type": "lambda"
    },
    {
      "activities": [
        {
          "display_name": "c()",
          "id": "675369415318225607",
          "name": "c",
          "namespace": "",
          "source_location": {
            "column": 10,
            "file": "t20012.cc",
            "line": 29,
            "translation_unit": "t20012.cc"
          },
          "type": "method"
        },
        {
          "display_name": "cc()",
          "id": "1451821704315336057",
          "name": "cc",
          "namespace": "",
          "source_location": {
            "column": 10,
            "file": "t20012.cc",
            "line": 31,
            "translation_unit": "t20012.cc"
          },
          "type": "method"
        },
        {
          "display_name": "ccc()",
          "id": "1956141408799600460",
          "name": "ccc",
          "namespace": "",
          "source_location": {
            "column": 10,
            "file": "t20012.cc",
            "line": 33,
            "translation_unit": "t20012.cc"
          },
          "type": "method"
        }
      ],
      "display_name": "C",
      "id": "2071958121786360262",
      "name": "C",
      "namespace": "clanguml::t20012",
      "source_location": {
        "column": 8,
        "file": "t20012.cc",
        "line": 28,
        "translation_unit": "t20012.cc"
      },
      "type": "class"
    },
    {
      "activities": [
        {
          "display_name": "R((lambda at t20012.cc:86:9) &&)",
          "id": "1976182450987832927",
          "name": "R",
          "namespace": "",
          "source_location": {
            "column": 5,
            "file": "t20012.cc",
            "line": 49,
            "translation_unit": "t20012.cc"
          },
          "type": "method"
        },
        {
          "display_name": "r()",
          "id": "1627963942447215983",
          "name": "r",
          "namespace": "",
          "source_location": {
            "column": 10,
            "file": "t20012.cc",
            "line": 54,
            "translation_unit": "t20012.cc"
          },
          "type": "method"
        }
      ],
      "display_name": "R<(lambda at t20012.cc:86:9)>",
      "id": "1418878159628415236",
      "name": "R",
      "namespace": "clanguml::t20012",
      "source_location": {
        "column": 30,
        "file": "t20012.cc",
        "line": 48,
        "translation_unit": "t20012.cc"
      },
      "type": "class"
    },
    {
      "activities": [
        {
          "display_name": "operator()()",
          "id": "1801444422355429914",
          "name": "operator()",
          "namespace": "",
          "type": "method"
        }
      ],
      "display_name": "tmain()::(lambda t20012.cc:86:9)",
      "id": "1523229682883773614",
      "name": "tmain()::(lambda t20012.cc:86:9)",
      "namespace": "clanguml::t20012",
      "source_location": {
        "column": 9,
        "file": "t20012.cc",
        "line": 86,
        "translation_unit": "t20012.cc"
      },
      "type": "lambda"
    }
  ],
  "sequences": [
    {
      "messages": [
        {
          "from": {
            "activity_id": "893699278278125827",
            "participant_id": "893699278278125827"
          },
          "name": "operator()()",
          "return_type": "",
          "scope": "normal",
          "source_location": {
            "column": 5,
            "file": "t20012.cc",
            "line": 73,
            "translation_unit": "t20012.cc"
          },
          "to": {
            "activity_id": "1314931307342523651",
            "participant_id": "1823127147500894672"
          },
          "type": "message"
        },
        {
          "from": {
            "activity_id": "1314931307342523651",
            "participant_id": "1823127147500894672"
          },
          "name": "a()",
          "return_type": "void",
          "scope": "normal",
          "source_location": {
            "column": 9,
            "file": "t20012.cc",
            "line": 68,
            "translation_unit": "t20012.cc"
          },
          "to": {
            "activity_id": "1871432932744498976",
            "participant_id": "1798184226128732119"
          },
          "type": "message"
        },
        {
          "from": {
            "activity_id": "1871432932744498976",
            "participant_id": "1798184226128732119"
          },
          "name": "aa()",
          "return_type": "void",
          "scope": "normal",
          "source_location": {
            "column": 16,
            "file": "t20012.cc",
            "line": 11,
            "translation_unit": "t20012.cc"
          },
          "to": {
            "activity_id": "1100933039353876539",
            "participant_id": "1798184226128732119"
          },
          "type": "message"
        },
        {
          "from": {
            "activity_id": "1100933039353876539",
            "participant_id": "1798184226128732119"
          },
          "name": "aaa()",
          "return_type": "void",
          "scope": "normal",
          "source_location": {
            "column": 17,
            "file": "t20012.cc",
            "line": 13,
            "translation_unit": "t20012.cc"
          },
          "to": {
            "activity_id": "941636185823691898",
            "participant_id": "1798184226128732119"
          },
          "type": "message"
        },
        {
          "from": {
            "activity_id": "1314931307342523651",
            "participant_id": "1823127147500894672"
          },
          "name": "b()",
          "return_type": "void",
          "scope": "normal",
          "source_location": {
            "column": 9,
            "file": "t20012.cc",
            "line": 69,
            "translation_unit": "t20012.cc"
          },
          "to": {
            "activity_id": "2142697410385270633",
            "participant_id": "1893469899260202653"
          },
          "type": "message"
        },
        {
          "from": {
            "activity_id": "2142697410385270633",
            "participant_id": "1893469899260202653"
          },
          "name": "bb()",
          "return_type": "void",
          "scope": "normal",
          "source_location": {
            "column": 16,
            "file": "t20012.cc",
            "line": 19,
            "translation_unit": "t20012.cc"
          },
          "to": {
            "activity_id": "973718340784931313",
            "participant_id": "1893469899260202653"
          },
          "type": "message"
        },
        {
          "from": {
            "activity_id": "973718340784931313",
            "participant_id": "1893469899260202653"
          },
          "name": "bbb()",
          "return_type": "void",
          "scope": "normal",
          "source_location": {
            "column": 17,
            "file": "t20012.cc",
            "line": 21,
            "translation_unit": "t20012.cc"
          },
          "to": {
            "activity_id": "195788529004378403",
            "participant_id": "1893469899260202653"
          },
          "type": "message"
        },
        {
          "from": {
            "activity_id": "893699278278125827",
            "participant_id": "893699278278125827"
          },
          "name": "operator()()",
          "return_type": "",
          "scope": "normal",
          "source_location": {
            "column": 5,
            "file": "t20012.cc",
            "line": 84,
            "translation_unit": "t20012.cc"
          },
          "to": {
            "activity_id": "1464047298179756286",
            "participant_id": "2103332104162021186"
          },
          "type": "message"
        },
        {
          "from": {
            "activity_id": "1464047298179756286",
            "participant_id": "2103332104162021186"
          },
          "name": "c()",
          "return_type": "void",
          "scope": "normal",
          "source_location": {
            "column": 9,
            "file": "t20012.cc",
            "line": 81,
            "translation_unit": "t20012.cc"
          },
          "to": {
            "activity_id": "675369415318225607",
            "participant_id": "2071958121786360262"
          },
          "type": "message"
        },
        {
          "from": {
            "activity_id": "675369415318225607",
            "participant_id": "2071958121786360262"
          },
          "name": "cc()",
          "return_type": "void",
          "scope": "normal",
          "source_location": {
            "column": 16,
            "file": "t20012.cc",
            "line": 29,
            "translation_unit": "t20012.cc"
          },
          "to": {
            "activity_id": "1451821704315336057",
            "participant_id": "2071958121786360262"
          },
          "type": "message"
        },
        {
          "from": {
            "activity_id": "1451821704315336057",
            "participant_id": "2071958121786360262"
          },
          "name": "ccc()",
          "return_type": "void",
          "scope": "normal",
          "source_location": {
            "column": 17,
            "file": "t20012.cc",
            "line": 31,
            "translation_unit": "t20012.cc"
          },
          "to": {
            "activity_id": "1956141408799600460",
            "participant_id": "2071958121786360262"
          },
          "type": "message"
        },
        {
          "from": {
            "activity_id": "1464047298179756286",
            "participant_id": "2103332104162021186"
          },
          "name": "operator()()",
          "return_type": "",
          "scope": "normal",
          "source_location": {
            "column": 9,
            "file": "t20012.cc",
            "line": 82,
            "translation_unit": "t20012.cc"
          },
          "to": {
            "activity_id": "1314931307342523651",
            "participant_id": "1823127147500894672"
          },
          "type": "message"
        },
        {
          "from": {
            "activity_id": "893699278278125827",
            "participant_id": "893699278278125827"
          },
          "name": "R((lambda at t20012.cc:86:9) &&)",
          "return_type": "void",
          "scope": "normal",
          "source_location": {
            "column": 7,
            "file": "t20012.cc",
            "line": 86,
            "translation_unit": "t20012.cc"
          },
          "to": {
            "activity_id": "1976182450987832927",
            "participant_id": "1418878159628415236"
          },
          "type": "message"
        },
        {
          "from": {
            "activity_id": "893699278278125827",
            "participant_id": "893699278278125827"
          },
          "name": "r()",
          "return_type": "void",
          "scope": "normal",
          "source_location": {
            "column": 5,
            "file": "t20012.cc",
            "line": 88,
            "translation_unit": "t20012.cc"
          },
          "to": {
            "activity_id": "1627963942447215983",
            "participant_id": "1418878159628415236"
          },
          "type": "message"
        },
        {
          "from": {
            "activity_id": "1627963942447215983",
            "participant_id": "1418878159628415236"
          },
          "name": "operator()()",
          "return_type": "",
          "scope": "normal",
          "source_location": {
            "column": 16,
            "file": "t20012.cc",
            "line": 54,
            "translation_unit": "t20012.cc"
          },
          "to": {
            "activity_id": "1801444422355429914",
            "participant_id": "1523229682883773614"
          },
          "type": "message"
        },
        {
          "from": {
            "activity_id": "1801444422355429914",
            "participant_id": "1523229682883773614"
          },
          "name": "c()",
          "return_type": "void",
          "scope": "normal",
          "source_location": {
            "column": 18,
            "file": "t20012.cc",
            "line": 86,
            "translation_unit": "t20012.cc"
          },
          "to": {
            "activity_id": "675369415318225607",
            "participant_id": "2071958121786360262"
          },
          "type": "message"
        }
      ],
      "start_from": {
        "id": 893699278278125827,
        "location": "clanguml::t20012::tmain()"
      }
    }
  ],
  "using_namespace": "clanguml::t20012"
}