0.5.4
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
compilation_database.h
Go to the documentation of this file.
1/**
2 * @file src/common/compilation_database.h
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#pragma once
19
20#include "common/model/enums.h"
23#include "config/config.h"
24#include "types.h"
25#include "util/error.h"
26#include "util/util.h"
27
28#include <clang/Frontend/CompilerInstance.h>
29#include <clang/Tooling/CompilationDatabase.h>
30#include <clang/Tooling/Tooling.h>
31
32#include <deque>
33#include <filesystem>
34#include <string>
35
36namespace clanguml::common {
37
38/**
39 * @brief Custom compilation database class
40 *
41 * This class provides custom specialization of Clang's
42 * [CompilationDatabase](https://clang.llvm.org/doxygen/classclang_1_1tooling_1_1CompilationDatabase.html),
43 * which provides the possibility of adjusting the compilation flags after
44 * they have been loaded from the `compile_commands.json` file.
45 *
46 * @embed{compilation_database_context_class.svg}
47 */
48class compilation_database : public clang::tooling::CompilationDatabase {
49public:
51 std::unique_ptr<clang::tooling::CompilationDatabase> base,
52 const clanguml::config::config &cfg);
53
54 ~compilation_database() override = default;
55
56 /**
57 * Loads the compilation database from directory specified on command
58 * line or in the configuration file.
59 *
60 * @param cfg Reference to config instance
61 * @return Instance of compilation_database.
62 */
63 static std::unique_ptr<compilation_database> auto_detect_from_directory(
64 const clanguml::config::config &cfg);
65
66 /**
67 * Retrieves and adjusts compilation commands from the database, for
68 * a given translation unit.
69 *
70 * @return List of adjusted compile commands.
71 */
72 std::vector<clang::tooling::CompileCommand> getCompileCommands(
73 clang::StringRef FilePath) const override;
74
75 /**
76 * Returns all files in the database.
77 *
78 * @return List of all files in compilation database.
79 */
80 std::vector<std::string> getAllFiles() const override;
81
82 /**
83 * Retrieves and adjusts all compilation commands from the database.
84 *
85 * @return List of adjusted compile commands.
86 */
87 std::vector<clang::tooling::CompileCommand>
88 getAllCompileCommands() const override;
89
90 /**
91 * Returns reference to clanguml's config instance.
92 *
93 * @return Reference to config instance.
94 */
95 const clanguml::config::config &config() const;
96
97 /**
98 * Returns reference to CompilationDatabase as was loaded from file.
99 *
100 * @return Reference to CompilationDatabase.
101 */
102 const clang::tooling::CompilationDatabase &base() const;
103
104 std::string guess_language_from_filename(const std::string &filename) const;
105
106 long count_matching_commands(const std::vector<std::string> &files) const;
107
108private:
110 std::vector<clang::tooling::CompileCommand> &commands) const;
111
112 /*!
113 * Pointer to the Clang's original compilation database.
114 *
115 * Actual instance of the compilation database is stored in here.
116 * The inheritance is just to keep the interface.
117 */
118 std::unique_ptr<clang::tooling::CompilationDatabase> base_;
119
120 /*!
121 * Reference to the instance of clanguml config.
122 */
124};
125
126using compilation_database_ptr = std::unique_ptr<compilation_database>;
127
128} // namespace clanguml::common