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: 546ed1f85cSDaniel Jasper CommandLineArgumentParser(StringRef CommandLine) 556ed1f85cSDaniel Jasper : Input(CommandLine), Position(Input.begin()-1) {} 566ed1f85cSDaniel Jasper 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 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 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 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 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 1116ed1f85cSDaniel Jasper bool skipEscapeCharacter() { 1126ed1f85cSDaniel Jasper if (*Position == '\\') { 1136ed1f85cSDaniel Jasper return next(); 1146ed1f85cSDaniel Jasper } 1156ed1f85cSDaniel Jasper return true; 1166ed1f85cSDaniel Jasper } 1176ed1f85cSDaniel Jasper 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 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 1359e60a2adSZachary Turner std::vector<std::string> unescapeCommandLine(JSONCommandLineSyntax Syntax, 1366ed1f85cSDaniel Jasper StringRef EscapedCommandLine) { 1379e60a2adSZachary Turner if (Syntax == JSONCommandLineSyntax::AutoDetect) { 13885d0f314SZachary Turner Syntax = JSONCommandLineSyntax::Gnu; 1399e60a2adSZachary Turner llvm::Triple Triple(llvm::sys::getProcessTriple()); 1409e60a2adSZachary Turner if (Triple.getOS() == llvm::Triple::OSType::Win32) { 1419e60a2adSZachary Turner // Assume Windows command line parsing on Win32 unless the triple 14285d0f314SZachary Turner // explicitly tells us otherwise. 1439e60a2adSZachary Turner if (!Triple.hasEnvironment() || 1449e60a2adSZachary Turner Triple.getEnvironment() == llvm::Triple::EnvironmentType::MSVC) 1459e60a2adSZachary Turner Syntax = JSONCommandLineSyntax::Windows; 1469e60a2adSZachary Turner } 1479e60a2adSZachary Turner } 1489e60a2adSZachary Turner 1499e60a2adSZachary Turner if (Syntax == JSONCommandLineSyntax::Windows) { 1509e60a2adSZachary Turner llvm::BumpPtrAllocator Alloc; 1519e60a2adSZachary Turner llvm::StringSaver Saver(Alloc); 1529e60a2adSZachary Turner llvm::SmallVector<const char *, 64> T; 1539e60a2adSZachary Turner llvm::cl::TokenizeWindowsCommandLine(EscapedCommandLine, Saver, T); 1549e60a2adSZachary Turner std::vector<std::string> Result(T.begin(), T.end()); 1559e60a2adSZachary Turner return Result; 1569e60a2adSZachary Turner } 1579e60a2adSZachary Turner assert(Syntax == JSONCommandLineSyntax::Gnu); 1586ed1f85cSDaniel Jasper CommandLineArgumentParser parser(EscapedCommandLine); 1596ed1f85cSDaniel Jasper return parser.parse(); 1606ed1f85cSDaniel Jasper } 1616ed1f85cSDaniel Jasper 1629d3530bdSSam McCall // This plugin locates a nearby compile_command.json file, and also infers 1639d3530bdSSam McCall // compile commands for files not present in the database. 1646ed1f85cSDaniel Jasper class JSONCompilationDatabasePlugin : public CompilationDatabasePlugin { 165cdba84c0SDavid Blaikie std::unique_ptr<CompilationDatabase> 166cdba84c0SDavid Blaikie loadFromDirectory(StringRef Directory, std::string &ErrorMessage) override { 167f857950dSDmitri Gribenko SmallString<1024> JSONDatabasePath(Directory); 1686ed1f85cSDaniel Jasper llvm::sys::path::append(JSONDatabasePath, "compile_commands.json"); 1699d3530bdSSam McCall auto Base = JSONCompilationDatabase::loadFromFile( 170024b0644SKrasimir Georgiev JSONDatabasePath, ErrorMessage, JSONCommandLineSyntax::AutoDetect); 171c3a73023SKadir Cetinkaya return Base ? inferTargetAndDriverMode( 17245ef055dSKadir Cetinkaya inferMissingCompileCommands(expandResponseFiles( 173b3b37783SKadir Cetinkaya std::move(Base), llvm::vfs::getRealFileSystem()))) 174c3a73023SKadir Cetinkaya : nullptr; 1756ed1f85cSDaniel Jasper } 1766ed1f85cSDaniel Jasper }; 1776ed1f85cSDaniel Jasper 1786366efedSEugene Zelenko } // namespace 17969b6277aSCraig Topper 1806ed1f85cSDaniel Jasper // Register the JSONCompilationDatabasePlugin with the 1816ed1f85cSDaniel Jasper // CompilationDatabasePluginRegistry using this statically initialized variable. 1826ed1f85cSDaniel Jasper static CompilationDatabasePluginRegistry::Add<JSONCompilationDatabasePlugin> 1836ed1f85cSDaniel Jasper X("json-compilation-database", "Reads JSON formatted compilation databases"); 1846ed1f85cSDaniel Jasper 1856366efedSEugene Zelenko namespace clang { 1866366efedSEugene Zelenko namespace tooling { 1876366efedSEugene Zelenko 1886ed1f85cSDaniel Jasper // This anchor is used to force the linker to link in the generated object file 1896ed1f85cSDaniel Jasper // and thus register the JSONCompilationDatabasePlugin. 190d574ac2fSNAKAMURA Takumi volatile int JSONAnchorSource = 0; 1916ed1f85cSDaniel Jasper 1926366efedSEugene Zelenko } // namespace tooling 1936366efedSEugene Zelenko } // namespace clang 1946366efedSEugene Zelenko 195cdba84c0SDavid Blaikie std::unique_ptr<JSONCompilationDatabase> 1966ed1f85cSDaniel Jasper JSONCompilationDatabase::loadFromFile(StringRef FilePath, 1979e60a2adSZachary Turner std::string &ErrorMessage, 1989e60a2adSZachary Turner JSONCommandLineSyntax Syntax) { 199fdbb6185SSam McCall // Don't mmap: if we're a long-lived process, the build system may overwrite. 2002d2b420aSRafael Espindola llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> DatabaseBuffer = 201*c83cd8feSAbhina Sreeskantharajan llvm::MemoryBuffer::getFile(FilePath, /*IsText=*/false, 202fdbb6185SSam McCall /*RequiresNullTerminator=*/true, 203fdbb6185SSam McCall /*IsVolatile=*/true); 2042d2b420aSRafael Espindola if (std::error_code Result = DatabaseBuffer.getError()) { 2056ed1f85cSDaniel Jasper ErrorMessage = "Error while opening JSON database: " + Result.message(); 206ccbc35edSCraig Topper return nullptr; 2076ed1f85cSDaniel Jasper } 208b8984329SAhmed Charles std::unique_ptr<JSONCompilationDatabase> Database( 2099e60a2adSZachary Turner new JSONCompilationDatabase(std::move(*DatabaseBuffer), Syntax)); 2106ed1f85cSDaniel Jasper if (!Database->parse(ErrorMessage)) 211ccbc35edSCraig Topper return nullptr; 212cdba84c0SDavid Blaikie return Database; 2136ed1f85cSDaniel Jasper } 2146ed1f85cSDaniel Jasper 215cdba84c0SDavid Blaikie std::unique_ptr<JSONCompilationDatabase> 2166ed1f85cSDaniel Jasper JSONCompilationDatabase::loadFromBuffer(StringRef DatabaseString, 2179e60a2adSZachary Turner std::string &ErrorMessage, 2189e60a2adSZachary Turner JSONCommandLineSyntax Syntax) { 219b8984329SAhmed Charles std::unique_ptr<llvm::MemoryBuffer> DatabaseBuffer( 220650e04e1SSam McCall llvm::MemoryBuffer::getMemBufferCopy(DatabaseString)); 221b8984329SAhmed Charles std::unique_ptr<JSONCompilationDatabase> Database( 2229e60a2adSZachary Turner new JSONCompilationDatabase(std::move(DatabaseBuffer), Syntax)); 2236ed1f85cSDaniel Jasper if (!Database->parse(ErrorMessage)) 224ccbc35edSCraig Topper return nullptr; 225cdba84c0SDavid Blaikie return Database; 2266ed1f85cSDaniel Jasper } 2276ed1f85cSDaniel Jasper 2286ed1f85cSDaniel Jasper std::vector<CompileCommand> 2296ed1f85cSDaniel Jasper JSONCompilationDatabase::getCompileCommands(StringRef FilePath) const { 230f857950dSDmitri Gribenko SmallString<128> NativeFilePath; 2316ed1f85cSDaniel Jasper llvm::sys::path::native(FilePath, NativeFilePath); 232965f8825SAlp Toker 23326cf9c43SDaniel Jasper std::string Error; 23426cf9c43SDaniel Jasper llvm::raw_string_ostream ES(Error); 23592e1b62dSYaron Keren StringRef Match = MatchTrie.findEquivalent(NativeFilePath, ES); 2363128a11eSArnaud A. de Grandmaison if (Match.empty()) 2376366efedSEugene Zelenko return {}; 2386366efedSEugene Zelenko const auto CommandsRefI = IndexByFile.find(Match); 2396ed1f85cSDaniel Jasper if (CommandsRefI == IndexByFile.end()) 2406366efedSEugene Zelenko return {}; 2416ed1f85cSDaniel Jasper std::vector<CompileCommand> Commands; 242251ad5e0SArgyrios Kyrtzidis getCommands(CommandsRefI->getValue(), Commands); 2436ed1f85cSDaniel Jasper return Commands; 2446ed1f85cSDaniel Jasper } 2456ed1f85cSDaniel Jasper 2466ed1f85cSDaniel Jasper std::vector<std::string> 2476ed1f85cSDaniel Jasper JSONCompilationDatabase::getAllFiles() const { 2486ed1f85cSDaniel Jasper std::vector<std::string> Result; 2496366efedSEugene Zelenko for (const auto &CommandRef : IndexByFile) 2506366efedSEugene Zelenko Result.push_back(CommandRef.first().str()); 2516ed1f85cSDaniel Jasper return Result; 2526ed1f85cSDaniel Jasper } 2536ed1f85cSDaniel Jasper 254251ad5e0SArgyrios Kyrtzidis std::vector<CompileCommand> 255251ad5e0SArgyrios Kyrtzidis JSONCompilationDatabase::getAllCompileCommands() const { 256251ad5e0SArgyrios Kyrtzidis std::vector<CompileCommand> Commands; 25764f67be3SArgyrios Kyrtzidis getCommands(AllCommands, Commands); 258251ad5e0SArgyrios Kyrtzidis return Commands; 259251ad5e0SArgyrios Kyrtzidis } 260251ad5e0SArgyrios Kyrtzidis 2615d9d7c59SSam McCall static llvm::StringRef stripExecutableExtension(llvm::StringRef Name) { 2625d9d7c59SSam McCall Name.consume_back(".exe"); 2635d9d7c59SSam McCall return Name; 2645d9d7c59SSam McCall } 2655d9d7c59SSam McCall 2665d9d7c59SSam McCall // There are compiler-wrappers (ccache, distcc, gomacc) that take the "real" 2675d9d7c59SSam McCall // compiler as an argument, e.g. distcc gcc -O3 foo.c. 2685d9d7c59SSam McCall // These end up in compile_commands.json when people set CC="distcc gcc". 2695d9d7c59SSam McCall // Clang's driver doesn't understand this, so we need to unwrap. 2705d9d7c59SSam McCall static bool unwrapCommand(std::vector<std::string> &Args) { 2715d9d7c59SSam McCall if (Args.size() < 2) 2725d9d7c59SSam McCall return false; 2735d9d7c59SSam McCall StringRef Wrapper = 2745d9d7c59SSam McCall stripExecutableExtension(llvm::sys::path::filename(Args.front())); 2752756e2eeSNathan Ridge if (Wrapper == "distcc" || Wrapper == "gomacc" || Wrapper == "ccache" || 2762756e2eeSNathan Ridge Wrapper == "sccache") { 2775d9d7c59SSam McCall // Most of these wrappers support being invoked 3 ways: 2785d9d7c59SSam McCall // `distcc g++ file.c` This is the mode we're trying to match. 2795d9d7c59SSam McCall // We need to drop `distcc`. 2805d9d7c59SSam McCall // `distcc file.c` This acts like compiler is cc or similar. 2815d9d7c59SSam McCall // Clang's driver can handle this, no change needed. 2825d9d7c59SSam McCall // `g++ file.c` g++ is a symlink to distcc. 2835d9d7c59SSam McCall // We don't even notice this case, and all is well. 2845d9d7c59SSam McCall // 2855d9d7c59SSam McCall // We need to distinguish between the first and second case. 2865d9d7c59SSam McCall // The wrappers themselves don't take flags, so Args[1] is a compiler flag, 2875d9d7c59SSam McCall // an input file, or a compiler. Inputs have extensions, compilers don't. 2885d9d7c59SSam McCall bool HasCompiler = 2895d9d7c59SSam McCall (Args[1][0] != '-') && 2905d9d7c59SSam McCall !llvm::sys::path::has_extension(stripExecutableExtension(Args[1])); 2915d9d7c59SSam McCall if (HasCompiler) { 2925d9d7c59SSam McCall Args.erase(Args.begin()); 2935d9d7c59SSam McCall return true; 2945d9d7c59SSam McCall } 2955d9d7c59SSam McCall // If !HasCompiler, wrappers act like GCC. Fine: so do we. 2965d9d7c59SSam McCall } 2975d9d7c59SSam McCall return false; 2985d9d7c59SSam McCall } 2995d9d7c59SSam McCall 3003ecd8c0aSManuel Klimek static std::vector<std::string> 3019e60a2adSZachary Turner nodeToCommandLine(JSONCommandLineSyntax Syntax, 3029e60a2adSZachary Turner const std::vector<llvm::yaml::ScalarNode *> &Nodes) { 3033ecd8c0aSManuel Klimek SmallString<1024> Storage; 304614a78c1SRussell Gallop std::vector<std::string> Arguments; 3055d9d7c59SSam McCall if (Nodes.size() == 1) 3065d9d7c59SSam McCall Arguments = unescapeCommandLine(Syntax, Nodes[0]->getValue(Storage)); 3075d9d7c59SSam McCall else 3086366efedSEugene Zelenko for (const auto *Node : Nodes) 309adcd0268SBenjamin Kramer Arguments.push_back(std::string(Node->getValue(Storage))); 3105d9d7c59SSam McCall // There may be multiple wrappers: using distcc and ccache together is common. 3115d9d7c59SSam McCall while (unwrapCommand(Arguments)) 3125d9d7c59SSam McCall ; 3133ecd8c0aSManuel Klimek return Arguments; 3143ecd8c0aSManuel Klimek } 3153ecd8c0aSManuel Klimek 316251ad5e0SArgyrios Kyrtzidis void JSONCompilationDatabase::getCommands( 317251ad5e0SArgyrios Kyrtzidis ArrayRef<CompileCommandRef> CommandsRef, 318251ad5e0SArgyrios Kyrtzidis std::vector<CompileCommand> &Commands) const { 3196366efedSEugene Zelenko for (const auto &CommandRef : CommandsRef) { 320f857950dSDmitri Gribenko SmallString<8> DirectoryStorage; 32174bcd21eSArgyrios Kyrtzidis SmallString<32> FilenameStorage; 322399aea30SJoerg Sonnenberger SmallString<32> OutputStorage; 3236366efedSEugene Zelenko auto Output = std::get<3>(CommandRef); 32474bcd21eSArgyrios Kyrtzidis Commands.emplace_back( 3256366efedSEugene Zelenko std::get<0>(CommandRef)->getValue(DirectoryStorage), 3266366efedSEugene Zelenko std::get<1>(CommandRef)->getValue(FilenameStorage), 3276366efedSEugene Zelenko nodeToCommandLine(Syntax, std::get<2>(CommandRef)), 328399aea30SJoerg Sonnenberger Output ? Output->getValue(OutputStorage) : ""); 329251ad5e0SArgyrios Kyrtzidis } 330251ad5e0SArgyrios Kyrtzidis } 331251ad5e0SArgyrios Kyrtzidis 3326ed1f85cSDaniel Jasper bool JSONCompilationDatabase::parse(std::string &ErrorMessage) { 3336ed1f85cSDaniel Jasper llvm::yaml::document_iterator I = YAMLStream.begin(); 3346ed1f85cSDaniel Jasper if (I == YAMLStream.end()) { 3356ed1f85cSDaniel Jasper ErrorMessage = "Error while parsing YAML."; 3366ed1f85cSDaniel Jasper return false; 3376ed1f85cSDaniel Jasper } 3386ed1f85cSDaniel Jasper llvm::yaml::Node *Root = I->getRoot(); 339ccbc35edSCraig Topper if (!Root) { 3406ed1f85cSDaniel Jasper ErrorMessage = "Error while parsing YAML."; 3416ed1f85cSDaniel Jasper return false; 3426ed1f85cSDaniel Jasper } 3436366efedSEugene Zelenko auto *Array = dyn_cast<llvm::yaml::SequenceNode>(Root); 344ccbc35edSCraig Topper if (!Array) { 3456ed1f85cSDaniel Jasper ErrorMessage = "Expected array."; 3466ed1f85cSDaniel Jasper return false; 3476ed1f85cSDaniel Jasper } 34854042e74SManuel Klimek for (auto &NextObject : *Array) { 3496366efedSEugene Zelenko auto *Object = dyn_cast<llvm::yaml::MappingNode>(&NextObject); 350ccbc35edSCraig Topper if (!Object) { 3516ed1f85cSDaniel Jasper ErrorMessage = "Expected object."; 3526ed1f85cSDaniel Jasper return false; 3536ed1f85cSDaniel Jasper } 354ccbc35edSCraig Topper llvm::yaml::ScalarNode *Directory = nullptr; 3553ecd8c0aSManuel Klimek llvm::Optional<std::vector<llvm::yaml::ScalarNode *>> Command; 356ccbc35edSCraig Topper llvm::yaml::ScalarNode *File = nullptr; 357399aea30SJoerg Sonnenberger llvm::yaml::ScalarNode *Output = nullptr; 35854042e74SManuel Klimek for (auto& NextKeyValue : *Object) { 3596366efedSEugene Zelenko auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey()); 36054042e74SManuel Klimek if (!KeyString) { 36154042e74SManuel Klimek ErrorMessage = "Expected strings as key."; 36254042e74SManuel Klimek return false; 36354042e74SManuel Klimek } 36454042e74SManuel Klimek SmallString<10> KeyStorage; 36554042e74SManuel Klimek StringRef KeyValue = KeyString->getValue(KeyStorage); 36654042e74SManuel Klimek llvm::yaml::Node *Value = NextKeyValue.getValue(); 367ccbc35edSCraig Topper if (!Value) { 3686ed1f85cSDaniel Jasper ErrorMessage = "Expected value."; 3696ed1f85cSDaniel Jasper return false; 3706ed1f85cSDaniel Jasper } 3716366efedSEugene Zelenko auto *ValueString = dyn_cast<llvm::yaml::ScalarNode>(Value); 3726366efedSEugene Zelenko auto *SequenceString = dyn_cast<llvm::yaml::SequenceNode>(Value); 373a735d6eaSSimon Pilgrim if (KeyValue == "arguments") { 374a735d6eaSSimon Pilgrim if (!SequenceString) { 37554042e74SManuel Klimek ErrorMessage = "Expected sequence as value."; 37654042e74SManuel Klimek return false; 3776ed1f85cSDaniel Jasper } 3783ecd8c0aSManuel Klimek Command = std::vector<llvm::yaml::ScalarNode *>(); 3793ecd8c0aSManuel Klimek for (auto &Argument : *SequenceString) { 3806366efedSEugene Zelenko auto *Scalar = dyn_cast<llvm::yaml::ScalarNode>(&Argument); 3813ecd8c0aSManuel Klimek if (!Scalar) { 3823ecd8c0aSManuel Klimek ErrorMessage = "Only strings are allowed in 'arguments'."; 3833ecd8c0aSManuel Klimek return false; 38454042e74SManuel Klimek } 3853ecd8c0aSManuel Klimek Command->push_back(Scalar); 3863ecd8c0aSManuel Klimek } 387a735d6eaSSimon Pilgrim } else { 388a735d6eaSSimon Pilgrim if (!ValueString) { 389a735d6eaSSimon Pilgrim ErrorMessage = "Expected string as value."; 390a735d6eaSSimon Pilgrim return false; 391a735d6eaSSimon Pilgrim } 392a735d6eaSSimon Pilgrim if (KeyValue == "directory") { 393a735d6eaSSimon Pilgrim Directory = ValueString; 39454042e74SManuel Klimek } else if (KeyValue == "command") { 3953ecd8c0aSManuel Klimek if (!Command) 3963ecd8c0aSManuel Klimek Command = std::vector<llvm::yaml::ScalarNode *>(1, ValueString); 39754042e74SManuel Klimek } else if (KeyValue == "file") { 3986ed1f85cSDaniel Jasper File = ValueString; 399399aea30SJoerg Sonnenberger } else if (KeyValue == "output") { 400399aea30SJoerg Sonnenberger Output = ValueString; 4016ed1f85cSDaniel Jasper } else { 402a735d6eaSSimon Pilgrim ErrorMessage = 403a735d6eaSSimon Pilgrim ("Unknown key: \"" + KeyString->getRawValue() + "\"").str(); 4046ed1f85cSDaniel Jasper return false; 4056ed1f85cSDaniel Jasper } 4066ed1f85cSDaniel Jasper } 407a735d6eaSSimon Pilgrim } 4086ed1f85cSDaniel Jasper if (!File) { 4096ed1f85cSDaniel Jasper ErrorMessage = "Missing key: \"file\"."; 4106ed1f85cSDaniel Jasper return false; 4116ed1f85cSDaniel Jasper } 4123ecd8c0aSManuel Klimek if (!Command) { 41354042e74SManuel Klimek ErrorMessage = "Missing key: \"command\" or \"arguments\"."; 4146ed1f85cSDaniel Jasper return false; 4156ed1f85cSDaniel Jasper } 4166ed1f85cSDaniel Jasper if (!Directory) { 4176ed1f85cSDaniel Jasper ErrorMessage = "Missing key: \"directory\"."; 4186ed1f85cSDaniel Jasper return false; 4196ed1f85cSDaniel Jasper } 420f857950dSDmitri Gribenko SmallString<8> FileStorage; 42126cf9c43SDaniel Jasper StringRef FileName = File->getValue(FileStorage); 422f857950dSDmitri Gribenko SmallString<128> NativeFilePath; 42326cf9c43SDaniel Jasper if (llvm::sys::path::is_relative(FileName)) { 424f857950dSDmitri Gribenko SmallString<8> DirectoryStorage; 425f857950dSDmitri Gribenko SmallString<128> AbsolutePath( 42626cf9c43SDaniel Jasper Directory->getValue(DirectoryStorage)); 42726cf9c43SDaniel Jasper llvm::sys::path::append(AbsolutePath, FileName); 4287ec1ec10SKadir Cetinkaya llvm::sys::path::remove_dots(AbsolutePath, /*remove_dot_dot=*/ true); 42992e1b62dSYaron Keren llvm::sys::path::native(AbsolutePath, NativeFilePath); 43026cf9c43SDaniel Jasper } else { 43126cf9c43SDaniel Jasper llvm::sys::path::native(FileName, NativeFilePath); 43226cf9c43SDaniel Jasper } 433399aea30SJoerg Sonnenberger auto Cmd = CompileCommandRef(Directory, File, *Command, Output); 43464f67be3SArgyrios Kyrtzidis IndexByFile[NativeFilePath].push_back(Cmd); 43564f67be3SArgyrios Kyrtzidis AllCommands.push_back(Cmd); 43692e1b62dSYaron Keren MatchTrie.insert(NativeFilePath); 4376ed1f85cSDaniel Jasper } 4386ed1f85cSDaniel Jasper return true; 4396ed1f85cSDaniel Jasper } 440