0.6.0
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
main.cc
Go to the documentation of this file.
1/**
2 * @file src/main.cc
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
19#include "cli/cli_handler.h"
23#include "util/util.h"
24
25#ifdef ENABLE_BACKWARD_CPP
26#define BACKWARD_HAS_DW 1
27#define BACKWARD_HAS_LIBUNWIND 1
28#include <backward-cpp/backward.hpp>
29#endif
30
31#include <cli11/CLI11.hpp>
32#include <spdlog/spdlog.h>
33
34#include <cstring>
35
36#ifdef ENABLE_BACKWARD_CPP
37namespace backward {
38backward::SignalHandling sh; // NOLINT
39} // namespace backward
40#endif
41
42using namespace clanguml;
43
44int main(int argc, const char *argv[])
45{
47
48 try {
49 auto res = cli.handle_options(argc, argv);
50
51 if (res == cli::cli_flow_t::kExit)
52 return 0;
53
54 if (res == cli::cli_flow_t::kError)
55 return 1;
56
57#if !defined(NDEBUG)
58 // Catch invalid logger message formats, e.g. missing arguments
59 spdlog::set_error_handler([](const std::string & /*msg*/) {
60 assert(0 == 1); // NOLINT
61 });
62#endif
63
64 const auto db =
66 cli.config);
67
68 const auto compilation_database_files = db->getAllFiles();
69
70 std::map<std::string /* diagram name */,
71 std::vector<std::string> /* translation units */>
72 translation_units_map;
73
74 // We have to generate the translation units list for each diagram
75 // before scheduling tasks, because std::filesystem::current_path
76 // cannot be trusted with multiple threads
78 cli.diagram_names, cli.config, compilation_database_files,
79 translation_units_map);
80
81 if (cli.progress) {
82 // llvm::errs() output stream mangles the stdout stream , we need
83 // to close it here so that it doesn't interfere with the rendering
84 // of progress indicators
85 llvm::errs().close();
86 }
87
89 cli.config, db, cli.get_runtime_config(), translation_units_map);
90 }
92 if (clanguml::logging::logger_type() == logging::logger_type_t::text) {
93 fmt::println(
94 "ERROR: Failed to load compilation database from {} due to: {}",
95 cli.config.compilation_database_dir(), e.what());
96 }
97 else {
98 LOG_ERROR("Failed to load compilation database from {} due to: {}",
99 cli.config.compilation_database_dir(), e.what());
100 }
101 return 1;
102 }
104 if (clanguml::logging::logger_type() == logging::logger_type_t::text) {
105 fmt::println(
106 "ERROR: Querying provided compiler driver {} did not provide "
107 "any "
108 "paths, please make sure the path is correct and that your "
109 "compiler is GCC-compatible: {}",
110 cli.config.query_driver(), e.what());
111 }
112 else {
113 LOG_ERROR(
114 "Querying provided compiler driver {} did not provide any "
115 "paths, please make sure the path is correct and that your "
116 "compiler is GCC-compatible: {}",
117 cli.config.query_driver(), e.what());
118 }
119 return 1;
120 }
122 if (clanguml::logging::logger_type() == logging::logger_type_t::text)
123 fmt::println("ERROR: {}", e.what());
124 else
125 LOG_ERROR("{}", e.what());
126 return 1;
127 }
128 catch (std::exception &e) {
129 if (clanguml::logging::logger_type() == logging::logger_type_t::text)
130 fmt::println("ERROR: {}", e.what());
131 else
132 LOG_ERROR("{}", e.what());
133 return 1;
134 }
135
136 return 0;
137}