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

Custom compilation database class. More...

Detailed Description

Custom compilation database class.

This class provides custom specialization of Clang's CompilationDatabase, which provides the possibility of adjusting the compilation flags after they have been loaded from the compile_commands.json file.

Definition at line 48 of file compilation_database.h.

#include <compilation_database.h>

Public Member Functions

 compilation_database (std::unique_ptr< clang::tooling::CompilationDatabase > base, const clanguml::config::config &cfg, bool is_fixed)
 
 ~compilation_database () override=default
 
std::vector< clang::tooling::CompileCommand > getCompileCommands (clang::StringRef FilePath) const override
 
std::vector< std::string > getAllFiles () const override
 
std::vector< clang::tooling::CompileCommand > getAllCompileCommands () const override
 
const clanguml::config::configconfig () const
 
const clang::tooling::CompilationDatabase & base () const
 
std::string guess_language_from_filename (const std::string &filename) const
 
size_t count_matching_commands (const std::vector< std::string > &files) const
 
bool is_fixed () const
 Determines whether this is a fixed or regular compilation database.
 

Static Public Member Functions

static std::unique_ptr< compilation_databaseauto_detect_from_directory (const clanguml::config::config &cfg)
 

Private Member Functions

bool match_filename (const clang::tooling::CompileCommand &command, const std::string &file) const
 
void adjust_compilation_database (std::vector< clang::tooling::CompileCommand > &commands) const
 

Private Attributes

std::unique_ptr< clang::tooling::CompilationDatabase > base_
 
const clanguml::config::configconfig_
 
bool is_fixed_
 

Constructor & Destructor Documentation

◆ compilation_database()

clanguml::common::compilation_database::compilation_database ( std::unique_ptr< clang::tooling::CompilationDatabase >  base,
const clanguml::config::config cfg,
bool  is_fixed 
)

Definition at line 50 of file compilation_database.cc.

53 : base_{std::move(base)}
54 , config_{cfg}
56{
57}

◆ ~compilation_database()

clanguml::common::compilation_database::~compilation_database ( )
overridedefault

Member Function Documentation

◆ adjust_compilation_database()

void clanguml::common::compilation_database::adjust_compilation_database ( std::vector< clang::tooling::CompileCommand > &  commands) const
private

Definition at line 120 of file compilation_database.cc.

122{
123#if !defined(_WIN32)
124 if (config().query_driver && !config().query_driver().empty()) {
125 for (auto &compile_command : commands) {
126 auto argv0 = config().query_driver() == "."
127 ? compile_command.CommandLine.at(0)
128 : config().query_driver();
129
130 util::query_driver_output_extractor extractor{
131 argv0, guess_language_from_filename(compile_command.Filename)};
132
133 extractor.execute();
134
135 std::vector<std::string> system_header_args;
136 for (const auto &path : extractor.system_include_paths()) {
137 system_header_args.emplace_back("-isystem");
138 system_header_args.emplace_back(path);
139 }
140
141 compile_command.CommandLine.insert(
142 compile_command.CommandLine.begin() + 1,
143 system_header_args.begin(), system_header_args.end());
144
145 if (!extractor.target().empty()) {
146 compile_command.CommandLine.insert(
147 compile_command.CommandLine.begin() + 1,
148 fmt::format("--target={}", extractor.target()));
149 }
150 }
151 }
152#endif
153
154 if (config().add_compile_flags && !config().add_compile_flags().empty()) {
155 for (auto &compile_command : commands) {
156 compile_command.CommandLine.insert(
157 // Add flags after argv[0]
158 compile_command.CommandLine.begin() + 1,
159 config().add_compile_flags().begin(),
160 config().add_compile_flags().end());
161 }
162 }
163
164 if (config().remove_compile_flags &&
165 !config().remove_compile_flags().empty()) {
166 for (auto &compile_command : commands) {
167 for (const auto &flag : config().remove_compile_flags()) {
168 util::erase_if(compile_command.CommandLine,
169 [&flag](const auto &arg) { return flag == arg; });
170 }
171 }
172 }
173}

◆ auto_detect_from_directory()

std::unique_ptr< compilation_database > clanguml::common::compilation_database::auto_detect_from_directory ( const clanguml::config::config cfg)
static

Loads the compilation database from directory specified on command line or in the configuration file.

Parameters
cfgReference to config instance
Returns
Instance of compilation_database.

Definition at line 26 of file compilation_database.cc.

