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/include_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
20#include "common/model/enums.h"
23#include "config/config.h"
25
26#include <clang/AST/RecursiveASTVisitor.h>
27#include <clang/Basic/SourceManager.h>
28#include <clang/Lex/PPCallbacks.h>
29
30#include <functional>
31#include <map>
32#include <memory>
33#include <set>
34#include <string>
35
37
39
43
44/**
45 * @brief Include diagram translation unit visitor wrapper
46 *
47 * This class implements the @link clang::RecursiveASTVisitor interface,
48 * for compatibility with other diagram visitors. However, for include
49 * diagrams this class does not inherit from
50 * @ref common::visitor::translation_unit_visitor, instead it contains an
51 * inner class @ref include_visitor, which implements `clang::PPCallbacks`
52 * interface to handle inclusion directives.
53 */
55 : public clang::RecursiveASTVisitor<translation_unit_visitor> {
56public:
57 /**
58 * This is an internal class for convenience to be able to access the
59 * include_visitor type from translation_unit_visitor type
60 */
61 class include_visitor : public clang::PPCallbacks,
63 public:
64 /**
65 * @brief Constructor.
66 *
67 * @param sm Reference to current tu source manager.
68 * @param diagram Reference to the include diagram model.
69 * @param config Reference to the diagram configuration.
70 */
71 include_visitor(clang::SourceManager &sm,
74
75 ~include_visitor() override = default;
76
77#if LLVM_VERSION_MAJOR >= 19
78 void InclusionDirective(clang::SourceLocation HashLoc,
79 const clang::Token &IncludeTok, clang::StringRef FileName,
80 bool IsAngled, clang::CharSourceRange FilenameRange,
81 clang::OptionalFileEntryRef File, clang::StringRef SearchPath,
82 clang::StringRef RelativePath, const clang::Module *SuggestedModule,
83 bool ModuleImported,
84 clang::SrcMgr::CharacteristicKind FileType) override;
85#elif LLVM_VERSION_MAJOR >= 16
86 void InclusionDirective(clang::SourceLocation HashLoc,
87 const clang::Token &IncludeTok, clang::StringRef FileName,
88 bool IsAngled, clang::CharSourceRange FilenameRange,
89 clang::OptionalFileEntryRef File, clang::StringRef SearchPath,
90 clang::StringRef RelativePath, const clang::Module *Imported,
91 clang::SrcMgr::CharacteristicKind FileType) override;
92#elif LLVM_VERSION_MAJOR > 14
93 void InclusionDirective(clang::SourceLocation hash_loc,
94 const clang::Token &include_tok, clang::StringRef file_name,
95 bool is_angled, clang::CharSourceRange filename_range,
96 clang::Optional<clang::FileEntryRef> file,
97 clang::StringRef search_path, clang::StringRef relative_path,
98 const clang::Module *imported,
99 clang::SrcMgr::CharacteristicKind file_type) override;
100#else
101 void InclusionDirective(clang::SourceLocation hash_loc,
102 const clang::Token &include_tok, clang::StringRef file_name,
103 bool is_angled, clang::CharSourceRange filename_range,
104 const clang::FileEntry *file, clang::StringRef search_path,
105 clang::StringRef relative_path, const clang::Module *imported,
106 clang::SrcMgr::CharacteristicKind file_type) override;
107#endif
108
109 /**
110 * @brief Handle internal header include directive
111 *
112 * @param include_path Include path
113 * @param is_system True, if the path points to a system path
114 * @param current_file_id File id
115 */
116 void process_internal_header(const std::filesystem::path &include_path,
117 bool is_system, eid_t current_file_id);
118
119 /**
120 * @brief Handle system header include directive
121 *
122 * @param include_path Include path
123 * @param current_file_id File id
124 */
126 const std::filesystem::path &include_path, eid_t current_file_id);
127
128 /**
129 * @brief Handle a source file
130 *
131 * This method allows to process path of the currently visited
132 * source file.
133 *
134 * @param file Absolute path to a source file
135 * @return Diagram element id, in case the file was added to the diagram
136 */
137 std::optional<eid_t> process_source_file(
138 const std::filesystem::path &file);
139 };
140
141 /**
142 * @brief Constructor
143 *
144 * @param sm Reference to the source manager for current tu
145 * @param diagram Reference to the include diagram model
146 * @param config Reference to the diagram configuration
147 */
148 translation_unit_visitor(clang::SourceManager &sm,
151
152 /**
153 * @brief Get reference to the include diagram model
154 *
155 * @return Reference to the include diagram model
156 */
158
159 /**
160 * @brief Get reference to the diagram configuration
161 *
162 * @return Reference to the diagram configuration
163 */
165
166 /**
167 * @brief Run any finalization after traversal is complete
168 */
169 void finalize();
170
171private:
172 // Reference to the output diagram model
174
175 // Reference to class diagram config
177};
178} // namespace clanguml::include_diagram::visitor