This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] gdb: set filename-display shortpath support


Display only non-common part of filename and compilation directory

This will be useful for projects that use separate build directory
inside project (like build/cmake and others).
---
V2: fixed coding style issues (thanks to Joel)
V3: fixed typo in initial condition for symtab_to_shortpath

 gdb/source.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++---
 gdb/source.h |  3 +++
 2 files changed, 52 insertions(+), 3 deletions(-)

diff --git a/gdb/source.c b/gdb/source.c
index 6234bfc..5b03e29 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -111,11 +111,13 @@ show_lines_to_list (struct ui_file *file, int from_tty,
 static const char filename_display_basename[] = "basename";
 static const char filename_display_relative[] = "relative";
 static const char filename_display_absolute[] = "absolute";
+static const char filename_display_shortpath[] = "shortpath";
 
 static const char *const filename_display_kind_names[] = {
   filename_display_basename,
   filename_display_relative,
   filename_display_absolute,
+  filename_display_shortpath,
   NULL
 };
 
@@ -1137,6 +1139,46 @@ symtab_to_fullname (struct symtab *s)
 /* See commentary in source.h.  */
 
 const char *
+symtab_to_shortpath (struct symtab *symtab)
+{
+  char *prev_slash_name = (char *) symtab->filename;
+  char *prev_slash_dir = (char *) symtab->dirname;
+  char *slash_name = (char *) symtab->filename;
+  char *slash_dir = (char *) symtab->dirname;
+  const char *shortpath = slash_name;
+
+  if (slash_dir != NULL)
+    return shortpath;
+
+  for (;;)
+    {
+      size_t min_part_len;
+
+      slash_name = strstr (slash_name, SLASH_STRING);
+      slash_dir = strstr (slash_dir, SLASH_STRING);
+      if (slash_name == NULL || slash_dir == NULL)
+        break;
+
+      slash_name++;
+      slash_dir++;
+
+      min_part_len = min (slash_name - prev_slash_name,
+                          slash_dir - prev_slash_dir);
+      if (strncmp (slash_name, slash_dir, min_part_len))
+        break;
+
+      shortpath = slash_name;
+
+      prev_slash_name = slash_name;
+      prev_slash_dir = slash_dir;
+    }
+
+  return shortpath;
+}
+
+/* See commentary in source.h.  */
+
+const char *
 symtab_to_filename_for_display (struct symtab *symtab)
 {
   if (filename_display_string == filename_display_basename)
@@ -1145,6 +1187,8 @@ symtab_to_filename_for_display (struct symtab *symtab)
     return symtab_to_fullname (symtab);
   else if (filename_display_string == filename_display_relative)
     return symtab->filename;
+  else if (filename_display_string == filename_display_shortpath)
+    return symtab_to_shortpath (symtab);
   else
     internal_error (__FILE__, __LINE__, _("invalid filename_display_string"));
 }
@@ -2093,9 +2137,11 @@ is not specified, print all substitution rules."),
 Set how to display filenames."), _("\
 Show how to display filenames."), _("\
 filename-display can be:\n\
-  basename - display only basename of a filename\n\
-  relative - display a filename relative to the compilation directory\n\
-  absolute - display an absolute filename\n\
+  basename  - display only basename of a filename\n\
+  relative  - display a filename relative to the compilation directory\n\
+  absolute  - display an absolute filename\n\
+  shortpath - display only non-common part of filename and compilation \
+directory\n\
 By default, relative filenames are displayed."),
 			NULL,
 			show_filename_display_string,
diff --git a/gdb/source.h b/gdb/source.h
index 33cad09..79e3565 100644
--- a/gdb/source.h
+++ b/gdb/source.h
@@ -52,6 +52,9 @@ extern char *rewrite_source_path (const char *path);
 
 extern const char *symtab_to_fullname (struct symtab *s);
 
+/* Returns only non-common part of filename and compilation directory. */
+extern const char *symtab_to_shortpath(struct symtab *symtab);
+
 /* Returns filename without the compile directory part, basename or absolute
    filename.  It depends on 'set filename-display' value.  */
 extern const char *symtab_to_filename_for_display (struct symtab *symtab);
-- 
1.8.4.3


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]