28{
29 std::string error_message;
30 auto res = clang::tooling::CompilationDatabase::autoDetectFromDirectory(
31 cfg.compilation_database_dir(), error_message);
32
33 if (!error_message.empty())
34 throw error::compilation_database_error(error_message);
35
36 if (res.get() == nullptr)
37 throw error::compilation_database_error(fmt::format(
38 "Autodetection of compilation database from directory '{}' failed",
40
41 // This is a workaround to determine whether Clang loaded a fixed
42 // compilation database or a proper one. This cannot be done with
43 // dynamic_cast if RTTI was not enabled in the LLVM build
44 bool is_fixed{res->getAllFiles().empty()};
45
46 return std::make_unique<compilation_database>(
47 std::move(res), cfg, is_fixed);
48}

◆ base()

const clang::tooling::CompilationDatabase & clanguml::common::compilation_database::base ( ) const

Returns reference to CompilationDatabase as was loaded from file.

Returns
Reference to CompilationDatabase.

Definition at line 66 of file compilation_database.cc.

67{
68 return *base_;
69}

◆ config()

const clanguml::config::config & clanguml::common::compilation_database::config ( ) const

Returns reference to clanguml's config instance.

Returns
Reference to config instance.

Definition at line 61 of file compilation_database.cc.

62{
63 return config_;
64}

◆ count_matching_commands()

size_t clanguml::common::compilation_database::count_matching_commands ( const std::vector< std::string > &  files) const

Definition at line 105 of file compilation_database.cc.

107{
108 if (is_fixed())
109 return files.size();
110
111 auto result{0UL};
112
113 for (const auto &f : files) {
114 result += getCompileCommands(f).size();
115 }
116
117 return result;
118}

◆ getAllCompileCommands()

std::vector< clang::tooling::CompileCommand > clanguml::common::compilation_database::getAllCompileCommands ( ) const
override

Retrieves and adjusts all compilation commands from the database.

Returns
List of adjusted compile commands.

Definition at line 87 of file compilation_database.cc.

88{
89 auto commands = base().getAllCompileCommands();
90
92
93 return commands;
94}

◆ getAllFiles()

std::vector< std::string > clanguml::common::compilation_database::getAllFiles ( ) const
override

Returns all files in the database.

Returns
List of all files in compilation database.

Definition at line 71 of file compilation_database.cc.

72{
73 return base().getAllFiles();
74}

◆ getCompileCommands()

std::vector< clang::tooling::CompileCommand > clanguml::common::compilation_database::getCompileCommands ( clang::StringRef  FilePath) const
override

Retrieves and adjusts compilation commands from the database, for a given translation unit.

Returns
List of adjusted compile commands.

Definition at line 77 of file compilation_database.cc.

78{
79 auto commands = base().getCompileCommands(FilePath);
80
82
83 return commands;
84}

◆ guess_language_from_filename()

std::string clanguml::common::compilation_database::guess_language_from_filename ( const std::string &  filename) const

Definition at line 96 of file compilation_database.cc.

98{
99 if (util::ends_with(filename, std::string{".c"}))
100 return "c";
101
102 return "c++";
103}

◆ is_fixed()

bool clanguml::common::compilation_database::is_fixed ( ) const

Determines whether this is a fixed or regular compilation database.

Fixed compilation database doesn't have any compilation commands attached to specific source files, just arguments that should be applied to any file.

Returns
True, if this is a fixed compilation database

Definition at line 59 of file compilation_database.cc.

59{ return is_fixed_; }

◆ match_filename()

bool clanguml::common::compilation_database::match_filename ( const clang::tooling::CompileCommand &  command,
const std::string &  file 
) const
private

Definition at line 175 of file compilation_database.cc.

178{
179 auto command_filename = std::filesystem::path{command.Filename};
180
181 if (!command_filename.is_absolute()) {
182 command_filename = config().root_directory() / command_filename;
183 }
184
185 return (command_filename == file) ||
186 (command_filename.lexically_normal().string() == file);
187}

Member Data Documentation

◆ base_

std::unique_ptr<clang::tooling::CompilationDatabase> clanguml::common::compilation_database::base_
private

Pointer to the Clang's original compilation database.

Actual instance of the compilation database is stored in here. The inheritance is just to keep the interface.

Definition at line 132 of file compilation_database.h.

◆ config_

const clanguml::config::config& clanguml::common::compilation_database::config_
private

Reference to the instance of clanguml config.

Definition at line 137 of file compilation_database.h.

◆ is_fixed_

bool clanguml::common::compilation_database::is_fixed_
private

True, if this is a fixed compilation database, e.g. loaded from compile_flags.txt

Definition at line 143 of file compilation_database.h.


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