0.5.4
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
query_driver_output_extractor.cc
Go to the documentation of this file.
1/**
2 * @file src/util/query_driver_include_extractor.cc
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
20
21#include "error.h"
22#include "util.h"
23
24#include <sstream>
25#include <string>
26
27namespace clanguml::util {
28
30 std::string command, std::string language)
31 : command_{std::move(command)}
32 , language_{std::move(language)}
33{
34}
35
37{
38 auto cmd =
39 fmt::format("{} -E -v -x {} /dev/null 2>&1", command_, language_);
40
41 LOG_DBG("Executing query driver command: {}", cmd);
42
43 auto driver_output = get_process_output(cmd);
44
46 extract_system_include_paths(driver_output);
47 extract_target(driver_output);
48
49 if (system_include_paths_.empty()) {
50 throw error::query_driver_no_paths(fmt::format(
51 "Compiler driver {} did not report any system include paths "
52 "in its output: {}",
53 command_, driver_output));
54 }
55
56 LOG_DBG("Extracted the following paths from compiler driver: {}",
57 fmt::join(system_include_paths_, ","));
58}
59
60void query_driver_output_extractor::extract_target(const std::string &output)
61{
62 std::istringstream f(output);
63 std::string line;
64
65 while (std::getline(f, line)) {
66 line = trim(line);
67 if (util::starts_with(line, std::string{"Target: "})) {
68 target_ = line.substr(strlen("Target: "));
69 break;
70 }
71 }
72}
73
75 const std::string &output)
76{
77 std::istringstream f(output);
78 std::string line;
79
80 bool in_include_paths_range{false};
81 while (std::getline(f, line)) {
82 line = trim(line);
83 if (line == "#include <...> search starts here:") {
84 in_include_paths_range = true;
85 continue;
86 }
87 if (line == "End of search list.") {
88 break;
89 }
90
91 if (in_include_paths_range) {
92 system_include_paths_.emplace_back(line);
93 }
94 }
95}
96
97const std::vector<std::string> &
99{
101}
102
104{
105 return target_;
106}
107} // namespace clanguml::util