Skip to content

Commit bf813d0

Browse files
committed
Add ImportProject::parseArgs()
1 parent c3421ea commit bf813d0

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

lib/importproject.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,64 @@ std::string ImportProject::collectArgs(const std::string &cmd, std::vector<std::
108108
return "";
109109
}
110110

111+
void ImportProject::parseArgs(FileSettings &fs, const std::vector<std::string> &args)
112+
{
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;
134+
};
135+
136+
for (; argPtr != args.cend(); argPtr++) {
137+
if (getOptArg("-I") || getOptArg("/I")) {
138+
if (std::find(fs.includePaths.cbegin(), fs.includePaths.cend(), optArg) == fs.includePaths.cend())
139+
fs.includePaths.push_back(std::move(optArg));
140+
} else if (getOptArg("-isystem")) {
141+
fs.systemIncludePaths.push_back(std::move(optArg));
142+
} else if (getOptArg("-D") || getOptArg("/D")) {
143+
defs += optArg + ";";
144+
} else if (getOptArg("-U") || getOptArg("/U")) {
145+
fs.undefs.insert(std::move(optArg));
146+
} else if (getOptArg("-std=") || getOptArg("/std:")) {
147+
fs.standard = std::move(optArg);
148+
} else if (getOptArg("-f")) {
149+
if (optArg == "pic")
150+
defs += "__pic__;";
151+
else if (optArg == "PIC")
152+
defs += "__PIC__;";
153+
else if (optArg == "pie")
154+
defs += "__pie__;";
155+
else if (optArg == "PIE")
156+
defs += "__PIE__;";
157+
} else if (getOptArg("-m")) {
158+
if (optArg == "unicode")
159+
defs += "UNICODE;";
160+
}
161+
162+
if (argPtr == args.cend())
163+
break;
164+
}
165+
166+
fsSetDefines(fs, std::move(defs));
167+
}
168+
111169
void ImportProject::ignorePaths(const std::vector<std::string> &ipaths, bool debug)
112170
{
113171
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)