16366efedSEugene Zelenko //===- JSONCompilationDatabase.cpp ----------------------------------------===// 26ed1f85cSDaniel Jasper // 36ed1f85cSDaniel Jasper // The LLVM Compiler Infrastructure 46ed1f85cSDaniel Jasper // 56ed1f85cSDaniel Jasper // This file is distributed under the University of Illinois Open Source 66ed1f85cSDaniel Jasper // License. See LICENSE.TXT for details. 76ed1f85cSDaniel Jasper // 86ed1f85cSDaniel Jasper //===----------------------------------------------------------------------===// 96ed1f85cSDaniel Jasper // 106ed1f85cSDaniel Jasper // This file contains the implementation of the JSONCompilationDatabase. 116ed1f85cSDaniel Jasper // 126ed1f85cSDaniel Jasper //===----------------------------------------------------------------------===// 136ed1f85cSDaniel Jasper 146ed1f85cSDaniel Jasper #include "clang/Tooling/JSONCompilationDatabase.h" 156366efedSEugene Zelenko #include "clang/Basic/LLVM.h" 166ed1f85cSDaniel Jasper #include "clang/Tooling/CompilationDatabase.h" 176ed1f85cSDaniel Jasper #include "clang/Tooling/CompilationDatabasePluginRegistry.h" 186366efedSEugene Zelenko #include "llvm/ADT/Optional.h" 196ed1f85cSDaniel Jasper #include "llvm/ADT/SmallString.h" 206366efedSEugene Zelenko #include "llvm/ADT/SmallVector.h" 216366efedSEugene Zelenko #include "llvm/ADT/StringRef.h" 226366efedSEugene Zelenko #include "llvm/ADT/Triple.h" 239e60a2adSZachary Turner #include "llvm/Support/Allocator.h" 246366efedSEugene Zelenko #include "llvm/Support/Casting.h" 259e60a2adSZachary Turner #include "llvm/Support/CommandLine.h" 266366efedSEugene Zelenko #include "llvm/Support/ErrorOr.h" 276366efedSEugene Zelenko #include "llvm/Support/Host.h" 286366efedSEugene Zelenko #include "llvm/Support/MemoryBuffer.h" 296ed1f85cSDaniel Jasper #include "llvm/Support/Path.h" 309e60a2adSZachary Turner #include "llvm/Support/StringSaver.h" 316366efedSEugene Zelenko #include "llvm/Support/YAMLParser.h" 326366efedSEugene Zelenko #include "llvm/Support/raw_ostream.h" 336366efedSEugene Zelenko #include <cassert> 346366efedSEugene Zelenko #include <memory> 356366efedSEugene Zelenko #include <string> 368a8e554aSRafael Espindola #include <system_error> 376366efedSEugene Zelenko #include <tuple> 386366efedSEugene Zelenko #include <utility> 396366efedSEugene Zelenko #include <vector> 406ed1f85cSDaniel Jasper 416366efedSEugene Zelenko using namespace clang; 426366efedSEugene Zelenko using namespace tooling; 436ed1f85cSDaniel Jasper 446ed1f85cSDaniel Jasper namespace { 456ed1f85cSDaniel Jasper 469fc8faf9SAdrian Prantl /// A parser for escaped strings of command line arguments. 476ed1f85cSDaniel Jasper /// 486ed1f85cSDaniel Jasper /// Assumes \-escaping for quoted arguments (see the documentation of 496ed1f85cSDaniel Jasper /// unescapeCommandLine(...)). 506ed1f85cSDaniel Jasper class CommandLineArgumentParser { 516ed1f85cSDaniel Jasper public: 526ed1f85cSDaniel Jasper CommandLineArgumentParser(StringRef CommandLine) 536ed1f85cSDaniel Jasper : Input(CommandLine), Position(Input.begin()-1) {} 546ed1f85cSDaniel Jasper 556ed1f85cSDaniel Jasper std::vector<std::string> parse() { 566ed1f85cSDaniel Jasper bool HasMoreInput = true; 576ed1f85cSDaniel Jasper while (HasMoreInput && nextNonWhitespace()) { 586ed1f85cSDaniel Jasper std::string Argument; 596ed1f85cSDaniel Jasper HasMoreInput = parseStringInto(Argument); 606ed1f85cSDaniel Jasper CommandLine.push_back(Argument); 616ed1f85cSDaniel Jasper } 626ed1f85cSDaniel Jasper return CommandLine; 636ed1f85cSDaniel Jasper } 646ed1f85cSDaniel Jasper 656ed1f85cSDaniel Jasper private: 666ed1f85cSDaniel Jasper // All private methods return true if there is more input available. 676ed1f85cSDaniel Jasper 686ed1f85cSDaniel Jasper bool parseStringInto(std::string &String) { 696ed1f85cSDaniel Jasper do { 706ed1f85cSDaniel Jasper if (*Position == '"') { 71fe7a3486SPeter Collingbourne if (!parseDoubleQuotedStringInto(String)) return false; 72fe7a3486SPeter Collingbourne } else if (*Position == '\'') { 73fe7a3486SPeter Collingbourne if (!parseSingleQuotedStringInto(String)) return false; 746ed1f85cSDaniel Jasper } else { 756ed1f85cSDaniel Jasper if (!parseFreeStringInto(String)) return false; 766ed1f85cSDaniel Jasper } 776ed1f85cSDaniel Jasper } while (*Position != ' '); 786ed1f85cSDaniel Jasper return true; 796ed1f85cSDaniel Jasper } 806ed1f85cSDaniel Jasper 81fe7a3486SPeter Collingbourne bool parseDoubleQuotedStringInto(std::string &String) { 826ed1f85cSDaniel Jasper if (!next()) return false; 836ed1f85cSDaniel Jasper while (*Position != '"') { 846ed1f85cSDaniel Jasper if (!skipEscapeCharacter()) return false; 856ed1f85cSDaniel Jasper String.push_back(*Position); 866ed1f85cSDaniel Jasper if (!next()) return false; 876ed1f85cSDaniel Jasper } 886ed1f85cSDaniel Jasper return next(); 896ed1f85cSDaniel Jasper } 906ed1f85cSDaniel Jasper 91fe7a3486SPeter Collingbourne bool parseSingleQuotedStringInto(std::string &String) { 92fe7a3486SPeter Collingbourne if (!next()) return false; 93fe7a3486SPeter Collingbourne while (*Position != '\'') { 94fe7a3486SPeter Collingbourne String.push_back(*Position); 95fe7a3486SPeter Collingbourne if (!next()) return false; 96fe7a3486SPeter Collingbourne } 97fe7a3486SPeter Collingbourne return next(); 98fe7a3486SPeter Collingbourne } 99fe7a3486SPeter Collingbourne 1006ed1f85cSDaniel Jasper bool parseFreeStringInto(std::string &String) { 1016ed1f85cSDaniel Jasper do { 1026ed1f85cSDaniel Jasper if (!skipEscapeCharacter()) return false; 1036ed1f85cSDaniel Jasper String.push_back(*Position); 1046ed1f85cSDaniel Jasper if (!next()) return false; 105fe7a3486SPeter Collingbourne } while (*Position != ' ' && *Position != '"' && *Position != '\''); 1066ed1f85cSDaniel Jasper return true; 1076ed1f85cSDaniel Jasper } 1086ed1f85cSDaniel Jasper 1096ed1f85cSDaniel Jasper bool skipEscapeCharacter() { 1106ed1f85cSDaniel Jasper if (*Position == '\\') { 1116ed1f85cSDaniel Jasper return next(); 1126ed1f85cSDaniel Jasper } 1136ed1f85cSDaniel Jasper return true; 1146ed1f85cSDaniel Jasper } 1156ed1f85cSDaniel Jasper 1166ed1f85cSDaniel Jasper bool nextNonWhitespace() { 1176ed1f85cSDaniel Jasper do { 1186ed1f85cSDaniel Jasper if (!next()) return false; 1196ed1f85cSDaniel Jasper } while (*Position == ' '); 1206ed1f85cSDaniel Jasper return true; 1216ed1f85cSDaniel Jasper } 1226ed1f85cSDaniel Jasper 1236ed1f85cSDaniel Jasper bool next() { 1246ed1f85cSDaniel Jasper ++Position; 1256ed1f85cSDaniel Jasper return Position != Input.end(); 1266ed1f85cSDaniel Jasper } 1276ed1f85cSDaniel Jasper 1286ed1f85cSDaniel Jasper const StringRef Input; 1296ed1f85cSDaniel Jasper StringRef::iterator Position; 1306ed1f85cSDaniel Jasper std::vector<std::string> CommandLine; 1316ed1f85cSDaniel Jasper }; 1326ed1f85cSDaniel Jasper 1339e60a2adSZachary Turner std::vector<std::string> unescapeCommandLine(JSONCommandLineSyntax Syntax, 1346ed1f85cSDaniel Jasper StringRef EscapedCommandLine) { 1359e60a2adSZachary Turner if (Syntax == JSONCommandLineSyntax::AutoDetect) { 13685d0f314SZachary Turner Syntax = JSONCommandLineSyntax::Gnu; 1379e60a2adSZachary Turner llvm::Triple Triple(llvm::sys::getProcessTriple()); 1389e60a2adSZachary Turner if (Triple.getOS() == llvm::Triple::OSType::Win32) { 1399e60a2adSZachary Turner // Assume Windows command line parsing on Win32 unless the triple 14085d0f314SZachary Turner // explicitly tells us otherwise. 1419e60a2adSZachary Turner if (!Triple.hasEnvironment() || 1429e60a2adSZachary Turner Triple.getEnvironment() == llvm::Triple::EnvironmentType::MSVC) 1439e60a2adSZachary Turner Syntax = JSONCommandLineSyntax::Windows; 1449e60a2adSZachary Turner } 1459e60a2adSZachary Turner } 1469e60a2adSZachary Turner 1479e60a2adSZachary Turner if (Syntax == JSONCommandLineSyntax::Windows) { 1489e60a2adSZachary Turner llvm::BumpPtrAllocator Alloc; 1499e60a2adSZachary Turner llvm::StringSaver Saver(Alloc); 1509e60a2adSZachary Turner llvm::SmallVector<const char *, 64> T; 1519e60a2adSZachary Turner llvm::cl::TokenizeWindowsCommandLine(EscapedCommandLine, Saver, T); 1529e60a2adSZachary Turner std::vector<std::string> Result(T.begin(), T.end()); 1539e60a2adSZachary Turner return Result; 1549e60a2adSZachary Turner } 1559e60a2adSZachary Turner assert(Syntax == JSONCommandLineSyntax::Gnu); 1566ed1f85cSDaniel Jasper CommandLineArgumentParser parser(EscapedCommandLine); 1576ed1f85cSDaniel Jasper return parser.parse(); 1586ed1f85cSDaniel Jasper } 1596ed1f85cSDaniel Jasper 160*9d3530bdSSam McCall // This plugin locates a nearby compile_command.json file, and also infers 161*9d3530bdSSam McCall // compile commands for files not present in the database. 1626ed1f85cSDaniel Jasper class JSONCompilationDatabasePlugin : public CompilationDatabasePlugin { 163cdba84c0SDavid Blaikie std::unique_ptr<CompilationDatabase> 164cdba84c0SDavid Blaikie loadFromDirectory(StringRef Directory, std::string &ErrorMessage) override { 165f857950dSDmitri Gribenko SmallString<1024> JSONDatabasePath(Directory); 1666ed1f85cSDaniel Jasper llvm::sys::path::append(JSONDatabasePath, "compile_commands.json"); 167*9d3530bdSSam McCall auto Base = JSONCompilationDatabase::loadFromFile( 168024b0644SKrasimir Georgiev JSONDatabasePath, ErrorMessage, JSONCommandLineSyntax::AutoDetect); 169*9d3530bdSSam McCall return Base ? inferMissingCompileCommands(std::move(Base)) : nullptr; 1706ed1f85cSDaniel Jasper } 1716ed1f85cSDaniel Jasper }; 1726ed1f85cSDaniel Jasper 1736366efedSEugene Zelenko } // namespace 17469b6277aSCraig Topper 1756ed1f85cSDaniel Jasper // Register the JSONCompilationDatabasePlugin with the 1766ed1f85cSDaniel Jasper // CompilationDatabasePluginRegistry using this statically initialized variable. 1776ed1f85cSDaniel Jasper static CompilationDatabasePluginRegistry::Add<JSONCompilationDatabasePlugin> 1786ed1f85cSDaniel Jasper X("json-compilation-database", "Reads JSON formatted compilation databases"); 1796ed1f85cSDaniel Jasper 1806366efedSEugene Zelenko namespace clang { 1816366efedSEugene Zelenko namespace tooling { 1826366efedSEugene Zelenko 1836ed1f85cSDaniel Jasper // This anchor is used to force the linker to link in the generated object file 1846ed1f85cSDaniel Jasper // and thus register the JSONCompilationDatabasePlugin. 185d574ac2fSNAKAMURA Takumi volatile int JSONAnchorSource = 0; 1866ed1f85cSDaniel Jasper 1876366efedSEugene Zelenko } // namespace tooling 1886366efedSEugene Zelenko } // namespace clang 1896366efedSEugene Zelenko 190cdba84c0SDavid Blaikie std::unique_ptr<JSONCompilationDatabase> 1916ed1f85cSDaniel Jasper JSONCompilationDatabase::loadFromFile(StringRef FilePath, 1929e60a2adSZachary Turner std::string &ErrorMessage, 1939e60a2adSZachary Turner JSONCommandLineSyntax Syntax) { 1942d2b420aSRafael Espindola llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> DatabaseBuffer = 1952d2b420aSRafael Espindola llvm::MemoryBuffer::getFile(FilePath); 1962d2b420aSRafael Espindola if (std::error_code Result = DatabaseBuffer.getError()) { 1976ed1f85cSDaniel Jasper ErrorMessage = "Error while opening JSON database: " + Result.message(); 198ccbc35edSCraig Topper return nullptr; 1996ed1f85cSDaniel Jasper } 200b8984329SAhmed Charles std::unique_ptr<JSONCompilationDatabase> Database( 2019e60a2adSZachary Turner new JSONCompilationDatabase(std::move(*DatabaseBuffer), Syntax)); 2026ed1f85cSDaniel Jasper if (!Database->parse(ErrorMessage)) 203ccbc35edSCraig Topper return nullptr; 204cdba84c0SDavid Blaikie return Database; 2056ed1f85cSDaniel Jasper } 2066ed1f85cSDaniel Jasper 207cdba84c0SDavid Blaikie std::unique_ptr<JSONCompilationDatabase> 2086ed1f85cSDaniel Jasper JSONCompilationDatabase::loadFromBuffer(StringRef DatabaseString, 2099e60a2adSZachary Turner std::string &ErrorMessage, 2109e60a2adSZachary Turner JSONCommandLineSyntax Syntax) { 211b8984329SAhmed Charles std::unique_ptr<llvm::MemoryBuffer> DatabaseBuffer( 2126ed1f85cSDaniel Jasper llvm::MemoryBuffer::getMemBuffer(DatabaseString)); 213b8984329SAhmed Charles std::unique_ptr<JSONCompilationDatabase> Database( 2149e60a2adSZachary Turner new JSONCompilationDatabase(std::move(DatabaseBuffer), Syntax)); 2156ed1f85cSDaniel Jasper if (!Database->parse(ErrorMessage)) 216ccbc35edSCraig Topper return nullptr; 217cdba84c0SDavid Blaikie return Database; 2186ed1f85cSDaniel Jasper } 2196ed1f85cSDaniel Jasper 2206ed1f85cSDaniel Jasper std::vector<CompileCommand> 2216ed1f85cSDaniel Jasper JSONCompilationDatabase::getCompileCommands(StringRef FilePath) const { 222f857950dSDmitri Gribenko SmallString<128> NativeFilePath; 2236ed1f85cSDaniel Jasper llvm::sys::path::native(FilePath, NativeFilePath); 224965f8825SAlp Toker 22526cf9c43SDaniel Jasper std::string Error; 22626cf9c43SDaniel Jasper llvm::raw_string_ostream ES(Error); 22792e1b62dSYaron Keren StringRef Match = MatchTrie.findEquivalent(NativeFilePath, ES); 2283128a11eSArnaud A. de Grandmaison if (Match.empty()) 2296366efedSEugene Zelenko return {}; 2306366efedSEugene Zelenko const auto CommandsRefI = IndexByFile.find(Match); 2316ed1f85cSDaniel Jasper if (CommandsRefI == IndexByFile.end()) 2326366efedSEugene Zelenko return {}; 2336ed1f85cSDaniel Jasper std::vector<CompileCommand> Commands; 234251ad5e0SArgyrios Kyrtzidis getCommands(CommandsRefI->getValue(), Commands); 2356ed1f85cSDaniel Jasper return Commands; 2366ed1f85cSDaniel Jasper } 2376ed1f85cSDaniel Jasper 2386ed1f85cSDaniel Jasper std::vector<std::string> 2396ed1f85cSDaniel Jasper JSONCompilationDatabase::getAllFiles() const { 2406ed1f85cSDaniel Jasper std::vector<std::string> Result; 2416366efedSEugene Zelenko for (const auto &CommandRef : IndexByFile) 2426366efedSEugene Zelenko Result.push_back(CommandRef.first().str()); 2436ed1f85cSDaniel Jasper return Result; 2446ed1f85cSDaniel Jasper } 2456ed1f85cSDaniel Jasper 246251ad5e0SArgyrios Kyrtzidis std::vector<CompileCommand> 247251ad5e0SArgyrios Kyrtzidis JSONCompilationDatabase::getAllCompileCommands() const { 248251ad5e0SArgyrios Kyrtzidis std::vector<CompileCommand> Commands; 24964f67be3SArgyrios Kyrtzidis getCommands(AllCommands, Commands); 250251ad5e0SArgyrios Kyrtzidis return Commands; 251251ad5e0SArgyrios Kyrtzidis } 252251ad5e0SArgyrios Kyrtzidis 2533ecd8c0aSManuel Klimek static std::vector<std::string> 2549e60a2adSZachary Turner nodeToCommandLine(JSONCommandLineSyntax Syntax, 2559e60a2adSZachary Turner const std::vector<llvm::yaml::ScalarNode *> &Nodes) { 2563ecd8c0aSManuel Klimek SmallString<1024> Storage; 2576366efedSEugene Zelenko if (Nodes.size() == 1) 2589e60a2adSZachary Turner return unescapeCommandLine(Syntax, Nodes[0]->getValue(Storage)); 2593ecd8c0aSManuel Klimek std::vector<std::string> Arguments; 2606366efedSEugene Zelenko for (const auto *Node : Nodes) 2613ecd8c0aSManuel Klimek Arguments.push_back(Node->getValue(Storage)); 2623ecd8c0aSManuel Klimek return Arguments; 2633ecd8c0aSManuel Klimek } 2643ecd8c0aSManuel Klimek 265251ad5e0SArgyrios Kyrtzidis void JSONCompilationDatabase::getCommands( 266251ad5e0SArgyrios Kyrtzidis ArrayRef<CompileCommandRef> CommandsRef, 267251ad5e0SArgyrios Kyrtzidis std::vector<CompileCommand> &Commands) const { 2686366efedSEugene Zelenko for (const auto &CommandRef : CommandsRef) { 269f857950dSDmitri Gribenko SmallString<8> DirectoryStorage; 27074bcd21eSArgyrios Kyrtzidis SmallString<32> FilenameStorage; 271399aea30SJoerg Sonnenberger SmallString<32> OutputStorage; 2726366efedSEugene Zelenko auto Output = std::get<3>(CommandRef); 27374bcd21eSArgyrios Kyrtzidis Commands.emplace_back( 2746366efedSEugene Zelenko std::get<0>(CommandRef)->getValue(DirectoryStorage), 2756366efedSEugene Zelenko std::get<1>(CommandRef)->getValue(FilenameStorage), 2766366efedSEugene Zelenko nodeToCommandLine(Syntax, std::get<2>(CommandRef)), 277399aea30SJoerg Sonnenberger Output ? Output->getValue(OutputStorage) : ""); 278251ad5e0SArgyrios Kyrtzidis } 279251ad5e0SArgyrios Kyrtzidis } 280251ad5e0SArgyrios Kyrtzidis 2816ed1f85cSDaniel Jasper bool JSONCompilationDatabase::parse(std::string &ErrorMessage) { 2826ed1f85cSDaniel Jasper llvm::yaml::document_iterator I = YAMLStream.begin(); 2836ed1f85cSDaniel Jasper if (I == YAMLStream.end()) { 2846ed1f85cSDaniel Jasper ErrorMessage = "Error while parsing YAML."; 2856ed1f85cSDaniel Jasper return false; 2866ed1f85cSDaniel Jasper } 2876ed1f85cSDaniel Jasper llvm::yaml::Node *Root = I->getRoot(); 288ccbc35edSCraig Topper if (!Root) { 2896ed1f85cSDaniel Jasper ErrorMessage = "Error while parsing YAML."; 2906ed1f85cSDaniel Jasper return false; 2916ed1f85cSDaniel Jasper } 2926366efedSEugene Zelenko auto *Array = dyn_cast<llvm::yaml::SequenceNode>(Root); 293ccbc35edSCraig Topper if (!Array) { 2946ed1f85cSDaniel Jasper ErrorMessage = "Expected array."; 2956ed1f85cSDaniel Jasper return false; 2966ed1f85cSDaniel Jasper } 29754042e74SManuel Klimek for (auto &NextObject : *Array) { 2986366efedSEugene Zelenko auto *Object = dyn_cast<llvm::yaml::MappingNode>(&NextObject); 299ccbc35edSCraig Topper if (!Object) { 3006ed1f85cSDaniel Jasper ErrorMessage = "Expected object."; 3016ed1f85cSDaniel Jasper return false; 3026ed1f85cSDaniel Jasper } 303ccbc35edSCraig Topper llvm::yaml::ScalarNode *Directory = nullptr; 3043ecd8c0aSManuel Klimek llvm::Optional<std::vector<llvm::yaml::ScalarNode *>> Command; 305ccbc35edSCraig Topper llvm::yaml::ScalarNode *File = nullptr; 306399aea30SJoerg Sonnenberger llvm::yaml::ScalarNode *Output = nullptr; 30754042e74SManuel Klimek for (auto& NextKeyValue : *Object) { 3086366efedSEugene Zelenko auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey()); 30954042e74SManuel Klimek if (!KeyString) { 31054042e74SManuel Klimek ErrorMessage = "Expected strings as key."; 31154042e74SManuel Klimek return false; 31254042e74SManuel Klimek } 31354042e74SManuel Klimek SmallString<10> KeyStorage; 31454042e74SManuel Klimek StringRef KeyValue = KeyString->getValue(KeyStorage); 31554042e74SManuel Klimek llvm::yaml::Node *Value = NextKeyValue.getValue(); 316ccbc35edSCraig Topper if (!Value) { 3176ed1f85cSDaniel Jasper ErrorMessage = "Expected value."; 3186ed1f85cSDaniel Jasper return false; 3196ed1f85cSDaniel Jasper } 3206366efedSEugene Zelenko auto *ValueString = dyn_cast<llvm::yaml::ScalarNode>(Value); 3216366efedSEugene Zelenko auto *SequenceString = dyn_cast<llvm::yaml::SequenceNode>(Value); 32254042e74SManuel Klimek if (KeyValue == "arguments" && !SequenceString) { 32354042e74SManuel Klimek ErrorMessage = "Expected sequence as value."; 32454042e74SManuel Klimek return false; 32554042e74SManuel Klimek } else if (KeyValue != "arguments" && !ValueString) { 3266ed1f85cSDaniel Jasper ErrorMessage = "Expected string as value."; 3276ed1f85cSDaniel Jasper return false; 3286ed1f85cSDaniel Jasper } 32954042e74SManuel Klimek if (KeyValue == "directory") { 3306ed1f85cSDaniel Jasper Directory = ValueString; 33154042e74SManuel Klimek } else if (KeyValue == "arguments") { 3323ecd8c0aSManuel Klimek Command = std::vector<llvm::yaml::ScalarNode *>(); 3333ecd8c0aSManuel Klimek for (auto &Argument : *SequenceString) { 3346366efedSEugene Zelenko auto *Scalar = dyn_cast<llvm::yaml::ScalarNode>(&Argument); 3353ecd8c0aSManuel Klimek if (!Scalar) { 3363ecd8c0aSManuel Klimek ErrorMessage = "Only strings are allowed in 'arguments'."; 3373ecd8c0aSManuel Klimek return false; 33854042e74SManuel Klimek } 3393ecd8c0aSManuel Klimek Command->push_back(Scalar); 3403ecd8c0aSManuel Klimek } 34154042e74SManuel Klimek } else if (KeyValue == "command") { 3423ecd8c0aSManuel Klimek if (!Command) 3433ecd8c0aSManuel Klimek Command = std::vector<llvm::yaml::ScalarNode *>(1, ValueString); 34454042e74SManuel Klimek } else if (KeyValue == "file") { 3456ed1f85cSDaniel Jasper File = ValueString; 346399aea30SJoerg Sonnenberger } else if (KeyValue == "output") { 347399aea30SJoerg Sonnenberger Output = ValueString; 3486ed1f85cSDaniel Jasper } else { 3496ed1f85cSDaniel Jasper ErrorMessage = ("Unknown key: \"" + 3506ed1f85cSDaniel Jasper KeyString->getRawValue() + "\"").str(); 3516ed1f85cSDaniel Jasper return false; 3526ed1f85cSDaniel Jasper } 3536ed1f85cSDaniel Jasper } 3546ed1f85cSDaniel Jasper if (!File) { 3556ed1f85cSDaniel Jasper ErrorMessage = "Missing key: \"file\"."; 3566ed1f85cSDaniel Jasper return false; 3576ed1f85cSDaniel Jasper } 3583ecd8c0aSManuel Klimek if (!Command) { 35954042e74SManuel Klimek ErrorMessage = "Missing key: \"command\" or \"arguments\"."; 3606ed1f85cSDaniel Jasper return false; 3616ed1f85cSDaniel Jasper } 3626ed1f85cSDaniel Jasper if (!Directory) { 3636ed1f85cSDaniel Jasper ErrorMessage = "Missing key: \"directory\"."; 3646ed1f85cSDaniel Jasper return false; 3656ed1f85cSDaniel Jasper } 366f857950dSDmitri Gribenko SmallString<8> FileStorage; 36726cf9c43SDaniel Jasper StringRef FileName = File->getValue(FileStorage); 368f857950dSDmitri Gribenko SmallString<128> NativeFilePath; 36926cf9c43SDaniel Jasper if (llvm::sys::path::is_relative(FileName)) { 370f857950dSDmitri Gribenko SmallString<8> DirectoryStorage; 371f857950dSDmitri Gribenko SmallString<128> AbsolutePath( 37226cf9c43SDaniel Jasper Directory->getValue(DirectoryStorage)); 37326cf9c43SDaniel Jasper llvm::sys::path::append(AbsolutePath, FileName); 37492e1b62dSYaron Keren llvm::sys::path::native(AbsolutePath, NativeFilePath); 37526cf9c43SDaniel Jasper } else { 37626cf9c43SDaniel Jasper llvm::sys::path::native(FileName, NativeFilePath); 37726cf9c43SDaniel Jasper } 378399aea30SJoerg Sonnenberger auto Cmd = CompileCommandRef(Directory, File, *Command, Output); 37964f67be3SArgyrios Kyrtzidis IndexByFile[NativeFilePath].push_back(Cmd); 38064f67be3SArgyrios Kyrtzidis AllCommands.push_back(Cmd); 38192e1b62dSYaron Keren MatchTrie.insert(NativeFilePath); 3826ed1f85cSDaniel Jasper } 3836ed1f85cSDaniel Jasper return true; 3846ed1f85cSDaniel Jasper } 385