0.6.0
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
clang_tool.h
Go to the documentation of this file.
1/**
2 * @file src/common/generators/clang_tool.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 <clang/Tooling/Tooling.h>
21
22#include "common/clang_utils.h"
25
26namespace clanguml::generators {
27
28using namespace clang;
29using namespace clang::tooling;
30
31struct diagnostic {
32 clang::DiagnosticsEngine::Level level{
33 clang::DiagnosticsEngine::Level::Ignored};
34 std::string description;
35 std::optional<clanguml::common::model::source_location> location;
36};
37
38std::string to_string(const diagnostic &d);
39
40void to_json(nlohmann::json &j, const diagnostic &a);
41
43public:
45 std::vector<diagnostic> d,
46 std::string description = "Clang failed to parse sources.");
47
48 std::vector<diagnostic> diagnostics;
49};
50
51class diagnostic_consumer : public clang::DiagnosticConsumer {
52public:
53 diagnostic_consumer(std::filesystem::path relative_to);
54
56 DiagnosticsEngine::Level diag_level, const Diagnostic &info) override;
57
58 /** If true, at least one of the diagnostics represents an error */
59 bool failed{false};
60
61 /** List of all diagnostics collected for a given TU */
62 std::vector<diagnostic> diagnostics;
63
64private:
65 std::filesystem::path relative_to_;
66};
67
68/**
69 * @brief Custom ClangTool implementation to enable better error handling
70 */
72public:
73 clang_tool(common::model::diagram_t diagram_type, std::string diagram_name,
75 const std::vector<std::string> &source_paths,
76 std::filesystem::path relative_to, bool quiet);
77
79
80 void append_arguments_adjuster(clang::tooling::ArgumentsAdjuster Adjuster);
81
82 void run(ToolAction *Action);
83
84private:
86 const std::string diagram_name_;
88 std::vector<std::string> source_paths_;
89 bool quiet_;
90
91 std::shared_ptr<PCHContainerOperations> pch_container_ops_;
92
93 llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> overlay_fs_;
94 llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> inmemory_fs_;
95 llvm::IntrusiveRefCntPtr<FileManager> files_;
96
97 std::vector<
98 std::pair<StringRef /* file_name */, StringRef /* file_contents */>>
100
102
103 ArgumentsAdjuster args_adjuster_;
104
105 std::unique_ptr<diagnostic_consumer> diag_consumer_;
106 llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> diag_opts_;
107};
108} // namespace clanguml::generators
109
110namespace clang {
111std::string to_string(clang::DiagnosticsEngine::Level level);
112} // namespace clang
113
114MAKE_TO_STRING_FORMATTER(clang::DiagnosticsEngine::Level)