16366efedSEugene Zelenko //===- JSONCompilationDatabase.cpp ----------------------------------------===// 26ed1f85cSDaniel Jasper // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 66ed1f85cSDaniel Jasper // 76ed1f85cSDaniel Jasper //===----------------------------------------------------------------------===// 86ed1f85cSDaniel Jasper // 96ed1f85cSDaniel Jasper // This file contains the implementation of the JSONCompilationDatabase. 106ed1f85cSDaniel Jasper // 116ed1f85cSDaniel Jasper //===----------------------------------------------------------------------===// 126ed1f85cSDaniel Jasper 136ed1f85cSDaniel Jasper #include "clang/Tooling/JSONCompilationDatabase.h" 146366efedSEugene Zelenko #include "clang/Basic/LLVM.h" 156ed1f85cSDaniel Jasper #include "clang/Tooling/CompilationDatabase.h" 166ed1f85cSDaniel Jasper #include "clang/Tooling/CompilationDatabasePluginRegistry.h" 17*c3a73023SKadir Cetinkaya #include "clang/Tooling/Tooling.h" 186366efedSEugene Zelenko #include "llvm/ADT/Optional.h" 19*c3a73023SKadir Cetinkaya #include "llvm/ADT/STLExtras.h" 206ed1f85cSDaniel Jasper #include "llvm/ADT/SmallString.h" 216366efedSEugene Zelenko #include "llvm/ADT/SmallVector.h" 226366efedSEugene Zelenko #include "llvm/ADT/StringRef.h" 236366efedSEugene Zelenko #include "llvm/ADT/Triple.h" 249e60a2adSZachary Turner #include "llvm/Support/Allocator.h" 256366efedSEugene Zelenko #include "llvm/Support/Casting.h" 269e60a2adSZachary Turner #include "llvm/Support/CommandLine.h" 276366efedSEugene Zelenko #include "llvm/Support/ErrorOr.h" 286366efedSEugene Zelenko #include "llvm/Support/Host.h" 296366efedSEugene Zelenko #include "llvm/Support/MemoryBuffer.h" 306ed1f85cSDaniel Jasper #include "llvm/Support/Path.h" 319e60a2adSZachary Turner #include "llvm/Support/StringSaver.h" 326366efedSEugene Zelenko #include "llvm/Support/YAMLParser.h" 336366efedSEugene Zelenko #include "llvm/Support/raw_ostream.h" 346366efedSEugene Zelenko #include <cassert> 356366efedSEugene Zelenko #include <memory> 366366efedSEugene Zelenko #include <string> 378a8e554aSRafael Espindola #include <system_error> 386366efedSEugene Zelenko #include <tuple> 396366efedSEugene Zelenko #include <utility> 406366efedSEugene Zelenko #include <vector> 416ed1f85cSDaniel Jasper 426366efedSEugene Zelenko using namespace clang; 436366efedSEugene Zelenko using namespace tooling; 446ed1f85cSDaniel Jasper 456ed1f85cSDaniel Jasper namespace { 466ed1f85cSDaniel Jasper 479fc8faf9SAdrian Prantl /// A parser for escaped strings of command line arguments. 486ed1f85cSDaniel Jasper /// 496ed1f85cSDaniel Jasper /// Assumes \-escaping for quoted arguments (see the documentation of 506ed1f85cSDaniel Jasper /// unescapeCommandLine(...)). 516ed1f85cSDaniel Jasper class CommandLineArgumentParser { 526ed1f85cSDaniel Jasper public: 536ed1f85cSDaniel Jasper CommandLineArgumentParser(StringRef CommandLine) 546ed1f85cSDaniel Jasper : Input(CommandLine), Position(Input.begin()-1) {} 556ed1f85cSDaniel Jasper 566ed1f85cSDaniel Jasper std::vector<std::string> parse() { 576ed1f85cSDaniel Jasper bool HasMoreInput = true; 586ed1f85cSDaniel Jasper while (HasMoreInput && nextNonWhitespace()) { 596ed1f85cSDaniel Jasper std::string Argument; 606ed1f85cSDaniel Jasper HasMoreInput = parseStringInto(Argument); 616ed1f85cSDaniel Jasper CommandLine.push_back(Argument); 626ed1f85cSDaniel Jasper } 636ed1f85cSDaniel Jasper return CommandLine; 646ed1f85cSDaniel Jasper } 656ed1f85cSDaniel Jasper 666ed1f85cSDaniel Jasper private: 676ed1f85cSDaniel Jasper // All private methods return true if there is more input available. 686ed1f85cSDaniel Jasper 696ed1f85cSDaniel Jasper bool parseStringInto(std::string &String) { 706ed1f85cSDaniel Jasper do { 716ed1f85cSDaniel Jasper if (*Position == '"') { 72fe7a3486SPeter Collingbourne if (!parseDoubleQuotedStringInto(String)) return false; 73fe7a3486SPeter Collingbourne } else if (*Position == '\'') { 74fe7a3486SPeter Collingbourne if (!parseSingleQuotedStringInto(String)) return false; 756ed1f85cSDaniel Jasper } else { 766ed1f85cSDaniel Jasper if (!parseFreeStringInto(String)) return false; 776ed1f85cSDaniel Jasper } 786ed1f85cSDaniel Jasper } while (*Position != ' '); 796ed1f85cSDaniel Jasper return true; 806ed1f85cSDaniel Jasper } 816ed1f85cSDaniel Jasper 82fe7a3486SPeter Collingbourne bool parseDoubleQuotedStringInto(std::string &String) { 836ed1f85cSDaniel Jasper if (!next()) return false; 846ed1f85cSDaniel Jasper while (*Position != '"') { 856ed1f85cSDaniel Jasper if (!skipEscapeCharacter()) return false; 866ed1f85cSDaniel Jasper String.push_back(*Position); 876ed1f85cSDaniel Jasper if (!next()) return false; 886ed1f85cSDaniel Jasper } 896ed1f85cSDaniel Jasper return next(); 906ed1f85cSDaniel Jasper } 916ed1f85cSDaniel Jasper 92fe7a3486SPeter Collingbourne bool parseSingleQuotedStringInto(std::string &String) { 93fe7a3486SPeter Collingbourne if (!next()) return false; 94fe7a3486SPeter Collingbourne while (*Position != '\'') { 95fe7a3486SPeter Collingbourne String.push_back(*Position); 96fe7a3486SPeter Collingbourne if (!next()) return false; 97fe7a3486SPeter Collingbourne } 98fe7a3486SPeter Collingbourne return next(); 99fe7a3486SPeter Collingbourne } 100fe7a3486SPeter Collingbourne 1016ed1f85cSDaniel Jasper bool parseFreeStringInto(std::string &String) { 1026ed1f85cSDaniel Jasper do { 1036ed1f85cSDaniel Jasper if (!skipEscapeCharacter()) return false; 1046ed1f85cSDaniel Jasper String.push_back(*Position); 1056ed1f85cSDaniel Jasper if (!next()) return false; 106fe7a3486SPeter Collingbourne } while (*Position != ' ' && *Position != '"' && *Position != '\''); 1076ed1f85cSDaniel Jasper return true; 1086ed1f85cSDaniel Jasper } 1096ed1f85cSDaniel Jasper 1106ed1f85cSDaniel Jasper bool skipEscapeCharacter() { 1116ed1f85cSDaniel Jasper if (*Position == '\\') { 1126ed1f85cSDaniel Jasper return next(); 1136ed1f85cSDaniel Jasper } 1146ed1f85cSDaniel Jasper return true; 1156ed1f85cSDaniel Jasper } 1166ed1f85cSDaniel Jasper 1176ed1f85cSDaniel Jasper bool nextNonWhitespace() { 1186ed1f85cSDaniel Jasper do { 1196ed1f85cSDaniel Jasper if (!next()) return false; 1206ed1f85cSDaniel Jasper } while (*Position == ' '); 1216ed1f85cSDaniel Jasper return true; 1226ed1f85cSDaniel Jasper } 1236ed1f85cSDaniel Jasper 1246ed1f85cSDaniel Jasper bool next() { 1256ed1f85cSDaniel Jasper ++Position; 1266ed1f85cSDaniel Jasper return Position != Input.end(); 1276ed1f85cSDaniel Jasper } 1286ed1f85cSDaniel Jasper 1296ed1f85cSDaniel Jasper const StringRef Input; 1306ed1f85cSDaniel Jasper StringRef::iterator Position; 1316ed1f85cSDaniel Jasper std::vector<std::string> CommandLine; 1326ed1f85cSDaniel Jasper }; 1336ed1f85cSDaniel Jasper 1349e60a2adSZachary Turner std::vector<std::string> unescapeCommandLine(JSONCommandLineSyntax Syntax, 1356ed1f85cSDaniel Jasper StringRef EscapedCommandLine) { 1369e60a2adSZachary Turner if (Syntax == JSONCommandLineSyntax::AutoDetect) { 13785d0f314SZachary Turner Syntax = JSONCommandLineSyntax::Gnu; 1389e60a2adSZachary Turner llvm::Triple Triple(llvm::sys::getProcessTriple()); 1399e60a2adSZachary Turner if (Triple.getOS() == llvm::Triple::OSType::Win32) { 1409e60a2adSZachary Turner // Assume Windows command line parsing on Win32 unless the triple 14185d0f314SZachary Turner // explicitly tells us otherwise. 1429e60a2adSZachary Turner if (!Triple.hasEnvironment() || 1439e60a2adSZachary Turner Triple.getEnvironment() == llvm::Triple::EnvironmentType::MSVC) 1449e60a2adSZachary Turner Syntax = JSONCommandLineSyntax::Windows; 1459e60a2adSZachary Turner } 1469e60a2adSZachary Turner } 1479e60a2adSZachary Turner 1489e60a2adSZachary Turner if (Syntax == JSONCommandLineSyntax::Windows) { 1499e60a2adSZachary Turner llvm::BumpPtrAllocator Alloc; 1509e60a2adSZachary Turner llvm::StringSaver Saver(Alloc); 1519e60a2adSZachary Turner llvm::SmallVector<const char *, 64> T; 1529e60a2adSZachary Turner llvm::cl::TokenizeWindowsCommandLine(EscapedCommandLine, Saver, T); 1539e60a2adSZachary Turner std::vector<std::string> Result(T.begin(), T.end()); 1549e60a2adSZachary Turner return Result; 1559e60a2adSZachary Turner } 1569e60a2adSZachary Turner assert(Syntax == JSONCommandLineSyntax::Gnu); 1576ed1f85cSDaniel Jasper CommandLineArgumentParser parser(EscapedCommandLine); 1586ed1f85cSDaniel Jasper return parser.parse(); 1596ed1f85cSDaniel Jasper } 1606ed1f85cSDaniel Jasper 1619d3530bdSSam McCall // This plugin locates a nearby compile_command.json file, and also infers 1629d3530bdSSam McCall // compile commands for files not present in the database. 1636ed1f85cSDaniel Jasper class JSONCompilationDatabasePlugin : public CompilationDatabasePlugin { 164cdba84c0SDavid Blaikie std::unique_ptr<CompilationDatabase> 165cdba84c0SDavid Blaikie loadFromDirectory(StringRef Directory, std::string &ErrorMessage) override { 166f857950dSDmitri Gribenko SmallString<1024> JSONDatabasePath(Directory); 1676ed1f85cSDaniel Jasper llvm::sys::path::append(JSONDatabasePath, "compile_commands.json"); 1689d3530bdSSam McCall auto Base = JSONCompilationDatabase::loadFromFile( 169024b0644SKrasimir Georgiev JSONDatabasePath, ErrorMessage, JSONCommandLineSyntax::AutoDetect); 170*c3a73023SKadir Cetinkaya return Base ? inferTargetAndDriverMode( 171*c3a73023SKadir Cetinkaya inferMissingCompileCommands(std::move(Base))) 172*c3a73023SKadir Cetinkaya : nullptr; 1736ed1f85cSDaniel Jasper } 1746ed1f85cSDaniel Jasper }; 1756ed1f85cSDaniel Jasper 1766366efedSEugene Zelenko } // namespace 17769b6277aSCraig Topper 1786ed1f85cSDaniel Jasper // Register the JSONCompilationDatabasePlugin with the 1796ed1f85cSDaniel Jasper // CompilationDatabasePluginRegistry using this statically initialized variable. 1806ed1f85cSDaniel Jasper static CompilationDatabasePluginRegistry::Add<JSONCompilationDatabasePlugin> 1816ed1f85cSDaniel Jasper X("json-compilation-database", "Reads JSON formatted compilation databases"); 1826ed1f85cSDaniel Jasper 1836366efedSEugene Zelenko namespace clang { 1846366efedSEugene Zelenko namespace tooling { 1856366efedSEugene Zelenko 1866ed1f85cSDaniel Jasper // This anchor is used to force the linker to link in the generated object file 1876ed1f85cSDaniel Jasper // and thus register the JSONCompilationDatabasePlugin. 188d574ac2fSNAKAMURA Takumi volatile int JSONAnchorSource = 0; 1896ed1f85cSDaniel Jasper 1906366efedSEugene Zelenko } // namespace tooling 1916366efedSEugene Zelenko } // namespace clang 1926366efedSEugene Zelenko 193cdba84c0SDavid Blaikie std::unique_ptr<JSONCompilationDatabase> 1946ed1f85cSDaniel Jasper JSONCompilationDatabase::loadFromFile(StringRef FilePath, 1959e60a2adSZachary Turner std::string &ErrorMessage, 1969e60a2adSZachary Turner JSONCommandLineSyntax Syntax) { 197fdbb6185SSam McCall // Don't mmap: if we're a long-lived process, the build system may overwrite. 1982d2b420aSRafael Espindola llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> DatabaseBuffer = 199fdbb6185SSam McCall llvm::MemoryBuffer::getFile(FilePath, /*FileSize=*/-1, 200fdbb6185SSam McCall /*RequiresNullTerminator=*/true, 201fdbb6185SSam McCall /*IsVolatile=*/true); 2022d2b420aSRafael Espindola if (std::error_code Result = DatabaseBuffer.getError()) { 2036ed1f85cSDaniel Jasper ErrorMessage = "Error while opening JSON database: " + Result.message(); 204ccbc35edSCraig Topper return nullptr; 2056ed1f85cSDaniel Jasper } 206b8984329SAhmed Charles std::unique_ptr<JSONCompilationDatabase> Database( 2079e60a2adSZachary Turner new JSONCompilationDatabase(std::move(*DatabaseBuffer), Syntax)); 2086ed1f85cSDaniel Jasper if (!Database->parse(ErrorMessage)) 209ccbc35edSCraig Topper return nullptr; 210cdba84c0SDavid Blaikie return Database; 2116ed1f85cSDaniel Jasper } 2126ed1f85cSDaniel Jasper 213cdba84c0SDavid Blaikie std::unique_ptr<JSONCompilationDatabase> 2146ed1f85cSDaniel Jasper JSONCompilationDatabase::loadFromBuffer(StringRef DatabaseString, 2159e60a2adSZachary Turner std::string &ErrorMessage, 2169e60a2adSZachary Turner JSONCommandLineSyntax Syntax) { 217b8984329SAhmed Charles std::unique_ptr<llvm::MemoryBuffer> DatabaseBuffer( 2186ed1f85cSDaniel Jasper llvm::MemoryBuffer::getMemBuffer(DatabaseString)); 219b8984329SAhmed Charles std::unique_ptr<JSONCompilationDatabase> Database( 2209e60a2adSZachary Turner new JSONCompilationDatabase(std::move(DatabaseBuffer), Syntax)); 2216ed1f85cSDaniel Jasper if (!Database->parse(ErrorMessage)) 222ccbc35edSCraig Topper return nullptr; 223cdba84c0SDavid Blaikie return Database; 2246ed1f85cSDaniel Jasper } 2256ed1f85cSDaniel Jasper 2266ed1f85cSDaniel Jasper std::vector<CompileCommand> 2276ed1f85cSDaniel Jasper JSONCompilationDatabase::getCompileCommands(StringRef FilePath) const { 228f857950dSDmitri Gribenko SmallString<128> NativeFilePath; 2296ed1f85cSDaniel Jasper llvm::sys::path::native(FilePath, NativeFilePath); 230965f8825SAlp Toker 23126cf9c43SDaniel Jasper std::string Error; 23226cf9c43SDaniel Jasper llvm::raw_string_ostream ES(Error); 23392e1b62dSYaron Keren StringRef Match = MatchTrie.findEquivalent(NativeFilePath, ES); 2343128a11eSArnaud A. de Grandmaison if (Match.empty()) 2356366efedSEugene Zelenko return {}; 2366366efedSEugene Zelenko const auto CommandsRefI = IndexByFile.find(Match); 2376ed1f85cSDaniel Jasper if (CommandsRefI == IndexByFile.end()) 2386366efedSEugene Zelenko return {}; 2396ed1f85cSDaniel Jasper std::vector<CompileCommand> Commands; 240251ad5e0SArgyrios Kyrtzidis getCommands(CommandsRefI->getValue(), Commands); 2416ed1f85cSDaniel Jasper return Commands; 2426ed1f85cSDaniel Jasper } 2436ed1f85cSDaniel Jasper 2446ed1f85cSDaniel Jasper std::vector<std::string> 2456ed1f85cSDaniel Jasper JSONCompilationDatabase::getAllFiles() const { 2466ed1f85cSDaniel Jasper std::vector<std::string> Result; 2476366efedSEugene Zelenko for (const auto &CommandRef : IndexByFile) 2486366efedSEugene Zelenko Result.push_back(CommandRef.first().str()); 2496ed1f85cSDaniel Jasper return Result; 2506ed1f85cSDaniel Jasper } 2516ed1f85cSDaniel Jasper 252251ad5e0SArgyrios Kyrtzidis std::vector<CompileCommand> 253251ad5e0SArgyrios Kyrtzidis JSONCompilationDatabase::getAllCompileCommands() const { 254251ad5e0SArgyrios Kyrtzidis std::vector<CompileCommand> Commands; 25564f67be3SArgyrios Kyrtzidis getCommands(AllCommands, Commands); 256251ad5e0SArgyrios Kyrtzidis return Commands; 257251ad5e0SArgyrios Kyrtzidis } 258251ad5e0SArgyrios Kyrtzidis 2593ecd8c0aSManuel Klimek static std::vector<std::string> 2609e60a2adSZachary Turner nodeToCommandLine(JSONCommandLineSyntax Syntax, 2619e60a2adSZachary Turner const std::vector<llvm::yaml::ScalarNode *> &Nodes) { 2623ecd8c0aSManuel Klimek SmallString<1024> Storage; 2636366efedSEugene Zelenko if (Nodes.size() == 1) 2649e60a2adSZachary Turner return unescapeCommandLine(Syntax, Nodes[0]->getValue(Storage)); 2653ecd8c0aSManuel Klimek std::vector<std::string> Arguments; 2666366efedSEugene Zelenko for (const auto *Node : Nodes) 2673ecd8c0aSManuel Klimek Arguments.push_back(Node->getValue(Storage)); 2683ecd8c0aSManuel Klimek return Arguments; 2693ecd8c0aSManuel Klimek } 2703ecd8c0aSManuel Klimek 271251ad5e0SArgyrios Kyrtzidis void JSONCompilationDatabase::getCommands( 272251ad5e0SArgyrios Kyrtzidis ArrayRef<CompileCommandRef> CommandsRef, 273251ad5e0SArgyrios Kyrtzidis std::vector<CompileCommand> &Commands) const { 2746366efedSEugene Zelenko for (const auto &CommandRef : CommandsRef) { 275f857950dSDmitri Gribenko SmallString<8> DirectoryStorage; 27674bcd21eSArgyrios Kyrtzidis SmallString<32> FilenameStorage; 277399aea30SJoerg Sonnenberger SmallString<32> OutputStorage; 2786366efedSEugene Zelenko auto Output = std::get<3>(CommandRef); 27974bcd21eSArgyrios Kyrtzidis Commands.emplace_back( 2806366efedSEugene Zelenko std::get<0>(CommandRef)->getValue(DirectoryStorage), 2816366efedSEugene Zelenko std::get<1>(CommandRef)->getValue(FilenameStorage), 2826366efedSEugene Zelenko nodeToCommandLine(Syntax, std::get<2>(CommandRef)), 283399aea30SJoerg Sonnenberger Output ? Output->getValue(OutputStorage) : ""); 284251ad5e0SArgyrios Kyrtzidis } 285251ad5e0SArgyrios Kyrtzidis } 286251ad5e0SArgyrios Kyrtzidis 2876ed1f85cSDaniel Jasper bool JSONCompilationDatabase::parse(std::string &ErrorMessage) { 2886ed1f85cSDaniel Jasper llvm::yaml::document_iterator I = YAMLStream.begin(); 2896ed1f85cSDaniel Jasper if (I == YAMLStream.end()) { 2906ed1f85cSDaniel Jasper ErrorMessage = "Error while parsing YAML."; 2916ed1f85cSDaniel Jasper return false; 2926ed1f85cSDaniel Jasper } 2936ed1f85cSDaniel Jasper llvm::yaml::Node *Root = I->getRoot(); 294ccbc35edSCraig Topper if (!Root) { 2956ed1f85cSDaniel Jasper ErrorMessage = "Error while parsing YAML."; 2966ed1f85cSDaniel Jasper return false; 2976ed1f85cSDaniel Jasper } 2986366efedSEugene Zelenko auto *Array = dyn_cast<llvm::yaml::SequenceNode>(Root); 299ccbc35edSCraig Topper if (!Array) { 3006ed1f85cSDaniel Jasper ErrorMessage = "Expected array."; 3016ed1f85cSDaniel Jasper return false; 3026ed1f85cSDaniel Jasper } 30354042e74SManuel Klimek for (auto &NextObject : *Array) { 3046366efedSEugene Zelenko auto *Object = dyn_cast<llvm::yaml::MappingNode>(&NextObject); 305ccbc35edSCraig Topper if (!Object) { 3066ed1f85cSDaniel Jasper ErrorMessage = "Expected object."; 3076ed1f85cSDaniel Jasper return false; 3086ed1f85cSDaniel Jasper } 309ccbc35edSCraig Topper llvm::yaml::ScalarNode *Directory = nullptr; 3103ecd8c0aSManuel Klimek llvm::Optional<std::vector<llvm::yaml::ScalarNode *>> Command; 311ccbc35edSCraig Topper llvm::yaml::ScalarNode *File = nullptr; 312399aea30SJoerg Sonnenberger llvm::yaml::ScalarNode *Output = nullptr; 31354042e74SManuel Klimek for (auto& NextKeyValue : *Object) { 3146366efedSEugene Zelenko auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey()); 31554042e74SManuel Klimek if (!KeyString) { 31654042e74SManuel Klimek ErrorMessage = "Expected strings as key."; 31754042e74SManuel Klimek return false; 31854042e74SManuel Klimek } 31954042e74SManuel Klimek SmallString<10> KeyStorage; 32054042e74SManuel Klimek StringRef KeyValue = KeyString->getValue(KeyStorage); 32154042e74SManuel Klimek llvm::yaml::Node *Value = NextKeyValue.getValue(); 322ccbc35edSCraig Topper if (!Value) { 3236ed1f85cSDaniel Jasper ErrorMessage = "Expected value."; 3246ed1f85cSDaniel Jasper return false; 3256ed1f85cSDaniel Jasper } 3266366efedSEugene Zelenko auto *ValueString = dyn_cast<llvm::yaml::ScalarNode>(Value); 3276366efedSEugene Zelenko auto *SequenceString = dyn_cast<llvm::yaml::SequenceNode>(Value); 32854042e74SManuel Klimek if (KeyValue == "arguments" && !SequenceString) { 32954042e74SManuel Klimek ErrorMessage = "Expected sequence as value."; 33054042e74SManuel Klimek return false; 33154042e74SManuel Klimek } else if (KeyValue != "arguments" && !ValueString) { 3326ed1f85cSDaniel Jasper ErrorMessage = "Expected string as value."; 3336ed1f85cSDaniel Jasper return false; 3346ed1f85cSDaniel Jasper } 33554042e74SManuel Klimek if (KeyValue == "directory") { 3366ed1f85cSDaniel Jasper Directory = ValueString; 33754042e74SManuel Klimek } else if (KeyValue == "arguments") { 3383ecd8c0aSManuel Klimek Command = std::vector<llvm::yaml::ScalarNode *>(); 3393ecd8c0aSManuel Klimek for (auto &Argument : *SequenceString) { 3406366efedSEugene Zelenko auto *Scalar = dyn_cast<llvm::yaml::ScalarNode>(&Argument); 3413ecd8c0aSManuel Klimek if (!Scalar) { 3423ecd8c0aSManuel Klimek ErrorMessage = "Only strings are allowed in 'arguments'."; 3433ecd8c0aSManuel Klimek return false; 34454042e74SManuel Klimek } 3453ecd8c0aSManuel Klimek Command->push_back(Scalar); 3463ecd8c0aSManuel Klimek } 34754042e74SManuel Klimek } else if (KeyValue == "command") { 3483ecd8c0aSManuel Klimek if (!Command) 3493ecd8c0aSManuel Klimek Command = std::vector<llvm::yaml::ScalarNode *>(1, ValueString); 35054042e74SManuel Klimek } else if (KeyValue == "file") { 3516ed1f85cSDaniel Jasper File = ValueString; 352399aea30SJoerg Sonnenberger } else if (KeyValue == "output") { 353399aea30SJoerg Sonnenberger Output = ValueString; 3546ed1f85cSDaniel Jasper } else { 3556ed1f85cSDaniel Jasper ErrorMessage = ("Unknown key: \"" + 3566ed1f85cSDaniel Jasper KeyString->getRawValue() + "\"").str(); 3576ed1f85cSDaniel Jasper return false; 3586ed1f85cSDaniel Jasper } 3596ed1f85cSDaniel Jasper } 3606ed1f85cSDaniel Jasper if (!File) { 3616ed1f85cSDaniel Jasper ErrorMessage = "Missing key: \"file\"."; 3626ed1f85cSDaniel Jasper return false; 3636ed1f85cSDaniel Jasper } 3643ecd8c0aSManuel Klimek if (!Command) { 36554042e74SManuel Klimek ErrorMessage = "Missing key: \"command\" or \"arguments\"."; 3666ed1f85cSDaniel Jasper return false; 3676ed1f85cSDaniel Jasper } 3686ed1f85cSDaniel Jasper if (!Directory) { 3696ed1f85cSDaniel Jasper ErrorMessage = "Missing key: \"directory\"."; 3706ed1f85cSDaniel Jasper return false; 3716ed1f85cSDaniel Jasper } 372f857950dSDmitri Gribenko SmallString<8> FileStorage; 37326cf9c43SDaniel Jasper StringRef FileName = File->getValue(FileStorage); 374f857950dSDmitri Gribenko SmallString<128> NativeFilePath; 37526cf9c43SDaniel Jasper if (llvm::sys::path::is_relative(FileName)) { 376f857950dSDmitri Gribenko SmallString<8> DirectoryStorage; 377f857950dSDmitri Gribenko SmallString<128> AbsolutePath( 37826cf9c43SDaniel Jasper Directory->getValue(DirectoryStorage)); 37926cf9c43SDaniel Jasper llvm::sys::path::append(AbsolutePath, FileName); 3807ec1ec10SKadir Cetinkaya llvm::sys::path::remove_dots(AbsolutePath, /*remove_dot_dot=*/ true); 38192e1b62dSYaron Keren llvm::sys::path::native(AbsolutePath, NativeFilePath); 38226cf9c43SDaniel Jasper } else { 38326cf9c43SDaniel Jasper llvm::sys::path::native(FileName, NativeFilePath); 38426cf9c43SDaniel Jasper } 385399aea30SJoerg Sonnenberger auto Cmd = CompileCommandRef(Directory, File, *Command, Output); 38664f67be3SArgyrios Kyrtzidis IndexByFile[NativeFilePath].push_back(Cmd); 38764f67be3SArgyrios Kyrtzidis AllCommands.push_back(Cmd); 38892e1b62dSYaron Keren MatchTrie.insert(NativeFilePath); 3896ed1f85cSDaniel Jasper } 3906ed1f85cSDaniel Jasper return true; 3916ed1f85cSDaniel Jasper } 392