1/* DWARF file and directory
2
3   Copyright (C) 1994-2023 Free Software Foundation, Inc.
4
5   Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6   Inc.  with support from Florida State University (under contract
7   with the Ada Joint Program Office), and Silicon Graphics, Inc.
8   Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9   based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10   support.
11
12   This file is part of GDB.
13
14   This program is free software; you can redistribute it and/or modify
15   it under the terms of the GNU General Public License as published by
16   the Free Software Foundation; either version 3 of the License, or
17   (at your option) any later version.
18
19   This program is distributed in the hope that it will be useful,
20   but WITHOUT ANY WARRANTY; without even the implied warranty of
21   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22   GNU General Public License for more details.
23
24   You should have received a copy of the GNU General Public License
25   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
26
27#ifndef GDB_DWARF2_FILE_AND_DIR_H
28#define GDB_DWARF2_FILE_AND_DIR_H
29
30#include "objfiles.h"
31#include "source.h"
32#include <string>
33
34/* The return type of find_file_and_directory.  Note, the enclosed
35   string pointers are only valid while this object is valid.  */
36
37struct file_and_directory
38{
39  file_and_directory (const char *name, const char *dir)
40    : m_name (name),
41      m_comp_dir (dir)
42  {
43  }
44
45  /* Return true if the file name is unknown.  */
46  bool is_unknown () const
47  {
48    return m_name == nullptr;
49  }
50
51  /* Set the compilation directory.  */
52  void set_comp_dir (std::string &&dir)
53  {
54    m_comp_dir_storage = std::move (dir);
55    m_comp_dir = nullptr;
56  }
57
58  /* Fetch the compilation directory.  This may return NULL in some
59     circumstances.  Note that the return value here is not stable --
60     it may change if this object is moved.  To get a stable pointer,
61     you should call intern_comp_dir.  */
62  const char *get_comp_dir () const
63  {
64    if (!m_comp_dir_storage.empty ())
65      return m_comp_dir_storage.c_str ();
66    return m_comp_dir;
67  }
68
69  /* If necessary, intern the compilation directory using OBJFILE's
70     string cache.  Returns the compilation directory.  */
71  const char *intern_comp_dir (struct objfile *objfile)
72  {
73    if (!m_comp_dir_storage.empty ())
74      {
75	m_comp_dir = objfile->intern (m_comp_dir_storage);
76	m_comp_dir_storage.clear ();
77      }
78    return m_comp_dir;
79  }
80
81  /* Fetch the filename.  This never returns NULL.  */
82  const char *get_name () const
83  {
84    return m_name == nullptr ? "<unknown>" : m_name;
85  }
86
87  /* Set the filename.  */
88  void set_name (gdb::unique_xmalloc_ptr<char> name)
89  {
90    m_name_storage = std::move (name);
91    m_name = m_name_storage.get ();
92  }
93
94  /* Return the full name, computing it if necessary.  */
95  const char *get_fullname ()
96  {
97    if (m_fullname == nullptr)
98      m_fullname = find_source_or_rewrite (get_name (), get_comp_dir ());
99    return m_fullname.get ();
100  }
101
102  /* Forget the full name.  */
103  void forget_fullname ()
104  {
105    m_fullname.reset ();
106  }
107
108private:
109
110  /* The filename.  */
111  const char *m_name;
112
113  /* Storage for the filename, if needed.  */
114  gdb::unique_xmalloc_ptr<char> m_name_storage;
115
116  /* The compilation directory.  NULL if not known.  If we needed to
117     compute a new string, it will be stored in the comp_dir_storage
118     member, and this will be NULL.  Otherwise, points directly to the
119     DW_AT_comp_dir string attribute.  */
120  const char *m_comp_dir;
121
122  /* The compilation directory, if it needed to be allocated.  */
123  std::string m_comp_dir_storage;
124
125  /* The full name.  */
126  gdb::unique_xmalloc_ptr<char> m_fullname;
127};
128
129#endif /* GDB_DWARF2_FILE_AND_DIR_H */
130