Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions lib/importproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ static std::string unescape(const std::string &in)
return out;
}

void ImportProject::fsParseCommand(FileSettings& fs, const std::string& command)
void ImportProject::fsParseCommand(FileSettings& fs, const std::string& command, bool doUnescape)
{
std::string defs;

Expand All @@ -281,10 +281,12 @@ void ImportProject::fsParseCommand(FileSettings& fs, const std::string& command)
if (F=='D') {
std::string defval = readUntil(command, &pos, " ");
defs += fval;
if (defval.size() >= 3 && startsWith(defval,"=\"") && defval.back()=='\"')
defval = "=" + unescape(defval.substr(2, defval.size() - 3));
else if (defval.size() >= 5 && startsWith(defval, "=\\\"") && endsWith(defval, "\\\""))
defval = "=\"" + unescape(defval.substr(3, defval.size() - 5)) + "\"";
if (doUnescape) {
if (defval.size() >= 3 && startsWith(defval,"=\"") && defval.back()=='\"')
defval = "=" + unescape(defval.substr(2, defval.size() - 3));
else if (defval.size() >= 5 && startsWith(defval, "=\\\"") && endsWith(defval, "\\\""))
defval = "=\"" + unescape(defval.substr(3, defval.size() - 5)) + "\"";
}
if (!defval.empty())
defs += defval;
defs += ';';
Expand Down Expand Up @@ -362,8 +364,10 @@ bool ImportProject::importCompileCommands(std::istream &istr)

const std::string directory = std::move(dirpath);

bool doUnescape = false;
std::string command;
if (obj.count("arguments")) {
doUnescape = false;
if (obj["arguments"].is<picojson::array>()) {
for (const picojson::value& arg : obj["arguments"].get<picojson::array>()) {
if (arg.is<std::string>()) {
Expand All @@ -378,6 +382,7 @@ bool ImportProject::importCompileCommands(std::istream &istr)
return false;
}
} else if (obj.count("command")) {
doUnescape = true;
if (obj["command"].is<std::string>()) {
command = obj["command"].get<std::string>();
} else {
Expand Down Expand Up @@ -413,7 +418,7 @@ bool ImportProject::importCompileCommands(std::istream &istr)
else
path = Path::simplifyPath(directory + file);
FileSettings fs{path, Standards::Language::None, 0}; // file will be identified later on
fsParseCommand(fs, command); // read settings; -D, -I, -U, -std, -m*, -f*
fsParseCommand(fs, command, doUnescape); // read settings; -D, -I, -U, -std, -m*, -f*
std::map<std::string, std::string, cppcheck::stricmp> variables;
fsSetIncludePaths(fs, directory, fs.includePaths, variables);
// Assign a unique index to each file path. If the file path already exists in the map,
Expand Down
2 changes: 1 addition & 1 deletion lib/importproject.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class CPPCHECKLIB WARN_UNUSED ImportProject {
CPPCHECK_GUI
};

static void fsParseCommand(FileSettings& fs, const std::string& command);
static void fsParseCommand(FileSettings& fs, const std::string& command, bool doUnescape);
static void fsSetDefines(FileSettings& fs, std::string defs);
static void fsSetIncludePaths(FileSettings& fs, const std::string &basepath, const std::list<std::string> &in, std::map<std::string, std::string, cppcheck::stricmp> &variables);

Expand Down
24 changes: 24 additions & 0 deletions test/testimportproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class TestImportProject : public TestFixture {
TEST_CASE(importCompileCommands11); // include path order
TEST_CASE(importCompileCommands12); // #13040: "directory" is parent directory, relative include paths
TEST_CASE(importCompileCommands13); // #13333: duplicate file entries
TEST_CASE(importCompileCommands14); // #14156
TEST_CASE(importCompileCommandsArgumentsSection); // Handle arguments section
TEST_CASE(importCompileCommandsNoCommandSection); // gracefully handles malformed json
TEST_CASE(importCompileCommandsDirectoryMissing); // 'directory' field missing
Expand Down Expand Up @@ -365,6 +366,29 @@ class TestImportProject : public TestFixture {
ASSERT_EQUALS(1, fs2.fileIndex);
}

void importCompileCommands14() const { // #14156
REDIRECT;
constexpr char json[] =
R"([{
"arguments": [
"/usr/bin/g++",
"-DTFS_LINUX_MODULE_NAME=\"tfs_linux\"",
"-g",
"-c",
"cli/main.cpp"
],
"directory": "/home/daniel/cppcheck",
"file": "/home/daniel/cppcheck/cli/main.cpp",
"output": "/home/daniel/cppcheck/cli/main.o"
}])";
std::istringstream istr(json);
TestImporter importer;
ASSERT_EQUALS(true, importer.importCompileCommands(istr));
ASSERT_EQUALS(1, importer.fileSettings.size());
const FileSettings &fs = importer.fileSettings.front();
ASSERT_EQUALS("TFS_LINUX_MODULE_NAME=\"tfs_linux\"", fs.defines);
}

void importCompileCommandsArgumentsSection() const {
REDIRECT;
constexpr char json[] = "[ { \"directory\": \"/tmp/\","
Expand Down