| 360 | } |
| 361 | |
| 362 | bool ImportProject::importCompileCommands(std::istream &istr) |
| 363 | { |
| 364 | picojson::value compileCommands; |
| 365 | istr >> compileCommands; |
| 366 | if (!compileCommands.is<picojson::array>()) { |
| 367 | errors.emplace_back("compilation database is not a JSON array"); |
| 368 | return false; |
| 369 | } |
| 370 | |
| 371 | std::map<std::string, std::size_t> fsFileIds; |
| 372 | |
| 373 | for (const picojson::value &fileInfo : compileCommands.get<picojson::array>()) { |
| 374 | picojson::object obj = fileInfo.get<picojson::object>(); |
| 375 | |
| 376 | if (obj.count("directory") == 0) { |
| 377 | errors.emplace_back("'directory' field in compilation database entry missing"); |
| 378 | return false; |
| 379 | } |
| 380 | |
| 381 | if (!obj["directory"].is<std::string>()) { |
| 382 | errors.emplace_back("'directory' field in compilation database entry is not a string"); |
| 383 | return false; |
| 384 | } |
| 385 | |
| 386 | std::string dirpath = Path::fromNativeSeparators(obj["directory"].get<std::string>()); |
| 387 | |
| 388 | /* CMAKE produces the directory without trailing / so add it if not |
| 389 | * there - it is needed by setIncludePaths() */ |
| 390 | if (!endsWith(dirpath, '/')) |
| 391 | dirpath += '/'; |
| 392 | |
| 393 | const std::string directory = std::move(dirpath); |
| 394 | |
| 395 | std::vector<std::string> arguments; |
| 396 | if (obj.count("arguments")) { |
| 397 | if (obj["arguments"].is<picojson::array>()) { |
| 398 | for (const picojson::value& arg : obj["arguments"].get<picojson::array>()) { |
| 399 | if (arg.is<std::string>()) |
| 400 | arguments.push_back(arg.get<std::string>()); |
| 401 | } |
| 402 | } else { |
| 403 | errors.emplace_back("'arguments' field in compilation database entry is not a JSON array"); |
| 404 | return false; |
| 405 | } |
| 406 | } else if (obj.count("command")) { |
| 407 | std::string command; |
| 408 | if (obj["command"].is<std::string>()) { |
| 409 | command = obj["command"].get<std::string>(); |
| 410 | } else { |
| 411 | errors.emplace_back("'command' field in compilation database entry is not a string"); |
| 412 | return false; |
| 413 | } |
| 414 | |
| 415 | std::string error = collectArgs(command, arguments); |
| 416 | if (!error.empty()) { |
| 417 | errors.emplace_back(error); |
| 418 | return false; |
| 419 | } |