0.5.4
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Protected Member Functions | Private Attributes | List of all members
clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT > Class Template Reference

Diagram translation unit visitor base class. More...

Detailed Description

template<typename ConfigT, typename DiagramT>
class clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >

Diagram translation unit visitor base class.

This class provides common interface for diagram translation unit visitors.

Definition at line 54 of file translation_unit_visitor.h.

#include <translation_unit_visitor.h>

Public Types

using config_t = ConfigT
 
using diagram_t = DiagramT
 

Public Member Functions

 translation_unit_visitor (clang::SourceManager &sm, DiagramT &diagram, const ConfigT &config)
 Constructor.
 
virtual ~translation_unit_visitor ()=default
 
void set_tu_path (const std::string &translation_unit_path)
 
const std::filesystem::path & tu_path () const
 Return relative path to current translation unit.
 
common::visitor::ast_id_mapperid_mapper () const
 Get reference to Clang AST to clang-uml id mapper.
 
clang::SourceManager & source_manager () const
 Get clang::SourceManager.
 
void set_source_location (const clang::Decl &decl, clanguml::common::model::source_location &element)
 Set source location in diagram element.
 
void set_source_location (const clang::Expr &expr, clanguml::common::model::source_location &element)
 Set source location in diagram element.
 
void set_source_location (const clang::Stmt &stmt, clanguml::common::model::source_location &element)
 
void set_qualified_name (const clang::NamedDecl &decl, clanguml::common::model::element &element)
 
void set_source_location (const clang::SourceLocation &location, clanguml::common::model::source_location &element)
 Set source location in diagram element.
 
void set_owning_module (const clang::Decl &decl, clanguml::common::model::element &element)
 
virtual void add_diagram_element (std::unique_ptr< common::model::template_element > element)
 
void process_comment (const clang::NamedDecl &decl, clanguml::common::model::decorated_element &e)
 Process comment directives in comment attached to a declaration.
 
std::string process_comment (const clang::RawComment *comment, clang::DiagnosticsEngine &de, clanguml::common::model::decorated_element &e)
 Process comment directives in raw comment.
 
bool skip_system_header_decl (const clang::NamedDecl *decl) const
 
bool should_include (const clang::NamedDecl *decl) const
 Check if the diagram should include a declaration.
 
DiagramT & diagram ()
 Get diagram model reference.
 
const DiagramT & diagram () const
 Get diagram model reference.
 
const ConfigT & config () const
 Get diagram config instance.
 

Protected Member Functions

std::set< const clang::RawComment * > & processed_comments ()
 

Private Attributes

DiagramT & diagram_
 
const ConfigT & config_
 
clang::SourceManager & source_manager_
 
std::unique_ptr< comment::comment_visitorcomment_visitor_
 
std::filesystem::path relative_to_path_
 
std::filesystem::path translation_unit_path_
 
std::set< const clang::RawComment * > processed_comments_
 
common::visitor::ast_id_mapper id_mapper_
 

Member Typedef Documentation

◆ config_t

template<typename ConfigT , typename DiagramT >
using clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::config_t = ConfigT

Definition at line 56 of file translation_unit_visitor.h.

◆ diagram_t

template<typename ConfigT , typename DiagramT >
using clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::diagram_t = DiagramT

Definition at line 57 of file translation_unit_visitor.h.

Constructor & Destructor Documentation

◆ translation_unit_visitor()

template<typename ConfigT , typename DiagramT >
clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::translation_unit_visitor ( clang::SourceManager &  sm,
DiagramT &  diagram,
const ConfigT &  config 
)
inlineexplicit

Constructor.

Parameters
smReference to clang::SourceManager instance
configReference to clanguml::config::diagram configuration instance

Definition at line 66 of file translation_unit_visitor.h.

70 , source_manager_{sm}
71 , relative_to_path_{config.root_directory()}
72 {
73 if (config.comment_parser() == config::comment_parser_t::plain) {
75 std::make_unique<comment::plain_visitor>(source_manager_);
76 }
77 else if (config.comment_parser() == config::comment_parser_t::clang) {
79 std::make_unique<comment::clang_visitor>(source_manager_);
80 }
81 }

