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/include_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
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 >= 16
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 *Imported,
83 clang::SrcMgr::CharacteristicKind FileType) override;
84#elif LLVM_VERSION_MAJOR > 14
85 void InclusionDirective(clang::SourceLocation hash_loc,
86 const clang::Token &include_tok, clang::StringRef file_name,
87 bool is_angled, clang::CharSourceRange filename_range,
88 clang::Optional<clang::FileEntryRef> file,
89 clang::StringRef search_path, clang::StringRef relative_path,
90 const clang::Module *imported,
91 clang::SrcMgr::CharacteristicKind file_type) override;
92#else
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 const clang::FileEntry *file, clang::StringRef search_path,
97 clang::StringRef relative_path, const clang::Module *imported,
98 clang::SrcMgr::CharacteristicKind file_type) override;
99#endif
100
101 /**
102 * @brief Handle internal header include directive
103 *
104 * @param include_path Include path
105 * @param is_system True, if the path points to a system path
106 * @param current_file_id File id
107 */
108 void process_internal_header(const std::filesystem::path &include_path,
109 bool is_system, eid_t current_file_id);
110
111 /**
112 * @brief Handle system header include directive
113 *
114 * @param include_path Include path
115 * @param current_file_id File id
116 */
118 const std::filesystem::path &include_path, eid_t current_file_id);
119
120 /**
121 * @brief Handle a source file
122 *
123 * This method allows to process path of the currently visited
124 * source file.
125 *
126 * @param file Absolute path to a source file
127 * @return Diagram element id, in case the file was added to the diagram
128 */
129 std::optional<eid_t> process_source_file(
130 const std::filesystem::path &file);
131 };
132
133 /**
134 * @brief Constructor
135 *
136 * @param sm Reference to the source manager for current tu
137 * @param diagram Reference to the include diagram model
138 * @param config Reference to the diagram configuration
139 */
140 translation_unit_visitor(clang::SourceManager &sm,
143
144 /**
145 * @brief Get reference to the include diagram model
146 *
147 * @return Reference to the include diagram model
148 */
150
151 /**
152 * @brief Get reference to the diagram configuration
153 *
154 * @return Reference to the diagram configuration
155 */
157
158 /**
159 * @brief Run any finalization after traversal is complete
160 */
161 void finalize();
162
163private:
164 // Reference to the output diagram model
166
167 // Reference to class diagram config
169};
170} // namespace clanguml::include_diagram::visitor