1*6366efedSEugene 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"
15*6366efedSEugene Zelenko #include "clang/Basic/LLVM.h"
166ed1f85cSDaniel Jasper #include "clang/Tooling/CompilationDatabase.h"
176ed1f85cSDaniel Jasper #include "clang/Tooling/CompilationDatabasePluginRegistry.h"
18*6366efedSEugene Zelenko #include "llvm/ADT/Optional.h"
196ed1f85cSDaniel Jasper #include "llvm/ADT/SmallString.h"
20*6366efedSEugene Zelenko #include "llvm/ADT/SmallVector.h"
21*6366efedSEugene Zelenko #include "llvm/ADT/StringRef.h"
22*6366efedSEugene Zelenko #include "llvm/ADT/Triple.h"
239e60a2adSZachary Turner #include "llvm/Support/Allocator.h"
24*6366efedSEugene Zelenko #include "llvm/Support/Casting.h"
259e60a2adSZachary Turner #include "llvm/Support/CommandLine.h"
26*6366efedSEugene Zelenko #include "llvm/Support/ErrorOr.h"
27*6366efedSEugene Zelenko #include "llvm/Support/Host.h"
28*6366efedSEugene Zelenko #include "llvm/Support/MemoryBuffer.h"
296ed1f85cSDaniel Jasper #include "llvm/Support/Path.h"
309e60a2adSZachary Turner #include "llvm/Support/StringSaver.h"
31*6366efedSEugene Zelenko #include "llvm/Support/YAMLParser.h"
32*6366efedSEugene Zelenko #include "llvm/Support/raw_ostream.h"
33*6366efedSEugene Zelenko #include <cassert>
34*6366efedSEugene Zelenko #include <memory>
35*6366efedSEugene Zelenko #include <string>
368a8e554aSRafael Espindola #include <system_error>
37*6366efedSEugene Zelenko #include <tuple>
38*6366efedSEugene Zelenko #include <utility>
39*6366efedSEugene Zelenko #include <vector>
406ed1f85cSDaniel Jasper 
41*6366efedSEugene Zelenko using namespace clang;
42*6366efedSEugene Zelenko using namespace tooling;
436ed1f85cSDaniel Jasper 
446ed1f85cSDaniel Jasper namespace {
456ed1f85cSDaniel Jasper 
466ed1f85cSDaniel Jasper /// \brief 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 
1606ed1f85cSDaniel Jasper class JSONCompilationDatabasePlugin : public CompilationDatabasePlugin {
161cdba84c0SDavid Blaikie   std::unique_ptr<CompilationDatabase>
162cdba84c0SDavid Blaikie   loadFromDirectory(StringRef Directory, std::string &ErrorMessage) override {
163f857950dSDmitri Gribenko     SmallString<1024> JSONDatabasePath(Directory);
1646ed1f85cSDaniel Jasper     llvm::sys::path::append(JSONDatabasePath, "compile_commands.json");
165024b0644SKrasimir Georgiev     return JSONCompilationDatabase::loadFromFile(
166024b0644SKrasimir Georgiev         JSONDatabasePath, ErrorMessage, JSONCommandLineSyntax::AutoDetect);
1676ed1f85cSDaniel Jasper   }
1686ed1f85cSDaniel Jasper };
1696ed1f85cSDaniel Jasper 
170*6366efedSEugene Zelenko } // namespace
17169b6277aSCraig Topper 
1726ed1f85cSDaniel Jasper // Register the JSONCompilationDatabasePlugin with the
1736ed1f85cSDaniel Jasper // CompilationDatabasePluginRegistry using this statically initialized variable.
1746ed1f85cSDaniel Jasper static CompilationDatabasePluginRegistry::Add<JSONCompilationDatabasePlugin>
1756ed1f85cSDaniel Jasper X("json-compilation-database", "Reads JSON formatted compilation databases");
1766ed1f85cSDaniel Jasper 
177*6366efedSEugene Zelenko namespace clang {
178*6366efedSEugene Zelenko namespace tooling {
179*6366efedSEugene Zelenko 
1806ed1f85cSDaniel Jasper // This anchor is used to force the linker to link in the generated object file
1816ed1f85cSDaniel Jasper // and thus register the JSONCompilationDatabasePlugin.
182d574ac2fSNAKAMURA Takumi volatile int JSONAnchorSource = 0;
1836ed1f85cSDaniel Jasper 
184*6366efedSEugene Zelenko } // namespace tooling
185*6366efedSEugene Zelenko } // namespace clang
186*6366efedSEugene Zelenko 
187cdba84c0SDavid Blaikie std::unique_ptr<JSONCompilationDatabase>
1886ed1f85cSDaniel Jasper JSONCompilationDatabase::loadFromFile(StringRef FilePath,
1899e60a2adSZachary Turner                                       std::string &ErrorMessage,
1909e60a2adSZachary Turner                                       JSONCommandLineSyntax Syntax) {
1912d2b420aSRafael Espindola   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> DatabaseBuffer =
1922d2b420aSRafael Espindola       llvm::MemoryBuffer::getFile(FilePath);
1932d2b420aSRafael Espindola   if (std::error_code Result = DatabaseBuffer.getError()) {
1946ed1f85cSDaniel Jasper     ErrorMessage = "Error while opening JSON database: " + Result.message();
195ccbc35edSCraig Topper     return nullptr;
1966ed1f85cSDaniel Jasper   }
197b8984329SAhmed Charles   std::unique_ptr<JSONCompilationDatabase> Database(
1989e60a2adSZachary Turner       new JSONCompilationDatabase(std::move(*DatabaseBuffer), Syntax));
1996ed1f85cSDaniel Jasper   if (!Database->parse(ErrorMessage))
200ccbc35edSCraig Topper     return nullptr;
201cdba84c0SDavid Blaikie   return Database;
2026ed1f85cSDaniel Jasper }
2036ed1f85cSDaniel Jasper 
204cdba84c0SDavid Blaikie std::unique_ptr<JSONCompilationDatabase>
2056ed1f85cSDaniel Jasper JSONCompilationDatabase::loadFromBuffer(StringRef DatabaseString,
2069e60a2adSZachary Turner                                         std::string &ErrorMessage,
2079e60a2adSZachary Turner                                         JSONCommandLineSyntax Syntax) {
208b8984329SAhmed Charles   std::unique_ptr<llvm::MemoryBuffer> DatabaseBuffer(
2096ed1f85cSDaniel Jasper       llvm::MemoryBuffer::getMemBuffer(DatabaseString));
210b8984329SAhmed Charles   std::unique_ptr<JSONCompilationDatabase> Database(
2119e60a2adSZachary Turner       new JSONCompilationDatabase(std::move(DatabaseBuffer), Syntax));
2126ed1f85cSDaniel Jasper   if (!Database->parse(ErrorMessage))
213ccbc35edSCraig Topper     return nullptr;
214cdba84c0SDavid Blaikie   return Database;
2156ed1f85cSDaniel Jasper }
2166ed1f85cSDaniel Jasper 
2176ed1f85cSDaniel Jasper std::vector<CompileCommand>
2186ed1f85cSDaniel Jasper JSONCompilationDatabase::getCompileCommands(StringRef FilePath) const {
219f857950dSDmitri Gribenko   SmallString<128> NativeFilePath;
2206ed1f85cSDaniel Jasper   llvm::sys::path::native(FilePath, NativeFilePath);
221965f8825SAlp Toker 
22226cf9c43SDaniel Jasper   std::string Error;
22326cf9c43SDaniel Jasper   llvm::raw_string_ostream ES(Error);
22492e1b62dSYaron Keren   StringRef Match = MatchTrie.findEquivalent(NativeFilePath, ES);
2253128a11eSArnaud A. de Grandmaison   if (Match.empty())
226*6366efedSEugene Zelenko     return {};
227*6366efedSEugene Zelenko   const auto CommandsRefI = IndexByFile.find(Match);
2286ed1f85cSDaniel Jasper   if (CommandsRefI == IndexByFile.end())
229*6366efedSEugene Zelenko     return {};
2306ed1f85cSDaniel Jasper   std::vector<CompileCommand> Commands;
231251ad5e0SArgyrios Kyrtzidis   getCommands(CommandsRefI->getValue(), Commands);
2326ed1f85cSDaniel Jasper   return Commands;
2336ed1f85cSDaniel Jasper }
2346ed1f85cSDaniel Jasper 
2356ed1f85cSDaniel Jasper std::vector<std::string>
2366ed1f85cSDaniel Jasper JSONCompilationDatabase::getAllFiles() const {
2376ed1f85cSDaniel Jasper   std::vector<std::string> Result;
238*6366efedSEugene Zelenko   for (const auto &CommandRef : IndexByFile)
239*6366efedSEugene Zelenko     Result.push_back(CommandRef.first().str());
2406ed1f85cSDaniel Jasper   return Result;
2416ed1f85cSDaniel Jasper }
2426ed1f85cSDaniel Jasper 
243251ad5e0SArgyrios Kyrtzidis std::vector<CompileCommand>
244251ad5e0SArgyrios Kyrtzidis JSONCompilationDatabase::getAllCompileCommands() const {
245251ad5e0SArgyrios Kyrtzidis   std::vector<CompileCommand> Commands;
24664f67be3SArgyrios Kyrtzidis   getCommands(AllCommands, Commands);
247251ad5e0SArgyrios Kyrtzidis   return Commands;
248251ad5e0SArgyrios Kyrtzidis }
249251ad5e0SArgyrios Kyrtzidis 
2503ecd8c0aSManuel Klimek static std::vector<std::string>
2519e60a2adSZachary Turner nodeToCommandLine(JSONCommandLineSyntax Syntax,
2529e60a2adSZachary Turner                   const std::vector<llvm::yaml::ScalarNode *> &Nodes) {
2533ecd8c0aSManuel Klimek   SmallString<1024> Storage;
254*6366efedSEugene Zelenko   if (Nodes.size() == 1)
2559e60a2adSZachary Turner     return unescapeCommandLine(Syntax, Nodes[0]->getValue(Storage));
2563ecd8c0aSManuel Klimek   std::vector<std::string> Arguments;
257*6366efedSEugene Zelenko   for (const auto *Node : Nodes)
2583ecd8c0aSManuel Klimek     Arguments.push_back(Node->getValue(Storage));
2593ecd8c0aSManuel Klimek   return Arguments;
2603ecd8c0aSManuel Klimek }
2613ecd8c0aSManuel Klimek 
262251ad5e0SArgyrios Kyrtzidis void JSONCompilationDatabase::getCommands(
263251ad5e0SArgyrios Kyrtzidis     ArrayRef<CompileCommandRef> CommandsRef,
264251ad5e0SArgyrios Kyrtzidis     std::vector<CompileCommand> &Commands) const {
265*6366efedSEugene Zelenko   for (const auto &CommandRef : CommandsRef) {
266f857950dSDmitri Gribenko     SmallString<8> DirectoryStorage;
26774bcd21eSArgyrios Kyrtzidis     SmallString<32> FilenameStorage;
268399aea30SJoerg Sonnenberger     SmallString<32> OutputStorage;
269*6366efedSEugene Zelenko     auto Output = std::get<3>(CommandRef);
27074bcd21eSArgyrios Kyrtzidis     Commands.emplace_back(
271*6366efedSEugene Zelenko         std::get<0>(CommandRef)->getValue(DirectoryStorage),
272*6366efedSEugene Zelenko         std::get<1>(CommandRef)->getValue(FilenameStorage),
273*6366efedSEugene Zelenko         nodeToCommandLine(Syntax, std::get<2>(CommandRef)),
274399aea30SJoerg Sonnenberger         Output ? Output->getValue(OutputStorage) : "");
275251ad5e0SArgyrios Kyrtzidis   }
276251ad5e0SArgyrios Kyrtzidis }
277251ad5e0SArgyrios Kyrtzidis 
2786ed1f85cSDaniel Jasper bool JSONCompilationDatabase::parse(std::string &ErrorMessage) {
2796ed1f85cSDaniel Jasper   llvm::yaml::document_iterator I = YAMLStream.begin();
2806ed1f85cSDaniel Jasper   if (I == YAMLStream.end()) {
2816ed1f85cSDaniel Jasper     ErrorMessage = "Error while parsing YAML.";
2826ed1f85cSDaniel Jasper     return false;
2836ed1f85cSDaniel Jasper   }
2846ed1f85cSDaniel Jasper   llvm::yaml::Node *Root = I->getRoot();
285ccbc35edSCraig Topper   if (!Root) {
2866ed1f85cSDaniel Jasper     ErrorMessage = "Error while parsing YAML.";
2876ed1f85cSDaniel Jasper     return false;
2886ed1f85cSDaniel Jasper   }
289*6366efedSEugene Zelenko   auto *Array = dyn_cast<llvm::yaml::SequenceNode>(Root);
290ccbc35edSCraig Topper   if (!Array) {
2916ed1f85cSDaniel Jasper     ErrorMessage = "Expected array.";
2926ed1f85cSDaniel Jasper     return false;
2936ed1f85cSDaniel Jasper   }
29454042e74SManuel Klimek   for (auto &NextObject : *Array) {
295*6366efedSEugene Zelenko     auto *Object = dyn_cast<llvm::yaml::MappingNode>(&NextObject);
296ccbc35edSCraig Topper     if (!Object) {
2976ed1f85cSDaniel Jasper       ErrorMessage = "Expected object.";
2986ed1f85cSDaniel Jasper       return false;
2996ed1f85cSDaniel Jasper     }
300ccbc35edSCraig Topper     llvm::yaml::ScalarNode *Directory = nullptr;
3013ecd8c0aSManuel Klimek     llvm::Optional<std::vector<llvm::yaml::ScalarNode *>> Command;
302ccbc35edSCraig Topper     llvm::yaml::ScalarNode *File = nullptr;
303399aea30SJoerg Sonnenberger     llvm::yaml::ScalarNode *Output = nullptr;
30454042e74SManuel Klimek     for (auto& NextKeyValue : *Object) {
305*6366efedSEugene Zelenko       auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
30654042e74SManuel Klimek       if (!KeyString) {
30754042e74SManuel Klimek         ErrorMessage = "Expected strings as key.";
30854042e74SManuel Klimek         return false;
30954042e74SManuel Klimek       }
31054042e74SManuel Klimek       SmallString<10> KeyStorage;
31154042e74SManuel Klimek       StringRef KeyValue = KeyString->getValue(KeyStorage);
31254042e74SManuel Klimek       llvm::yaml::Node *Value = NextKeyValue.getValue();
313ccbc35edSCraig Topper       if (!Value) {
3146ed1f85cSDaniel Jasper         ErrorMessage = "Expected value.";
3156ed1f85cSDaniel Jasper         return false;
3166ed1f85cSDaniel Jasper       }
317*6366efedSEugene Zelenko       auto *ValueString = dyn_cast<llvm::yaml::ScalarNode>(Value);
318*6366efedSEugene Zelenko       auto *SequenceString = dyn_cast<llvm::yaml::SequenceNode>(Value);
31954042e74SManuel Klimek       if (KeyValue == "arguments" && !SequenceString) {
32054042e74SManuel Klimek         ErrorMessage = "Expected sequence as value.";
32154042e74SManuel Klimek         return false;
32254042e74SManuel Klimek       } else if (KeyValue != "arguments" && !ValueString) {
3236ed1f85cSDaniel Jasper         ErrorMessage = "Expected string as value.";
3246ed1f85cSDaniel Jasper         return false;
3256ed1f85cSDaniel Jasper       }
32654042e74SManuel Klimek       if (KeyValue == "directory") {
3276ed1f85cSDaniel Jasper         Directory = ValueString;
32854042e74SManuel Klimek       } else if (KeyValue == "arguments") {
3293ecd8c0aSManuel Klimek         Command = std::vector<llvm::yaml::ScalarNode *>();
3303ecd8c0aSManuel Klimek         for (auto &Argument : *SequenceString) {
331*6366efedSEugene Zelenko           auto *Scalar = dyn_cast<llvm::yaml::ScalarNode>(&Argument);
3323ecd8c0aSManuel Klimek           if (!Scalar) {
3333ecd8c0aSManuel Klimek             ErrorMessage = "Only strings are allowed in 'arguments'.";
3343ecd8c0aSManuel Klimek             return false;
33554042e74SManuel Klimek           }
3363ecd8c0aSManuel Klimek           Command->push_back(Scalar);
3373ecd8c0aSManuel Klimek         }
33854042e74SManuel Klimek       } else if (KeyValue == "command") {
3393ecd8c0aSManuel Klimek         if (!Command)
3403ecd8c0aSManuel Klimek           Command = std::vector<llvm::yaml::ScalarNode *>(1, ValueString);
34154042e74SManuel Klimek       } else if (KeyValue == "file") {
3426ed1f85cSDaniel Jasper         File = ValueString;
343399aea30SJoerg Sonnenberger       } else if (KeyValue == "output") {
344399aea30SJoerg Sonnenberger         Output = ValueString;
3456ed1f85cSDaniel Jasper       } else {
3466ed1f85cSDaniel Jasper         ErrorMessage = ("Unknown key: \"" +
3476ed1f85cSDaniel Jasper                         KeyString->getRawValue() + "\"").str();
3486ed1f85cSDaniel Jasper         return false;
3496ed1f85cSDaniel Jasper       }
3506ed1f85cSDaniel Jasper     }
3516ed1f85cSDaniel Jasper     if (!File) {
3526ed1f85cSDaniel Jasper       ErrorMessage = "Missing key: \"file\".";
3536ed1f85cSDaniel Jasper       return false;
3546ed1f85cSDaniel Jasper     }
3553ecd8c0aSManuel Klimek     if (!Command) {
35654042e74SManuel Klimek       ErrorMessage = "Missing key: \"command\" or \"arguments\".";
3576ed1f85cSDaniel Jasper       return false;
3586ed1f85cSDaniel Jasper     }
3596ed1f85cSDaniel Jasper     if (!Directory) {
3606ed1f85cSDaniel Jasper       ErrorMessage = "Missing key: \"directory\".";
3616ed1f85cSDaniel Jasper       return false;
3626ed1f85cSDaniel Jasper     }
363f857950dSDmitri Gribenko     SmallString<8> FileStorage;
36426cf9c43SDaniel Jasper     StringRef FileName = File->getValue(FileStorage);
365f857950dSDmitri Gribenko     SmallString<128> NativeFilePath;
36626cf9c43SDaniel Jasper     if (llvm::sys::path::is_relative(FileName)) {
367f857950dSDmitri Gribenko       SmallString<8> DirectoryStorage;
368f857950dSDmitri Gribenko       SmallString<128> AbsolutePath(
36926cf9c43SDaniel Jasper           Directory->getValue(DirectoryStorage));
37026cf9c43SDaniel Jasper       llvm::sys::path::append(AbsolutePath, FileName);
37192e1b62dSYaron Keren       llvm::sys::path::native(AbsolutePath, NativeFilePath);
37226cf9c43SDaniel Jasper     } else {
37326cf9c43SDaniel Jasper       llvm::sys::path::native(FileName, NativeFilePath);
37426cf9c43SDaniel Jasper     }
375399aea30SJoerg Sonnenberger     auto Cmd = CompileCommandRef(Directory, File, *Command, Output);
37664f67be3SArgyrios Kyrtzidis     IndexByFile[NativeFilePath].push_back(Cmd);
37764f67be3SArgyrios Kyrtzidis     AllCommands.push_back(Cmd);
37892e1b62dSYaron Keren     MatchTrie.insert(NativeFilePath);
3796ed1f85cSDaniel Jasper   }
3806ed1f85cSDaniel Jasper   return true;
3816ed1f85cSDaniel Jasper }
382