Skip to content

Commit f64f698

Browse files
committed
adjust command line arguments prior to running tool
1 parent cceb1c5 commit f64f698

File tree

4 files changed

+164
-21
lines changed

4 files changed

+164
-21
lines changed

source/AST/AbsoluteCompilationDatabase.cpp

Lines changed: 157 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,175 @@
99
// Official repository: https://github.com/cppalliance/mrdox
1010
//
1111

12+
#include "ConfigImpl.hpp"
1213
#include "Support/Debug.hpp"
1314
#include "Support/Path.hpp"
1415
#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>
1524
#include <llvm/Support/FileSystem.h>
1625
#include <llvm/Support/raw_ostream.h>
1726

1827
namespace clang {
1928
namespace mrdox {
2029

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+
21171
AbsoluteCompilationDatabase::
22172
AbsoluteCompilationDatabase(
23173
llvm::StringRef workingDir,
24-
CompilationDatabase const& inner)
174+
CompilationDatabase const& inner,
175+
std::shared_ptr<const Config> config)
25176
{
26177
namespace fs = llvm::sys::fs;
27178
namespace path = llvm::sys::path;
179+
auto config_impl = std::dynamic_pointer_cast<
180+
const ConfigImpl>(config);
28181

29182
auto allCommands = inner.getAllCompileCommands();
30183
AllCommands_.reserve(allCommands.size());
@@ -36,7 +189,9 @@ AbsoluteCompilationDatabase(
36189
cmd.CommandLine = cmd0.CommandLine;
37190
cmd.Heuristic = cmd0.Heuristic;
38191
cmd.Output = cmd0.Output;
39-
cmd.CommandLine = cmd0.CommandLine;
192+
cmd.CommandLine = adjustCommandLine(
193+
cmd0.CommandLine,
194+
config_impl->additionalDefines_);
40195

41196
if(path::is_absolute(cmd0.Directory))
42197
{

source/AST/AbsoluteCompilationDatabase.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#ifndef MRDOX_LIB_AST_ABSOLUTECOMPILATIONDATABASE_HPP
1212
#define MRDOX_LIB_AST_ABSOLUTECOMPILATIONDATABASE_HPP
1313

14+
#include <mrdox/Config.hpp>
1415
#include <clang/Tooling/JSONCompilationDatabase.h>
1516
#include <llvm/ADT/StringMap.h>
1617
#include <vector>
@@ -41,7 +42,8 @@ class AbsoluteCompilationDatabase
4142
*/
4243
AbsoluteCompilationDatabase(
4344
llvm::StringRef workingDir,
44-
CompilationDatabase const& inner);
45+
CompilationDatabase const& inner,
46+
std::shared_ptr<const Config> config);
4547

4648
std::vector<tooling::CompileCommand>
4749
getCompileCommands(

source/CorpusImpl.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104,29 +104,14 @@ build(
104104
auto config = std::dynamic_pointer_cast<ConfigImpl const>(config_);
105105
auto corpus = std::make_unique<CorpusImpl>(config);
106106

107-
// Build arguments adjuster
108-
tooling::ArgumentsAdjuster ArgAdjuster;
109-
{
110-
for(auto const& define : config->additionalDefines_)
111-
{
112-
std::string s;
113-
llvm::raw_string_ostream os(s);
114-
os << "-D" << define;
115-
ArgAdjuster = tooling::combineAdjusters(
116-
tooling::getInsertArgumentAdjuster(
117-
s.c_str(), tooling::ArgumentInsertPosition::END),
118-
ArgAdjuster);
119-
}
120-
}
121-
122107
// Traverse the AST for all translation units
123108
// and emit serializd bitcode into tool results.
124109
// This operation happens ona thread pool.
125110
if(corpus->config.verboseOutput)
126111
reportInfo("Mapping declarations");
127112
if(auto err = ex.execute(
128113
makeFrontendActionFactory(
129-
*ex.getExecutionContext(), *config), ArgAdjuster))
114+
*ex.getExecutionContext(), *config)))
130115
{
131116
if(! corpus->config.ignoreFailures)
132117
return toError(std::move(err));

source/GenerateAction.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ DoGenerateAction()
6262
path::remove_filename(workingDir);
6363

6464
// Convert relative paths to absolute
65-
AbsoluteCompilationDatabase compilations(workingDir, *jsonCompilations);
66-
65+
AbsoluteCompilationDatabase compilations(
66+
workingDir, *jsonCompilations, *config);
67+
6768
// Create the ToolExecutor from the compilation database
6869
int ThreadCount = 0;
6970
auto ex = std::make_unique<tooling::AllTUsToolExecutor>(compilations, ThreadCount);

0 commit comments

Comments
 (0)