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"
17c3a73023SKadir Cetinkaya #include "clang/Tooling/Tooling.h"
186366efedSEugene Zelenko #include "llvm/ADT/Optional.h"
19c3a73023SKadir 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"
32b3b37783SKadir Cetinkaya #include "llvm/Support/VirtualFileSystem.h"
336366efedSEugene Zelenko #include "llvm/Support/YAMLParser.h"
346366efedSEugene Zelenko #include "llvm/Support/raw_ostream.h"
356366efedSEugene Zelenko #include <cassert>
366366efedSEugene Zelenko #include <memory>
376366efedSEugene Zelenko #include <string>
388a8e554aSRafael Espindola #include <system_error>
396366efedSEugene Zelenko #include <tuple>
406366efedSEugene Zelenko #include <utility>
416366efedSEugene Zelenko #include <vector>
426ed1f85cSDaniel Jasper 
436366efedSEugene Zelenko using namespace clang;
446366efedSEugene Zelenko using namespace tooling;
456ed1f85cSDaniel Jasper 
466ed1f85cSDaniel Jasper namespace {
476ed1f85cSDaniel Jasper 
489fc8faf9SAdrian Prantl /// A parser for escaped strings of command line arguments.
496ed1f85cSDaniel Jasper ///
506ed1f85cSDaniel Jasper /// Assumes \-escaping for quoted arguments (see the documentation of
516ed1f85cSDaniel Jasper /// unescapeCommandLine(...)).
526ed1f85cSDaniel Jasper class CommandLineArgumentParser {
536ed1f85cSDaniel Jasper  public:
CommandLineArgumentParser(StringRef CommandLine)546ed1f85cSDaniel Jasper   CommandLineArgumentParser(StringRef CommandLine)
556ed1f85cSDaniel Jasper       : Input(CommandLine), Position(Input.begin()-1) {}
566ed1f85cSDaniel Jasper 
parse()576ed1f85cSDaniel Jasper   std::vector<std::string> parse() {
586ed1f85cSDaniel Jasper     bool HasMoreInput = true;
596ed1f85cSDaniel Jasper     while (HasMoreInput && nextNonWhitespace()) {
606ed1f85cSDaniel Jasper       std::string Argument;
616ed1f85cSDaniel Jasper       HasMoreInput = parseStringInto(Argument);
626ed1f85cSDaniel Jasper       CommandLine.push_back(Argument);
636ed1f85cSDaniel Jasper     }
646ed1f85cSDaniel Jasper     return CommandLine;
656ed1f85cSDaniel Jasper   }
666ed1f85cSDaniel Jasper 
676ed1f85cSDaniel Jasper  private:
686ed1f85cSDaniel Jasper   // All private methods return true if there is more input available.
696ed1f85cSDaniel Jasper 
parseStringInto(std::string & String)706ed1f85cSDaniel Jasper   bool parseStringInto(std::string &String) {
716ed1f85cSDaniel Jasper     do {
726ed1f85cSDaniel Jasper       if (*Position == '"') {
73fe7a3486SPeter Collingbourne         if (!parseDoubleQuotedStringInto(String)) return false;
74fe7a3486SPeter Collingbourne       } else if (*Position == '\'') {
75fe7a3486SPeter Collingbourne         if (!parseSingleQuotedStringInto(String)) return false;
766ed1f85cSDaniel Jasper       } else {
776ed1f85cSDaniel Jasper         if (!parseFreeStringInto(String)) return false;
786ed1f85cSDaniel Jasper       }
796ed1f85cSDaniel Jasper     } while (*Position != ' ');
806ed1f85cSDaniel Jasper     return true;
816ed1f85cSDaniel Jasper   }
826ed1f85cSDaniel Jasper 
parseDoubleQuotedStringInto(std::string & String)83fe7a3486SPeter Collingbourne   bool parseDoubleQuotedStringInto(std::string &String) {
846ed1f85cSDaniel Jasper     if (!next()) return false;
856ed1f85cSDaniel Jasper     while (*Position != '"') {
866ed1f85cSDaniel Jasper       if (!skipEscapeCharacter()) return false;
876ed1f85cSDaniel Jasper       String.push_back(*Position);
886ed1f85cSDaniel Jasper       if (!next()) return false;
896ed1f85cSDaniel Jasper     }
906ed1f85cSDaniel Jasper     return next();
916ed1f85cSDaniel Jasper   }
926ed1f85cSDaniel Jasper 
parseSingleQuotedStringInto(std::string & String)93fe7a3486SPeter Collingbourne   bool parseSingleQuotedStringInto(std::string &String) {
94fe7a3486SPeter Collingbourne     if (!next()) return false;
95fe7a3486SPeter Collingbourne     while (*Position != '\'') {
96fe7a3486SPeter Collingbourne       String.push_back(*Position);
97fe7a3486SPeter Collingbourne       if (!next()) return false;
98fe7a3486SPeter Collingbourne     }
99fe7a3486SPeter Collingbourne     return next();
100fe7a3486SPeter Collingbourne   }
101fe7a3486SPeter Collingbourne 
parseFreeStringInto(std::string & String)1026ed1f85cSDaniel Jasper   bool parseFreeStringInto(std::string &String) {
1036ed1f85cSDaniel Jasper     do {
1046ed1f85cSDaniel Jasper       if (!skipEscapeCharacter()) return false;
1056ed1f85cSDaniel Jasper       String.push_back(*Position);
1066ed1f85cSDaniel Jasper       if (!next()) return false;
107fe7a3486SPeter Collingbourne     } while (*Position != ' ' && *Position != '"' && *Position != '\'');
1086ed1f85cSDaniel Jasper     return true;
1096ed1f85cSDaniel Jasper   }
1106ed1f85cSDaniel Jasper 
skipEscapeCharacter()1116ed1f85cSDaniel Jasper   bool skipEscapeCharacter() {
1126ed1f85cSDaniel Jasper     if (*Position == '\\') {
1136ed1f85cSDaniel Jasper       return next();
1146ed1f85cSDaniel Jasper     }
1156ed1f85cSDaniel Jasper     return true;
1166ed1f85cSDaniel Jasper   }
1176ed1f85cSDaniel Jasper 
nextNonWhitespace()1186ed1f85cSDaniel Jasper   bool nextNonWhitespace() {
1196ed1f85cSDaniel Jasper     do {
1206ed1f85cSDaniel Jasper       if (!next()) return false;
1216ed1f85cSDaniel Jasper     } while (*Position == ' ');
1226ed1f85cSDaniel Jasper     return true;
1236ed1f85cSDaniel Jasper   }
1246ed1f85cSDaniel Jasper 
next()1256ed1f85cSDaniel Jasper   bool next() {
1266ed1f85cSDaniel Jasper     ++Position;
1276ed1f85cSDaniel Jasper     return Position != Input.end();
1286ed1f85cSDaniel Jasper   }
1296ed1f85cSDaniel Jasper 
1306ed1f85cSDaniel Jasper   const StringRef Input;
1316ed1f85cSDaniel Jasper   StringRef::iterator Position;
1326ed1f85cSDaniel Jasper   std::vector<std::string> CommandLine;
1336ed1f85cSDaniel Jasper };
1346ed1f85cSDaniel Jasper 
unescapeCommandLine(JSONCommandLineSyntax Syntax,StringRef EscapedCommandLine)1359e60a2adSZachary Turner std::vector<std::string> unescapeCommandLine(JSONCommandLineSyntax Syntax,
1366ed1f85cSDaniel Jasper                                              StringRef EscapedCommandLine) {
1379e60a2adSZachary Turner   if (Syntax == JSONCommandLineSyntax::AutoDetect) {
138*d9b9a7f4SJeremy Drake #ifdef _WIN32
139*d9b9a7f4SJeremy Drake     // Assume Windows command line parsing on Win32
1409e60a2adSZachary Turner     Syntax = JSONCommandLineSyntax::Windows;
141*d9b9a7f4SJeremy Drake #else
142*d9b9a7f4SJeremy Drake     Syntax = JSONCommandLineSyntax::Gnu;
143*d9b9a7f4SJeremy Drake #endif
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>
loadFromDirectory(StringRef Directory,std::string & ErrorMessage)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);
168c3a73023SKadir Cetinkaya     return Base ? inferTargetAndDriverMode(
16945ef055dSKadir Cetinkaya                       inferMissingCompileCommands(expandResponseFiles(
170b3b37783SKadir Cetinkaya                           std::move(Base), llvm::vfs::getRealFileSystem())))
171c3a73023SKadir Cetinkaya                 : nullptr;
1726ed1f85cSDaniel Jasper   }
1736ed1f85cSDaniel Jasper };
1746ed1f85cSDaniel Jasper 
1756366efedSEugene Zelenko } // namespace
17669b6277aSCraig Topper 
1776ed1f85cSDaniel Jasper // Register the JSONCompilationDatabasePlugin with the
1786ed1f85cSDaniel Jasper // CompilationDatabasePluginRegistry using this statically initialized variable.
1796ed1f85cSDaniel Jasper static CompilationDatabasePluginRegistry::Add<JSONCompilationDatabasePlugin>
1806ed1f85cSDaniel Jasper X("json-compilation-database", "Reads JSON formatted compilation databases");
1816ed1f85cSDaniel Jasper 
1826366efedSEugene Zelenko namespace clang {
1836366efedSEugene Zelenko namespace tooling {
1846366efedSEugene Zelenko 
1856ed1f85cSDaniel Jasper // This anchor is used to force the linker to link in the generated object file
1866ed1f85cSDaniel Jasper // and thus register the JSONCompilationDatabasePlugin.
187d574ac2fSNAKAMURA Takumi volatile int JSONAnchorSource = 0;
1886ed1f85cSDaniel Jasper 
1896366efedSEugene Zelenko } // namespace tooling
1906366efedSEugene Zelenko } // namespace clang
1916366efedSEugene Zelenko 
192cdba84c0SDavid Blaikie std::unique_ptr<JSONCompilationDatabase>
loadFromFile(StringRef FilePath,std::string & ErrorMessage,JSONCommandLineSyntax Syntax)1936ed1f85cSDaniel Jasper JSONCompilationDatabase::loadFromFile(StringRef FilePath,
1949e60a2adSZachary Turner                                       std::string &ErrorMessage,
1959e60a2adSZachary Turner                                       JSONCommandLineSyntax Syntax) {
196fdbb6185SSam McCall   // Don't mmap: if we're a long-lived process, the build system may overwrite.
1972d2b420aSRafael Espindola   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> DatabaseBuffer =
198c83cd8feSAbhina Sreeskantharajan       llvm::MemoryBuffer::getFile(FilePath, /*IsText=*/false,
199fdbb6185SSam McCall                                   /*RequiresNullTerminator=*/true,
200fdbb6185SSam McCall                                   /*IsVolatile=*/true);
2012d2b420aSRafael Espindola   if (std::error_code Result = DatabaseBuffer.getError()) {
2026ed1f85cSDaniel Jasper     ErrorMessage = "Error while opening JSON database: " + Result.message();
203ccbc35edSCraig Topper     return nullptr;
2046ed1f85cSDaniel Jasper   }
205b8984329SAhmed Charles   std::unique_ptr<JSONCompilationDatabase> Database(
2069e60a2adSZachary Turner       new JSONCompilationDatabase(std::move(*DatabaseBuffer), Syntax));
2076ed1f85cSDaniel Jasper   if (!Database->parse(ErrorMessage))
208ccbc35edSCraig Topper     return nullptr;
209cdba84c0SDavid Blaikie   return Database;
2106ed1f85cSDaniel Jasper }
2116ed1f85cSDaniel Jasper 
212cdba84c0SDavid Blaikie std::unique_ptr<JSONCompilationDatabase>
loadFromBuffer(StringRef DatabaseString,std::string & ErrorMessage,JSONCommandLineSyntax Syntax)2136ed1f85cSDaniel Jasper JSONCompilationDatabase::loadFromBuffer(StringRef DatabaseString,
2149e60a2adSZachary Turner                                         std::string &ErrorMessage,
2159e60a2adSZachary Turner                                         JSONCommandLineSyntax Syntax) {
216b8984329SAhmed Charles   std::unique_ptr<llvm::MemoryBuffer> DatabaseBuffer(
217650e04e1SSam McCall       llvm::MemoryBuffer::getMemBufferCopy(DatabaseString));
218b8984329SAhmed Charles   std::unique_ptr<JSONCompilationDatabase> Database(
2199e60a2adSZachary Turner       new JSONCompilationDatabase(std::move(DatabaseBuffer), Syntax));
2206ed1f85cSDaniel Jasper   if (!Database->parse(ErrorMessage))
221ccbc35edSCraig Topper     return nullptr;
222cdba84c0SDavid Blaikie   return Database;
2236ed1f85cSDaniel Jasper }
2246ed1f85cSDaniel Jasper 
2256ed1f85cSDaniel Jasper std::vector<CompileCommand>
getCompileCommands(StringRef FilePath) const2266ed1f85cSDaniel Jasper JSONCompilationDatabase::getCompileCommands(StringRef FilePath) const {
227f857950dSDmitri Gribenko   SmallString<128> NativeFilePath;
2286ed1f85cSDaniel Jasper   llvm::sys::path::native(FilePath, NativeFilePath);
229965f8825SAlp Toker 
23026cf9c43SDaniel Jasper   std::string Error;
23126cf9c43SDaniel Jasper   llvm::raw_string_ostream ES(Error);
23292e1b62dSYaron Keren   StringRef Match = MatchTrie.findEquivalent(NativeFilePath, ES);
2333128a11eSArnaud A. de Grandmaison   if (Match.empty())
2346366efedSEugene Zelenko     return {};
2356366efedSEugene Zelenko   const auto CommandsRefI = IndexByFile.find(Match);
2366ed1f85cSDaniel Jasper   if (CommandsRefI == IndexByFile.end())
2376366efedSEugene Zelenko     return {};
2386ed1f85cSDaniel Jasper   std::vector<CompileCommand> Commands;
239251ad5e0SArgyrios Kyrtzidis   getCommands(CommandsRefI->getValue(), Commands);
2406ed1f85cSDaniel Jasper   return Commands;
2416ed1f85cSDaniel Jasper }
2426ed1f85cSDaniel Jasper 
2436ed1f85cSDaniel Jasper std::vector<std::string>
getAllFiles() const2446ed1f85cSDaniel Jasper JSONCompilationDatabase::getAllFiles() const {
2456ed1f85cSDaniel Jasper   std::vector<std::string> Result;
2466366efedSEugene Zelenko   for (const auto &CommandRef : IndexByFile)
2476366efedSEugene Zelenko     Result.push_back(CommandRef.first().str());
2486ed1f85cSDaniel Jasper   return Result;
2496ed1f85cSDaniel Jasper }
2506ed1f85cSDaniel Jasper 
251251ad5e0SArgyrios Kyrtzidis std::vector<CompileCommand>
getAllCompileCommands() const252251ad5e0SArgyrios Kyrtzidis JSONCompilationDatabase::getAllCompileCommands() const {
253251ad5e0SArgyrios Kyrtzidis   std::vector<CompileCommand> Commands;
25464f67be3SArgyrios Kyrtzidis   getCommands(AllCommands, Commands);
255251ad5e0SArgyrios Kyrtzidis   return Commands;
256251ad5e0SArgyrios Kyrtzidis }
257251ad5e0SArgyrios Kyrtzidis 
stripExecutableExtension(llvm::StringRef Name)2585d9d7c59SSam McCall static llvm::StringRef stripExecutableExtension(llvm::StringRef Name) {
2595d9d7c59SSam McCall   Name.consume_back(".exe");
2605d9d7c59SSam McCall   return Name;
2615d9d7c59SSam McCall }
2625d9d7c59SSam McCall 
2635d9d7c59SSam McCall // There are compiler-wrappers (ccache, distcc, gomacc) that take the "real"
2645d9d7c59SSam McCall // compiler as an argument, e.g. distcc gcc -O3 foo.c.
2655d9d7c59SSam McCall // These end up in compile_commands.json when people set CC="distcc gcc".
2665d9d7c59SSam McCall // Clang's driver doesn't understand this, so we need to unwrap.
unwrapCommand(std::vector<std::string> & Args)2675d9d7c59SSam McCall static bool unwrapCommand(std::vector<std::string> &Args) {
2685d9d7c59SSam McCall   if (Args.size() < 2)
2695d9d7c59SSam McCall     return false;
2705d9d7c59SSam McCall   StringRef Wrapper =
2715d9d7c59SSam McCall       stripExecutableExtension(llvm::sys::path::filename(Args.front()));
2722756e2eeSNathan Ridge   if (Wrapper == "distcc" || Wrapper == "gomacc" || Wrapper == "ccache" ||
2732756e2eeSNathan Ridge       Wrapper == "sccache") {
2745d9d7c59SSam McCall     // Most of these wrappers support being invoked 3 ways:
2755d9d7c59SSam McCall     // `distcc g++ file.c` This is the mode we're trying to match.
2765d9d7c59SSam McCall     //                     We need to drop `distcc`.
2775d9d7c59SSam McCall     // `distcc file.c`     This acts like compiler is cc or similar.
2785d9d7c59SSam McCall     //                     Clang's driver can handle this, no change needed.
2795d9d7c59SSam McCall     // `g++ file.c`        g++ is a symlink to distcc.
2805d9d7c59SSam McCall     //                     We don't even notice this case, and all is well.
2815d9d7c59SSam McCall     //
2825d9d7c59SSam McCall     // We need to distinguish between the first and second case.
2835d9d7c59SSam McCall     // The wrappers themselves don't take flags, so Args[1] is a compiler flag,
2845d9d7c59SSam McCall     // an input file, or a compiler. Inputs have extensions, compilers don't.
2855d9d7c59SSam McCall     bool HasCompiler =
2865d9d7c59SSam McCall         (Args[1][0] != '-') &&
2875d9d7c59SSam McCall         !llvm::sys::path::has_extension(stripExecutableExtension(Args[1]));
2885d9d7c59SSam McCall     if (HasCompiler) {
2895d9d7c59SSam McCall       Args.erase(Args.begin());
2905d9d7c59SSam McCall       return true;
2915d9d7c59SSam McCall     }
2925d9d7c59SSam McCall     // If !HasCompiler, wrappers act like GCC. Fine: so do we.
2935d9d7c59SSam McCall   }
2945d9d7c59SSam McCall   return false;
2955d9d7c59SSam McCall }
2965d9d7c59SSam McCall 
2973ecd8c0aSManuel Klimek static std::vector<std::string>
nodeToCommandLine(JSONCommandLineSyntax Syntax,const std::vector<llvm::yaml::ScalarNode * > & Nodes)2989e60a2adSZachary Turner nodeToCommandLine(JSONCommandLineSyntax Syntax,
2999e60a2adSZachary Turner                   const std::vector<llvm::yaml::ScalarNode *> &Nodes) {
3003ecd8c0aSManuel Klimek   SmallString<1024> Storage;
301614a78c1SRussell Gallop   std::vector<std::string> Arguments;
3025d9d7c59SSam McCall   if (Nodes.size() == 1)
3035d9d7c59SSam McCall     Arguments = unescapeCommandLine(Syntax, Nodes[0]->getValue(Storage));
3045d9d7c59SSam McCall   else
3056366efedSEugene Zelenko     for (const auto *Node : Nodes)
306adcd0268SBenjamin Kramer       Arguments.push_back(std::string(Node->getValue(Storage)));
3075d9d7c59SSam McCall   // There may be multiple wrappers: using distcc and ccache together is common.
3085d9d7c59SSam McCall   while (unwrapCommand(Arguments))
3095d9d7c59SSam McCall     ;
3103ecd8c0aSManuel Klimek   return Arguments;
3113ecd8c0aSManuel Klimek }
3123ecd8c0aSManuel Klimek 
getCommands(ArrayRef<CompileCommandRef> CommandsRef,std::vector<CompileCommand> & Commands) const313251ad5e0SArgyrios Kyrtzidis void JSONCompilationDatabase::getCommands(
314251ad5e0SArgyrios Kyrtzidis     ArrayRef<CompileCommandRef> CommandsRef,
315251ad5e0SArgyrios Kyrtzidis     std::vector<CompileCommand> &Commands) const {
3166366efedSEugene Zelenko   for (const auto &CommandRef : CommandsRef) {
317f857950dSDmitri Gribenko     SmallString<8> DirectoryStorage;
31874bcd21eSArgyrios Kyrtzidis     SmallString<32> FilenameStorage;
319399aea30SJoerg Sonnenberger     SmallString<32> OutputStorage;
3206366efedSEugene Zelenko     auto Output = std::get<3>(CommandRef);
32174bcd21eSArgyrios Kyrtzidis     Commands.emplace_back(
3226366efedSEugene Zelenko         std::get<0>(CommandRef)->getValue(DirectoryStorage),
3236366efedSEugene Zelenko         std::get<1>(CommandRef)->getValue(FilenameStorage),
3246366efedSEugene Zelenko         nodeToCommandLine(Syntax, std::get<2>(CommandRef)),
325399aea30SJoerg Sonnenberger         Output ? Output->getValue(OutputStorage) : "");
326251ad5e0SArgyrios Kyrtzidis   }
327251ad5e0SArgyrios Kyrtzidis }
328251ad5e0SArgyrios Kyrtzidis 
parse(std::string & ErrorMessage)3296ed1f85cSDaniel Jasper bool JSONCompilationDatabase::parse(std::string &ErrorMessage) {
3306ed1f85cSDaniel Jasper   llvm::yaml::document_iterator I = YAMLStream.begin();
3316ed1f85cSDaniel Jasper   if (I == YAMLStream.end()) {
3326ed1f85cSDaniel Jasper     ErrorMessage = "Error while parsing YAML.";
3336ed1f85cSDaniel Jasper     return false;
3346ed1f85cSDaniel Jasper   }
3356ed1f85cSDaniel Jasper   llvm::yaml::Node *Root = I->getRoot();
336ccbc35edSCraig Topper   if (!Root) {
3376ed1f85cSDaniel Jasper     ErrorMessage = "Error while parsing YAML.";
3386ed1f85cSDaniel Jasper     return false;
3396ed1f85cSDaniel Jasper   }
3406366efedSEugene Zelenko   auto *Array = dyn_cast<llvm::yaml::SequenceNode>(Root);
341ccbc35edSCraig Topper   if (!Array) {
3426ed1f85cSDaniel Jasper     ErrorMessage = "Expected array.";
3436ed1f85cSDaniel Jasper     return false;
3446ed1f85cSDaniel Jasper   }
34554042e74SManuel Klimek   for (auto &NextObject : *Array) {
3466366efedSEugene Zelenko     auto *Object = dyn_cast<llvm::yaml::MappingNode>(&NextObject);
347ccbc35edSCraig Topper     if (!Object) {
3486ed1f85cSDaniel Jasper       ErrorMessage = "Expected object.";
3496ed1f85cSDaniel Jasper       return false;
3506ed1f85cSDaniel Jasper     }
351ccbc35edSCraig Topper     llvm::yaml::ScalarNode *Directory = nullptr;
3523ecd8c0aSManuel Klimek     llvm::Optional<std::vector<llvm::yaml::ScalarNode *>> Command;
353ccbc35edSCraig Topper     llvm::yaml::ScalarNode *File = nullptr;
354399aea30SJoerg Sonnenberger     llvm::yaml::ScalarNode *Output = nullptr;
35554042e74SManuel Klimek     for (auto& NextKeyValue : *Object) {
3566366efedSEugene Zelenko       auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
35754042e74SManuel Klimek       if (!KeyString) {
35854042e74SManuel Klimek         ErrorMessage = "Expected strings as key.";
35954042e74SManuel Klimek         return false;
36054042e74SManuel Klimek       }
36154042e74SManuel Klimek       SmallString<10> KeyStorage;
36254042e74SManuel Klimek       StringRef KeyValue = KeyString->getValue(KeyStorage);
36354042e74SManuel Klimek       llvm::yaml::Node *Value = NextKeyValue.getValue();
364ccbc35edSCraig Topper       if (!Value) {
3656ed1f85cSDaniel Jasper         ErrorMessage = "Expected value.";
3666ed1f85cSDaniel Jasper         return false;
3676ed1f85cSDaniel Jasper       }
3686366efedSEugene Zelenko       auto *ValueString = dyn_cast<llvm::yaml::ScalarNode>(Value);
3696366efedSEugene Zelenko       auto *SequenceString = dyn_cast<llvm::yaml::SequenceNode>(Value);
370a735d6eaSSimon Pilgrim       if (KeyValue == "arguments") {
371a735d6eaSSimon Pilgrim         if (!SequenceString) {
37254042e74SManuel Klimek           ErrorMessage = "Expected sequence as value.";
37354042e74SManuel Klimek           return false;
3746ed1f85cSDaniel Jasper         }
3753ecd8c0aSManuel Klimek         Command = std::vector<llvm::yaml::ScalarNode *>();
3763ecd8c0aSManuel Klimek         for (auto &Argument : *SequenceString) {
3776366efedSEugene Zelenko           auto *Scalar = dyn_cast<llvm::yaml::ScalarNode>(&Argument);
3783ecd8c0aSManuel Klimek           if (!Scalar) {
3793ecd8c0aSManuel Klimek             ErrorMessage = "Only strings are allowed in 'arguments'.";
3803ecd8c0aSManuel Klimek             return false;
38154042e74SManuel Klimek           }
3823ecd8c0aSManuel Klimek           Command->push_back(Scalar);
3833ecd8c0aSManuel Klimek         }
384a735d6eaSSimon Pilgrim       } else {
385a735d6eaSSimon Pilgrim         if (!ValueString) {
386a735d6eaSSimon Pilgrim           ErrorMessage = "Expected string as value.";
387a735d6eaSSimon Pilgrim           return false;
388a735d6eaSSimon Pilgrim         }
389a735d6eaSSimon Pilgrim         if (KeyValue == "directory") {
390a735d6eaSSimon Pilgrim           Directory = ValueString;
39154042e74SManuel Klimek         } else if (KeyValue == "command") {
3923ecd8c0aSManuel Klimek           if (!Command)
3933ecd8c0aSManuel Klimek             Command = std::vector<llvm::yaml::ScalarNode *>(1, ValueString);
39454042e74SManuel Klimek         } else if (KeyValue == "file") {
3956ed1f85cSDaniel Jasper           File = ValueString;
396399aea30SJoerg Sonnenberger         } else if (KeyValue == "output") {
397399aea30SJoerg Sonnenberger           Output = ValueString;
3986ed1f85cSDaniel Jasper         } else {
399a735d6eaSSimon Pilgrim           ErrorMessage =
400a735d6eaSSimon Pilgrim               ("Unknown key: \"" + KeyString->getRawValue() + "\"").str();
4016ed1f85cSDaniel Jasper           return false;
4026ed1f85cSDaniel Jasper         }
4036ed1f85cSDaniel Jasper       }
404a735d6eaSSimon Pilgrim     }
4056ed1f85cSDaniel Jasper     if (!File) {
4066ed1f85cSDaniel Jasper       ErrorMessage = "Missing key: \"file\".";
4076ed1f85cSDaniel Jasper       return false;
4086ed1f85cSDaniel Jasper     }
4093ecd8c0aSManuel Klimek     if (!Command) {
41054042e74SManuel Klimek       ErrorMessage = "Missing key: \"command\" or \"arguments\".";
4116ed1f85cSDaniel Jasper       return false;
4126ed1f85cSDaniel Jasper     }
4136ed1f85cSDaniel Jasper     if (!Directory) {
4146ed1f85cSDaniel Jasper       ErrorMessage = "Missing key: \"directory\".";
4156ed1f85cSDaniel Jasper       return false;
4166ed1f85cSDaniel Jasper     }
417f857950dSDmitri Gribenko     SmallString<8> FileStorage;
41826cf9c43SDaniel Jasper     StringRef FileName = File->getValue(FileStorage);
419f857950dSDmitri Gribenko     SmallString<128> NativeFilePath;
42026cf9c43SDaniel Jasper     if (llvm::sys::path::is_relative(FileName)) {
421f857950dSDmitri Gribenko       SmallString<8> DirectoryStorage;
422f857950dSDmitri Gribenko       SmallString<128> AbsolutePath(
42326cf9c43SDaniel Jasper           Directory->getValue(DirectoryStorage));
42426cf9c43SDaniel Jasper       llvm::sys::path::append(AbsolutePath, FileName);
4257ec1ec10SKadir Cetinkaya       llvm::sys::path::remove_dots(AbsolutePath, /*remove_dot_dot=*/ true);
42692e1b62dSYaron Keren       llvm::sys::path::native(AbsolutePath, NativeFilePath);
42726cf9c43SDaniel Jasper     } else {
42826cf9c43SDaniel Jasper       llvm::sys::path::native(FileName, NativeFilePath);
42926cf9c43SDaniel Jasper     }
430399aea30SJoerg Sonnenberger     auto Cmd = CompileCommandRef(Directory, File, *Command, Output);
43164f67be3SArgyrios Kyrtzidis     IndexByFile[NativeFilePath].push_back(Cmd);
43264f67be3SArgyrios Kyrtzidis     AllCommands.push_back(Cmd);
43392e1b62dSYaron Keren     MatchTrie.insert(NativeFilePath);
4346ed1f85cSDaniel Jasper   }
4356ed1f85cSDaniel Jasper   return true;
4366ed1f85cSDaniel Jasper }
437