16366efedSEugene Zelenko //===- JSONCompilationDatabase.cpp ----------------------------------------===// 26ed1f85cSDaniel Jasper // 3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 5*2946cd70SChandler 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" 176366efedSEugene Zelenko #include "llvm/ADT/Optional.h" 186ed1f85cSDaniel Jasper #include "llvm/ADT/SmallString.h" 196366efedSEugene Zelenko #include "llvm/ADT/SmallVector.h" 206366efedSEugene Zelenko #include "llvm/ADT/StringRef.h" 216366efedSEugene Zelenko #include "llvm/ADT/Triple.h" 229e60a2adSZachary Turner #include "llvm/Support/Allocator.h" 236366efedSEugene Zelenko #include "llvm/Support/Casting.h" 249e60a2adSZachary Turner #include "llvm/Support/CommandLine.h" 256366efedSEugene Zelenko #include "llvm/Support/ErrorOr.h" 266366efedSEugene Zelenko #include "llvm/Support/Host.h" 276366efedSEugene Zelenko #include "llvm/Support/MemoryBuffer.h" 286ed1f85cSDaniel Jasper #include "llvm/Support/Path.h" 299e60a2adSZachary Turner #include "llvm/Support/StringSaver.h" 306366efedSEugene Zelenko #include "llvm/Support/YAMLParser.h" 316366efedSEugene Zelenko #include "llvm/Support/raw_ostream.h" 326366efedSEugene Zelenko #include <cassert> 336366efedSEugene Zelenko #include <memory> 346366efedSEugene Zelenko #include <string> 358a8e554aSRafael Espindola #include <system_error> 366366efedSEugene Zelenko #include <tuple> 376366efedSEugene Zelenko #include <utility> 386366efedSEugene Zelenko #include <vector> 396ed1f85cSDaniel Jasper 406366efedSEugene Zelenko using namespace clang; 416366efedSEugene Zelenko using namespace tooling; 426ed1f85cSDaniel Jasper 436ed1f85cSDaniel Jasper namespace { 446ed1f85cSDaniel Jasper 459fc8faf9SAdrian Prantl /// A parser for escaped strings of command line arguments. 466ed1f85cSDaniel Jasper /// 476ed1f85cSDaniel Jasper /// Assumes \-escaping for quoted arguments (see the documentation of 486ed1f85cSDaniel Jasper /// unescapeCommandLine(...)). 496ed1f85cSDaniel Jasper class CommandLineArgumentParser { 506ed1f85cSDaniel Jasper public: 516ed1f85cSDaniel Jasper CommandLineArgumentParser(StringRef CommandLine) 526ed1f85cSDaniel Jasper : Input(CommandLine), Position(Input.begin()-1) {} 536ed1f85cSDaniel Jasper 546ed1f85cSDaniel Jasper std::vector<std::string> parse() { 556ed1f85cSDaniel Jasper bool HasMoreInput = true; 566ed1f85cSDaniel Jasper while (HasMoreInput && nextNonWhitespace()) { 576ed1f85cSDaniel Jasper std::string Argument; 586ed1f85cSDaniel Jasper HasMoreInput = parseStringInto(Argument); 596ed1f85cSDaniel Jasper CommandLine.push_back(Argument); 606ed1f85cSDaniel Jasper } 616ed1f85cSDaniel Jasper return CommandLine; 626ed1f85cSDaniel Jasper } 636ed1f85cSDaniel Jasper 646ed1f85cSDaniel Jasper private: 656ed1f85cSDaniel Jasper // All private methods return true if there is more input available. 666ed1f85cSDaniel Jasper 676ed1f85cSDaniel Jasper bool parseStringInto(std::string &String) { 686ed1f85cSDaniel Jasper do { 696ed1f85cSDaniel Jasper if (*Position == '"') { 70fe7a3486SPeter Collingbourne if (!parseDoubleQuotedStringInto(String)) return false; 71fe7a3486SPeter Collingbourne } else if (*Position == '\'') { 72fe7a3486SPeter Collingbourne if (!parseSingleQuotedStringInto(String)) return false; 736ed1f85cSDaniel Jasper } else { 746ed1f85cSDaniel Jasper if (!parseFreeStringInto(String)) return false; 756ed1f85cSDaniel Jasper } 766ed1f85cSDaniel Jasper } while (*Position != ' '); 776ed1f85cSDaniel Jasper return true; 786ed1f85cSDaniel Jasper } 796ed1f85cSDaniel Jasper 80fe7a3486SPeter Collingbourne bool parseDoubleQuotedStringInto(std::string &String) { 816ed1f85cSDaniel Jasper if (!next()) return false; 826ed1f85cSDaniel Jasper while (*Position != '"') { 836ed1f85cSDaniel Jasper if (!skipEscapeCharacter()) return false; 846ed1f85cSDaniel Jasper String.push_back(*Position); 856ed1f85cSDaniel Jasper if (!next()) return false; 866ed1f85cSDaniel Jasper } 876ed1f85cSDaniel Jasper return next(); 886ed1f85cSDaniel Jasper } 896ed1f85cSDaniel Jasper 90fe7a3486SPeter Collingbourne bool parseSingleQuotedStringInto(std::string &String) { 91fe7a3486SPeter Collingbourne if (!next()) return false; 92fe7a3486SPeter Collingbourne while (*Position != '\'') { 93fe7a3486SPeter Collingbourne String.push_back(*Position); 94fe7a3486SPeter Collingbourne if (!next()) return false; 95fe7a3486SPeter Collingbourne } 96fe7a3486SPeter Collingbourne return next(); 97fe7a3486SPeter Collingbourne } 98fe7a3486SPeter Collingbourne 996ed1f85cSDaniel Jasper bool parseFreeStringInto(std::string &String) { 1006ed1f85cSDaniel Jasper do { 1016ed1f85cSDaniel Jasper if (!skipEscapeCharacter()) return false; 1026ed1f85cSDaniel Jasper String.push_back(*Position); 1036ed1f85cSDaniel Jasper if (!next()) return false; 104fe7a3486SPeter Collingbourne } while (*Position != ' ' && *Position != '"' && *Position != '\''); 1056ed1f85cSDaniel Jasper return true; 1066ed1f85cSDaniel Jasper } 1076ed1f85cSDaniel Jasper 1086ed1f85cSDaniel Jasper bool skipEscapeCharacter() { 1096ed1f85cSDaniel Jasper if (*Position == '\\') { 1106ed1f85cSDaniel Jasper return next(); 1116ed1f85cSDaniel Jasper } 1126ed1f85cSDaniel Jasper return true; 1136ed1f85cSDaniel Jasper } 1146ed1f85cSDaniel Jasper 1156ed1f85cSDaniel Jasper bool nextNonWhitespace() { 1166ed1f85cSDaniel Jasper do { 1176ed1f85cSDaniel Jasper if (!next()) return false; 1186ed1f85cSDaniel Jasper } while (*Position == ' '); 1196ed1f85cSDaniel Jasper return true; 1206ed1f85cSDaniel Jasper } 1216ed1f85cSDaniel Jasper 1226ed1f85cSDaniel Jasper bool next() { 1236ed1f85cSDaniel Jasper ++Position; 1246ed1f85cSDaniel Jasper return Position != Input.end(); 1256ed1f85cSDaniel Jasper } 1266ed1f85cSDaniel Jasper 1276ed1f85cSDaniel Jasper const StringRef Input; 1286ed1f85cSDaniel Jasper StringRef::iterator Position; 1296ed1f85cSDaniel Jasper std::vector<std::string> CommandLine; 1306ed1f85cSDaniel Jasper }; 1316ed1f85cSDaniel Jasper 1329e60a2adSZachary Turner std::vector<std::string> unescapeCommandLine(JSONCommandLineSyntax Syntax, 1336ed1f85cSDaniel Jasper StringRef EscapedCommandLine) { 1349e60a2adSZachary Turner if (Syntax == JSONCommandLineSyntax::AutoDetect) { 13585d0f314SZachary Turner Syntax = JSONCommandLineSyntax::Gnu; 1369e60a2adSZachary Turner llvm::Triple Triple(llvm::sys::getProcessTriple()); 1379e60a2adSZachary Turner if (Triple.getOS() == llvm::Triple::OSType::Win32) { 1389e60a2adSZachary Turner // Assume Windows command line parsing on Win32 unless the triple 13985d0f314SZachary Turner // explicitly tells us otherwise. 1409e60a2adSZachary Turner if (!Triple.hasEnvironment() || 1419e60a2adSZachary Turner Triple.getEnvironment() == llvm::Triple::EnvironmentType::MSVC) 1429e60a2adSZachary Turner Syntax = JSONCommandLineSyntax::Windows; 1439e60a2adSZachary Turner } 1449e60a2adSZachary Turner } 1459e60a2adSZachary Turner 1469e60a2adSZachary Turner if (Syntax == JSONCommandLineSyntax::Windows) { 1479e60a2adSZachary Turner llvm::BumpPtrAllocator Alloc; 1489e60a2adSZachary Turner llvm::StringSaver Saver(Alloc); 1499e60a2adSZachary Turner llvm::SmallVector<const char *, 64> T; 1509e60a2adSZachary Turner llvm::cl::TokenizeWindowsCommandLine(EscapedCommandLine, Saver, T); 1519e60a2adSZachary Turner std::vector<std::string> Result(T.begin(), T.end()); 1529e60a2adSZachary Turner return Result; 1539e60a2adSZachary Turner } 1549e60a2adSZachary Turner assert(Syntax == JSONCommandLineSyntax::Gnu); 1556ed1f85cSDaniel Jasper CommandLineArgumentParser parser(EscapedCommandLine); 1566ed1f85cSDaniel Jasper return parser.parse(); 1576ed1f85cSDaniel Jasper } 1586ed1f85cSDaniel Jasper 1599d3530bdSSam McCall // This plugin locates a nearby compile_command.json file, and also infers 1609d3530bdSSam McCall // compile commands for files not present in the database. 1616ed1f85cSDaniel Jasper class JSONCompilationDatabasePlugin : public CompilationDatabasePlugin { 162cdba84c0SDavid Blaikie std::unique_ptr<CompilationDatabase> 163cdba84c0SDavid Blaikie loadFromDirectory(StringRef Directory, std::string &ErrorMessage) override { 164f857950dSDmitri Gribenko SmallString<1024> JSONDatabasePath(Directory); 1656ed1f85cSDaniel Jasper llvm::sys::path::append(JSONDatabasePath, "compile_commands.json"); 1669d3530bdSSam McCall auto Base = JSONCompilationDatabase::loadFromFile( 167024b0644SKrasimir Georgiev JSONDatabasePath, ErrorMessage, JSONCommandLineSyntax::AutoDetect); 1689d3530bdSSam McCall return Base ? inferMissingCompileCommands(std::move(Base)) : nullptr; 1696ed1f85cSDaniel Jasper } 1706ed1f85cSDaniel Jasper }; 1716ed1f85cSDaniel Jasper 1726366efedSEugene Zelenko } // namespace 17369b6277aSCraig Topper 1746ed1f85cSDaniel Jasper // Register the JSONCompilationDatabasePlugin with the 1756ed1f85cSDaniel Jasper // CompilationDatabasePluginRegistry using this statically initialized variable. 1766ed1f85cSDaniel Jasper static CompilationDatabasePluginRegistry::Add<JSONCompilationDatabasePlugin> 1776ed1f85cSDaniel Jasper X("json-compilation-database", "Reads JSON formatted compilation databases"); 1786ed1f85cSDaniel Jasper 1796366efedSEugene Zelenko namespace clang { 1806366efedSEugene Zelenko namespace tooling { 1816366efedSEugene Zelenko 1826ed1f85cSDaniel Jasper // This anchor is used to force the linker to link in the generated object file 1836ed1f85cSDaniel Jasper // and thus register the JSONCompilationDatabasePlugin. 184d574ac2fSNAKAMURA Takumi volatile int JSONAnchorSource = 0; 1856ed1f85cSDaniel Jasper 1866366efedSEugene Zelenko } // namespace tooling 1876366efedSEugene Zelenko } // namespace clang 1886366efedSEugene Zelenko 189cdba84c0SDavid Blaikie std::unique_ptr<JSONCompilationDatabase> 1906ed1f85cSDaniel Jasper JSONCompilationDatabase::loadFromFile(StringRef FilePath, 1919e60a2adSZachary Turner std::string &ErrorMessage, 1929e60a2adSZachary Turner JSONCommandLineSyntax Syntax) { 1932d2b420aSRafael Espindola llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> DatabaseBuffer = 1942d2b420aSRafael Espindola llvm::MemoryBuffer::getFile(FilePath); 1952d2b420aSRafael Espindola if (std::error_code Result = DatabaseBuffer.getError()) { 1966ed1f85cSDaniel Jasper ErrorMessage = "Error while opening JSON database: " + Result.message(); 197ccbc35edSCraig Topper return nullptr; 1986ed1f85cSDaniel Jasper } 199b8984329SAhmed Charles std::unique_ptr<JSONCompilationDatabase> Database( 2009e60a2adSZachary Turner new JSONCompilationDatabase(std::move(*DatabaseBuffer), Syntax)); 2016ed1f85cSDaniel Jasper if (!Database->parse(ErrorMessage)) 202ccbc35edSCraig Topper return nullptr; 203cdba84c0SDavid Blaikie return Database; 2046ed1f85cSDaniel Jasper } 2056ed1f85cSDaniel Jasper 206cdba84c0SDavid Blaikie std::unique_ptr<JSONCompilationDatabase> 2076ed1f85cSDaniel Jasper JSONCompilationDatabase::loadFromBuffer(StringRef DatabaseString, 2089e60a2adSZachary Turner std::string &ErrorMessage, 2099e60a2adSZachary Turner JSONCommandLineSyntax Syntax) { 210b8984329SAhmed Charles std::unique_ptr<llvm::MemoryBuffer> DatabaseBuffer( 2116ed1f85cSDaniel Jasper llvm::MemoryBuffer::getMemBuffer(DatabaseString)); 212b8984329SAhmed Charles std::unique_ptr<JSONCompilationDatabase> Database( 2139e60a2adSZachary Turner new JSONCompilationDatabase(std::move(DatabaseBuffer), Syntax)); 2146ed1f85cSDaniel Jasper if (!Database->parse(ErrorMessage)) 215ccbc35edSCraig Topper return nullptr; 216cdba84c0SDavid Blaikie return Database; 2176ed1f85cSDaniel Jasper } 2186ed1f85cSDaniel Jasper 2196ed1f85cSDaniel Jasper std::vector<CompileCommand> 2206ed1f85cSDaniel Jasper JSONCompilationDatabase::getCompileCommands(StringRef FilePath) const { 221f857950dSDmitri Gribenko SmallString<128> NativeFilePath; 2226ed1f85cSDaniel Jasper llvm::sys::path::native(FilePath, NativeFilePath); 223965f8825SAlp Toker 22426cf9c43SDaniel Jasper std::string Error; 22526cf9c43SDaniel Jasper llvm::raw_string_ostream ES(Error); 22692e1b62dSYaron Keren StringRef Match = MatchTrie.findEquivalent(NativeFilePath, ES); 2273128a11eSArnaud A. de Grandmaison if (Match.empty()) 2286366efedSEugene Zelenko return {}; 2296366efedSEugene Zelenko const auto CommandsRefI = IndexByFile.find(Match); 2306ed1f85cSDaniel Jasper if (CommandsRefI == IndexByFile.end()) 2316366efedSEugene Zelenko return {}; 2326ed1f85cSDaniel Jasper std::vector<CompileCommand> Commands; 233251ad5e0SArgyrios Kyrtzidis getCommands(CommandsRefI->getValue(), Commands); 2346ed1f85cSDaniel Jasper return Commands; 2356ed1f85cSDaniel Jasper } 2366ed1f85cSDaniel Jasper 2376ed1f85cSDaniel Jasper std::vector<std::string> 2386ed1f85cSDaniel Jasper JSONCompilationDatabase::getAllFiles() const { 2396ed1f85cSDaniel Jasper std::vector<std::string> Result; 2406366efedSEugene Zelenko for (const auto &CommandRef : IndexByFile) 2416366efedSEugene Zelenko Result.push_back(CommandRef.first().str()); 2426ed1f85cSDaniel Jasper return Result; 2436ed1f85cSDaniel Jasper } 2446ed1f85cSDaniel Jasper 245251ad5e0SArgyrios Kyrtzidis std::vector<CompileCommand> 246251ad5e0SArgyrios Kyrtzidis JSONCompilationDatabase::getAllCompileCommands() const { 247251ad5e0SArgyrios Kyrtzidis std::vector<CompileCommand> Commands; 24864f67be3SArgyrios Kyrtzidis getCommands(AllCommands, Commands); 249251ad5e0SArgyrios Kyrtzidis return Commands; 250251ad5e0SArgyrios Kyrtzidis } 251251ad5e0SArgyrios Kyrtzidis 2523ecd8c0aSManuel Klimek static std::vector<std::string> 2539e60a2adSZachary Turner nodeToCommandLine(JSONCommandLineSyntax Syntax, 2549e60a2adSZachary Turner const std::vector<llvm::yaml::ScalarNode *> &Nodes) { 2553ecd8c0aSManuel Klimek SmallString<1024> Storage; 2566366efedSEugene Zelenko if (Nodes.size() == 1) 2579e60a2adSZachary Turner return unescapeCommandLine(Syntax, Nodes[0]->getValue(Storage)); 2583ecd8c0aSManuel Klimek std::vector<std::string> Arguments; 2596366efedSEugene Zelenko for (const auto *Node : Nodes) 2603ecd8c0aSManuel Klimek Arguments.push_back(Node->getValue(Storage)); 2613ecd8c0aSManuel Klimek return Arguments; 2623ecd8c0aSManuel Klimek } 2633ecd8c0aSManuel Klimek 264251ad5e0SArgyrios Kyrtzidis void JSONCompilationDatabase::getCommands( 265251ad5e0SArgyrios Kyrtzidis ArrayRef<CompileCommandRef> CommandsRef, 266251ad5e0SArgyrios Kyrtzidis std::vector<CompileCommand> &Commands) const { 2676366efedSEugene Zelenko for (const auto &CommandRef : CommandsRef) { 268f857950dSDmitri Gribenko SmallString<8> DirectoryStorage; 26974bcd21eSArgyrios Kyrtzidis SmallString<32> FilenameStorage; 270399aea30SJoerg Sonnenberger SmallString<32> OutputStorage; 2716366efedSEugene Zelenko auto Output = std::get<3>(CommandRef); 27274bcd21eSArgyrios Kyrtzidis Commands.emplace_back( 2736366efedSEugene Zelenko std::get<0>(CommandRef)->getValue(DirectoryStorage), 2746366efedSEugene Zelenko std::get<1>(CommandRef)->getValue(FilenameStorage), 2756366efedSEugene Zelenko nodeToCommandLine(Syntax, std::get<2>(CommandRef)), 276399aea30SJoerg Sonnenberger Output ? Output->getValue(OutputStorage) : ""); 277251ad5e0SArgyrios Kyrtzidis } 278251ad5e0SArgyrios Kyrtzidis } 279251ad5e0SArgyrios Kyrtzidis 2806ed1f85cSDaniel Jasper bool JSONCompilationDatabase::parse(std::string &ErrorMessage) { 2816ed1f85cSDaniel Jasper llvm::yaml::document_iterator I = YAMLStream.begin(); 2826ed1f85cSDaniel Jasper if (I == YAMLStream.end()) { 2836ed1f85cSDaniel Jasper ErrorMessage = "Error while parsing YAML."; 2846ed1f85cSDaniel Jasper return false; 2856ed1f85cSDaniel Jasper } 2866ed1f85cSDaniel Jasper llvm::yaml::Node *Root = I->getRoot(); 287ccbc35edSCraig Topper if (!Root) { 2886ed1f85cSDaniel Jasper ErrorMessage = "Error while parsing YAML."; 2896ed1f85cSDaniel Jasper return false; 2906ed1f85cSDaniel Jasper } 2916366efedSEugene Zelenko auto *Array = dyn_cast<llvm::yaml::SequenceNode>(Root); 292ccbc35edSCraig Topper if (!Array) { 2936ed1f85cSDaniel Jasper ErrorMessage = "Expected array."; 2946ed1f85cSDaniel Jasper return false; 2956ed1f85cSDaniel Jasper } 29654042e74SManuel Klimek for (auto &NextObject : *Array) { 2976366efedSEugene Zelenko auto *Object = dyn_cast<llvm::yaml::MappingNode>(&NextObject); 298ccbc35edSCraig Topper if (!Object) { 2996ed1f85cSDaniel Jasper ErrorMessage = "Expected object."; 3006ed1f85cSDaniel Jasper return false; 3016ed1f85cSDaniel Jasper } 302ccbc35edSCraig Topper llvm::yaml::ScalarNode *Directory = nullptr; 3033ecd8c0aSManuel Klimek llvm::Optional<std::vector<llvm::yaml::ScalarNode *>> Command; 304ccbc35edSCraig Topper llvm::yaml::ScalarNode *File = nullptr; 305399aea30SJoerg Sonnenberger llvm::yaml::ScalarNode *Output = nullptr; 30654042e74SManuel Klimek for (auto& NextKeyValue : *Object) { 3076366efedSEugene Zelenko auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey()); 30854042e74SManuel Klimek if (!KeyString) { 30954042e74SManuel Klimek ErrorMessage = "Expected strings as key."; 31054042e74SManuel Klimek return false; 31154042e74SManuel Klimek } 31254042e74SManuel Klimek SmallString<10> KeyStorage; 31354042e74SManuel Klimek StringRef KeyValue = KeyString->getValue(KeyStorage); 31454042e74SManuel Klimek llvm::yaml::Node *Value = NextKeyValue.getValue(); 315ccbc35edSCraig Topper if (!Value) { 3166ed1f85cSDaniel Jasper ErrorMessage = "Expected value."; 3176ed1f85cSDaniel Jasper return false; 3186ed1f85cSDaniel Jasper } 3196366efedSEugene Zelenko auto *ValueString = dyn_cast<llvm::yaml::ScalarNode>(Value); 3206366efedSEugene Zelenko auto *SequenceString = dyn_cast<llvm::yaml::SequenceNode>(Value); 32154042e74SManuel Klimek if (KeyValue == "arguments" && !SequenceString) { 32254042e74SManuel Klimek ErrorMessage = "Expected sequence as value."; 32354042e74SManuel Klimek return false; 32454042e74SManuel Klimek } else if (KeyValue != "arguments" && !ValueString) { 3256ed1f85cSDaniel Jasper ErrorMessage = "Expected string as value."; 3266ed1f85cSDaniel Jasper return false; 3276ed1f85cSDaniel Jasper } 32854042e74SManuel Klimek if (KeyValue == "directory") { 3296ed1f85cSDaniel Jasper Directory = ValueString; 33054042e74SManuel Klimek } else if (KeyValue == "arguments") { 3313ecd8c0aSManuel Klimek Command = std::vector<llvm::yaml::ScalarNode *>(); 3323ecd8c0aSManuel Klimek for (auto &Argument : *SequenceString) { 3336366efedSEugene Zelenko auto *Scalar = dyn_cast<llvm::yaml::ScalarNode>(&Argument); 3343ecd8c0aSManuel Klimek if (!Scalar) { 3353ecd8c0aSManuel Klimek ErrorMessage = "Only strings are allowed in 'arguments'."; 3363ecd8c0aSManuel Klimek return false; 33754042e74SManuel Klimek } 3383ecd8c0aSManuel Klimek Command->push_back(Scalar); 3393ecd8c0aSManuel Klimek } 34054042e74SManuel Klimek } else if (KeyValue == "command") { 3413ecd8c0aSManuel Klimek if (!Command) 3423ecd8c0aSManuel Klimek Command = std::vector<llvm::yaml::ScalarNode *>(1, ValueString); 34354042e74SManuel Klimek } else if (KeyValue == "file") { 3446ed1f85cSDaniel Jasper File = ValueString; 345399aea30SJoerg Sonnenberger } else if (KeyValue == "output") { 346399aea30SJoerg Sonnenberger Output = ValueString; 3476ed1f85cSDaniel Jasper } else { 3486ed1f85cSDaniel Jasper ErrorMessage = ("Unknown key: \"" + 3496ed1f85cSDaniel Jasper KeyString->getRawValue() + "\"").str(); 3506ed1f85cSDaniel Jasper return false; 3516ed1f85cSDaniel Jasper } 3526ed1f85cSDaniel Jasper } 3536ed1f85cSDaniel Jasper if (!File) { 3546ed1f85cSDaniel Jasper ErrorMessage = "Missing key: \"file\"."; 3556ed1f85cSDaniel Jasper return false; 3566ed1f85cSDaniel Jasper } 3573ecd8c0aSManuel Klimek if (!Command) { 35854042e74SManuel Klimek ErrorMessage = "Missing key: \"command\" or \"arguments\"."; 3596ed1f85cSDaniel Jasper return false; 3606ed1f85cSDaniel Jasper } 3616ed1f85cSDaniel Jasper if (!Directory) { 3626ed1f85cSDaniel Jasper ErrorMessage = "Missing key: \"directory\"."; 3636ed1f85cSDaniel Jasper return false; 3646ed1f85cSDaniel Jasper } 365f857950dSDmitri Gribenko SmallString<8> FileStorage; 36626cf9c43SDaniel Jasper StringRef FileName = File->getValue(FileStorage); 367f857950dSDmitri Gribenko SmallString<128> NativeFilePath; 36826cf9c43SDaniel Jasper if (llvm::sys::path::is_relative(FileName)) { 369f857950dSDmitri Gribenko SmallString<8> DirectoryStorage; 370f857950dSDmitri Gribenko SmallString<128> AbsolutePath( 37126cf9c43SDaniel Jasper Directory->getValue(DirectoryStorage)); 37226cf9c43SDaniel Jasper llvm::sys::path::append(AbsolutePath, FileName); 37392e1b62dSYaron Keren llvm::sys::path::native(AbsolutePath, NativeFilePath); 37426cf9c43SDaniel Jasper } else { 37526cf9c43SDaniel Jasper llvm::sys::path::native(FileName, NativeFilePath); 37626cf9c43SDaniel Jasper } 377399aea30SJoerg Sonnenberger auto Cmd = CompileCommandRef(Directory, File, *Command, Output); 37864f67be3SArgyrios Kyrtzidis IndexByFile[NativeFilePath].push_back(Cmd); 37964f67be3SArgyrios Kyrtzidis AllCommands.push_back(Cmd); 38092e1b62dSYaron Keren MatchTrie.insert(NativeFilePath); 3816ed1f85cSDaniel Jasper } 3826ed1f85cSDaniel Jasper return true; 3836ed1f85cSDaniel Jasper } 384