0.5.4
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
clanguml::util::query_driver_output_extractor Class Reference

Executed compiler frontend and extract default system paths. More...

Detailed Description

Executed compiler frontend and extract default system paths.

This class - inspired by the clangd language server - will invoke the provided compiler command and query it for its default system paths, which then will be added to each compile command in the database.

Definition at line 33 of file query_driver_output_extractor.h.

#include <query_driver_output_extractor.h>

Public Member Functions

 query_driver_output_extractor (std::string command, std::string language)
 Constructor.
 
 ~query_driver_output_extractor ()=default
 
void execute ()
 Execute the command and extract compiler flags and include paths.
 
void extract_target (const std::string &output)
 Extract target name from the compiler output.
 
void extract_system_include_paths (const std::string &output)
 Extract system include paths from the compiler output.
 
const std::string & target () const
 Name of the target of the compiler command (e.g. x86_64-linux-gnu)
 
const std::vector< std::string > & system_include_paths () const
 Return list of include system paths.
 

Private Attributes

std::string command_
 
std::string language_
 
std::string target_
 
std::vector< std::string > system_include_paths_
 

Constructor & Destructor Documentation

◆ query_driver_output_extractor()

clanguml::util::query_driver_output_extractor::query_driver_output_extractor ( std::string  command,
std::string  language 
)

Constructor.

Parameters
commandCommand to execute the compiler frontend
languageLanguage name to query for (C or C++)

Definition at line 29 of file query_driver_output_extractor.cc.

31 : command_{std::move(command)}
32 , language_{std::move(language)}
33{
34}

◆ ~query_driver_output_extractor()

clanguml::util::query_driver_output_extractor::~query_driver_output_extractor ( )
default

Member Function Documentation

◆ execute()

void clanguml::util::query_driver_output_extractor::execute ( )

Execute the command and extract compiler flags and include paths.

Definition at line 36 of file query_driver_output_extractor.cc.

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}

◆ extract_system_include_paths()

void clanguml::util::query_driver_output_extractor::extract_system_include_paths ( const std::string &  output)

Extract system include paths from the compiler output.

Parameters
outputCompiler query driver output

Definition at line 74 of file query_driver_output_extractor.cc.

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}

◆ extract_target()

void clanguml::util::query_driver_output_extractor::extract_target ( const std::string &  output)

Extract target name from the compiler output.

Parameters
outputCompiler query driver output

Definition at line 60 of file query_driver_output_extractor.cc.

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}

◆ system_include_paths()

const std::vector< std::string > & clanguml::util::query_driver_output_extractor::system_include_paths ( ) const

Return list of include system paths.

Returns
List of include system paths

Definition at line 98 of file query_driver_output_extractor.cc.

99{
101}

◆ target()

const std::string & clanguml::util::query_driver_output_extractor::target ( ) const

Name of the target of the compiler command (e.g. x86_64-linux-gnu)

Returns
Target name

Definition at line 103 of file query_driver_output_extractor.cc.

104{
105 return target_;
106}

Member Data Documentation

◆ command_

std::string clanguml::util::query_driver_output_extractor::command_
private

Definition at line 79 of file query_driver_output_extractor.h.

◆ language_

std::string clanguml::util::query_driver_output_extractor::language_
private

Definition at line 80 of file query_driver_output_extractor.h.

◆ system_include_paths_

std::vector<std::string> clanguml::util::query_driver_output_extractor::system_include_paths_
private

Definition at line 82 of file query_driver_output_extractor.h.

◆ target_

std::string clanguml::util::query_driver_output_extractor::target_
private

Definition at line 81 of file query_driver_output_extractor.h.


The documentation for this class was generated from the following files: