180814287SRaphael Isemann //===-- OptionValuePathMappings.cpp ---------------------------------------===// 267cc0636SGreg Clayton // 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 667cc0636SGreg Clayton // 767cc0636SGreg Clayton //===----------------------------------------------------------------------===// 867cc0636SGreg Clayton 967cc0636SGreg Clayton #include "lldb/Interpreter/OptionValuePathMappings.h" 1067cc0636SGreg Clayton 11dbd7fabaSJonas Devlieghere #include "lldb/Host/FileSystem.h" 12145d95c9SPavel Labath #include "lldb/Utility/Args.h" 135713a05bSZachary Turner #include "lldb/Utility/FileSpec.h" 14bf9a7730SZachary Turner #include "lldb/Utility/Stream.h" 1567cc0636SGreg Clayton 1667cc0636SGreg Clayton using namespace lldb; 1767cc0636SGreg Clayton using namespace lldb_private; 1893c1b3caSPavel Labath 19b9c1b51eSKate Stone static bool VerifyPathExists(const char *path) { 206a311641SGreg Clayton if (path && path[0]) 21dbd7fabaSJonas Devlieghere return FileSystem::Instance().Exists(path); 226a311641SGreg Clayton else 236a311641SGreg Clayton return false; 246a311641SGreg Clayton } 256a311641SGreg Clayton 26b9c1b51eSKate Stone void OptionValuePathMappings::DumpValue(const ExecutionContext *exe_ctx, 27b9c1b51eSKate Stone Stream &strm, uint32_t dump_mask) { 2867cc0636SGreg Clayton if (dump_mask & eDumpOptionType) 2967cc0636SGreg Clayton strm.Printf("(%s)", GetTypeAsCString()); 30b9c1b51eSKate Stone if (dump_mask & eDumpOptionValue) { 3167cc0636SGreg Clayton if (dump_mask & eDumpOptionType) 3267cc0636SGreg Clayton strm.Printf(" =%s", (m_path_mappings.GetSize() > 0) ? "\n" : ""); 3367cc0636SGreg Clayton m_path_mappings.Dump(&strm); 3467cc0636SGreg Clayton } 3567cc0636SGreg Clayton } 3667cc0636SGreg Clayton 3797206d57SZachary Turner Status OptionValuePathMappings::SetValueFromString(llvm::StringRef value, 38b9c1b51eSKate Stone VarSetOperationType op) { 3997206d57SZachary Turner Status error; 40771ef6d4SMalcolm Parsons Args args(value.str()); 4167cc0636SGreg Clayton const size_t argc = args.GetArgumentCount(); 4267cc0636SGreg Clayton 43b9c1b51eSKate Stone switch (op) { 4467cc0636SGreg Clayton case eVarSetOperationClear: 4567cc0636SGreg Clayton Clear(); 46332e8b1cSGreg Clayton NotifyValueChanged(); 4767cc0636SGreg Clayton break; 4867cc0636SGreg Clayton 4967cc0636SGreg Clayton case eVarSetOperationReplace: 50b9c1b51eSKate Stone // Must be at least one index + 1 pair of paths, and the pair count must be 51b9c1b51eSKate Stone // even 52b9c1b51eSKate Stone if (argc >= 3 && (((argc - 1) & 1) == 0)) { 533a6ba367SMichał Górny uint32_t idx; 5467cc0636SGreg Clayton const uint32_t count = m_path_mappings.GetSize(); 553a6ba367SMichał Górny if (!llvm::to_integer(args.GetArgumentAtIndex(0), idx) || idx > count) { 56b9c1b51eSKate Stone error.SetErrorStringWithFormat( 573a6ba367SMichał Górny "invalid file list index %s, index must be 0 through %u", 583a6ba367SMichał Górny args.GetArgumentAtIndex(0), count); 59b9c1b51eSKate Stone } else { 606a311641SGreg Clayton bool changed = false; 61ca47ac3dSWalter Erquinigo for (size_t i = 1; i < argc; idx++, i += 2) { 626a311641SGreg Clayton const char *orginal_path = args.GetArgumentAtIndex(i); 636a311641SGreg Clayton const char *replace_path = args.GetArgumentAtIndex(i + 1); 64b9c1b51eSKate Stone if (VerifyPathExists(replace_path)) { 65*dfd499a6SXu Jun if (!m_path_mappings.Replace(orginal_path, replace_path, idx, 66*dfd499a6SXu Jun m_notify_changes)) 67*dfd499a6SXu Jun m_path_mappings.Append(orginal_path, replace_path, 68*dfd499a6SXu Jun m_notify_changes); 696a311641SGreg Clayton changed = true; 70b9c1b51eSKate Stone } else { 7130350c25SWalter Erquinigo std::string previousError = 7230350c25SWalter Erquinigo error.Fail() ? std::string(error.AsCString()) + "\n" : ""; 73b9c1b51eSKate Stone error.SetErrorStringWithFormat( 7430350c25SWalter Erquinigo "%sthe replacement path doesn't exist: \"%s\"", 7530350c25SWalter Erquinigo previousError.c_str(), replace_path); 766a311641SGreg Clayton } 776a311641SGreg Clayton } 786a311641SGreg Clayton if (changed) 79332e8b1cSGreg Clayton NotifyValueChanged(); 8067cc0636SGreg Clayton } 81b9c1b51eSKate Stone } else { 82b9c1b51eSKate Stone error.SetErrorString("replace operation takes an array index followed by " 83b9c1b51eSKate Stone "one or more path pairs"); 8467cc0636SGreg Clayton } 8567cc0636SGreg Clayton break; 8667cc0636SGreg Clayton 8767cc0636SGreg Clayton case eVarSetOperationAssign: 88b9c1b51eSKate Stone if (argc < 2 || (argc & 1)) { 8967cc0636SGreg Clayton error.SetErrorString("assign operation takes one or more path pairs"); 9067cc0636SGreg Clayton break; 9167cc0636SGreg Clayton } 9267cc0636SGreg Clayton m_path_mappings.Clear(m_notify_changes); 9367cc0636SGreg Clayton // Fall through to append case 9462e0681aSJason Molenda LLVM_FALLTHROUGH; 9567cc0636SGreg Clayton case eVarSetOperationAppend: 96b9c1b51eSKate Stone if (argc < 2 || (argc & 1)) { 9767cc0636SGreg Clayton error.SetErrorString("append operation takes one or more path pairs"); 9867cc0636SGreg Clayton break; 99b9c1b51eSKate Stone } else { 1006a311641SGreg Clayton bool changed = false; 101b9c1b51eSKate Stone for (size_t i = 0; i < argc; i += 2) { 1026a311641SGreg Clayton const char *orginal_path = args.GetArgumentAtIndex(i); 1036a311641SGreg Clayton const char *replace_path = args.GetArgumentAtIndex(i + 1); 104b9c1b51eSKate Stone if (VerifyPathExists(replace_path)) { 105*dfd499a6SXu Jun m_path_mappings.Append(orginal_path, replace_path, m_notify_changes); 10667cc0636SGreg Clayton m_value_was_set = true; 1076a311641SGreg Clayton changed = true; 108b9c1b51eSKate Stone } else { 10930350c25SWalter Erquinigo std::string previousError = 11030350c25SWalter Erquinigo error.Fail() ? std::string(error.AsCString()) + "\n" : ""; 111b9c1b51eSKate Stone error.SetErrorStringWithFormat( 11230350c25SWalter Erquinigo "%sthe replacement path doesn't exist: \"%s\"", 11330350c25SWalter Erquinigo previousError.c_str(), replace_path); 1146a311641SGreg Clayton } 1156a311641SGreg Clayton } 1166a311641SGreg Clayton if (changed) 117332e8b1cSGreg Clayton NotifyValueChanged(); 11867cc0636SGreg Clayton } 11967cc0636SGreg Clayton break; 12067cc0636SGreg Clayton 12167cc0636SGreg Clayton case eVarSetOperationInsertBefore: 12267cc0636SGreg Clayton case eVarSetOperationInsertAfter: 123b9c1b51eSKate Stone // Must be at least one index + 1 pair of paths, and the pair count must be 124b9c1b51eSKate Stone // even 125b9c1b51eSKate Stone if (argc >= 3 && (((argc - 1) & 1) == 0)) { 1263a6ba367SMichał Górny uint32_t idx; 12767cc0636SGreg Clayton const uint32_t count = m_path_mappings.GetSize(); 1283a6ba367SMichał Górny if (!llvm::to_integer(args.GetArgumentAtIndex(0), idx) || idx > count) { 129b9c1b51eSKate Stone error.SetErrorStringWithFormat( 1303a6ba367SMichał Górny "invalid file list index %s, index must be 0 through %u", 1313a6ba367SMichał Górny args.GetArgumentAtIndex(0), count); 132b9c1b51eSKate Stone } else { 1336a311641SGreg Clayton bool changed = false; 13467cc0636SGreg Clayton if (op == eVarSetOperationInsertAfter) 13567cc0636SGreg Clayton ++idx; 13630350c25SWalter Erquinigo for (size_t i = 1; i < argc; i += 2) { 1376a311641SGreg Clayton const char *orginal_path = args.GetArgumentAtIndex(i); 1386a311641SGreg Clayton const char *replace_path = args.GetArgumentAtIndex(i + 1); 139b9c1b51eSKate Stone if (VerifyPathExists(replace_path)) { 140*dfd499a6SXu Jun m_path_mappings.Insert(orginal_path, replace_path, idx, 141*dfd499a6SXu Jun m_notify_changes); 1426a311641SGreg Clayton changed = true; 14330350c25SWalter Erquinigo idx++; 144b9c1b51eSKate Stone } else { 14530350c25SWalter Erquinigo std::string previousError = 14630350c25SWalter Erquinigo error.Fail() ? std::string(error.AsCString()) + "\n" : ""; 147b9c1b51eSKate Stone error.SetErrorStringWithFormat( 14830350c25SWalter Erquinigo "%sthe replacement path doesn't exist: \"%s\"", 14930350c25SWalter Erquinigo previousError.c_str(), replace_path); 1506a311641SGreg Clayton } 1516a311641SGreg Clayton } 1526a311641SGreg Clayton if (changed) 153332e8b1cSGreg Clayton NotifyValueChanged(); 15467cc0636SGreg Clayton } 155b9c1b51eSKate Stone } else { 156b9c1b51eSKate Stone error.SetErrorString("insert operation takes an array index followed by " 157b9c1b51eSKate Stone "one or more path pairs"); 15867cc0636SGreg Clayton } 15967cc0636SGreg Clayton break; 16067cc0636SGreg Clayton 16167cc0636SGreg Clayton case eVarSetOperationRemove: 162b9c1b51eSKate Stone if (argc > 0) { 16367cc0636SGreg Clayton std::vector<int> remove_indexes; 164ca47ac3dSWalter Erquinigo for (size_t i = 0; i < argc; ++i) { 1653a6ba367SMichał Górny int idx; 1663a6ba367SMichał Górny if (!llvm::to_integer(args.GetArgumentAtIndex(i), idx) || idx < 0 || 1673a6ba367SMichał Górny idx >= (int)m_path_mappings.GetSize()) { 168b9c1b51eSKate Stone error.SetErrorStringWithFormat( 169b9c1b51eSKate Stone "invalid array index '%s', aborting remove operation", 170b9c1b51eSKate Stone args.GetArgumentAtIndex(i)); 171ca47ac3dSWalter Erquinigo break; 172ca47ac3dSWalter Erquinigo } else 173ca47ac3dSWalter Erquinigo remove_indexes.push_back(idx); 17467cc0636SGreg Clayton } 175ca47ac3dSWalter Erquinigo 176ca47ac3dSWalter Erquinigo // Sort and then erase in reverse so indexes are always valid 177ca47ac3dSWalter Erquinigo llvm::sort(remove_indexes.begin(), remove_indexes.end()); 178ca47ac3dSWalter Erquinigo for (auto index : llvm::reverse(remove_indexes)) 179ca47ac3dSWalter Erquinigo m_path_mappings.Remove(index, m_notify_changes); 180ca47ac3dSWalter Erquinigo NotifyValueChanged(); 181b9c1b51eSKate Stone } else { 18267cc0636SGreg Clayton error.SetErrorString("remove operation takes one or more array index"); 18367cc0636SGreg Clayton } 18467cc0636SGreg Clayton break; 18567cc0636SGreg Clayton 18667cc0636SGreg Clayton case eVarSetOperationInvalid: 187c95f7e2aSPavel Labath error = OptionValue::SetValueFromString(value, op); 18867cc0636SGreg Clayton break; 18967cc0636SGreg Clayton } 19067cc0636SGreg Clayton return error; 19167cc0636SGreg Clayton } 192