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" 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) { 193*fdbb6185SSam McCall // Don't mmap: if we're a long-lived process, the build system may overwrite. 1942d2b420aSRafael Espindola llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> DatabaseBuffer = 195*fdbb6185SSam McCall llvm::MemoryBuffer::getFile(FilePath, /*FileSize=*/-1, 196*fdbb6185SSam McCall /*RequiresNullTerminator=*/true, 197*fdbb6185SSam McCall /*IsVolatile=*/true); 1982d2b420aSRafael Espindola if (std::error_code Result = DatabaseBuffer.getError()) { 1996ed1f85cSDaniel Jasper ErrorMessage = "Error while opening JSON database: " + Result.message(); 200ccbc35edSCraig Topper return nullptr; 2016ed1f85cSDaniel Jasper } 202b8984329SAhmed Charles std::unique_ptr<JSONCompilationDatabase> Database( 2039e60a2adSZachary Turner new JSONCompilationDatabase(std::move(*DatabaseBuffer), Syntax)); 2046ed1f85cSDaniel Jasper if (!Database->parse(ErrorMessage)) 205ccbc35edSCraig Topper return nullptr; 206cdba84c0SDavid Blaikie return Database; 2076ed1f85cSDaniel Jasper } 2086ed1f85cSDaniel Jasper 209cdba84c0SDavid Blaikie std::unique_ptr<JSONCompilationDatabase> 2106ed1f85cSDaniel Jasper JSONCompilationDatabase::loadFromBuffer(StringRef DatabaseString, 2119e60a2adSZachary Turner std::string &ErrorMessage, 2129e60a2adSZachary Turner JSONCommandLineSyntax Syntax) { 213b8984329SAhmed Charles std::unique_ptr<llvm::MemoryBuffer> DatabaseBuffer( 2146ed1f85cSDaniel Jasper llvm::MemoryBuffer::getMemBuffer(DatabaseString)); 215b8984329SAhmed Charles std::unique_ptr<JSONCompilationDatabase> Database( 2169e60a2adSZachary Turner new JSONCompilationDatabase(std::move(DatabaseBuffer), Syntax)); 2176ed1f85cSDaniel Jasper if (!Database->parse(ErrorMessage)) 218ccbc35edSCraig Topper return nullptr; 219cdba84c0SDavid Blaikie return Database; 2206ed1f85cSDaniel Jasper } 2216ed1f85cSDaniel Jasper 2226ed1f85cSDaniel Jasper std::vector<CompileCommand> 2236ed1f85cSDaniel Jasper JSONCompilationDatabase::getCompileCommands(StringRef FilePath) const { 224f857950dSDmitri Gribenko SmallString<128> NativeFilePath; 2256ed1f85cSDaniel Jasper llvm::sys::path::native(FilePath, NativeFilePath); 226965f8825SAlp Toker 22726cf9c43SDaniel Jasper std::string Error; 22826cf9c43SDaniel Jasper llvm::raw_string_ostream ES(Error); 22992e1b62dSYaron Keren StringRef Match = MatchTrie.findEquivalent(NativeFilePath, ES); 2303128a11eSArnaud A. de Grandmaison if (Match.empty()) 2316366efedSEugene Zelenko return {}; 2326366efedSEugene Zelenko const auto CommandsRefI = IndexByFile.find(Match); 2336ed1f85cSDaniel Jasper if (CommandsRefI == IndexByFile.end()) 2346366efedSEugene Zelenko return {}; 2356ed1f85cSDaniel Jasper std::vector<CompileCommand> Commands; 236251ad5e0SArgyrios Kyrtzidis getCommands(CommandsRefI->getValue(), Commands); 2376ed1f85cSDaniel Jasper return Commands; 2386ed1f85cSDaniel Jasper } 2396ed1f85cSDaniel Jasper 2406ed1f85cSDaniel Jasper std::vector<std::string> 2416ed1f85cSDaniel Jasper JSONCompilationDatabase::getAllFiles() const { 2426ed1f85cSDaniel Jasper std::vector<std::string> Result; 2436366efedSEugene Zelenko for (const auto &CommandRef : IndexByFile) 2446366efedSEugene Zelenko Result.push_back(CommandRef.first().str()); 2456ed1f85cSDaniel Jasper return Result; 2466ed1f85cSDaniel Jasper } 2476ed1f85cSDaniel Jasper 248251ad5e0SArgyrios Kyrtzidis std::vector<CompileCommand> 249251ad5e0SArgyrios Kyrtzidis JSONCompilationDatabase::getAllCompileCommands() const { 250251ad5e0SArgyrios Kyrtzidis std::vector<CompileCommand> Commands; 25164f67be3SArgyrios Kyrtzidis getCommands(AllCommands, Commands); 252251ad5e0SArgyrios Kyrtzidis return Commands; 253251ad5e0SArgyrios Kyrtzidis } 254251ad5e0SArgyrios Kyrtzidis 2553ecd8c0aSManuel Klimek static std::vector<std::string> 2569e60a2adSZachary Turner nodeToCommandLine(JSONCommandLineSyntax Syntax, 2579e60a2adSZachary Turner const std::vector<llvm::yaml::ScalarNode *> &Nodes) { 2583ecd8c0aSManuel Klimek SmallString<1024> Storage; 2596366efedSEugene Zelenko if (Nodes.size() == 1) 2609e60a2adSZachary Turner return unescapeCommandLine(Syntax, Nodes[0]->getValue(Storage)); 2613ecd8c0aSManuel Klimek std::vector<std::string> Arguments; 2626366efedSEugene Zelenko for (const auto *Node : Nodes) 2633ecd8c0aSManuel Klimek Arguments.push_back(Node->getValue(Storage)); 2643ecd8c0aSManuel Klimek return Arguments; 2653ecd8c0aSManuel Klimek } 2663ecd8c0aSManuel Klimek 267251ad5e0SArgyrios Kyrtzidis void JSONCompilationDatabase::getCommands( 268251ad5e0SArgyrios Kyrtzidis ArrayRef<CompileCommandRef> CommandsRef, 269251ad5e0SArgyrios Kyrtzidis std::vector<CompileCommand> &Commands) const { 2706366efedSEugene Zelenko for (const auto &CommandRef : CommandsRef) { 271f857950dSDmitri Gribenko SmallString<8> DirectoryStorage; 27274bcd21eSArgyrios Kyrtzidis SmallString<32> FilenameStorage; 273399aea30SJoerg Sonnenberger SmallString<32> OutputStorage; 2746366efedSEugene Zelenko auto Output = std::get<3>(CommandRef); 27574bcd21eSArgyrios Kyrtzidis Commands.emplace_back( 2766366efedSEugene Zelenko std::get<0>(CommandRef)->getValue(DirectoryStorage), 2776366efedSEugene Zelenko std::get<1>(CommandRef)->getValue(FilenameStorage), 2786366efedSEugene Zelenko nodeToCommandLine(Syntax, std::get<2>(CommandRef)), 279399aea30SJoerg Sonnenberger Output ? Output->getValue(OutputStorage) : ""); 280251ad5e0SArgyrios Kyrtzidis } 281251ad5e0SArgyrios Kyrtzidis } 282251ad5e0SArgyrios Kyrtzidis 2836ed1f85cSDaniel Jasper bool JSONCompilationDatabase::parse(std::string &ErrorMessage) { 2846ed1f85cSDaniel Jasper llvm::yaml::document_iterator I = YAMLStream.begin(); 2856ed1f85cSDaniel Jasper if (I == YAMLStream.end()) { 2866ed1f85cSDaniel Jasper ErrorMessage = "Error while parsing YAML."; 2876ed1f85cSDaniel Jasper return false; 2886ed1f85cSDaniel Jasper } 2896ed1f85cSDaniel Jasper llvm::yaml::Node *Root = I->getRoot(); 290ccbc35edSCraig Topper if (!Root) { 2916ed1f85cSDaniel Jasper ErrorMessage = "Error while parsing YAML."; 2926ed1f85cSDaniel Jasper return false; 2936ed1f85cSDaniel Jasper } 2946366efedSEugene Zelenko auto *Array = dyn_cast<llvm::yaml::SequenceNode>(Root); 295ccbc35edSCraig Topper if (!Array) { 2966ed1f85cSDaniel Jasper ErrorMessage = "Expected array."; 2976ed1f85cSDaniel Jasper return false; 2986ed1f85cSDaniel Jasper } 29954042e74SManuel Klimek for (auto &NextObject : *Array) { 3006366efedSEugene Zelenko auto *Object = dyn_cast<llvm::yaml::MappingNode>(&NextObject); 301ccbc35edSCraig Topper if (!Object) { 3026ed1f85cSDaniel Jasper ErrorMessage = "Expected object."; 3036ed1f85cSDaniel Jasper return false; 3046ed1f85cSDaniel Jasper } 305ccbc35edSCraig Topper llvm::yaml::ScalarNode *Directory = nullptr; 3063ecd8c0aSManuel Klimek llvm::Optional<std::vector<llvm::yaml::ScalarNode *>> Command; 307ccbc35edSCraig Topper llvm::yaml::ScalarNode *File = nullptr; 308399aea30SJoerg Sonnenberger llvm::yaml::ScalarNode *Output = nullptr; 30954042e74SManuel Klimek for (auto& NextKeyValue : *Object) { 3106366efedSEugene Zelenko auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey()); 31154042e74SManuel Klimek if (!KeyString) { 31254042e74SManuel Klimek ErrorMessage = "Expected strings as key."; 31354042e74SManuel Klimek return false; 31454042e74SManuel Klimek } 31554042e74SManuel Klimek SmallString<10> KeyStorage; 31654042e74SManuel Klimek StringRef KeyValue = KeyString->getValue(KeyStorage); 31754042e74SManuel Klimek llvm::yaml::Node *Value = NextKeyValue.getValue(); 318ccbc35edSCraig Topper if (!Value) { 3196ed1f85cSDaniel Jasper ErrorMessage = "Expected value."; 3206ed1f85cSDaniel Jasper return false; 3216ed1f85cSDaniel Jasper } 3226366efedSEugene Zelenko auto *ValueString = dyn_cast<llvm::yaml::ScalarNode>(Value); 3236366efedSEugene Zelenko auto *SequenceString = dyn_cast<llvm::yaml::SequenceNode>(Value); 32454042e74SManuel Klimek if (KeyValue == "arguments" && !SequenceString) { 32554042e74SManuel Klimek ErrorMessage = "Expected sequence as value."; 32654042e74SManuel Klimek return false; 32754042e74SManuel Klimek } else if (KeyValue != "arguments" && !ValueString) { 3286ed1f85cSDaniel Jasper ErrorMessage = "Expected string as value."; 3296ed1f85cSDaniel Jasper return false; 3306ed1f85cSDaniel Jasper } 33154042e74SManuel Klimek if (KeyValue == "directory") { 3326ed1f85cSDaniel Jasper Directory = ValueString; 33354042e74SManuel Klimek } else if (KeyValue == "arguments") { 3343ecd8c0aSManuel Klimek Command = std::vector<llvm::yaml::ScalarNode *>(); 3353ecd8c0aSManuel Klimek for (auto &Argument : *SequenceString) { 3366366efedSEugene Zelenko auto *Scalar = dyn_cast<llvm::yaml::ScalarNode>(&Argument); 3373ecd8c0aSManuel Klimek if (!Scalar) { 3383ecd8c0aSManuel Klimek ErrorMessage = "Only strings are allowed in 'arguments'."; 3393ecd8c0aSManuel Klimek return false; 34054042e74SManuel Klimek } 3413ecd8c0aSManuel Klimek Command->push_back(Scalar); 3423ecd8c0aSManuel Klimek } 34354042e74SManuel Klimek } else if (KeyValue == "command") { 3443ecd8c0aSManuel Klimek if (!Command) 3453ecd8c0aSManuel Klimek Command = std::vector<llvm::yaml::ScalarNode *>(1, ValueString); 34654042e74SManuel Klimek } else if (KeyValue == "file") { 3476ed1f85cSDaniel Jasper File = ValueString; 348399aea30SJoerg Sonnenberger } else if (KeyValue == "output") { 349399aea30SJoerg Sonnenberger Output = ValueString; 3506ed1f85cSDaniel Jasper } else { 3516ed1f85cSDaniel Jasper ErrorMessage = ("Unknown key: \"" + 3526ed1f85cSDaniel Jasper KeyString->getRawValue() + "\"").str(); 3536ed1f85cSDaniel Jasper return false; 3546ed1f85cSDaniel Jasper } 3556ed1f85cSDaniel Jasper } 3566ed1f85cSDaniel Jasper if (!File) { 3576ed1f85cSDaniel Jasper ErrorMessage = "Missing key: \"file\"."; 3586ed1f85cSDaniel Jasper return false; 3596ed1f85cSDaniel Jasper } 3603ecd8c0aSManuel Klimek if (!Command) { 36154042e74SManuel Klimek ErrorMessage = "Missing key: \"command\" or \"arguments\"."; 3626ed1f85cSDaniel Jasper return false; 3636ed1f85cSDaniel Jasper } 3646ed1f85cSDaniel Jasper if (!Directory) { 3656ed1f85cSDaniel Jasper ErrorMessage = "Missing key: \"directory\"."; 3666ed1f85cSDaniel Jasper return false; 3676ed1f85cSDaniel Jasper } 368f857950dSDmitri Gribenko SmallString<8> FileStorage; 36926cf9c43SDaniel Jasper StringRef FileName = File->getValue(FileStorage); 370f857950dSDmitri Gribenko SmallString<128> NativeFilePath; 37126cf9c43SDaniel Jasper if (llvm::sys::path::is_relative(FileName)) { 372f857950dSDmitri Gribenko SmallString<8> DirectoryStorage; 373f857950dSDmitri Gribenko SmallString<128> AbsolutePath( 37426cf9c43SDaniel Jasper Directory->getValue(DirectoryStorage)); 37526cf9c43SDaniel Jasper llvm::sys::path::append(AbsolutePath, FileName); 3767ec1ec10SKadir Cetinkaya llvm::sys::path::remove_dots(AbsolutePath, /*remove_dot_dot=*/ true); 37792e1b62dSYaron Keren llvm::sys::path::native(AbsolutePath, NativeFilePath); 37826cf9c43SDaniel Jasper } else { 37926cf9c43SDaniel Jasper llvm::sys::path::native(FileName, NativeFilePath); 38026cf9c43SDaniel Jasper } 381399aea30SJoerg Sonnenberger auto Cmd = CompileCommandRef(Directory, File, *Command, Output); 38264f67be3SArgyrios Kyrtzidis IndexByFile[NativeFilePath].push_back(Cmd); 38364f67be3SArgyrios Kyrtzidis AllCommands.push_back(Cmd); 38492e1b62dSYaron Keren MatchTrie.insert(NativeFilePath); 3856ed1f85cSDaniel Jasper } 3866ed1f85cSDaniel Jasper return true; 3876ed1f85cSDaniel Jasper } 388