binutils-gdb/gdb/linespec.h
Pedro Alves c45ec17c07 A smarter linespec completer
Continuing the theme of the explicit locations patch, this patch gets
rid of the need for quoting function names in linespec TAB completion.
To recap, when you have overloads in your program, and you want to set
a breakpoint in one of them:

 void function(int);  // set breakpoint here.
 void function(long);

 (gdb) b function(i[TAB]
 <all the symbols in the program that start with "i" are uselessly shown...>

This patch gets rid of the need for quoting by switching the linespec
completer to use the custom completion word point mechanism added in
the previous explicit location patch (extending it as needed), to
correctly determine the right completion word point.  In the case
above, we want the completer to figure out that it's completing a
function name that starts with "function(i", and it now does.

We also want the completer to know when it's potentially completing a
source file name, for:

(gdb) break source.[TAB] -> source.c:
(gdb) break source.c:  # Type line number or function name now

And we want it to know to complete label names, which it doesn't today:

(gdb) break function:lab[TAB]

etc., etc.

So what we want is for completion to grok the input string as closely
to how the linespec parser groks it.

With that in mind, the solution suggests itself - make the linespec
completer use the same parsing code as normal linespec parsing.

That's what the patch does.  The old completer is replaced by one that
reuses the actual linespec parser as much as possible.  This (ideally)
eliminate differences between what completion understands and actually
setting breakpoints understands by design.

The completer now offers sensible completion candidates depending on
which component of the linespec is being completed, source filename,
function, line number, expression, and (a new addition), labels.  For
example, when completing the function part, we now show the full name
of the method as completion candidates, instead of showing whatever
comes after what readline considered the word break character:

 (gdb) break klass::method[TAB]
 klass:method1(int)
 klass:method2()

If input is past the function, then we now offer keyword condidates:

  (gdb) b function(int) [TAB]
  if      task    thread

If input is past a keyword, we offer expression completion, which is
different from linespec completion:

  (gdb) b main if 1 + glo[TAB]
  global

(e.g., completes on types, struct data fields, etc.)

As mentioned, this teaches the linespec completer about completing
label symbols too:

  (gdb) b source.c:function:lab[TAB]

A nice convenience is that when completion uniquely matches a source
name, gdb adds the ":" automatically for you:

  (gdb) b filenam[TAB]
  (gdb) b filename.c:  # ':' auto-added, cursor right after it.

It's the little details.  :-)

I worked on this patch in parallel with writing the (big) testcase
added closer to the end of the series, which exercises many many
tricky cases around quoting and whitespace insertion placement.  In
general, I think it now all Just Works.

gdb/ChangeLog:
2017-07-17  Pedro Alves  <palves@redhat.com>

	* completer.c (complete_source_filenames): New function.
	(complete_address_and_linespec_locations): New function.
	(location_completer): Use complete_address_and_linespec_locations.
	(completion_tracker::build_completion_result): Honor the tracker's
	request to suppress append.
	* completer.h (completion_tracker::suppress_append_ws)
	(completion_tracker::set_suppress_append_ws): New methods.
	(completion_tracker::m_suppress_append_ws): New field.
	(complete_source_filenames): New declaration.
	* linespec.c (linespec_complete_what): New.
	(struct ls_parser) <complete_what, completion_word,
	completion_quote_char, completion_quote_end, completion_tracker>:
	New fields.
	(string_find_incomplete_keyword_at_end): New.
	(linespec_lexer_lex_string): Record quote char.  If in completion
	mode, don't throw.
	(linespec_lexer_consume_token): Advance the completion word point.
	(linespec_lexer_peek_token): Save/restore completion info.
	(save_stream_and_consume_token): New.
	(set_completion_after_number): New.
	(linespec_parse_basic): Set what to complete next depending on
	token.  Handle function and label completions specially.
	(parse_linespec): Disable objc shortcut in completion mode.  Set
	what to complete next depending on token type.  Skip keyword if in
	completion mode.
	(complete_linespec_component, linespec_complete): New.
	* linespec.h (linespec_complete): Declare.

gdb/testsuite/ChangeLog:
2017-07-17  Pedro Alves  <palves@redhat.com>

	* gdb.base/completion.exp: Adjust expected output.
	* gdb.linespec/ls-errs.exp: Don't send tab characters, now that
	the completer works.
2017-07-17 20:29:37 +01:00

218 lines
7.5 KiB
C

/* Header for GDB line completion.
Copyright (C) 2000-2017 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#if !defined (LINESPEC_H)
#define LINESPEC_H 1
struct symtab;
#include "location.h"
#include "vec.h"
/* Flags to pass to decode_line_1 and decode_line_full. */
enum decode_line_flags
{
/* Set this flag if you want the resulting SALs to describe the
first line of indicated functions. */
DECODE_LINE_FUNFIRSTLINE = 1,
/* Set this flag if you want "list mode". In this mode, a
FILE:LINE linespec will always return a result, and such
linespecs will not be expanded to all matches. */
DECODE_LINE_LIST_MODE = 2
};
/* decode_line_full returns a vector of these. */
struct linespec_sals
{
/* This is the location corresponding to the sals contained in this
object. It can be passed as the FILTER argument to future calls
to decode_line_full. This is freed by the linespec_result
destructor. */
char *canonical;
/* Sals. The 'sals' field is destroyed by the linespec_result
destructor. */
struct symtabs_and_lines sals;
};
typedef struct linespec_sals linespec_sals;
DEF_VEC_O (linespec_sals);
/* An instance of this may be filled in by decode_line_1. The caller
must make copies of any data that it needs to keep. */
struct linespec_result
{
linespec_result ()
: special_display (0),
pre_expanded (0),
sals (NULL)
{
}
~linespec_result ();
linespec_result (const linespec_result &) = delete;
linespec_result &operator= (const linespec_result &) = delete;
/* If non-zero, the linespec should be displayed to the user. This
is used by "unusual" linespecs where the ordinary `info break'
display mechanism would do the wrong thing. */
int special_display;
/* If non-zero, the linespec result should be considered to be a
"pre-expanded" multi-location linespec. A pre-expanded linespec
holds all matching locations in a single linespec_sals
object. */
int pre_expanded;
/* If PRE_EXPANDED is non-zero, this is set to the location entered
by the user. */
event_location_up location;
/* The sals. The vector will be freed by the destructor. */
VEC (linespec_sals) *sals;
};
/* Decode a linespec using the provided default symtab and line. */
extern struct symtabs_and_lines
decode_line_1 (const struct event_location *location, int flags,
struct program_space *search_pspace,
struct symtab *default_symtab, int default_line);
/* Parse LOCATION and return results. This is the "full"
interface to this module, which handles multiple results
properly.
For FLAGS, see decode_line_flags. DECODE_LINE_LIST_MODE is not
valid for this function.
If SEARCH_PSPACE is not NULL, symbol search is restricted to just
that program space.
DEFAULT_SYMTAB and DEFAULT_LINE describe the default location.
DEFAULT_SYMTAB can be NULL, in which case the current symtab and
line are used.
CANONICAL is where the results are stored. It must not be NULL.
SELECT_MODE must be one of the multiple_symbols_* constants, or
NULL. It determines how multiple results will be handled. If
NULL, the appropriate CLI value will be used.
FILTER can either be NULL or a string holding a canonical name.
This is only valid when SELECT_MODE is multiple_symbols_all.
Multiple results are handled differently depending on the
arguments:
. With multiple_symbols_cancel, an exception is thrown.
. With multiple_symbols_ask, a menu is presented to the user. The
user may select none, in which case an exception is thrown; or all,
which is handled like multiple_symbols_all, below. Otherwise,
CANONICAL->SALS will have one entry for each name the user chose.
. With multiple_symbols_all, CANONICAL->SALS will have a single
entry describing all the matching locations. If FILTER is
non-NULL, then only locations whose canonical name is equal (in the
strcmp sense) to FILTER will be returned; all others will be
filtered out. */
extern void decode_line_full (const struct event_location *location, int flags,
struct program_space *search_pspace,
struct symtab *default_symtab, int default_line,
struct linespec_result *canonical,
const char *select_mode,
const char *filter);
/* Given a string, return the line specified by it, using the current
source symtab and line as defaults.
This is for commands like "list" and "breakpoint". */
extern struct symtabs_and_lines decode_line_with_current_source (char *, int);
/* Given a string, return the line specified by it, using the last displayed
codepoint's values as defaults, or nothing if they aren't valid. */
extern struct symtabs_and_lines decode_line_with_last_displayed (char *, int);
/* Does P represent one of the keywords? If so, return
the keyword. If not, return NULL. */
extern const char *linespec_lexer_lex_keyword (const char *p);
/* Parse a line offset from STRING. */
extern struct line_offset linespec_parse_line_offset (const char *string);
/* Return the quote characters permitted by the linespec parser. */
extern const char *get_gdb_linespec_parser_quote_characters (void);
/* Does STRING represent an Ada operator? If so, return the length
of the decoded operator name. If not, return 0. */
extern int is_ada_operator (const char *string);
/* Find an instance of the character C in the string S that is outside
of all parenthesis pairs, single-quoted strings, and double-quoted
strings. Also, ignore the char within a template name, like a ','
within foo<int, int>. */
extern const char *find_toplevel_char (const char *s, char c);
/* Find the end of the (first) linespec pointed to by *STRINGP.
STRINGP will be advanced to this point. */
extern void linespec_lex_to_end (char **stringp);
extern const char * const linespec_keywords[];
/* Complete a linespec. */
extern void linespec_complete (completion_tracker &tracker,
const char *text);
/* Complete a function symbol, in linespec mode. If SOURCE_FILENAME
is non-NULL, limits completion to the list of functions defined in
source files that match SOURCE_FILENAME. */
extern void linespec_complete_function (completion_tracker &tracker,
const char *function,
const char *source_filename);
/* Complete a label symbol, in linespec mode. Only labels of
functions named FUNCTION_NAME are considered. If SOURCE_FILENAME
is non-NULL, limits completion to labels of functions defined in
source files that match SOURCE_FILENAME. */
extern void linespec_complete_label (completion_tracker &tracker,
const struct language_defn *language,
const char *source_filename,
const char *function_name,
const char *label_name);
/* Evaluate the expression pointed to by EXP_PTR into a CORE_ADDR,
advancing EXP_PTR past any parsed text. */
extern CORE_ADDR linespec_expression_to_pc (const char **exp_ptr);
#endif /* defined (LINESPEC_H) */