Skip to content

Commit 94f6927

Browse files
committed
Add ImportProject::parseArgs()
1 parent aca52d9 commit 94f6927

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

lib/importproject.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,65 @@ std::string ImportProject::collectArgs(const std::string &cmd, std::vector<std::
120120
return "";
121121
}
122122

123+
void ImportProject::parseArgs(FileSettings &fs, const std::vector<std::string> &args)
124+
{
125+
std::string defs;
126+
std::string optArg;
127+
auto argPtr = args.cbegin();
128+
129+
// Check for an option and extract its argument
130+
const auto getOptArg = [&](const std::string &name) {
131+
if (argPtr == args.cend())
132+
return false;
133+
if (!startsWith(*argPtr, name))
134+
return false;
135+
if (argPtr->size() == name.size()) {
136+
// Flag and argument are separated
137+
argPtr++;
138+
if (argPtr == args.cend())
139+
return false;
140+
optArg = *argPtr;
141+
} else {
142+
// Flag and argument are concatenated
143+
optArg = argPtr->substr(name.size());
144+
}
145+
return true;
146+
};
147+
148+
for (; argPtr != args.cend(); argPtr++) {
149+
optArg = "";
150+
if (getOptArg("-I") || getOptArg("/I")) {
151+
if (std::find(fs.includePaths.cbegin(), fs.includePaths.cend(), optArg) == fs.includePaths.cend())
152+
fs.includePaths.push_back(optArg);
153+
} else if (getOptArg("-isystem")) {
154+
fs.systemIncludePaths.push_back(std::move(optArg));
155+
} else if (getOptArg("-D") || getOptArg("/D")) {
156+
defs += optArg + ";";
157+
} else if (getOptArg("-U") || getOptArg("/U")) {
158+
fs.undefs.insert(optArg);
159+
} else if (getOptArg("-std=") || getOptArg("/std:")) {
160+
fs.standard = optArg;
161+
} else if (getOptArg("-f")) {
162+
if (optArg == "pic")
163+
defs += "__pic__;";
164+
else if (optArg == "PIC")
165+
defs += "__PIC__;";
166+
else if (optArg == "pie")
167+
defs += "__pie__;";
168+
else if (optArg == "PIE")
169+
defs += "__PIE__;";
170+
} else if (getOptArg("-m")) {
171+
if (optArg == "unicode")
172+
defs += "UNICODE;";
173+
}
174+
175+
if (argPtr == args.cend())
176+
break;
177+
}
178+
179+
fsSetDefines(fs, std::move(defs));
180+
}
181+
123182
void ImportProject::ignorePaths(const std::vector<std::string> &ipaths, bool debug)
124183
{
125184
PathMatch matcher(ipaths, Path::getCurrentPath());

lib/importproject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class CPPCHECKLIB WARN_UNUSED ImportProject {
104104
bool importCompileCommands(std::istream &istr);
105105
bool importCppcheckGuiProject(std::istream &istr, Settings &settings, Suppressions &supprs);
106106
static std::string collectArgs(const std::string &cmd, std::vector<std::string> &args);
107+
static void parseArgs(FileSettings &fs, const std::vector<std::string> &args);
107108

108109
private:
109110
struct SharedItemsProject {

0 commit comments

Comments
 (0)