+/* See description in linespec.h. */
+
+void
+linespec_complete (completion_tracker &tracker, const char *text)
+{
+ linespec_parser parser;
+ struct cleanup *cleanup;
+ const char *orig = text;
+
+ linespec_parser_new (&parser, 0, current_language, NULL, NULL, 0, NULL);
+ cleanup = make_cleanup (linespec_parser_delete, &parser);
+ parser.lexer.saved_arg = text;
+ PARSER_STREAM (&parser) = text;
+
+ parser.completion_tracker = &tracker;
+ PARSER_STATE (&parser)->is_linespec = 1;
+
+ /* Parse as much as possible. parser.completion_word will hold
+ furthest completion point we managed to parse to. */
+ TRY
+ {
+ parse_linespec (&parser, text);
+ }
+ CATCH (except, RETURN_MASK_ERROR)
+ {
+ }
+ END_CATCH
+
+ if (parser.completion_quote_char != '\0'
+ && parser.completion_quote_end != NULL
+ && parser.completion_quote_end[1] == '\0')
+ {
+ /* If completing a quoted string with the cursor right at
+ terminating quote char, complete the completion word without
+ interpretation, so that readline advances the cursor one
+ whitespace past the quote, even if there's no match. This
+ makes these cases behave the same:
+
+ before: "b function()"
+ after: "b function() "
+
+ before: "b 'function()'"
+ after: "b 'function()' "
+
+ and trusts the user in this case:
+
+ before: "b 'not_loaded_function_yet()'"
+ after: "b 'not_loaded_function_yet()' "
+ */
+ parser.complete_what = linespec_complete_what::NOTHING;
+ parser.completion_quote_char = '\0';
+
+ gdb::unique_xmalloc_ptr<char> text_copy
+ (xstrdup (parser.completion_word));
+ tracker.add_completion (std::move (text_copy));
+ }
+
+ tracker.set_quote_char (parser.completion_quote_char);
+
+ if (parser.complete_what == linespec_complete_what::LABEL)
+ {
+ parser.complete_what = linespec_complete_what::NOTHING;
+
+ const char *func_name = PARSER_EXPLICIT (&parser)->function_name;
+
+ VEC (symbolp) *function_symbols;
+ VEC (bound_minimal_symbol_d) *minimal_symbols;
+ find_linespec_symbols (PARSER_STATE (&parser),
+ PARSER_RESULT (&parser)->file_symtabs,
+ func_name,
+ &function_symbols, &minimal_symbols);
+
+ PARSER_RESULT (&parser)->function_symbols = function_symbols;
+ PARSER_RESULT (&parser)->minimal_symbols = minimal_symbols;
+
+ complete_label (tracker, &parser, parser.completion_word);
+ }
+ else if (parser.complete_what == linespec_complete_what::FUNCTION)
+ {
+ /* While parsing/lexing, we didn't know whether the completion
+ word completes to a unique function/source name already or
+ not.
+
+ E.g.:
+ "b function() <tab>"
+ may need to complete either to:
+ "b function() const"
+ or to:
+ "b function() if/thread/task"
+
+ Or, this:
+ "b foo t"
+ may need to complete either to:
+ "b foo template_fun<T>()"
+ with "foo" being the template function's return type, or to:
+ "b foo thread/task"
+
+ Or, this:
+ "b file<TAB>"
+ may need to complete either to a source file name:
+ "b file.c"
+ or this, also a filename, but a unique completion:
+ "b file.c:"
+ or to a function name:
+ "b file_function"
+
+ Address that by completing assuming source or function, and
+ seeing if we find a completion that matches exactly the
+ completion word. If so, then it must be a function (see note
+ below) and we advance the completion word to the end of input
+ and switch to KEYWORD completion mode.
+
+ Note: if we find a unique completion for a source filename,
+ then it won't match the completion word, because the LCD will
+ contain a trailing ':'. And if we're completing at or after
+ the ':', then complete_linespec_component won't try to
+ complete on source filenames. */
+
+ const char *text = parser.completion_word;
+ const char *word = parser.completion_word;
+
+ complete_linespec_component (&parser, tracker,
+ parser.completion_word,
+ linespec_complete_what::FUNCTION,
+ PARSER_EXPLICIT (&parser)->source_filename);
+
+ parser.complete_what = linespec_complete_what::NOTHING;
+
+ if (tracker.quote_char ())
+ {
+ /* The function/file name was not close-quoted, so this
+ can't be a keyword. Note: complete_linespec_component
+ may have swapped the original quote char for ':' when we
+ get here, but that still indicates the same. */
+ }
+ else if (!tracker.have_completions ())
+ {
+ size_t key_start;
+ size_t wordlen = strlen (parser.completion_word);
+
+ key_start
+ = string_find_incomplete_keyword_at_end (linespec_keywords,
+ parser.completion_word,
+ wordlen);
+
+ if (key_start != -1
+ || (wordlen > 0
+ && parser.completion_word[wordlen - 1] == ' '))
+ {
+ parser.completion_word += key_start;
+ parser.complete_what = linespec_complete_what::KEYWORD;
+ }
+ }
+ else if (tracker.completes_to_completion_word (word))
+ {
+ /* Skip the function and complete on keywords. */
+ parser.completion_word += strlen (word);
+ parser.complete_what = linespec_complete_what::KEYWORD;
+ tracker.discard_completions ();
+ }
+ }
+
+ tracker.advance_custom_word_point_by (parser.completion_word - orig);
+
+ complete_linespec_component (&parser, tracker,
+ parser.completion_word,
+ parser.complete_what,
+ PARSER_EXPLICIT (&parser)->source_filename);
+
+ /* If we're past the "filename:function:label:offset" linespec, and
+ didn't find any match, then assume the user might want to create
+ a pending breakpoint anyway and offer the keyword
+ completions. */
+ if (!parser.completion_quote_char
+ && (parser.complete_what == linespec_complete_what::FUNCTION
+ || parser.complete_what == linespec_complete_what::LABEL
+ || parser.complete_what == linespec_complete_what::NOTHING)
+ && !tracker.have_completions ())
+ {
+ const char *end
+ = parser.completion_word + strlen (parser.completion_word);
+
+ if (end > orig && end[-1] == ' ')
+ {
+ tracker.advance_custom_word_point_by (end - parser.completion_word);
+
+ complete_linespec_component (&parser, tracker, end,
+ linespec_complete_what::KEYWORD,
+ NULL);
+ }
+ }
+
+ do_cleanups (cleanup);
+}
+