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,
56 file_matches (const char *lastfile,
57 size_t match_file_len, const char *match_file,
58 Dwarf_Files *files, size_t idx,
59 bool *lastfile_matches)
61 if (idx >= files->nfiles)
63 const char *file = files->info[idx].name;
66 size_t len = strlen (file);
67 *lastfile_matches = (len >= match_file_len
68 && !memcmp (match_file, file, match_file_len)
69 && (len == match_file_len
70 || file[len - match_file_len - 1] == '/'));
72 return *lastfile_matches;
75 /* Search SCOPES[0..NSCOPES-1] for a variable called NAME.
76 Ignore the first SKIP_SHADOWS scopes that match the name.
77 If MATCH_FILE is not null, accept only declaration in that source file;
78 if MATCH_LINENO or MATCH_LINECOL are also nonzero, accept only declaration
79 at that line and column.
81 If successful, fill in *RESULT with the DIE of the variable found,
82 and return N where SCOPES[N] is the scope defining the variable.
83 Return -1 for errors or -2 for no matching variable found. */
86 dwarf_getscopevar (Dwarf_Die *scopes, int nscopes,
87 const char *name, int skip_shadows,
88 const char *match_file, int match_lineno, int match_linecol,
91 /* Match against the given file name. */
92 size_t match_file_len = match_file == NULL ? 0 : strlen (match_file);
93 bool lastfile_matches = false;
94 const char *lastfile = NULL;
96 /* Start with the innermost scope and move out. */
97 for (int out = 0; out < nscopes; ++out)
98 if (INTUSE(dwarf_haschildren) (&scopes[out]))
100 if (INTUSE(dwarf_child) (&scopes[out], result) != 0)
104 switch (INTUSE(dwarf_tag) (result))
106 case DW_TAG_variable:
107 case DW_TAG_formal_parameter:
114 /* Only get here for a variable or parameter. Check the name. */
115 const char *diename = INTUSE(dwarf_diename) (result);
116 if (diename != NULL && !strcmp (name, diename))
118 /* We have a matching name. */
120 if (skip_shadows > 0)
122 /* Punt this scope for the one it shadows. */
127 if (match_file != NULL)
129 /* Check its decl_file. */
133 if (getattr (result, DW_AT_decl_file, &i) != 0
134 || getfiles (&scopes[out], &files) != 0)
137 if (!file_matches (lastfile, match_file_len, match_file,
138 files, i, &lastfile_matches))
142 && (getattr (result, DW_AT_decl_line, &i) != 0
143 || (int) i != match_lineno))
145 if (match_linecol > 0
146 && (getattr (result, DW_AT_decl_column, &i) != 0
147 || (int) i != match_linecol))
151 /* We have a winner! */
155 while (INTUSE(dwarf_siblingof) (result, result) == 0);