1 /* Find a named variable or parameter within given scopes.
2 Copyright (C) 2005-2009 Red Hat, Inc.
3 This file is part of elfutils.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
8 * the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 3 of the License, or (at
10 your option) any later version
14 * the GNU General Public License as published by the Free
15 Software Foundation; either version 2 of the License, or (at
16 your option) any later version
18 or both in parallel, as here.
20 elfutils is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received copies of the GNU General Public License and
26 the GNU Lesser General Public License along with this program. If
27 not, see <http://www.gnu.org/licenses/>. */
39 /* Find the containing CU's files. */
41 getfiles (Dwarf_Die *die, Dwarf_Files **files)
43 return INTUSE(dwarf_getsrcfiles) (&CUDIE (die->cu), files, NULL);
46 /* Fetch an attribute that should have a constant integer form. */
48 getattr (Dwarf_Die *die, int search_name, Dwarf_Word *value)
50 Dwarf_Attribute attr_mem;
51 return INTUSE(dwarf_formudata) (INTUSE(dwarf_attr) (die, search_name,
55 /* Search SCOPES[0..NSCOPES-1] for a variable called NAME.
56 Ignore the first SKIP_SHADOWS scopes that match the name.
57 If MATCH_FILE is not null, accept only declaration in that source file;
58 if MATCH_LINENO or MATCH_LINECOL are also nonzero, accept only declaration
59 at that line and column.
61 If successful, fill in *RESULT with the DIE of the variable found,
62 and return N where SCOPES[N] is the scope defining the variable.
63 Return -1 for errors or -2 for no matching variable found. */
66 dwarf_getscopevar (Dwarf_Die *scopes, int nscopes,
67 const char *name, int skip_shadows,
68 const char *match_file, int match_lineno, int match_linecol,
71 /* Match against the given file name. */
72 size_t match_file_len = match_file == NULL ? 0 : strlen (match_file);
73 bool lastfile_matches = false;
74 const char *lastfile = NULL;
75 inline bool file_matches (Dwarf_Files *files, size_t idx)
77 if (idx >= files->nfiles)
80 const char *file = files->info[idx].name;
83 size_t len = strlen (file);
84 lastfile_matches = (len >= match_file_len
85 && !memcmp (match_file, file, match_file_len)
86 && (len == match_file_len
87 || file[len - match_file_len - 1] == '/'));
89 return lastfile_matches;
92 /* Start with the innermost scope and move out. */
93 for (int out = 0; out < nscopes; ++out)
94 if (INTUSE(dwarf_haschildren) (&scopes[out]))
96 if (INTUSE(dwarf_child) (&scopes[out], result) != 0)
100 switch (INTUSE(dwarf_tag) (result))
102 case DW_TAG_variable:
103 case DW_TAG_formal_parameter:
110 /* Only get here for a variable or parameter. Check the name. */
111 const char *diename = INTUSE(dwarf_diename) (result);
112 if (diename != NULL && !strcmp (name, diename))
114 /* We have a matching name. */
116 if (skip_shadows > 0)
118 /* Punt this scope for the one it shadows. */
123 if (match_file != NULL)
125 /* Check its decl_file. */
129 if (getattr (result, DW_AT_decl_file, &i) != 0
130 || getfiles (&scopes[out], &files) != 0)
133 if (!file_matches (files, i))
137 && (getattr (result, DW_AT_decl_line, &i) != 0
138 || (int) i != match_lineno))
140 if (match_linecol > 0
141 && (getattr (result, DW_AT_decl_column, &i) != 0
142 || (int) i != match_linecol))
146 /* We have a winner! */
150 while (INTUSE(dwarf_siblingof) (result, result) == 0);