Highlight source code using GNU Source Highlight
[external/binutils.git] / gdb / source-cache.c
1 /* Cache of styled source file text
2    Copyright (C) 2018 Free Software Foundation, Inc.
3
4    This file is part of GDB.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 #include "defs.h"
20 #include "source-cache.h"
21 #include "common/scoped_fd.h"
22 #include "source.h"
23 #include "cli/cli-style.h"
24
25 #ifdef HAVE_SOURCE_HIGHLIGHT
26 #include <fstream>
27 #include <sstream>
28 #include <srchilite/sourcehighlight.h>
29 #include <srchilite/langmap.h>
30 #endif
31
32 /* The number of source files we'll cache.  */
33
34 #define MAX_ENTRIES 5
35
36 /* See source-cache.h.  */
37
38 source_cache g_source_cache;
39
40 /* See source-cache.h.  */
41
42 bool
43 source_cache::get_plain_source_lines (struct symtab *s, int first_line,
44                                       int last_line, std::string *lines)
45 {
46   scoped_fd desc (open_source_file (s));
47   if (desc.get () < 0)
48     return false;
49
50   if (s->line_charpos == 0)
51     find_source_lines (s, desc.get ());
52
53   if (first_line < 1 || first_line > s->nlines || last_line < 1)
54     return false;
55
56   if (lseek (desc.get (), s->line_charpos[first_line - 1], SEEK_SET) < 0)
57     perror_with_name (symtab_to_filename_for_display (s));
58
59   int last_charpos;
60   if (last_line >= s->nlines)
61     {
62       struct stat st;
63
64       if (fstat (desc.get (), &st) < 0)
65         perror_with_name (symtab_to_filename_for_display (s));
66       /* We could cache this in line_charpos... */
67       last_charpos = st.st_size;
68     }
69   else
70     last_charpos = s->line_charpos[last_line];
71
72   lines->resize (last_charpos - s->line_charpos[first_line - 1]);
73   if (myread (desc.get (), &(*lines)[0], lines->size ()) < 0)
74     perror_with_name (symtab_to_filename_for_display (s));
75
76   return true;
77 }
78
79 /* See source-cache.h.  */
80
81 bool
82 source_cache::extract_lines (const struct source_text &text, int first_line,
83                              int last_line, std::string *lines)
84 {
85   int lineno = 1;
86   std::string::size_type pos = 0;
87   std::string::size_type first_pos = std::string::npos;
88
89   while (pos != std::string::npos && lineno <= last_line)
90     {
91       std::string::size_type new_pos = text.contents.find ('\n', pos);
92
93       if (lineno == first_line)
94         first_pos = pos;
95
96       pos = new_pos;
97       if (lineno == last_line || pos == std::string::npos)
98         {
99           if (pos == std::string::npos)
100             pos = text.contents.size ();
101           *lines = text.contents.substr (first_pos, pos - first_pos);
102           return true;
103         }
104       ++lineno;
105       ++pos;
106     }
107
108   return false;
109 }
110
111 /* Return the Source Highlight language name, given a gdb language
112    LANG.  Returns NULL if the language is not known.  */
113
114 static const char *
115 get_language_name (enum language lang)
116 {
117   switch (lang)
118     {
119     case language_c:
120     case language_objc:
121       return "c.lang";
122
123     case language_cplus:
124       return "cpp.lang";
125
126     case language_d:
127       return "d.lang";
128
129     case language_go:
130       return "go.lang";
131
132     case language_fortran:
133       return "fortran.lang";
134
135     case language_m2:
136       /* Not handled by Source Highlight.  */
137       break;
138
139     case language_asm:
140       return "asm.lang";
141
142     case language_pascal:
143       return "pascal.lang";
144
145     case language_opencl:
146       /* Not handled by Source Highlight.  */
147       break;
148
149     case language_rust:
150       /* Not handled by Source Highlight.  */
151       break;
152
153     case language_ada:
154       return "ada.lang";
155
156     default:
157       break;
158     }
159
160   return nullptr;
161 }
162
163 /* See source-cache.h.  */
164
165 bool
166 source_cache::get_source_lines (struct symtab *s, int first_line,
167                                 int last_line, std::string *lines)
168 {
169   if (first_line < 1 || last_line < 1 || first_line > last_line)
170     return false;
171
172 #ifdef HAVE_SOURCE_HIGHLIGHT
173   if (can_emit_style_escape (gdb_stdout))
174     {
175       const char *fullname = symtab_to_fullname (s);
176
177       for (const auto &item : m_source_map)
178         {
179           if (item.fullname == fullname)
180             return extract_lines (item, first_line, last_line, lines);
181         }
182
183       const char *lang_name = get_language_name (SYMTAB_LANGUAGE (s));
184       if (lang_name != nullptr)
185         {
186           std::ifstream input (fullname);
187           if (input.is_open ())
188             {
189               srchilite::SourceHighlight highlighter ("esc.outlang");
190               highlighter.setStyleFile("esc.style");
191
192               std::ostringstream output;
193               highlighter.highlight (input, output, lang_name, fullname);
194
195               source_text result = { fullname, output.str () };
196               m_source_map.push_back (std::move (result));
197
198               if (m_source_map.size () > MAX_ENTRIES)
199                 m_source_map.erase (m_source_map.begin ());
200
201               return extract_lines (m_source_map.back (), first_line,
202                                     last_line, lines);
203             }
204         }
205     }
206 #endif /* HAVE_SOURCE_HIGHLIGHT */
207
208   return get_plain_source_lines (s, first_line, last_line, lines);
209 }