◆ ~translation_unit_visitor()

template<typename ConfigT , typename DiagramT >
virtual clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::~translation_unit_visitor ( )
virtualdefault

Member Function Documentation

◆ add_diagram_element()

template<typename ConfigT , typename DiagramT >
virtual void clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::add_diagram_element ( std::unique_ptr< common::model::template_element element)
inlinevirtual

Reimplemented in clanguml::class_diagram::visitor::translation_unit_visitor.

Definition at line 238 of file translation_unit_visitor.h.

240 {
241 }

◆ config()

template<typename ConfigT , typename DiagramT >
const ConfigT & clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::config ( ) const
inline

Get diagram config instance.

Returns
Reference to config instance

Definition at line 347 of file translation_unit_visitor.h.

347{ return config_; }

◆ diagram() [1/2]

template<typename ConfigT , typename DiagramT >
DiagramT & clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::diagram ( )
inline

Get diagram model reference.

Returns
Reference to diagram model created by the visitor

Definition at line 333 of file translation_unit_visitor.h.

333{ return diagram_; }

◆ diagram() [2/2]

template<typename ConfigT , typename DiagramT >
const DiagramT & clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::diagram ( ) const
inline

Get diagram model reference.

Returns
Reference to diagram model created by the visitor

Definition at line 340 of file translation_unit_visitor.h.

340{ return diagram_; }

◆ id_mapper()

template<typename ConfigT , typename DiagramT >
common::visitor::ast_id_mapper & clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::id_mapper ( ) const
inline

Get reference to Clang AST to clang-uml id mapper.

Returns
Reference to Clang AST to clang-uml id mapper

Definition at line 106 of file translation_unit_visitor.h.

106{ return id_mapper_; }

◆ process_comment() [1/2]

template<typename ConfigT , typename DiagramT >
void clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::process_comment ( const clang::NamedDecl &  decl,
clanguml::common::model::decorated_element e 
)
inline

Process comment directives in comment attached to a declaration.

Parameters
declReference to clang::NamedDecl
elementReference to element to be updated

Definition at line 249 of file translation_unit_visitor.h.

251 {
252 assert(comment_visitor_.get() != nullptr);
253
254 comment_visitor_->visit(decl, e);
255
256 auto *comment = decl.getASTContext().getRawCommentForDeclNoCache(&decl);
257
258 process_comment(comment, decl.getASTContext().getDiagnostics(), e);
259 }

◆ process_comment() [2/2]

template<typename ConfigT , typename DiagramT >
std::string clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::process_comment ( const clang::RawComment *  comment,
clang::DiagnosticsEngine &  de,
clanguml::common::model::decorated_element e 
)
inline

Process comment directives in raw comment.

Parameters
commentclang::RawComment pointer
deReference to clang::DiagnosticsEngine
elementReference to element to be updated
Returns
Comment with uml directives stripped from it

Definition at line 269 of file translation_unit_visitor.h.

272 {
273 if (comment == nullptr)
274 return {};
275
276 auto [it, inserted] = processed_comments().emplace(comment);
277
278 if (!inserted)
279 return {};
280
281 // Process clang-uml decorators in the comments
282 // TODO: Refactor to use standard block comments processable by
283 // clang comments
284 const auto &[decorators, stripped_comment] =
285 decorators::parse(comment->getFormattedText(source_manager(), de));
286
287 e.add_decorators(decorators);
288
289 return stripped_comment;
290 }

◆ processed_comments()

template<typename ConfigT , typename DiagramT >
std::set< const clang::RawComment * > & clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::processed_comments ( )
inlineprotected

Definition at line 350 of file translation_unit_visitor.h.

351 {
352 return processed_comments_;
353 }

◆ set_owning_module()

template<typename ConfigT , typename DiagramT >
void clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::set_owning_module ( const clang::Decl &  decl,
clanguml::common::model::element element 
)
inline

