gdb: add target_ops::supports_displaced_step
[deliverable/binutils-gdb.git] / gdb / source-cache.c
index 9039f8fde2a75996d3d9b6642929132516cf1ee3..9196e3a19e3f60fcdab1225bd306e6a594f7bf7e 100644 (file)
@@ -1,5 +1,5 @@
 /* Cache of styled source file text
-   Copyright (C) 2018-2019 Free Software Foundation, Inc.
+   Copyright (C) 2018-2020 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -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.027208 seconds and 4 git commands to generate.