Rewrite/enhance explicit locations completer, parse left->right
[deliverable/binutils-gdb.git] / gdb / linespec.c
index 72bcd604ff7dea86737025c751088b257e7be887..20ac71daa4a1a99f5c19669e15c9b1e30533c55f 100644 (file)
@@ -216,9 +216,9 @@ enum ls_token_type
 };
 typedef enum ls_token_type linespec_token_type;
 
-/* List of keywords  */
-
-static const char * const linespec_keywords[] = { "if", "thread", "task" };
+/* List of keywords.  This is NULL-terminated so that it can be used
+   as enum completer.  */
+const char * const linespec_keywords[] = { "if", "thread", "task", NULL };
 #define IF_KEYWORD_INDEX 0
 
 /* A token of the linespec lexer  */
@@ -400,7 +400,7 @@ linespec_lexer_lex_keyword (const char *p)
 
   if (p != NULL)
     {
-      for (i = 0; i < ARRAY_SIZE (linespec_keywords); ++i)
+      for (i = 0; linespec_keywords[i] != NULL; ++i)
        {
          int len = strlen (linespec_keywords[i]);
 
@@ -421,7 +421,7 @@ linespec_lexer_lex_keyword (const char *p)
                {
                  p += len;
                  p = skip_spaces_const (p);
-                 for (j = 0; j < ARRAY_SIZE (linespec_keywords); ++j)
+                 for (j = 0; linespec_keywords[j] != NULL; ++j)
                    {
                      int nextlen = strlen (linespec_keywords[j]);
 
@@ -688,10 +688,9 @@ linespec_lexer_lex_string (linespec_parser *parser)
            {
              if ((PARSER_STATE (parser)->language->la_language
                   == language_cplus)
-                 && (PARSER_STREAM (parser) - start) > 8
-                 /* strlen ("operator") */)
+                 && (PARSER_STREAM (parser) - start) > CP_OPERATOR_LEN)
                {
-                 const char *p = strstr (start, "operator");
+                 const char *p = strstr (start, CP_OPERATOR_STR);
 
                  if (p != NULL && is_operator_name (p))
                    {
@@ -1293,7 +1292,8 @@ decode_line_2 (struct linespec_state *self,
               struct symtabs_and_lines *result,
               const char *select_mode)
 {
-  char *args, *prompt;
+  char *args;
+  const char *prompt;
   int i;
   struct cleanup *old_chain;
   VEC (const_char_ptr) *filters = NULL;
@@ -1714,7 +1714,7 @@ linespec_parse_basic (linespec_parser *parser)
              if (token.type != LSTOKEN_NUMBER)
                unexpected_linespec_error (parser);
 
-             /* Record the lione offset and get the next token.  */
+             /* Record the line offset and get the next token.  */
              name = copy_token_string (token);
              cleanup = make_cleanup (xfree, name);
 
@@ -1749,8 +1749,9 @@ canonicalize_linespec (struct linespec_state *state, const linespec_p ls)
     return;
 
   /* Save everything as an explicit location.  */
-  canon = state->canonical->location
+  state->canonical->location
     = new_explicit_location (&ls->explicit_loc);
+  canon = state->canonical->location.get ();
   explicit_loc = get_explicit_location (canon);
 
   if (explicit_loc->label_name != NULL)
@@ -2450,6 +2451,25 @@ linespec_lex_to_end (char **stringp)
   do_cleanups (cleanup);
 }
 
+/* See linespec.h.  */
+
+void
+linespec_complete_function (completion_tracker &tracker,
+                           const char *function,
+                           const char *source_filename)
+{
+  complete_symbol_mode mode = complete_symbol_mode::LINESPEC;
+
+  if (source_filename != NULL)
+    {
+      collect_file_symbol_completion_matches (tracker, mode,
+                                             function, function,
+                                             source_filename);
+    }
+  else
+    collect_symbol_completion_matches (tracker, mode, function, function);
+}
+
 /* A helper function for decode_line_full and decode_line_1 to
    turn LOCATION into symtabs_and_lines.  */
 
@@ -2552,7 +2572,8 @@ decode_line_full (const struct event_location *location, int flags,
                       search_pspace, default_symtab,
                       default_line, canonical);
   cleanups = make_cleanup (linespec_parser_delete, &parser);
-  save_current_program_space ();
+
+  scoped_restore_current_program_space restore_pspace;
 
   result = event_location_to_sals (&parser, location);
   state = PARSER_STATE (&parser);
@@ -2614,7 +2635,8 @@ decode_line_1 (const struct event_location *location, int flags,
                       search_pspace, default_symtab,
                       default_line, NULL);
   cleanups = make_cleanup (linespec_parser_delete, &parser);
-  save_current_program_space ();
+
+  scoped_restore_current_program_space restore_pspace;
 
   result = event_location_to_sals (&parser, location);
 
@@ -2629,8 +2651,6 @@ decode_line_with_current_source (char *string, int flags)
 {
   struct symtabs_and_lines sals;
   struct symtab_and_line cursal;
-  struct event_location *location;
-  struct cleanup *cleanup;
 
   if (string == 0)
     error (_("Empty line specification."));
@@ -2639,15 +2659,14 @@ decode_line_with_current_source (char *string, int flags)
      and get a default source symtab+line or it will recursively call us!  */
   cursal = get_current_source_symtab_and_line ();
 
-  location = string_to_event_location (&string, current_language);
-  cleanup = make_cleanup_delete_event_location (location);
-  sals = decode_line_1 (location, flags, NULL,
+  event_location_up location = string_to_event_location (&string,
+                                                        current_language);
+  sals = decode_line_1 (location.get (), flags, NULL,
                        cursal.symtab, cursal.line);
 
   if (*string)
     error (_("Junk at end of line specification: %s"), string);
 
-  do_cleanups (cleanup);
   return sals;
 }
 
@@ -2657,25 +2676,23 @@ struct symtabs_and_lines
 decode_line_with_last_displayed (char *string, int flags)
 {
   struct symtabs_and_lines sals;
-  struct event_location *location;
-  struct cleanup *cleanup;
 
   if (string == 0)
     error (_("Empty line specification."));
 
-  location = string_to_event_location (&string, current_language);
-  cleanup = make_cleanup_delete_event_location (location);
+  event_location_up location = string_to_event_location (&string,
+                                                        current_language);
   if (last_displayed_sal_is_valid ())
-    sals = decode_line_1 (location, flags, NULL,
+    sals = decode_line_1 (location.get (), flags, NULL,
                          get_last_displayed_symtab (),
                          get_last_displayed_line ());
   else
-    sals = decode_line_1 (location, flags, NULL, (struct symtab *) NULL, 0);
+    sals = decode_line_1 (location.get (), flags, NULL,
+                         (struct symtab *) NULL, 0);
 
   if (*string)
     error (_("Junk at end of line specification: %s"), string);
 
-  do_cleanups (cleanup);
   return sals;
 }
 
@@ -2793,6 +2810,8 @@ decode_objc (struct linespec_state *self, linespec_p ls, const char *arg)
   return values;
 }
 
+namespace {
+
 /* A function object that serves as symbol_found_callback_ftype
    callback for iterate_over_symbols.  This is used by
    lookup_prefix_sym to collect type symbols.  */
@@ -2859,6 +2878,8 @@ decode_compound_collector::operator () (symbol *sym)
   return true; /* Continue iterating.  */
 }
 
+} // namespace
+
 /* Return any symbols corresponding to CLASS_NAME in FILE_SYMTABS.  */
 
 static VEC (symbolp) *
@@ -3095,6 +3116,8 @@ find_method (struct linespec_state *self, VEC (symtab_ptr) *file_symtabs,
 
 \f
 
+namespace {
+
 /* This function object is a callback for iterate_over_symtabs, used
    when collecting all matching symtabs.  */
 
@@ -3148,6 +3171,8 @@ symtab_collector::operator () (struct symtab *symtab)
   return false;
 }
 
+} // namespace
+
 /* Given a file name, return a VEC of all matching symtabs.  If
    SEARCH_PSPACE is not NULL, the search is restricted to just that
    program space.  */
@@ -3465,9 +3490,7 @@ decode_digits_ordinary (struct linespec_state *self,
 
   for (ix = 0; VEC_iterate (symtab_ptr, ls->file_symtabs, ix, elt); ++ix)
     {
-      int i;
-      VEC (CORE_ADDR) *pcs;
-      CORE_ADDR pc;
+      std::vector<CORE_ADDR> pcs;
 
       /* The logic above should ensure this.  */
       gdb_assert (elt != NULL);
@@ -3475,7 +3498,7 @@ decode_digits_ordinary (struct linespec_state *self,
       set_current_program_space (SYMTAB_PSPACE (elt));
 
       pcs = find_pcs_for_symtab_line (elt, line, best_entry);
-      for (i = 0; VEC_iterate (CORE_ADDR, pcs, i, pc); ++i)
+      for (CORE_ADDR pc : pcs)
        {
          struct symtab_and_line sal;
 
@@ -3486,8 +3509,6 @@ decode_digits_ordinary (struct linespec_state *self,
          sal.pc = pc;
          add_sal_to_sals_basic (sals, &sal);
        }
-
-      VEC_free (CORE_ADDR, pcs);
     }
 }
 
@@ -3887,45 +3908,17 @@ symbol_to_sal (struct symtab_and_line *result,
   return 0;
 }
 
-/* See the comment in linespec.h.  */
-
-void
-init_linespec_result (struct linespec_result *lr)
-{
-  memset (lr, 0, sizeof (*lr));
-}
-
-/* See the comment in linespec.h.  */
-
-void
-destroy_linespec_result (struct linespec_result *ls)
+linespec_result::~linespec_result ()
 {
   int i;
   struct linespec_sals *lsal;
 
-  delete_event_location (ls->location);
-  for (i = 0; VEC_iterate (linespec_sals, ls->sals, i, lsal); ++i)
+  for (i = 0; VEC_iterate (linespec_sals, sals, i, lsal); ++i)
     {
       xfree (lsal->canonical);
       xfree (lsal->sals.sals);
     }
-  VEC_free (linespec_sals, ls->sals);
-}
-
-/* Cleanup function for a linespec_result.  */
-
-static void
-cleanup_linespec_result (void *a)
-{
-  destroy_linespec_result ((struct linespec_result *) a);
-}
-
-/* See the comment in linespec.h.  */
-
-struct cleanup *
-make_cleanup_destroy_linespec_result (struct linespec_result *ls)
-{
-  return make_cleanup (cleanup_linespec_result, ls);
+  VEC_free (linespec_sals, sals);
 }
 
 /* Return the quote characters permitted by the linespec parser.  */
This page took 0.028115 seconds and 4 git commands to generate.