Definition at line 216 of file translation_unit_visitor.h.

218 {
219 if (const clang::Module *module = decl.getOwningModule();
220 module != nullptr) {
221 std::string module_name = module->Name;
222 bool is_private{false};
223#if LLVM_VERSION_MAJOR < 15
224 is_private = module->Kind ==
225 clang::Module::ModuleKind::PrivateModuleFragment;
226#else
227 is_private = module->isPrivateModule();
228#endif
229 if (is_private) {
230 // Clang just maps private modules names to "<private>"
231 module_name = module->getTopLevelModule()->Name;
232 }
233 element.set_module(module_name);
234 element.set_module_private(is_private);
235 }
236 }

◆ set_qualified_name()

template<typename ConfigT , typename DiagramT >
void clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::set_qualified_name ( const clang::NamedDecl &  decl,
clanguml::common::model::element element 
)
inline

Definition at line 145 of file translation_unit_visitor.h.

147 {
148 common::model::namespace_ ns{decl.getQualifiedNameAsString()};
149 element.set_name(ns.name());
150 ns.pop_back();
151 element.set_namespace(ns);
152 }

◆ set_source_location() [1/4]

template<typename ConfigT , typename DiagramT >
void clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::set_source_location ( const clang::Decl &  decl,
clanguml::common::model::source_location element 
)
inline

Set source location in diagram element.

Parameters
declReference to clang::Decl
elementReference to element to be updated

Definition at line 121 of file translation_unit_visitor.h.

123 {
124 set_source_location(decl.getLocation(), element);
125 }

◆ set_source_location() [2/4]

template<typename ConfigT , typename DiagramT >
void clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::set_source_location ( const clang::Expr &  expr,
clanguml::common::model::source_location element 
)
inline

Set source location in diagram element.

Parameters
exprReference to clang::Expr
elementReference to element to be updated

Definition at line 133 of file translation_unit_visitor.h.

135 {
136 set_source_location(expr.getBeginLoc(), element);
137 }

◆ set_source_location() [3/4]

template<typename ConfigT , typename DiagramT >
void clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::set_source_location ( const clang::SourceLocation &  location,
clanguml::common::model::source_location element 
)
inline

Set source location in diagram element.

Parameters
locationReference to clang::SourceLocation
elementReference to element to be updated

Definition at line 160 of file translation_unit_visitor.h.

162 {
163 namespace fs = std::filesystem;
164
165 std::string file;
166 unsigned line{};
167 unsigned column{};
168
169 if (location.isValid()) {
170 file = source_manager_.getFilename(location).str();
171 line = source_manager_.getSpellingLineNumber(location);
172 column = source_manager_.getSpellingColumnNumber(location);
173
174 if (file.empty()) {
175 // Why do I have to do this?
176 parse_source_location(location.printToString(source_manager()),
177 file, line, column);
178 }
179 }
180 else {
181 auto success = parse_source_location(
182 location.printToString(source_manager()), file, line, column);
183 if (!success) {
184 LOG_DBG("Failed to extract source location for element from {}",
185 location.printToString(source_manager_));
186 return;
187 }
188 }
189
190 // ensure the path is absolute
191 fs::path file_path{file};
192 if (!file_path.is_absolute()) {
193 file_path = fs::absolute(file_path);
194 }
195
196 file_path = fs::weakly_canonical(file_path);
197
198 file = file_path.string();
199
200 element.set_file(file);
201
202 if (util::is_relative_to(file_path, relative_to_path_)) {
204 fs::relative(element.file(), relative_to_path_).string()));
205 }
206 else {
207 element.set_file_relative("");
208 }
209
210 element.set_translation_unit(tu_path().string());
211 element.set_line(line);
212 element.set_column(column);
213 element.set_location_id(location.getHashValue());
214 }

◆ set_source_location() [4/4]

template<typename ConfigT , typename DiagramT >
void clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::set_source_location ( const clang::Stmt &  stmt,
clanguml::common::model::source_location element 
)
inline

