9
9
// Official repository: https://github.com/cppalliance/mrdox
10
10
//
11
11
12
+ #include " ConfigImpl.hpp"
12
13
#include " Support/Debug.hpp"
13
14
#include " Support/Path.hpp"
14
15
#include " AST/AbsoluteCompilationDatabase.hpp"
16
+ #include < fmt/format.h>
17
+ #include < clang/Basic/LangStandard.h>
18
+ #include < clang/Driver/Driver.h>
19
+ #include < clang/Driver/Options.h>
20
+ #include < clang/Driver/Types.h>
21
+ #include < llvm/Option/ArgList.h>
22
+ #include < llvm/Option/OptSpecifier.h>
23
+ #include < llvm/Option/OptTable.h>
15
24
#include < llvm/Support/FileSystem.h>
16
25
#include < llvm/Support/raw_ostream.h>
17
26
18
27
namespace clang {
19
28
namespace mrdox {
20
29
30
+ template <typename ... Opts>
31
+ static
32
+ bool
33
+ optionMatchesAny (
34
+ const llvm::opt::Option& opt,
35
+ Opts&&... opts)
36
+ {
37
+ return (opt.matches (opts) || ...);
38
+ }
39
+
40
+ static
41
+ std::vector<std::string>
42
+ adjustCommandLine (
43
+ const std::vector<std::string>& cmdline,
44
+ const std::vector<std::string>& additional_defines)
45
+ {
46
+ std::vector<std::string> new_cmdline;
47
+ std::vector<std::string> discarded_cmdline;
48
+ llvm::opt::InputArgList args;
49
+ StringRef driver_mode;
50
+ if (! cmdline.empty ())
51
+ {
52
+ std::vector<const char *> raw_cmdline;
53
+ raw_cmdline.reserve (cmdline.size ());
54
+ for (const auto & s : cmdline)
55
+ raw_cmdline.push_back (s.c_str ());
56
+ args = llvm::opt::InputArgList (raw_cmdline.data (),
57
+ raw_cmdline.data () + raw_cmdline.size ());
58
+ driver_mode = driver::getDriverMode (
59
+ raw_cmdline.front (), raw_cmdline);
60
+ new_cmdline.push_back (cmdline.front ());
61
+ }
62
+ const llvm::opt::OptTable& opts_table =
63
+ clang::driver::getDriverOptTable ();
64
+
65
+ bool is_clang_cl = ! cmdline.empty () &&
66
+ driver::IsClangCL (driver_mode);
67
+ int included_flags = 0 ;
68
+ int excluded_flags = 0 ;
69
+ if (is_clang_cl)
70
+ {
71
+ // suppress all warnings
72
+ new_cmdline.emplace_back (" /w" );
73
+ included_flags =
74
+ driver::options::CoreOption |
75
+ driver::options::CLOption |
76
+ driver::options::CLDXCOption;
77
+ }
78
+ else
79
+ {
80
+ // suppress all warnings
81
+ new_cmdline.emplace_back (" -w" );
82
+ excluded_flags =
83
+ driver::options::CLOption |
84
+ driver::options::CLDXCOption;
85
+ }
86
+ new_cmdline.emplace_back (" -fsyntax-only" );
87
+
88
+ for (const auto & def : additional_defines)
89
+ new_cmdline.emplace_back (fmt::format (" -D{}" , def));
90
+
91
+ for (unsigned idx = 1 ; idx < cmdline.size ();)
92
+ {
93
+ const unsigned old_idx = idx;
94
+ std::unique_ptr<llvm::opt::Arg> arg =
95
+ opts_table.ParseOneArg (args, idx,
96
+ included_flags, excluded_flags);
97
+
98
+ if (! arg)
99
+ {
100
+ discarded_cmdline.insert (
101
+ discarded_cmdline.end (),
102
+ cmdline.begin () + old_idx,
103
+ cmdline.begin () + idx);
104
+ continue ;
105
+ }
106
+
107
+ const llvm::opt::Option opt =
108
+ arg->getOption ().getUnaliasedOption ();
109
+
110
+ // discard the option if it affects warnings,
111
+ // is ignored, or turns warnings into errors
112
+ if (optionMatchesAny (opt,
113
+ // unknown options
114
+ driver::options::OPT_UNKNOWN,
115
+ // diagnostic options
116
+ driver::options::OPT_Diag_Group,
117
+ driver::options::OPT_W_value_Group,
118
+ driver::options::OPT__SLASH_wd,
119
+ // language conformance options
120
+ driver::options::OPT_pedantic_Group,
121
+ driver::options::OPT__SLASH_permissive,
122
+ driver::options::OPT__SLASH_permissive_,
123
+
124
+ // ignored options
125
+ driver::options::OPT_cl_ignored_Group,
126
+ driver::options::OPT_cl_ignored_Group,
127
+ driver::options::OPT_clang_ignored_f_Group,
128
+ driver::options::OPT_clang_ignored_gcc_optimization_f_Group,
129
+ driver::options::OPT_clang_ignored_legacy_options_Group,
130
+ driver::options::OPT_clang_ignored_m_Group,
131
+ driver::options::OPT_flang_ignored_w_Group
132
+ #if 0
133
+ // input file options
134
+ driver::options::OPT_INPUT,
135
+ // output file options
136
+ driver::options::OPT_o,
137
+ driver::options::OPT__SLASH_o,
138
+ driver::options::OPT__SLASH_Fo,
139
+ driver::options::OPT__SLASH_Fe,
140
+ driver::options::OPT__SLASH_Fd,
141
+ driver::options::OPT__SLASH_FA,
142
+ driver::options::OPT__SLASH_Fa,
143
+ driver::options::OPT__SLASH_Fi,
144
+ driver::options::OPT__SLASH_FR,
145
+ driver::options::OPT__SLASH_Fr,
146
+ driver::options::OPT__SLASH_Fm,
147
+ driver::options::OPT__SLASH_Fx,
148
+ #endif
149
+ // driver::options::OPT__SLASH_TP
150
+ // driver::options::OPT__SLASH_Tp
151
+ // driver::options::OPT__SLASH_TC
152
+ // driver::options::OPT__SLASH_Tc
153
+ ))
154
+ {
155
+ discarded_cmdline.insert (
156
+ discarded_cmdline.end (),
157
+ cmdline.begin () + old_idx,
158
+ cmdline.begin () + idx);
159
+ continue ;
160
+ }
161
+
162
+ new_cmdline.insert (
163
+ new_cmdline.end (),
164
+ cmdline.begin () + old_idx,
165
+ cmdline.begin () + idx);
166
+ }
167
+
168
+ return new_cmdline;
169
+ }
170
+
21
171
AbsoluteCompilationDatabase::
22
172
AbsoluteCompilationDatabase (
23
173
llvm::StringRef workingDir,
24
- CompilationDatabase const & inner)
174
+ CompilationDatabase const & inner,
175
+ std::shared_ptr<const Config> config)
25
176
{
26
177
namespace fs = llvm::sys::fs;
27
178
namespace path = llvm::sys::path;
179
+ auto config_impl = std::dynamic_pointer_cast<
180
+ const ConfigImpl>(config);
28
181
29
182
auto allCommands = inner.getAllCompileCommands ();
30
183
AllCommands_.reserve (allCommands.size ());
@@ -36,7 +189,9 @@ AbsoluteCompilationDatabase(
36
189
cmd.CommandLine = cmd0.CommandLine ;
37
190
cmd.Heuristic = cmd0.Heuristic ;
38
191
cmd.Output = cmd0.Output ;
39
- cmd.CommandLine = cmd0.CommandLine ;
192
+ cmd.CommandLine = adjustCommandLine (
193
+ cmd0.CommandLine ,
194
+ config_impl->additionalDefines_ );
40
195
41
196
if (path::is_absolute (cmd0.Directory ))
42
197
{
0 commit comments