+}
+
+/* See linespec.h. */
+
+void
+linespec_complete_function (completion_tracker &tracker,
+ const char *function,
+ symbol_name_match_type func_match_type,
+ const char *source_filename)
+{
+ complete_symbol_mode mode = complete_symbol_mode::LINESPEC;
+
+ if (source_filename != NULL)
+ {
+ collect_file_symbol_completion_matches (tracker, mode, func_match_type,
+ function, function, source_filename);
+ }
+ else
+ {
+ collect_symbol_completion_matches (tracker, mode, func_match_type,
+ function, function);
+
+ }
+}
+
+/* Helper for complete_linespec to simplify it. SOURCE_FILENAME is
+ only meaningful if COMPONENT is FUNCTION. */
+
+static void
+complete_linespec_component (linespec_parser *parser,
+ completion_tracker &tracker,
+ const char *text,
+ linespec_complete_what component,
+ const char *source_filename)
+{
+ if (component == linespec_complete_what::KEYWORD)
+ {
+ complete_on_enum (tracker, linespec_keywords, text, text);
+ }
+ else if (component == linespec_complete_what::EXPRESSION)
+ {
+ const char *word
+ = advance_to_expression_complete_word_point (tracker, text);
+ complete_expression (tracker, text, word);
+ }
+ else if (component == linespec_complete_what::FUNCTION)
+ {
+ completion_list fn_list;
+
+ symbol_name_match_type match_type
+ = PARSER_EXPLICIT (parser)->func_name_match_type;
+ linespec_complete_function (tracker, text, match_type, source_filename);
+ if (source_filename == NULL)
+ {
+ /* Haven't seen a source component, like in "b
+ file.c:function[TAB]". Maybe this wasn't a function, but
+ a filename instead, like "b file.[TAB]". */
+ fn_list = complete_source_filenames (text);
+ }
+
+ /* If we only have a single filename completion, append a ':' for
+ the user, since that's the only thing that can usefully follow
+ the filename. */
+ if (fn_list.size () == 1 && !tracker.have_completions ())
+ {
+ char *fn = fn_list[0].release ();
+
+ /* If we also need to append a quote char, it needs to be
+ appended before the ':'. Append it now, and make ':' the
+ new "quote" char. */
+ if (tracker.quote_char ())
+ {
+ char quote_char_str[2] = { (char) tracker.quote_char () };
+
+ fn = reconcat (fn, fn, quote_char_str, (char *) NULL);
+ tracker.set_quote_char (':');
+ }
+ else
+ fn = reconcat (fn, fn, ":", (char *) NULL);
+ fn_list[0].reset (fn);
+
+ /* Tell readline to skip appending a space. */
+ tracker.set_suppress_append_ws (true);
+ }
+ tracker.add_completions (std::move (fn_list));
+ }
+}
+
+/* Helper for linespec_complete_label. Find labels that match
+ LABEL_NAME in the function symbols listed in the PARSER, and add
+ them to the tracker. */
+
+static void
+complete_label (completion_tracker &tracker,
+ linespec_parser *parser,
+ const char *label_name)
+{
+ std::vector<block_symbol> label_function_symbols;
+ std::vector<block_symbol> *labels
+ = find_label_symbols (PARSER_STATE (parser),
+ PARSER_RESULT (parser)->function_symbols,
+ &label_function_symbols,
+ label_name, true);
+
+ if (labels != nullptr)
+ {
+ for (const auto &label : *labels)
+ {
+ char *match = xstrdup (label.symbol->search_name ());
+ tracker.add_completion (gdb::unique_xmalloc_ptr<char> (match));
+ }
+ delete labels;
+ }
+}
+
+/* See linespec.h. */
+
+void
+linespec_complete_label (completion_tracker &tracker,
+ const struct language_defn *language,
+ const char *source_filename,
+ const char *function_name,
+ symbol_name_match_type func_name_match_type,
+ const char *label_name)
+{
+ linespec_parser parser (0, language, NULL, NULL, 0, NULL);
+
+ line_offset unknown_offset = { 0, LINE_OFFSET_UNKNOWN };
+
+ try
+ {
+ convert_explicit_location_to_linespec (PARSER_STATE (&parser),
+ PARSER_RESULT (&parser),
+ source_filename,
+ function_name,
+ func_name_match_type,
+ NULL, unknown_offset);
+ }
+ catch (const gdb_exception_error &ex)
+ {
+ return;
+ }
+
+ complete_label (tracker, &parser, label_name);
+}
+
+/* See description in linespec.h. */
+
+void
+linespec_complete (completion_tracker &tracker, const char *text,
+ symbol_name_match_type match_type)
+{
+ const char *orig = text;
+
+ linespec_parser parser (0, current_language, NULL, NULL, 0, NULL);
+ parser.lexer.saved_arg = text;
+ PARSER_EXPLICIT (&parser)->func_name_match_type = match_type;
+ 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, match_type);
+ }
+ catch (const gdb_exception_error &except)
+ {
+ }
+
+ 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;
+
+ std::vector<block_symbol> function_symbols;
+ std::vector<bound_minimal_symbol> minimal_symbols;
+ find_linespec_symbols (PARSER_STATE (&parser),
+ PARSER_RESULT (&parser)->file_symtabs,
+ func_name, match_type,
+ &function_symbols, &minimal_symbols);
+
+ PARSER_RESULT (&parser)->function_symbols
+ = new std::vector<block_symbol> (std::move (function_symbols));
+ PARSER_RESULT (&parser)->minimal_symbols
+ = new std::vector<bound_minimal_symbol> (std::move (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 *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);
+ }
+ }