Definition at line 139 of file translation_unit_visitor.h.

141 {
142 set_source_location(stmt.getBeginLoc(), element);
143 }

◆ set_tu_path()

template<typename ConfigT , typename DiagramT >
void clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::set_tu_path ( const std::string &  translation_unit_path)
inline

Definition at line 85 of file translation_unit_visitor.h.

86 {
87 translation_unit_path_ = relative(
88 std::filesystem::path{translation_unit_path}, relative_to_path_);
89 translation_unit_path_.make_preferred();
90 }

◆ should_include()

template<typename ConfigT , typename DiagramT >
bool clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::should_include ( const clang::NamedDecl *  decl) const
inline

Check if the diagram should include a declaration.

Parameters
declClang declaration.
Returns
True, if the entity should be included in the diagram.

Definition at line 305 of file translation_unit_visitor.h.

306 {
307 if (decl == nullptr)
308 return false;
309
310 if (skip_system_header_decl(decl))
311 return false;
312
313 if (config().filter_mode() == config::filter_mode_t::advanced)
314 return true;
315
316 auto should_include_namespace = diagram().should_include(
317 common::model::namespace_{decl->getQualifiedNameAsString()});
318
319 const auto decl_file =
320 decl->getLocation().printToString(source_manager());
321
322 const auto should_include_decl_file =
323 diagram().should_include(common::model::source_file{decl_file});
324
325 return should_include_namespace && should_include_decl_file;
326 }

◆ skip_system_header_decl()

template<typename ConfigT , typename DiagramT >
bool clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::skip_system_header_decl ( const clang::NamedDecl *  decl) const
inline

Definition at line 292 of file translation_unit_visitor.h.

293 {
294 return !config().include_system_headers() &&
295 source_manager().isInSystemHeader(
296 decl->getSourceRange().getBegin());
297 }

◆ source_manager()

template<typename ConfigT , typename DiagramT >
clang::SourceManager & clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::source_manager ( ) const
inline

Get clang::SourceManager.

Returns
Reference to clang::SourceManager used by this translation unit visitor

Definition at line 113 of file translation_unit_visitor.h.

113{ return source_manager_; }

◆ tu_path()

template<typename ConfigT , typename DiagramT >
const std::filesystem::path & clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::tu_path ( ) const
inline

Return relative path to current translation unit.

Returns
Current translation unit path

Definition at line 96 of file translation_unit_visitor.h.

97 {
99 }

Member Data Documentation

◆ comment_visitor_

template<typename ConfigT , typename DiagramT >
std::unique_ptr<comment::comment_visitor> clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::comment_visitor_
private

Definition at line 364 of file translation_unit_visitor.h.

◆ config_

template<typename ConfigT , typename DiagramT >
const ConfigT& clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::config_
private

Definition at line 360 of file translation_unit_visitor.h.

◆ diagram_

template<typename ConfigT , typename DiagramT >
DiagramT& clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::diagram_
private

Definition at line 357 of file translation_unit_visitor.h.

◆ id_mapper_

template<typename ConfigT , typename DiagramT >
common::visitor::ast_id_mapper clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::id_mapper_
mutableprivate

Definition at line 372 of file translation_unit_visitor.h.

◆ processed_comments_

template<typename ConfigT , typename DiagramT >
std::set<const clang::RawComment *> clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::processed_comments_
private

Definition at line 370 of file translation_unit_visitor.h.

◆ relative_to_path_

template<typename ConfigT , typename DiagramT >
std::filesystem::path clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::relative_to_path_
private

Definition at line 366 of file translation_unit_visitor.h.

◆ source_manager_

template<typename ConfigT , typename DiagramT >
clang::SourceManager& clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::source_manager_
private

Definition at line 362 of file translation_unit_visitor.h.

◆ translation_unit_path_

template<typename ConfigT , typename DiagramT >
std::filesystem::path clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::translation_unit_path_
private

Definition at line 368 of file translation_unit_visitor.h.


The documentation for this class was generated from the following file: