0.5.4
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
translation_unit_visitor.h
Go to the documentation of this file.
1/**
2 * @file src/package_diagram/visitor/translation_unit_visitor.h
3 *
4 * Copyright (c) 2021-2024 Bartek Kryza <bkryza@gmail.com>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18#pragma once
19
21#include "config/config.h"
23
24#include <clang/AST/RecursiveASTVisitor.h>
25#include <clang/Basic/SourceManager.h>
26
27#include <common/model/enums.h>
29#include <functional>
30#include <map>
31#include <memory>
32#include <string>
33
35
37
39 std::vector<std::pair<eid_t, common::model::relationship_t>>;
40
44
45/**
46 * @brief Package diagram translation unit visitor
47 *
48 * This class implements the `clang::RecursiveASTVisitor` interface
49 * for selected visitors relevant to generating package diagrams.
50 */
52 : public clang::RecursiveASTVisitor<translation_unit_visitor>,
54public:
55 /**
56 * @brief Constructor.
57 *
58 * @param sm Current source manager reference
59 * @param diagram Diagram model
60 * @param config Diagram configuration
61 */
62 translation_unit_visitor(clang::SourceManager &sm,
65
66 ~translation_unit_visitor() override = default;
67
68 /**
69 * \defgroup Implementation of ResursiveASTVisitor methods
70 * @{
71 */
72 virtual bool VisitNamespaceDecl(clang::NamespaceDecl *ns);
73
74 virtual bool VisitEnumDecl(clang::EnumDecl *decl);
75
76 virtual bool VisitCXXRecordDecl(clang::CXXRecordDecl *cls);
77
78 virtual bool VisitRecordDecl(clang::RecordDecl *cls);
79
80 virtual bool VisitClassTemplateDecl(clang::ClassTemplateDecl *decl);
81
82 virtual bool VisitFunctionDecl(clang::FunctionDecl *function_declaration);
83 /** @} */
84
85 /**
86 * @brief Finalize diagram model
87 */
88 void finalize();
89
90private:
91 /**
92 * @brief Get global package id for declaration.
93 *
94 * This method calculates package diagram id based on either the namespace
95 * of the provided declaration or filesystem path of the source file
96 * where it was declared - depending on the diagram configuration.
97 *
98 * This is necessary to infer dependency relationships between packages.
99 *
100 * @param cls C++ entity declaration
101 * @return Id of the package containing that declaration
102 */
103 eid_t get_package_id(const clang::Decl *cls);
104
105 /**
106 * @brief Process class declaration
107 *
108 * @param cls Class declaration
109 * @param relationships List of relationships discovered from this class
110 */
112 const clang::CXXRecordDecl &cls, found_relationships_t &relationships);
113
114 /**
115 * @brief Process class children
116 *
117 * @param cls Class declaration
118 * @param relationships List of relationships discovered from this class
119 */
121 const clang::CXXRecordDecl &cls, found_relationships_t &relationships);
122
123 /**
124 * @brief Process record children
125 *
126 * @param cls Record declaration
127 * @param relationships List of relationships discovered from this class
128 */
130 const clang::RecordDecl &cls, found_relationships_t &relationships);
131
132 /**
133 * @brief Process record bases
134 *
135 * @param cls Class declaration
136 * @param relationships List of relationships discovered from this class
137 */
139 const clang::CXXRecordDecl &cls, found_relationships_t &relationships);
140
141 /**
142 * @brief Process method declaration
143 *
144 * @param method Method declaration
145 * @param relationships List of relationships discovered from this method
146 */
147 void process_method(const clang::CXXMethodDecl &method,
148 found_relationships_t &relationships);
149
150 /**
151 * @brief Process template method declaration
152 *
153 * @param method Method declaration
154 * @param relationships List of relationships discovered from this method
155 */
156 void process_template_method(const clang::FunctionTemplateDecl &method,
157 found_relationships_t &relationships);
158
159 /**
160 * @brief Process member field
161 *
162 * @param field_declaration Field declaration
163 * @param relationships List of relationships discovered from this field
164 */
165 void process_field(const clang::FieldDecl &field_declaration,
166 found_relationships_t &relationships);
167
168 /**
169 * @brief Process static member field
170 *
171 * @param field_declaration Field declaration
172 * @param relationships List of relationships discovered from this field
173 */
174 void process_static_field(const clang::VarDecl &field_declaration,
175 found_relationships_t &relationships);
176
177 /**
178 * @brief Process friend declaration
179 *
180 * @param friend_declaration Field declaration
181 * @param relationships List of relationships discovered from this friend
182 */
183 void process_friend(const clang::FriendDecl &friend_declaration,
184 found_relationships_t &relationships);
185
186 /**
187 * @brief Process type
188 *
189 * @param type Reference to some C++ type
190 * @param relationships List of relationships discovered from this friend
191 * @param relationship_hint Default relationship type for discovered
192 * relationships
193 */
194 bool find_relationships(const clang::QualType &type,
195 found_relationships_t &relationships,
196 common::model::relationship_t relationship_hint =
198
199 /**
200 * @brief Add discovered relationships for `cls` to the diagram.
201 *
202 * @param cls C/C++ entity declaration
203 * @param relationships List of discovered relationships
204 */
206 clang::Decl *cls, found_relationships_t &relationships);
207
208 std::vector<eid_t> get_parent_package_ids(eid_t id);
209};
210} // namespace clanguml::package_diagram::visitor