Skip to content

Commit f181b47

Browse files
committed
Rewrite parseArgs
1 parent 3a1f174 commit f181b47

File tree

1 file changed

+46
-35
lines changed

1 file changed

+46
-35
lines changed

lib/importproject.cpp

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -110,45 +110,56 @@ std::string ImportProject::collectArgs(const std::string &cmd, std::vector<std::
110110

111111
void ImportProject::parseArgs(FileSettings &fs, const std::vector<std::string> &args)
112112
{
113-
std::string defs;
114-
std::string optArg;
115-
auto argPtr = args.cbegin();
116-
117-
// Check for an option and extract its argument
118-
const auto getOptArg = [&](const std::string &name) {
119-
if (argPtr == args.cend())
120-
return false;
121-
if (!startsWith(*argPtr, name))
122-
return false;
123-
if (argPtr->size() == name.size()) {
124-
// Flag and argument are separated
125-
argPtr++;
126-
if (argPtr == args.cend())
127-
return false;
128-
optArg = *argPtr;
129-
} else {
130-
// Flag and argument are concatenated
131-
optArg = argPtr->substr(name.size());
132-
}
133-
return true;
113+
const auto getOptArg = [&args](std::initializer_list<std::string> optNames,
114+
std::size_t &i) {
115+
const auto &arg = args[i];
116+
const auto it = std::find_if(optNames.begin(),
117+
optNames.end(),
118+
[&arg] (const std::string &optName) {
119+
return startsWith(arg, optName);
120+
});
121+
if (it == optNames.end())
122+
return std::string();
123+
124+
const std::size_t optLen = it->size();
125+
if (arg.size() == optLen)
126+
return i++ >= args.size() ? std::string() : args[i];
127+
128+
return arg.substr(optLen);
134129
};
135130

136-
for (; argPtr != args.cend(); argPtr++) {
137-
// https://github.com/llvm/llvm-project/issues/172018
138-
// NOLINTBEGIN(bugprone-use-after-move)
139-
if (getOptArg("-I") || getOptArg("/I")) {
131+
std::string defs;
132+
for (std::size_t i = 0; i < args.size(); i++) {
133+
std::string optArg;
134+
135+
if (!(optArg = getOptArg({ "-I", "/I" }, i)).empty()) {
140136
if (std::none_of(fs.includePaths.cbegin(), fs.includePaths.cend(),
141137
[&](const std::string &path) { return path == optArg; }))
142138
fs.includePaths.push_back(std::move(optArg));
143-
} else if (getOptArg("-isystem")) {
139+
continue;
140+
}
141+
142+
if (!(optArg = getOptArg({ "-isystem" }, i)).empty()) {
144143
fs.systemIncludePaths.push_back(std::move(optArg));
145-
} else if (getOptArg("-D") || getOptArg("/D")) {
144+
continue;
145+
}
146+
147+
if (!(optArg = getOptArg({ "-D", "/D" }, i)).empty()) {
146148
defs += optArg + ";";
147-
} else if (getOptArg("-U") || getOptArg("/U")) {
149+
continue;
150+
}
151+
152+
if (!(optArg = getOptArg({ "-U", "/U" }, i)).empty()) {
148153
fs.undefs.insert(std::move(optArg));
149-
} else if (getOptArg("-std=") || getOptArg("/std:")) {
154+
continue;
155+
}
156+
157+
if (!(optArg = getOptArg({ "-std=", "/std:" }, i)).empty()) {
150158
fs.standard = std::move(optArg);
151-
} else if (getOptArg("-f")) {
159+
continue;
160+
}
161+
162+
if (!(optArg = getOptArg({ "-f" }, i)).empty()) {
152163
if (optArg == "pic")
153164
defs += "__pic__;";
154165
else if (optArg == "PIC")
@@ -157,14 +168,14 @@ void ImportProject::parseArgs(FileSettings &fs, const std::vector<std::string> &
157168
defs += "__pie__;";
158169
else if (optArg == "PIE")
159170
defs += "__PIE__;";
160-
} else if (getOptArg("-m")) {
171+
continue;
172+
}
173+
174+
if (!(optArg = getOptArg({ "-m" }, i)).empty()) {
161175
if (optArg == "unicode")
162176
defs += "UNICODE;";
177+
continue;
163178
}
164-
165-
if (argPtr == args.cend())
166-
break;
167-
// NOLINTEND(bugprone-use-after-move)
168179
}
169180

170181
fsSetDefines(fs, std::move(defs));

0 commit comments

Comments
 (0)