-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathCompileCommand.cpp
164 lines (141 loc) · 5.62 KB
/
CompileCommand.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. All rights reserved.
*/
#include "CompileCommand.h"
#include "BaseCommand.h"
#include "Paths.h"
#include "printers/CCJsonPrinter.h"
#include "utils/StringUtils.h"
#include "loguru.h"
#include <algorithm>
#include <iterator>
#include <set>
#include <utility>
namespace utbot {
// See https://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html
static const std::unordered_set<std::string> gccSpecificOptions = {
"-std", "-fpermitted-flt-eval-methods", "-fopenacc-dim", "-fopenacc-kernels", "-fsso-struct"
};
static const std::unordered_set<std::string> gccSpecificFlags = {
"-ansi", "-fgnu89-inline",
"-fpermitted-flt-eval-methods",
"-fallow-parameterless-variadic-functions",
"-fno-asm",
"-fno-builtin",
"-fno-builtin-function", "-fgimple",
"-fhosted",
"-ffreestanding",
"-fopenacc",
"-fopenmp", "-fopenmp-simd",
"-fms-extensions", "-fplan9-extensions",
"-fallow-single-precision", "-fcond-mismatch", "-flax-vector-conversions",
"-fsigned-bitfields", "-fsigned-char",
"-funsigned-bitfields", "-funsigned-char"
};
CompileCommand::CompileCommand(CompileCommand const &other) : BaseCommand(other) {
compiler = commandLine.begin();
sourcePath =
std::next(commandLine.begin(),
std::distance<const_iterator>(other.commandLine.begin(), other.sourcePath));
output = std::next(commandLine.begin(),
std::distance<const_iterator>(other.commandLine.begin(), other.output));
}
CompileCommand::CompileCommand(CompileCommand &&other) noexcept : BaseCommand(std::move(other)),
sourcePath(other.sourcePath),
compiler(other.compiler),
output(other.output) {
}
CompileCommand &CompileCommand::operator=(const CompileCommand &other) {
if (this == &other) {
return *this;
}
CompileCommand tmp(other);
swap(*this, tmp);
return *this;
}
CompileCommand &CompileCommand::operator=(CompileCommand &&other) noexcept {
if (this == &other) {
return *this;
}
CompileCommand tmp(std::move(other));
swap(*this, tmp);
return *this;
}
CompileCommand::CompileCommand(std::vector<std::string> arguments,
fs::path directory,
fs::path sourcePath)
: BaseCommand(std::move(arguments), std::move(directory)) {
compiler = commandLine.begin();
{
auto it = std::find_if(commandLine.begin(), commandLine.end(), [&sourcePath](std::string const &arg) {
return fs::path(arg).filename() == sourcePath.filename();
});
this->sourcePath = it;
*this->sourcePath = sourcePath;
}
{
auto it = findOutput();
if (it != commandLine.end()) {
this->output = it;
*this->output = Paths::getCCJsonFileFullPath(*it, this->directory);
} else {
auto path = Paths::getCCJsonFileFullPath(Paths::replaceExtension(*this->sourcePath, ".o"), this->directory);
this->output = std::next(addFlagsToBegin({ "-o", path }));
}
}
}
void swap(CompileCommand &a, CompileCommand &b) noexcept {
std::swap(a.directory, b.directory);
std::swap(a.commandLine, b.commandLine);
std::swap(a.environmentVariables, b.environmentVariables);
std::swap(a.optimizationLevel, b.optimizationLevel);
std::swap(a.sourcePath, b.sourcePath);
std::swap(a.compiler, b.compiler);
std::swap(a.output, b.output);
}
fs::path CompileCommand::getSourcePath() const {
return *sourcePath;
}
void CompileCommand::setSourcePath(fs::path sourcePath) {
*(this->sourcePath) = std::move(sourcePath);
}
fs::path CompileCommand::getCompiler() const {
return *compiler;
}
void CompileCommand::setCompiler(fs::path compiler) {
*(this->compiler) = std::move(compiler);
}
fs::path CompileCommand::getOutput() const {
return *output;
}
bool CompileCommand::isArchiveCommand() const {
return false;
}
void CompileCommand::setOutput(fs::path output) {
*(this->output) = std::move(output);
}
void CompileCommand::removeGccFlags() {
CollectionUtils::erase(commandLine, "--coverage");
CollectionUtils::erase(commandLine, "-fprofile-dir=.");
}
void CompileCommand::filterCFlags() {
size_t erased = CollectionUtils::erase_if(commandLine, [](std::string const &arg) {
size_t pos = arg.find('=');
if (pos != std::string::npos) {
return CollectionUtils::contains(gccSpecificOptions, arg.substr(0, pos));
}
return CollectionUtils::contains(gccSpecificFlags, arg);
});
LOG_S(DEBUG) << erased << " C specific flags erased from compile arguments";
}
void CompileCommand::removeIncludeFlags() {
CollectionUtils::erase_if(commandLine, [](const std::string &arg) {
return StringUtils::startsWith(arg, "-I");
});
}
void CompileCommand::removeWerror() {
CollectionUtils::erase_if(commandLine, [](const std::string &arg) {
return StringUtils::startsWith(arg, "-Werror");
});
}
}