0.6.0
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-2025 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
38using found_relationships_t = std::vector<
39 std::tuple<eid_t, common::model::relationship_t, const clang::Decl *>>;
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 virtual bool VisitObjCCategoryDecl(clang::ObjCCategoryDecl *decl);
85
86 virtual bool VisitObjCProtocolDecl(clang::ObjCProtocolDecl *decl);
87
88 virtual bool VisitObjCInterfaceDecl(clang::ObjCInterfaceDecl *decl);
89 /** @} */
90
91 /**
92 * @brief Finalize diagram model
93 */
94 void finalize();
95
96private:
97 /**
98 * @brief Get global package id for declaration.
99 *
100 * This method calculates package diagram id based on either the namespace
101 * of the provided declaration or filesystem path of the source file
102 * where it was declared - depending on the diagram configuration.
103 *
104 * This is necessary to infer dependency relationships between packages.
105 *
106 * @param cls C++ entity declaration
107 * @return Id of the package containing that declaration
108 */
109 eid_t get_package_id(const clang::Decl *cls);
110
111 /**
112 * @brief Process class declaration
113 *
114 * @param cls Class declaration
115 * @param relationships List of relationships discovered from this class
116 */
118 const clang::CXXRecordDecl &cls, found_relationships_t &relationships);
119
120 /**
121 * @brief Process class children
122 *
123 * @param cls Class declaration
124 * @param relationships List of relationships discovered from this class
125 */
127 const clang::CXXRecordDecl &cls, found_relationships_t &relationships);
128
129 /**
130 * @brief Process record children
131 *
132 * @param cls Record declaration
133 * @param relationships List of relationships discovered from this class
134 */
136 const clang::RecordDecl &cls, found_relationships_t &relationships);
137
138 /**
139 * @brief Process ObjC container children
140 *
141 * ObjC container in Clang is a base class for any object (e.g. interface,
142 * protocol or category) that can contain methods.
143 *
144 * @param cls ObjC container declaration
145 * @param relationships List of relationships discovered from this objc
146 * container
147 */
148 void process_objc_container_children(const clang::ObjCContainerDecl &cls,
149 found_relationships_t &relationships);
150
151 /**
152 * @brief Process record bases
153 *
154 * @param cls Class declaration
155 * @param relationships List of relationships discovered from this class
156 */
158 const clang::CXXRecordDecl &cls, found_relationships_t &relationships);
159
160 /**
161 * @brief Process method declaration
162 *
163 * @param method Method declaration
164 * @param relationships List of relationships discovered from this method
165 */
166 void process_method(const clang::CXXMethodDecl &method,
167 found_relationships_t &relationships);
168
169 /**
170 * @brief Process ObjC method declaration
171 *
172 * @param method Method declaration
173 * @param relationships List of relationships discovered from this method
174 */
176 const clang::ObjCMethodDecl &mf, found_relationships_t &relationships);
177
178 /**
179 * @brief Process template method declaration
180 *
181 * @param method Method declaration
182 * @param relationships List of relationships discovered from this method
183 */
184 void process_template_method(const clang::FunctionTemplateDecl &method,
185 found_relationships_t &relationships);
186
187 /**
188 * @brief Process member field
189 *
190 * @param field_declaration Field declaration
191 * @param relationships List of relationships discovered from this field
192 */
193 void process_field(const clang::FieldDecl &field_declaration,
194 found_relationships_t &relationships);
195
196 /**
197 * @brief Process ObjC property
198 *
199 * @param property_declaration Property declaration
200 * @param relationships List of relationships discovered from this property
201 */
203 const clang::ObjCPropertyDecl &property_declaration,
204 found_relationships_t &relationships);
205
206 /**
207 * @brief Process ObjC protocol implemented by certain interface
208 *
209 * @param protocol_declaration Protocol declaration
210 * @param relationships List of relationships discovered from this protocol
211 */
213 const clang::ObjCProtocolDecl &protocol_declaration,
214 found_relationships_t &relationships);
215
216 /**
217 * @brief Process static member field
218 *
219 * @param field_declaration Field declaration
220 * @param relationships List of relationships discovered from this field
221 */
222 void process_static_field(const clang::VarDecl &field_declaration,
223 found_relationships_t &relationships);
224
225 /**
226 * @brief Process friend declaration
227 *
228 * @param friend_declaration Field declaration
229 * @param relationships List of relationships discovered from this friend
230 */
231 void process_friend(const clang::FriendDecl &friend_declaration,
232 found_relationships_t &relationships);
233
234 /**
235 * @brief Process type
236 *
237 * @param type Reference to some C++ type
238 * @param relationships List of relationships discovered from this friend
239 * @param relationship_hint Default relationship type for discovered
240 * relationships
241 */
242 bool find_relationships(const clang::Decl *decl,
243 const clang::QualType &type, found_relationships_t &relationships,
244 common::model::relationship_t relationship_hint =
246
247 /**
248 * @brief Add discovered relationships for `cls` to the diagram.
249 *
250 * @param cls C/C++ entity declaration
251 * @param relationships List of discovered relationships
252 */
254 clang::Decl *cls, found_relationships_t &relationships);
255
256 std::vector<eid_t> get_parent_package_ids(eid_t id);
257};
258} // namespace clanguml::package_diagram::visitor