0.6.0
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 ()
 
std::string get_file_path (const std::string &file_location) const
 

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 189 of file translation_unit_visitor.h.

191 {
192 }

◆ 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 298 of file translation_unit_visitor.h.

298{ 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 284 of file translation_unit_visitor.h.

284{ 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 291 of file translation_unit_visitor.h.

291{ return diagram_; }

◆ get_file_path()

template<typename ConfigT , typename DiagramT >
std::string clanguml::common::visitor::translation_unit_visitor< ConfigT, DiagramT >::get_file_path ( const std::string &  file_location) const
inlineprotected

Definition at line 306 of file translation_unit_visitor.h.

307 {
308 std::string file_path;
309 unsigned line{};
310 unsigned column{};
311
313 file_location, file_path, line, column))
314 return file_path;
315
316 return file_location;
317 }

◆ 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 200 of file translation_unit_visitor.h.

202 {
203 assert(comment_visitor_.get() != nullptr);
204
205 comment_visitor_->visit(decl, e);
206
207 auto *comment = decl.getASTContext().getRawCommentForDeclNoCache(&decl);
208
209 process_comment(comment, decl.getASTContext().getDiagnostics(), e);
210 }

◆ 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 220 of file translation_unit_visitor.h.

223 {
224 if (comment == nullptr)
225 return {};
226
227 auto [it, inserted] = processed_comments().emplace(comment);
228
229 if (!inserted)
230 return {};
231
232 // Process clang-uml decorators in the comments
233 // TODO: Refactor to use standard block comments processable by
234 // clang comments
235 const auto &[decorators, stripped_comment] =
236 decorators::parse(comment->getFormattedText(source_manager(), de));
237
238 e.add_decorators(decorators);
239
240 return stripped_comment;
241 }

◆ 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 301 of file translation_unit_visitor.h.

302 {
303 return processed_comments_;
304 }

◆ 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 167 of file translation_unit_visitor.h.

169 {
170 if (const clang::Module *module = decl.getOwningModule();
171 module != nullptr) {
172 std::string module_name = module->Name;
173 bool is_private{false};
174#if LLVM_VERSION_MAJOR < 15
175 is_private = module->Kind ==
176 clang::Module::ModuleKind::PrivateModuleFragment;
177#else
178 is_private = module->isPrivateModule();
179#endif
180 if (is_private) {
181 // Clang just maps private modules names to "<private>"
182 module_name = module->getTopLevelModule()->Name;
183 }
184 element.set_module(module_name);
185 element.set_module_private(is_private);
186 }
187 }

◆ 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 {
164 source_manager(), location, element, tu_path(), relative_to_path_);
165 }

◆ 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 256 of file translation_unit_visitor.h.

257 {
258 if (decl == nullptr)
259 return false;
260
261 if (skip_system_header_decl(decl))
262 return false;
263
264 if (config().filter_mode() == config::filter_mode_t::advanced)
265 return true;
266
267 auto should_include_namespace = diagram().should_include(
268 common::model::namespace_{decl->getQualifiedNameAsString()});
269
270 const auto decl_file =
271 decl->getLocation().printToString(source_manager());
272
273 const auto should_include_decl_file = diagram().should_include(
274 common::model::source_file{get_file_path(decl_file)});
275
276 return should_include_namespace && should_include_decl_file;
277 }

◆ 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 243 of file translation_unit_visitor.h.

244 {
245 return !config().include_system_headers() &&
246 source_manager().isInSystemHeader(
247 decl->getSourceRange().getBegin());
248 }

◆ 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 328 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 324 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 321 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 336 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 334 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 330 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 326 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 332 of file translation_unit_visitor.h.


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