diff --git a/lldb/include/lldb/Interpreter/CommandAlias.h b/lldb/include/lldb/Interpreter/CommandAlias.h index 049d54f301f7..4658a1e444f4 100644 --- a/lldb/include/lldb/Interpreter/CommandAlias.h +++ b/lldb/include/lldb/Interpreter/CommandAlias.h @@ -58,13 +58,13 @@ public: bool IsDashDashCommand() override; - const char *GetHelp() override; + llvm::StringRef GetHelp() override; - const char *GetHelpLong() override; + llvm::StringRef GetHelpLong() override; - void SetHelp(const char *str) override; + void SetHelp(llvm::StringRef str) override; - void SetHelpLong(const char *str) override; + void SetHelpLong(llvm::StringRef str) override; bool Execute(const char *args_string, CommandReturnObject &result) override; diff --git a/lldb/include/lldb/Interpreter/CommandObject.h b/lldb/include/lldb/Interpreter/CommandObject.h index 0a11811f949d..272d021b185a 100644 --- a/lldb/include/lldb/Interpreter/CommandObject.h +++ b/lldb/include/lldb/Interpreter/CommandObject.h @@ -122,19 +122,19 @@ public: CommandInterpreter &GetCommandInterpreter() { return m_interpreter; } - virtual const char *GetHelp(); + virtual llvm::StringRef GetHelp(); - virtual const char *GetHelpLong(); + virtual llvm::StringRef GetHelpLong(); - virtual const char *GetSyntax(); + virtual llvm::StringRef GetSyntax(); llvm::StringRef GetCommandName() const; - virtual void SetHelp(const char *str); + virtual void SetHelp(llvm::StringRef str); - virtual void SetHelpLong(const char *str); + virtual void SetHelpLong(llvm::StringRef str); - void SetSyntax(const char *str); + void SetSyntax(llvm::StringRef str); // override this to return true if you want to enable the user to delete // the Command object from the Command dictionary (aliases have their own @@ -167,7 +167,7 @@ public: StringList &commands_found, StringList &commands_help) {} - void FormatLongHelpText(Stream &output_strm, const char *long_help); + void FormatLongHelpText(Stream &output_strm, llvm::StringRef long_help); void GenerateHelpText(CommandReturnObject &result); @@ -219,7 +219,7 @@ public: bool ParseOptions(Args &args, CommandReturnObject &result); - void SetCommandName(const char *name); + void SetCommandName(llvm::StringRef name); //------------------------------------------------------------------ /// The input array contains a parsed version of the line. The insertion diff --git a/lldb/include/lldb/Interpreter/CommandObjectMultiword.h b/lldb/include/lldb/Interpreter/CommandObjectMultiword.h index 07f51e344aa0..c36f9d139fd7 100644 --- a/lldb/include/lldb/Interpreter/CommandObjectMultiword.h +++ b/lldb/include/lldb/Interpreter/CommandObjectMultiword.h @@ -90,7 +90,7 @@ public: // used for this object. virtual CommandObject *GetProxyCommandObject() = 0; - const char *GetHelpLong() override; + llvm::StringRef GetHelpLong() override; bool IsRemovable() const override; diff --git a/lldb/source/API/SBCommandInterpreter.cpp b/lldb/source/API/SBCommandInterpreter.cpp index 45f8ab364b24..aa4953999b84 100644 --- a/lldb/source/API/SBCommandInterpreter.cpp +++ b/lldb/source/API/SBCommandInterpreter.cpp @@ -542,11 +542,13 @@ const char *SBCommand::GetName() { } const char *SBCommand::GetHelp() { - return (IsValid() ? m_opaque_sp->GetHelp() : nullptr); + return (IsValid() ? ConstString(m_opaque_sp->GetHelp()).AsCString() + : nullptr); } const char *SBCommand::GetHelpLong() { - return (IsValid() ? m_opaque_sp->GetHelpLong() : nullptr); + return (IsValid() ? ConstString(m_opaque_sp->GetHelpLong()).AsCString() + : nullptr); } void SBCommand::SetHelp(const char *help) { diff --git a/lldb/source/Commands/CommandObjectCommands.cpp b/lldb/source/Commands/CommandObjectCommands.cpp index 1106b37720c4..b94000fe8ffe 100644 --- a/lldb/source/Commands/CommandObjectCommands.cpp +++ b/lldb/source/Commands/CommandObjectCommands.cpp @@ -1282,7 +1282,7 @@ public: : CommandObjectRaw(interpreter, name), m_function_name(funct), m_synchro(synch), m_fetched_help_long(false) { if (!help.empty()) - SetHelp(help.c_str()); + SetHelp(help); else { StreamString stream; stream.Printf("For more information run 'help %s'", name.c_str()); @@ -1298,17 +1298,19 @@ public: ScriptedCommandSynchronicity GetSynchronicity() { return m_synchro; } - const char *GetHelpLong() override { - if (!m_fetched_help_long) { - ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter(); - if (scripter) { - std::string docstring; - m_fetched_help_long = scripter->GetDocumentationForItem( - m_function_name.c_str(), docstring); - if (!docstring.empty()) - SetHelpLong(docstring.c_str()); - } - } + llvm::StringRef GetHelpLong() override { + if (m_fetched_help_long) + return CommandObjectRaw::GetHelpLong(); + + ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter(); + if (!scripter) + return CommandObjectRaw::GetHelpLong(); + + std::string docstring; + m_fetched_help_long = + scripter->GetDocumentationForItem(m_function_name.c_str(), docstring); + if (!docstring.empty()) + SetHelpLong(docstring); return CommandObjectRaw::GetHelpLong(); } @@ -1371,31 +1373,34 @@ public: ScriptedCommandSynchronicity GetSynchronicity() { return m_synchro; } - const char *GetHelp() override { - if (!m_fetched_help_short) { - ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter(); - if (scripter) { - std::string docstring; - m_fetched_help_short = - scripter->GetShortHelpForCommandObject(m_cmd_obj_sp, docstring); - if (!docstring.empty()) - SetHelp(docstring.c_str()); - } - } + llvm::StringRef GetHelp() override { + if (m_fetched_help_short) + return CommandObjectRaw::GetHelp(); + ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter(); + if (!scripter) + return CommandObjectRaw::GetHelp(); + std::string docstring; + m_fetched_help_short = + scripter->GetShortHelpForCommandObject(m_cmd_obj_sp, docstring); + if (!docstring.empty()) + SetHelp(docstring); + return CommandObjectRaw::GetHelp(); } - const char *GetHelpLong() override { - if (!m_fetched_help_long) { - ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter(); - if (scripter) { - std::string docstring; - m_fetched_help_long = - scripter->GetLongHelpForCommandObject(m_cmd_obj_sp, docstring); - if (!docstring.empty()) - SetHelpLong(docstring.c_str()); - } - } + llvm::StringRef GetHelpLong() override { + if (m_fetched_help_long) + return CommandObjectRaw::GetHelpLong(); + + ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter(); + if (!scripter) + return CommandObjectRaw::GetHelpLong(); + + std::string docstring; + m_fetched_help_long = + scripter->GetLongHelpForCommandObject(m_cmd_obj_sp, docstring); + if (!docstring.empty()) + SetHelpLong(docstring); return CommandObjectRaw::GetHelpLong(); } diff --git a/lldb/source/Commands/CommandObjectMultiword.cpp b/lldb/source/Commands/CommandObjectMultiword.cpp index 9194b1d51032..249863b4e8a8 100644 --- a/lldb/source/Commands/CommandObjectMultiword.cpp +++ b/lldb/source/Commands/CommandObjectMultiword.cpp @@ -278,11 +278,11 @@ CommandObjectProxy::CommandObjectProxy(CommandInterpreter &interpreter, CommandObjectProxy::~CommandObjectProxy() = default; -const char *CommandObjectProxy::GetHelpLong() { +llvm::StringRef CommandObjectProxy::GetHelpLong() { CommandObject *proxy_command = GetProxyCommandObject(); if (proxy_command) return proxy_command->GetHelpLong(); - return nullptr; + return llvm::StringRef(); } bool CommandObjectProxy::IsRemovable() const { diff --git a/lldb/source/Commands/CommandObjectType.cpp b/lldb/source/Commands/CommandObjectType.cpp index 64614299bf09..e991817edc5d 100644 --- a/lldb/source/Commands/CommandObjectType.cpp +++ b/lldb/source/Commands/CommandObjectType.cpp @@ -2841,26 +2841,25 @@ public: Options *GetOptions() override { return &m_option_group; } - const char *GetHelpLong() override { - if (m_cmd_help_long.empty()) { - StreamString stream; - // FIXME: hardcoding languages is not good - lldb::LanguageType languages[] = {eLanguageTypeObjC, - eLanguageTypeC_plus_plus}; + llvm::StringRef GetHelpLong() override { + if (!m_cmd_help_long.empty()) + return m_cmd_help_long; - for (const auto lang_type : languages) { - if (auto language = Language::FindPlugin(lang_type)) { - if (const char *help = - language->GetLanguageSpecificTypeLookupHelp()) { - stream.Printf("%s\n", help); - } + StreamString stream; + // FIXME: hardcoding languages is not good + lldb::LanguageType languages[] = {eLanguageTypeObjC, + eLanguageTypeC_plus_plus}; + + for (const auto lang_type : languages) { + if (auto language = Language::FindPlugin(lang_type)) { + if (const char *help = language->GetLanguageSpecificTypeLookupHelp()) { + stream.Printf("%s\n", help); } } - - if (stream.GetData()) - m_cmd_help_long.assign(stream.GetString()); } - return this->CommandObject::GetHelpLong(); + + m_cmd_help_long = stream.GetString(); + return m_cmd_help_long; } bool DoExecute(const char *raw_command_line, diff --git a/lldb/source/Interpreter/CommandAlias.cpp b/lldb/source/Interpreter/CommandAlias.cpp index ba6ac7d40d79..d774a4ee4775 100644 --- a/lldb/source/Interpreter/CommandAlias.cpp +++ b/lldb/source/Interpreter/CommandAlias.cpp @@ -224,28 +224,28 @@ std::pair CommandAlias::Desugar() { // allow CommandAlias objects to provide their own help, but fallback to the // info // for the underlying command if no customization has been provided -void CommandAlias::SetHelp(const char *str) { +void CommandAlias::SetHelp(llvm::StringRef str) { this->CommandObject::SetHelp(str); m_did_set_help = true; } -void CommandAlias::SetHelpLong(const char *str) { +void CommandAlias::SetHelpLong(llvm::StringRef str) { this->CommandObject::SetHelpLong(str); m_did_set_help_long = true; } -const char *CommandAlias::GetHelp() { +llvm::StringRef CommandAlias::GetHelp() { if (!m_cmd_help_short.empty() || m_did_set_help) - return m_cmd_help_short.c_str(); + return m_cmd_help_short; if (IsValid()) return m_underlying_command_sp->GetHelp(); - return nullptr; + return llvm::StringRef(); } -const char *CommandAlias::GetHelpLong() { +llvm::StringRef CommandAlias::GetHelpLong() { if (!m_cmd_help_long.empty() || m_did_set_help_long) - return m_cmd_help_long.c_str(); + return m_cmd_help_long; if (IsValid()) return m_underlying_command_sp->GetHelpLong(); - return nullptr; + return llvm::StringRef(); } diff --git a/lldb/source/Interpreter/CommandObject.cpp b/lldb/source/Interpreter/CommandObject.cpp index b04c6f754b5f..b3b52db57e94 100644 --- a/lldb/source/Interpreter/CommandObject.cpp +++ b/lldb/source/Interpreter/CommandObject.cpp @@ -52,48 +52,42 @@ CommandObject::CommandObject(CommandInterpreter &interpreter, llvm::StringRef na CommandObject::~CommandObject() {} -const char *CommandObject::GetHelp() { return m_cmd_help_short.c_str(); } +llvm::StringRef CommandObject::GetHelp() { return m_cmd_help_short; } -const char *CommandObject::GetHelpLong() { return m_cmd_help_long.c_str(); } +llvm::StringRef CommandObject::GetHelpLong() { return m_cmd_help_long; } -const char *CommandObject::GetSyntax() { - if (m_cmd_syntax.length() == 0) { - StreamString syntax_str; - syntax_str.Printf("%s", GetCommandName().str().c_str()); - if (!IsDashDashCommand() && GetOptions() != nullptr) - syntax_str.Printf(" "); - if (m_arguments.size() > 0) { - syntax_str.Printf(" "); - if (!IsDashDashCommand() && WantsRawCommandString() && GetOptions() && - GetOptions()->NumCommandOptions()) - syntax_str.Printf("-- "); - GetFormattedCommandArguments(syntax_str); - } - m_cmd_syntax = syntax_str.GetData(); +llvm::StringRef CommandObject::GetSyntax() { + if (m_cmd_syntax.empty()) + return m_cmd_syntax; + + StreamString syntax_str; + syntax_str.PutCString(GetCommandName()); + + if (!IsDashDashCommand() && GetOptions() != nullptr) + syntax_str.PutCString(" "); + + if (!m_arguments.empty()) { + syntax_str.PutCString(" "); + + if (!IsDashDashCommand() && WantsRawCommandString() && GetOptions() && + GetOptions()->NumCommandOptions()) + syntax_str.PutCString("-- "); + GetFormattedCommandArguments(syntax_str); } + m_cmd_syntax = syntax_str.GetData(); - return m_cmd_syntax.c_str(); + return m_cmd_syntax; } llvm::StringRef CommandObject::GetCommandName() const { return m_cmd_name; } -void CommandObject::SetCommandName(const char *name) { m_cmd_name = name; } +void CommandObject::SetCommandName(llvm::StringRef name) { m_cmd_name = name; } -void CommandObject::SetHelp(const char *cstr) { - if (cstr) - m_cmd_help_short = cstr; - else - m_cmd_help_short.assign(""); -} +void CommandObject::SetHelp(llvm::StringRef str) { m_cmd_help_short = str; } -void CommandObject::SetHelpLong(const char *cstr) { - if (cstr) - m_cmd_help_long = cstr; - else - m_cmd_help_long.assign(""); -} +void CommandObject::SetHelpLong(llvm::StringRef str) { m_cmd_help_long = str; } -void CommandObject::SetSyntax(const char *cstr) { m_cmd_syntax = cstr; } +void CommandObject::SetSyntax(llvm::StringRef str) { m_cmd_syntax = str; } Options *CommandObject::GetOptions() { // By default commands don't have options unless this virtual function @@ -331,15 +325,15 @@ bool CommandObject::HelpTextContainsWord(const char *search_word, bool found_word = false; - const char *short_help = GetHelp(); - const char *long_help = GetHelpLong(); - const char *syntax_help = GetSyntax(); + llvm::StringRef short_help = GetHelp(); + llvm::StringRef long_help = GetHelpLong(); + llvm::StringRef syntax_help = GetSyntax(); - if (search_short_help && short_help && strcasestr(short_help, search_word)) + if (search_short_help && short_help.contains_lower(search_word)) found_word = true; - else if (search_long_help && long_help && strcasestr(long_help, search_word)) + else if (search_long_help && long_help.contains_lower(search_word)) found_word = true; - else if (search_syntax && syntax_help && strcasestr(syntax_help, search_word)) + else if (search_syntax && syntax_help.contains_lower(search_word)) found_word = true; if (!found_word && search_options && GetOptions() != nullptr) { @@ -842,7 +836,7 @@ static const char *ExprPathHelpTextCallback() { } void CommandObject::FormatLongHelpText(Stream &output_strm, - const char *long_help) { + llvm::StringRef long_help) { CommandInterpreter &interpreter = GetCommandInterpreter(); std::stringstream lineStream(long_help); std::string line; @@ -884,8 +878,8 @@ void CommandObject::GenerateHelpText(Stream &output_strm) { output_strm, this, GetCommandInterpreter().GetDebugger().GetTerminalWidth()); } - const char *long_help = GetHelpLong(); - if ((long_help != nullptr) && (strlen(long_help) > 0)) { + llvm::StringRef long_help = GetHelpLong(); + if (!long_help.empty()) { FormatLongHelpText(output_strm, long_help); } if (!IsDashDashCommand() && options && options->NumCommandOptions() > 0) { diff --git a/lldb/source/Interpreter/CommandObjectRegexCommand.cpp b/lldb/source/Interpreter/CommandObjectRegexCommand.cpp index 22824b2d8912..79d3bb1b5bee 100644 --- a/lldb/source/Interpreter/CommandObjectRegexCommand.cpp +++ b/lldb/source/Interpreter/CommandObjectRegexCommand.cpp @@ -71,7 +71,7 @@ bool CommandObjectRegexCommand::DoExecute(const char *command, } } result.SetStatus(eReturnStatusFailed); - if (GetSyntax() != nullptr) + if (!GetSyntax().empty()) result.AppendError(GetSyntax()); else result.AppendErrorWithFormat("Command contents '%s' failed to match any "