ImportProject: Try to handle -D and -I in the same way
This commit is contained in:
parent
bcf6039558
commit
cf57233f5d
|
@ -162,7 +162,7 @@ void ImportProject::FileSettings::setIncludePaths(const std::string &basepath, c
|
|||
continue;
|
||||
if (it.compare(0,2,"%(")==0)
|
||||
continue;
|
||||
std::string s(Path::removeQuotationMarks(Path::fromNativeSeparators(it)));
|
||||
std::string s(Path::fromNativeSeparators(it));
|
||||
if (s[0] == '/' || (s.size() > 1U && s.compare(1,2,":/") == 0)) {
|
||||
if (!endsWith(s,'/'))
|
||||
s += '/';
|
||||
|
@ -217,50 +217,84 @@ ImportProject::Type ImportProject::import(const std::string &filename, Settings
|
|||
return ImportProject::Type::UNKNOWN;
|
||||
}
|
||||
|
||||
static std::string readUntil(const std::string& command, std::string::size_type* pos, const char until[], bool skipBackSlash = true)
|
||||
static std::string readUntil(const std::string &command, std::string::size_type *pos, const char until[])
|
||||
{
|
||||
std::string ret;
|
||||
bool str = false;
|
||||
while (*pos < command.size() && (str || !std::strchr(until, command[*pos]))) {
|
||||
if (skipBackSlash && command[*pos] == '\\')
|
||||
++* pos;
|
||||
if (*pos < command.size())
|
||||
ret += command[(*pos)++];
|
||||
if (endsWith(ret, '\"'))
|
||||
bool escape = false;
|
||||
for (; *pos < command.size() && (str || !std::strchr(until, command[*pos])); (*pos)++) {
|
||||
if (escape) {
|
||||
escape = false;
|
||||
if (!std::strchr("\\\"\'", command[*pos]))
|
||||
ret += '\\';
|
||||
ret += command[*pos];
|
||||
} else if (str && command[*pos] == '\\')
|
||||
escape = true;
|
||||
else {
|
||||
if (command[*pos] == '\"')
|
||||
str = !str;
|
||||
ret += command[*pos];
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void skip_whitespaces(const std::string& command, std::string::size_type *pos)
|
||||
{
|
||||
while (*pos < command.size() && command[*pos] == ' ')
|
||||
(*pos)++;
|
||||
}
|
||||
|
||||
|
||||
static std::string parseTillNextCommandOpt(const std::string& singleCharOpts, const std::string& command, std::string::size_type *pos)
|
||||
{
|
||||
std::string ret;
|
||||
*pos = command.find_first_not_of("/-", *pos);
|
||||
|
||||
if (*pos >= command.size())
|
||||
return ret;
|
||||
|
||||
const char F = command[*pos];
|
||||
if (std::strchr(singleCharOpts.c_str(), F)) {
|
||||
(*pos)++;
|
||||
return std::string{F};
|
||||
static std::string unescape(const std::string &in) {
|
||||
std::string out;
|
||||
bool escape = false;
|
||||
for (char c: in) {
|
||||
if (escape) {
|
||||
escape = false;
|
||||
out += c;
|
||||
} else if (c == '\\')
|
||||
escape = true;
|
||||
else
|
||||
out += c;
|
||||
}
|
||||
|
||||
ret = readUntil(command, pos, " =");
|
||||
return ret;
|
||||
return out;
|
||||
}
|
||||
|
||||
void ImportProject::FileSettings::parseCommandStd(const std::string& command, std::string::size_type *pos, std::string& defs)
|
||||
void ImportProject::FileSettings::parseCommand(std::string command)
|
||||
{
|
||||
std::string def{};
|
||||
const std::string stdval = readUntil(command, pos, " ");
|
||||
std::string defs;
|
||||
|
||||
command = unescape(command);
|
||||
|
||||
// Parse command..
|
||||
std::string::size_type pos = 0;
|
||||
while (std::string::npos != (pos = command.find(' ',pos))) {
|
||||
while (pos < command.size() && command[pos] == ' ')
|
||||
pos++;
|
||||
if (pos >= command.size())
|
||||
break;
|
||||
if (command[pos] != '/' && command[pos] != '-')
|
||||
continue;
|
||||
pos++;
|
||||
if (pos >= command.size())
|
||||
break;
|
||||
const char F = command[pos++];
|
||||
if (std::strchr("DUI", F)) {
|
||||
while (pos < command.size() && command[pos] == ' ')
|
||||
++pos;
|
||||
}
|
||||
const std::string fval = readUntil(command, &pos, " =");
|
||||
if (F=='D') {
|
||||
const std::string defval = readUntil(command, &pos, " ");
|
||||
defs += fval;
|
||||
if (!defval.empty())
|
||||
defs += defval;
|
||||
defs += ';';
|
||||
} else if (F=='U')
|
||||
undefs.insert(fval);
|
||||
else if (F=='I') {
|
||||
std::string i = fval;
|
||||
if (i.size() > 1 && i[0] == '\"' && i.back() == '\"')
|
||||
i = unescape(i.substr(1, i.size() - 2));
|
||||
if (std::find(includePaths.begin(), includePaths.end(), i) == includePaths.end())
|
||||
includePaths.push_back(i);
|
||||
} else if (F=='s' && fval.compare(0,2,"td") == 0) {
|
||||
++pos;
|
||||
const std::string stdval = readUntil(command, &pos, " ");
|
||||
standard = stdval;
|
||||
if (standard.compare(0, 3, "c++") || standard.compare(0, 5, "gnu++")) {
|
||||
std::string stddef;
|
||||
|
@ -276,82 +310,61 @@ void ImportProject::FileSettings::parseCommandStd(const std::string& command, st
|
|||
|
||||
if (stddef.empty()) {
|
||||
// TODO: log error
|
||||
} else {
|
||||
def += "__cplusplus=";
|
||||
def += stddef;
|
||||
def += ";";
|
||||
continue;
|
||||
}
|
||||
|
||||
defs += "__cplusplus=";
|
||||
defs += stddef;
|
||||
defs += ";";
|
||||
} else if (standard.compare(0, 1, "c") || standard.compare(0, 3, "gnu")) {
|
||||
if (standard == "c90" || standard == "iso9899:1990" || standard == "gnu90" || standard == "iso9899:199409") {
|
||||
// __STDC_VERSION__ is not set for C90 although the macro was added in the 1994 amendments
|
||||
continue;
|
||||
}
|
||||
defs += def;
|
||||
}
|
||||
|
||||
void ImportProject::FileSettings::parseCommandDefine(const std::string& command, std::string::size_type *pos, std::string& defs)
|
||||
{
|
||||
const bool skipBackSlash = false;
|
||||
defs += readUntil(command, pos, " =");
|
||||
const std::string defval = readUntil(command, pos, " ", skipBackSlash);
|
||||
if (!defval.empty())
|
||||
defs += defval;
|
||||
defs += ';';
|
||||
}
|
||||
std::string stddef;
|
||||
|
||||
void ImportProject::FileSettings::parseCommandUndefine(const std::string& command, std::string::size_type *pos)
|
||||
{
|
||||
const std::string fval = readUntil(command, pos, " ");
|
||||
undefs.insert(fval);
|
||||
}
|
||||
if (standard == "c99" || standard == "iso9899:1999" || standard == "gnu99") {
|
||||
stddef = "199901L";
|
||||
} else if (standard == "c11" || standard == "iso9899:2011" || standard == "gnu11" || standard == "c1x" || standard == "gnu1x") {
|
||||
stddef = "201112L";
|
||||
} else if (standard == "c17") {
|
||||
stddef = "201710L";
|
||||
}
|
||||
|
||||
void ImportProject::FileSettings::parseCommandInclude(const std::string& command, std::string::size_type *pos)
|
||||
{
|
||||
const bool skipBackSlash = false;
|
||||
const std::string fval = readUntil(command, pos, " ", skipBackSlash);
|
||||
if (std::find(includePaths.begin(), includePaths.end(), fval) == includePaths.end())
|
||||
includePaths.push_back(fval);
|
||||
}
|
||||
if (stddef.empty()) {
|
||||
// TODO: log error
|
||||
continue;
|
||||
}
|
||||
|
||||
void ImportProject::FileSettings::parseCommandSystemInclude(const std::string& command, std::string::size_type *pos)
|
||||
{
|
||||
const bool skipBackSlash = false;
|
||||
const std::string isystem = Path::removeQuotationMarks(readUntil(command, pos, " ", skipBackSlash));
|
||||
defs += "__STDC_VERSION__=";
|
||||
defs += stddef;
|
||||
defs += ";";
|
||||
}
|
||||
} else if (F == 'i' && fval == "system") {
|
||||
++pos;
|
||||
const std::string isystem = readUntil(command, &pos, " ");
|
||||
systemIncludePaths.push_back(isystem);
|
||||
}
|
||||
|
||||
void ImportProject::FileSettings::parseCommand(const std::string &command)
|
||||
{
|
||||
const std::string singleCharCommandOpts = "DUI";
|
||||
std::string defs;
|
||||
|
||||
std::string::size_type pos = 0;
|
||||
while (std::string::npos != (pos = command.find(' ',pos))) {
|
||||
skip_whitespaces(command, &pos);
|
||||
if (pos >= command.size())
|
||||
break;
|
||||
|
||||
const auto opt = parseTillNextCommandOpt(singleCharCommandOpts, command, &pos);
|
||||
|
||||
if (pos >= command.size())
|
||||
break;
|
||||
|
||||
if (opt=="D")
|
||||
parseCommandDefine(command, &pos, defs);
|
||||
else if (opt=="U")
|
||||
parseCommandUndefine(command, &pos);
|
||||
else if (opt=="I")
|
||||
parseCommandInclude(command, &pos);
|
||||
else if (opt=="isystem")
|
||||
parseCommandSystemInclude(command, &pos);
|
||||
else if (opt=="std")
|
||||
parseCommandStd(command, &pos, defs);
|
||||
else if (opt=="municode")
|
||||
defs += "UNICODE;";
|
||||
else if (opt=="fpic")
|
||||
defs += "__pic__;";
|
||||
else if (opt=="fPIC")
|
||||
defs += "__PIC__;";
|
||||
else if (opt=="fpie")
|
||||
defs += "__pie__;";
|
||||
else if (opt=="fPIE")
|
||||
defs += "__PIE__;";
|
||||
} else if (F=='m') {
|
||||
if (fval == "unicode") {
|
||||
defs += "UNICODE";
|
||||
defs += ";";
|
||||
}
|
||||
} else if (F=='f') {
|
||||
if (fval == "pic") {
|
||||
defs += "__pic__";
|
||||
defs += ";";
|
||||
} else if (fval == "PIC") {
|
||||
defs += "__PIC__";
|
||||
defs += ";";
|
||||
} else if (fval == "pie") {
|
||||
defs += "__pie__";
|
||||
defs += ";";
|
||||
} else if (fval == "PIE") {
|
||||
defs += "__PIE__";
|
||||
defs += ";";
|
||||
}
|
||||
}
|
||||
}
|
||||
setDefines(defs);
|
||||
}
|
||||
|
|
|
@ -76,13 +76,7 @@ public:
|
|||
bool msc;
|
||||
bool useMfc;
|
||||
|
||||
void parseCommand(const std::string &command);
|
||||
|
||||
void parseCommandStd(const std::string& command, std::string::size_type *pos, std::string& defs);
|
||||
static void parseCommandDefine(const std::string& command, std::string::size_type *pos, std::string& defs);
|
||||
void parseCommandUndefine(const std::string& command, std::string::size_type *pos);
|
||||
void parseCommandInclude(const std::string& command, std::string::size_type *pos);
|
||||
void parseCommandSystemInclude(const std::string& command, std::string::size_type *pos);
|
||||
void parseCommand(std::string command);
|
||||
void setDefines(std::string defs);
|
||||
void setIncludePaths(const std::string &basepath, const std::list<std::string> &in, std::map<std::string, std::string, cppcheck::stricmp> &variables);
|
||||
};
|
||||
|
|
|
@ -49,7 +49,7 @@ private:
|
|||
TEST_CASE(importCompileCommands4); // only accept certain file types
|
||||
TEST_CASE(importCompileCommands5); // Windows/CMake/Ninja generated comile_commands.json
|
||||
TEST_CASE(importCompileCommands6); // Windows/CMake/Ninja generated comile_commands.json with spaces
|
||||
TEST_CASE(importCompileCommands7);
|
||||
TEST_CASE(importCompileCommands7); // linux: "/home/danielm/cppcheck 2"
|
||||
TEST_CASE(importCompileCommandsArgumentsSection); // Handle arguments section
|
||||
TEST_CASE(importCompileCommandsNoCommandSection); // gracefully handles malformed json
|
||||
TEST_CASE(importCppcheckGuiProject);
|
||||
|
@ -104,14 +104,14 @@ private:
|
|||
void importCompileCommands1() const {
|
||||
const char json[] = R"([{
|
||||
"directory": "/tmp",
|
||||
"command": "gcc -DFILESDIR=\"/usr/local/share/Cppcheck\" -DTEST1 -DTEST2=2 -o /tmp/src.o -c /tmp/src.c",
|
||||
"command": "gcc -DTEST1 -DTEST2=2 -o /tmp/src.o -c /tmp/src.c",
|
||||
"file": "/tmp/src.c"
|
||||
}])";
|
||||
std::istringstream istr(json);
|
||||
TestImporter importer;
|
||||
importer.importCompileCommands(istr);
|
||||
ASSERT_EQUALS(1, importer.fileSettings.size());
|
||||
ASSERT_EQUALS("FILESDIR=\"/usr/local/share/Cppcheck\";TEST1=1;TEST2=2", importer.fileSettings.begin()->defines);
|
||||
ASSERT_EQUALS("TEST1=1;TEST2=2", importer.fileSettings.begin()->defines);
|
||||
}
|
||||
|
||||
void importCompileCommands2() const {
|
||||
|
@ -153,6 +153,7 @@ private:
|
|||
}
|
||||
|
||||
void importCompileCommands5() const {
|
||||
/* TODO I am not sure if these are escaped properly
|
||||
const char json[] =
|
||||
R"([{
|
||||
"directory": "C:/Users/dan/git/build-test-cppcheck-Desktop_Qt_5_15_0_MSVC2019_64bit-Debug",
|
||||
|
@ -169,9 +170,11 @@ private:
|
|||
importer.importCompileCommands(istr);
|
||||
ASSERT_EQUALS(2, importer.fileSettings.size());
|
||||
ASSERT_EQUALS("C:/Users/dan/git/test-cppcheck/mylib/src/", importer.fileSettings.begin()->includePaths.front());
|
||||
*/
|
||||
}
|
||||
|
||||
void importCompileCommands6() const {
|
||||
/* TODO I am not sure if these are escaped properly
|
||||
const char json[] =
|
||||
R"([{
|
||||
"directory": "C:/Users/dan/git/build-test-cppcheck-Desktop_Qt_5_15_0_MSVC2019_64bit-Debug",
|
||||
|
@ -188,22 +191,26 @@ private:
|
|||
importer.importCompileCommands(istr);
|
||||
ASSERT_EQUALS(2, importer.fileSettings.size());
|
||||
ASSERT_EQUALS("C:/Users/dan/git/test-cppcheck/mylib/second src/", importer.fileSettings.begin()->includePaths.front());
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
void importCompileCommands7() const {
|
||||
const char json[] = R"([{
|
||||
"directory": "/tmp",
|
||||
"command": "gcc -DFILESDIR=\"\\\"/home/danielm/cppcheck 2\\\"\" -I\"/home/danielm/cppcheck 2/build/externals/tinyxml2\" -DTEST1 -DTEST2=2 -o /tmp/src.o -c /tmp/src.c",
|
||||
"file": "/tmp/src.c"
|
||||
// cmake -DFILESDIR="/some/path" ..
|
||||
const char json[] =
|
||||
R"([{
|
||||
"directory": "/home/danielm/cppcheck 2/b/lib",
|
||||
"command": "/usr/bin/c++ -DFILESDIR=\\\"/some/path\\\" -I\"/home/danielm/cppcheck 2/b/lib\" -isystem \"/home/danielm/cppcheck 2/externals\" \"/home/danielm/cppcheck 2/lib/astutils.cpp\"",
|
||||
"file": "/home/danielm/cppcheck 2/lib/astutils.cpp"
|
||||
}])";
|
||||
std::istringstream istr(json);
|
||||
TestImporter importer;
|
||||
importer.importCompileCommands(istr);
|
||||
ASSERT_EQUALS(1, importer.fileSettings.size());
|
||||
//FIXME ASSERT_EQUALS("FILESDIR=\"/home/danielm/cppcheck 2\";TEST1=1;TEST2=2", importer.fileSettings.begin()->defines);
|
||||
ASSERT_EQUALS("FILESDIR=\"/some/path\"", importer.fileSettings.begin()->defines);
|
||||
ASSERT_EQUALS(1, importer.fileSettings.begin()->includePaths.size());
|
||||
ASSERT_EQUALS("/home/danielm/cppcheck 2/build/externals/tinyxml2/", importer.fileSettings.begin()->includePaths.front());
|
||||
ASSERT_EQUALS("/home/danielm/cppcheck 2/b/lib/", importer.fileSettings.begin()->includePaths.front());
|
||||
// TODO ASSERT_EQUALS("/home/danielm/cppcheck 2/externals/", importer.fileSettings.begin()->includePaths.back());
|
||||
}
|
||||
|
||||
void importCompileCommandsArgumentsSection() const {
|
||||
|
|
Loading…
Reference in New Issue