Allow DW_ATE_UTF for Rust characters
[deliverable/binutils-gdb.git] / gdb / source-cache.c
index 9039f8fde2a75996d3d9b6642929132516cf1ee3..593681a5c0fe40908ffa166884a9f5cf53fc387a 100644 (file)
@@ -1,5 +1,5 @@
 /* Cache of styled source file text
-   Copyright (C) 2018-2019 Free Software Foundation, Inc.
+   Copyright (C) 2018-2021 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -68,8 +68,8 @@ source_cache::get_plain_source_lines (struct symtab *s,
   time_t mtime = 0;
   if (SYMTAB_OBJFILE (s) != NULL && SYMTAB_OBJFILE (s)->obfd != NULL)
     mtime = SYMTAB_OBJFILE (s)->mtime;
-  else if (exec_bfd)
-    mtime = exec_bfd_mtime;
+  else if (current_program_space->exec_bfd ())
+    mtime = current_program_space->ebfd_mtime;
 
   if (mtime && mtime < st.st_mtime)
     warning (_("Source file is more recent than executable."));
@@ -135,8 +135,7 @@ get_language_name (enum language lang)
       break;
 
     case language_rust:
-      /* Not handled by Source Highlight.  */
-      break;
+      return "rust.lang";
 
     case language_ada:
       return "ada.lang";
@@ -177,11 +176,21 @@ source_cache::ensure (struct symtab *s)
        }
     }
 
-  std::string contents = get_plain_source_lines (s, fullname);
+  std::string contents;
+  try
+    {
+      contents = get_plain_source_lines (s, fullname);
+    }
+  catch (const gdb_exception_error &e)
+    {
+      /* If 's' is not found, an exception is thrown.  */
+      return false;
+    }
 
-#ifdef HAVE_SOURCE_HIGHLIGHT
   if (source_styling && gdb_stdout->can_emit_style_escape ())
     {
+#ifdef HAVE_SOURCE_HIGHLIGHT
+      bool already_styled = false;
       const char *lang_name = get_language_name (SYMTAB_LANGUAGE (s));
       if (lang_name != nullptr)
        {
@@ -191,20 +200,40 @@ source_cache::ensure (struct symtab *s)
             conditional compilation in source-cache.h.  */
          static srchilite::SourceHighlight *highlighter;
 
-         if (highlighter == nullptr)
+         try
            {
-             highlighter = new srchilite::SourceHighlight ("esc.outlang");
-             highlighter->setStyleFile ("esc.style");
+             if (highlighter == nullptr)
+               {
+                 highlighter = new srchilite::SourceHighlight ("esc.outlang");
+                 highlighter->setStyleFile ("esc.style");
+               }
+
+             std::istringstream input (contents);
+             std::ostringstream output;
+             highlighter->highlight (input, output, lang_name, fullname);
+             contents = output.str ();
+             already_styled = true;
            }
+         catch (...)
+           {
+             /* Source Highlight will throw an exception if
+                highlighting fails.  One possible reason it can fail
+                is if the language is unknown -- which matters to gdb
+                because Rust support wasn't added until after 3.1.8.
+                Ignore exceptions here and fall back to
+                un-highlighted text. */
+           }
+       }
 
-         std::istringstream input (contents);
-         std::ostringstream output;
-         highlighter->highlight (input, output, lang_name, fullname);
-
-         contents = output.str ();
+      if (!already_styled)
+#endif /* HAVE_SOURCE_HIGHLIGHT */
+       {
+         gdb::optional<std::string> ext_contents;
+         ext_contents = ext_lang_colorize (fullname, contents);
+         if (ext_contents.has_value ())
+           contents = std::move (*ext_contents);
        }
     }
-#endif /* HAVE_SOURCE_HIGHLIGHT */
 
   source_text result = { std::move (fullname), std::move (contents) };
   m_source_map.push_back (std::move (result));
@@ -226,7 +255,8 @@ source_cache::get_line_charpos (struct symtab *s,
   auto iter = m_offset_cache.find (fullname);
   if (iter == m_offset_cache.end ())
     {
-      ensure (s);
+      if (!ensure (s))
+       return false;
       iter = m_offset_cache.find (fullname);
       /* cache_source_text ensured this was entered.  */
       gdb_assert (iter != m_offset_cache.end ());
@@ -312,6 +342,7 @@ static void extract_lines_test ()
 }
 #endif
 
+void _initialize_source_cache ();
 void
 _initialize_source_cache ()
 {
This page took 0.024764 seconds and 4 git commands to generate.