VirtualBox

source: kBuild/trunk/src/kmk/variable.h@ 2770

Last change on this file since 2770 was 2770, checked in by bird, 10 years ago

string expansion debugged and enabled. fixed access-after-alloc bug in func_sort and could lead to heap corruption.

  • Property svn:eol-style set to native
File size: 21.2 KB
Line 
1/* Definitions for using variables in GNU Make.
2Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
4Foundation, Inc.
5This file is part of GNU Make.
6
7GNU Make is free software; you can redistribute it and/or modify it under the
8terms of the GNU General Public License as published by the Free Software
9Foundation; either version 3 of the License, or (at your option) any later
10version.
11
12GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License along with
17this program. If not, see <http://www.gnu.org/licenses/>. */
18
19#include "hash.h"
20
21/* Codes in a variable definition saying where the definition came from.
22 Increasing numeric values signify less-overridable definitions. */
23enum variable_origin
24 {
25 o_default, /* Variable from the default set. */
26 o_env, /* Variable from environment. */
27 o_file, /* Variable given in a makefile. */
28 o_env_override, /* Variable from environment, if -e. */
29 o_command, /* Variable given by user. */
30 o_override, /* Variable from an `override' directive. */
31#ifdef CONFIG_WITH_LOCAL_VARIABLES
32 o_local, /* Variable from an 'local' directive. */
33#endif
34 o_automatic, /* Automatic variable -- cannot be set. */
35 o_invalid /* Core dump time. */
36 };
37
38enum variable_flavor
39 {
40 f_bogus, /* Bogus (error) */
41 f_simple, /* Simple definition (:=) */
42 f_recursive, /* Recursive definition (=) */
43 f_append, /* Appending definition (+=) */
44#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
45 f_prepend, /* Prepending definition (>=) */
46#endif
47 f_conditional /* Conditional definition (?=) */
48 };
49
50/* Structure that represents one variable definition.
51 Each bucket of the hash table is a chain of these,
52 chained through `next'. */
53
54#define EXP_COUNT_BITS 15 /* This gets all the bitfields into 32 bits */
55#define EXP_COUNT_MAX ((1<<EXP_COUNT_BITS)-1)
56#ifdef CONFIG_WITH_VALUE_LENGTH
57#define VAR_ALIGN_VALUE_ALLOC(len) ( ((len) + (unsigned int)15) & ~(unsigned int)15 )
58#endif
59
60struct variable
61 {
62#ifndef CONFIG_WITH_STRCACHE2
63 char *name; /* Variable name. */
64#else
65 const char *name; /* Variable name (in varaible_strcache). */
66#endif
67 int length; /* strlen (name) */
68#ifdef CONFIG_WITH_VALUE_LENGTH
69 unsigned int value_length; /* The length of the value. */
70 unsigned int value_alloc_len; /* The amount of memory we've actually allocated. */
71 /* FIXME: make lengths unsigned! */
72#endif
73 char *value; /* Variable value. */
74 struct floc fileinfo; /* Where the variable was defined. */
75 unsigned int recursive:1; /* Gets recursively re-evaluated. */
76 unsigned int append:1; /* Nonzero if an appending target-specific
77 variable. */
78 unsigned int conditional:1; /* Nonzero if set with a ?=. */
79 unsigned int per_target:1; /* Nonzero if a target-specific variable. */
80 unsigned int special:1; /* Nonzero if this is a special variable. */
81 unsigned int exportable:1; /* Nonzero if the variable _could_ be
82 exported. */
83 unsigned int expanding:1; /* Nonzero if currently being expanded. */
84 unsigned int private_var:1; /* Nonzero avoids inheritance of this
85 target-specific variable. */
86 unsigned int exp_count:EXP_COUNT_BITS;
87 /* If >1, allow this many self-referential
88 expansions. */
89#ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
90 unsigned int rdonly_val:1; /* VALUE is read only (strcache/const). */
91#endif
92#ifdef KMK
93 unsigned int alias:1; /* Nonzero if alias. VALUE points to the real variable. */
94 unsigned int aliased:1; /* Nonzero if aliased. Cannot be undefined. */
95#endif
96 enum variable_flavor
97 flavor ENUM_BITFIELD (3); /* Variable flavor. */
98 enum variable_origin
99#ifdef CONFIG_WITH_LOCAL_VARIABLES
100 origin ENUM_BITFIELD (4); /* Variable origin. */
101#else
102 origin ENUM_BITFIELD (3); /* Variable origin. */
103#endif
104 enum variable_export
105 {
106 v_export, /* Export this variable. */
107 v_noexport, /* Don't export this variable. */
108 v_ifset, /* Export it if it has a non-default value. */
109 v_default /* Decide in target_environment. */
110 } export ENUM_BITFIELD (2);
111#ifdef CONFIG_WITH_MAKE_STATS
112 unsigned int changes; /* Variable modification count. */
113 unsigned int reallocs; /* Realloc on value count. */
114 unsigned int references; /* Lookup count. */
115 unsigned long long cTicksEvalVal; /* Number of ticks spend in cEvalVal. */
116#endif
117#if defined (CONFIG_WITH_COMPILER) || defined (CONFIG_WITH_MAKE_STATS)
118 unsigned int evalval_count; /* Times used with $(evalval ) or $(evalctx ) since last change. */
119 unsigned int expand_count; /* Times expanded since last change (not to be confused with exp_count). */
120#endif
121#ifdef CONFIG_WITH_COMPILER
122 struct kmk_cc_evalprog *evalprog; /* Pointer to evalval/evalctx "program". */
123 struct kmk_cc_expandprog *expandprog; /* Pointer to variable expand "program". */
124#endif
125 };
126
127/* Update statistics and invalidates optimizations when a variable changes. */
128#ifdef CONFIG_WITH_COMPILER
129# define VARIABLE_CHANGED(v) \
130 do { \
131 MAKE_STATS_2((v)->changes++); \
132 if ((v)->evalprog || (v)->expandprog) kmk_cc_variable_changed(v); \
133 (v)->expand_count = 0; \
134 (v)->evalval_count = 0; \
135 } while (0)
136#else
137# define VARIABLE_CHANGED(v) MAKE_STATS_2((v)->changes++)
138#endif
139
140
141
142/* Structure that represents a variable set. */
143
144struct variable_set
145 {
146 struct hash_table table; /* Hash table of variables. */
147 };
148
149/* Structure that represents a list of variable sets. */
150
151struct variable_set_list
152 {
153 struct variable_set_list *next; /* Link in the chain. */
154 struct variable_set *set; /* Variable set. */
155 int next_is_parent; /* True if next is a parent target. */
156 };
157
158/* Structure used for pattern-specific variables. */
159
160struct pattern_var
161 {
162 struct pattern_var *next;
163 const char *suffix;
164 const char *target;
165 unsigned int len;
166 struct variable variable;
167 };
168
169extern char *variable_buffer;
170extern struct variable_set_list *current_variable_set_list;
171extern struct variable *default_goal_var;
172
173#ifdef KMK
174extern struct variable_set global_variable_set;
175extern struct variable_set_list global_setlist;
176extern unsigned int variable_buffer_length;
177# define VARIABLE_BUFFER_ZONE 5
178#endif
179
180/* expand.c */
181#ifndef KMK
182char *
183variable_buffer_output (char *ptr, const char *string, unsigned int length);
184#else /* KMK */
185/* Subroutine of variable_expand and friends:
186 The text to add is LENGTH chars starting at STRING to the variable_buffer.
187 The text is added to the buffer at PTR, and the updated pointer into
188 the buffer is returned as the value. Thus, the value returned by
189 each call to variable_buffer_output should be the first argument to
190 the following call. */
191
192__inline static char *
193variable_buffer_output (char *ptr, const char *string, unsigned int length)
194{
195 register unsigned int newlen = length + (ptr - variable_buffer);
196
197 if ((newlen + VARIABLE_BUFFER_ZONE) > variable_buffer_length)
198 {
199 unsigned int offset = ptr - variable_buffer;
200 variable_buffer_length = variable_buffer_length <= 1024
201 ? 2048 : variable_buffer_length * 4;
202 if (variable_buffer_length < newlen + 100)
203 variable_buffer_length = (newlen + 100 + 1023) & ~1023U;
204 variable_buffer = xrealloc (variable_buffer, variable_buffer_length);
205 ptr = variable_buffer + offset;
206 }
207
208# ifndef _MSC_VER
209 switch (length)
210 {
211 case 4: ptr[3] = string[3];
212 case 3: ptr[2] = string[2];
213 case 2: ptr[1] = string[1];
214 case 1: ptr[0] = string[0];
215 case 0:
216 break;
217 default:
218 memcpy (ptr, string, length);
219 break;
220 }
221# else
222 memcpy (ptr, string, length);
223# endif
224 return ptr + length;
225}
226
227#endif /* KMK */
228char *variable_expand (const char *line);
229char *variable_expand_for_file (const char *line, struct file *file);
230#if defined (CONFIG_WITH_VALUE_LENGTH) || defined (CONFIG_WITH_COMMANDS_FUNC)
231char *variable_expand_for_file_2 (char *o, const char *line, unsigned int lenght,
232 struct file *file, unsigned int *value_lenp);
233#endif
234char *allocated_variable_expand_for_file (const char *line, struct file *file);
235#ifndef CONFIG_WITH_VALUE_LENGTH
236#define allocated_variable_expand(line) \
237 allocated_variable_expand_for_file (line, (struct file *) 0)
238#else /* CONFIG_WITH_VALUE_LENGTH */
239# define allocated_variable_expand(line) \
240 allocated_variable_expand_2 (line, -1, NULL)
241char *allocated_variable_expand_2 (const char *line, unsigned int length, unsigned int *value_lenp);
242char *allocated_variable_expand_3 (const char *line, unsigned int length,
243 unsigned int *value_lenp, unsigned int *buffer_lengthp);
244void recycle_variable_buffer (char *buffer, unsigned int length);
245#endif /* CONFIG_WITH_VALUE_LENGTH */
246char *expand_argument (const char *str, const char *end);
247#ifndef CONFIG_WITH_VALUE_LENGTH
248char *
249variable_expand_string (char *line, const char *string, long length);
250#else /* CONFIG_WITH_VALUE_LENGTH */
251char *
252variable_expand_string_2 (char *line, const char *string, long length, char **eol);
253__inline static char *
254variable_expand_string (char *line, const char *string, long length)
255{
256 char *ignored;
257 return variable_expand_string_2 (line, string, length, &ignored);
258}
259#endif /* CONFIG_WITH_VALUE_LENGTH */
260void install_variable_buffer (char **bufp, unsigned int *lenp);
261char *install_variable_buffer_with_hint (char **bufp, unsigned int *lenp, unsigned int size_hint);
262void restore_variable_buffer (char *buf, unsigned int len);
263char *ensure_variable_buffer_space(char *ptr, unsigned int size);
264#ifdef CONFIG_WITH_VALUE_LENGTH
265void append_expanded_string_to_variable (struct variable *v, const char *value,
266 unsigned int value_len, int append);
267#endif
268
269/* function.c */
270#ifndef CONFIG_WITH_VALUE_LENGTH
271int handle_function (char **op, const char **stringp);
272#else
273int handle_function (char **op, const char **stringp, const char *nameend, const char *eol);
274#endif
275#ifdef CONFIG_WITH_COMPILER
276typedef char *(*make_function_ptr_t) (char *, char **, const char *);
277make_function_ptr_t lookup_function_for_compiler (const char *name, unsigned int len,
278 unsigned char *minargsp, unsigned char *maxargsp,
279 char *expargsp, const char **funcnamep);
280#endif
281int pattern_matches (const char *pattern, const char *percent, const char *str);
282char *subst_expand (char *o, const char *text, const char *subst,
283 const char *replace, unsigned int slen, unsigned int rlen,
284 int by_word);
285char *patsubst_expand_pat (char *o, const char *text, const char *pattern,
286 const char *replace, const char *pattern_percent,
287 const char *replace_percent);
288char *patsubst_expand (char *o, const char *text, char *pattern, char *replace);
289#ifdef CONFIG_WITH_COMMANDS_FUNC
290char *func_commands (char *o, char **argv, const char *funcname);
291#endif
292#if defined (CONFIG_WITH_VALUE_LENGTH)
293/* Avoid calling handle_function for every variable, do the
294 basic checks in variable_expand_string_2. */
295extern char func_char_map[256];
296# define MAX_FUNCTION_LENGTH 12
297# define MIN_FUNCTION_LENGTH 2
298MY_INLINE const char *
299may_be_function_name (const char *name, const char *eos)
300{
301 unsigned char ch;
302 unsigned int len = name - eos;
303
304 /* Minimum length is MIN + whitespace. Check this directly.
305 ASSUMES: MIN_FUNCTION_LENGTH == 2 */
306
307 if (MY_PREDICT_TRUE(len < MIN_FUNCTION_LENGTH + 1
308 || !func_char_map[(int)(name[0])]
309 || !func_char_map[(int)(name[1])]))
310 return 0;
311 if (MY_PREDICT_TRUE(!func_char_map[ch = name[2]]))
312 return isspace (ch) ? name + 2 : 0;
313
314 name += 3;
315 if (len > MAX_FUNCTION_LENGTH)
316 len = MAX_FUNCTION_LENGTH - 3;
317 else if (len == 3)
318 len -= 3;
319 if (!len)
320 return 0;
321
322 /* Loop over the remaining possiblities. */
323
324 while (func_char_map[ch = *name])
325 {
326 if (!len--)
327 return 0;
328 name++;
329 }
330 if (ch == '\0' || isblank (ch))
331 return name;
332 return 0;
333}
334#endif /* CONFIG_WITH_VALUE_LENGTH */
335
336/* expand.c */
337#ifndef CONFIG_WITH_VALUE_LENGTH
338char *recursively_expand_for_file (struct variable *v, struct file *file);
339#define recursively_expand(v) recursively_expand_for_file (v, NULL)
340#else
341char *recursively_expand_for_file (struct variable *v, struct file *file,
342 unsigned int *value_lenp);
343#define recursively_expand(v) recursively_expand_for_file (v, NULL, NULL)
344#endif
345#ifdef CONFIG_WITH_COMPILER
346char *reference_recursive_variable (char *o, struct variable *v);
347#endif
348
349/* variable.c */
350struct variable_set_list *create_new_variable_set (void);
351void free_variable_set (struct variable_set_list *);
352struct variable_set_list *push_new_variable_scope (void);
353void pop_variable_scope (void);
354void define_automatic_variables (void);
355void initialize_file_variables (struct file *file, int reading);
356void print_file_variables (const struct file *file);
357void print_variable_set (struct variable_set *set, char *prefix);
358void merge_variable_set_lists (struct variable_set_list **to_list,
359 struct variable_set_list *from_list);
360#ifndef CONFIG_WITH_VALUE_LENGTH
361struct variable *do_variable_definition (const struct floc *flocp,
362 const char *name, const char *value,
363 enum variable_origin origin,
364 enum variable_flavor flavor,
365 int target_var);
366#else /* CONFIG_WITH_VALUE_LENGTH */
367# define do_variable_definition(flocp, varname, value, origin, flavor, target_var) \
368 do_variable_definition_2 ((flocp), (varname), (value), ~0U, 0, NULL, \
369 (origin), (flavor), (target_var))
370struct variable *do_variable_definition_2 (const struct floc *flocp,
371 const char *varname,
372 const char *value,
373 unsigned int value_len,
374 int simple_value, char *free_value,
375 enum variable_origin origin,
376 enum variable_flavor flavor,
377 int target_var);
378#endif /* CONFIG_WITH_VALUE_LENGTH */
379char *parse_variable_definition (const char *line,
380 enum variable_flavor *flavor);
381struct variable *assign_variable_definition (struct variable *v, char *line IF_WITH_VALUE_LENGTH_PARAM(char *eos));
382struct variable *try_variable_definition (const struct floc *flocp, char *line
383 IF_WITH_VALUE_LENGTH_PARAM(char *eos),
384 enum variable_origin origin,
385 int target_var);
386void init_hash_global_variable_set (void);
387void hash_init_function_table (void);
388struct variable *lookup_variable (const char *name, unsigned int length);
389struct variable *lookup_variable_in_set (const char *name, unsigned int length,
390 const struct variable_set *set);
391#ifdef CONFIG_WITH_STRCACHE2
392struct variable *lookup_variable_strcached (const char *name);
393#endif
394
395#ifdef CONFIG_WITH_VALUE_LENGTH
396void append_string_to_variable (struct variable *v, const char *value,
397 unsigned int value_len, int append);
398struct variable * do_variable_definition_append (const struct floc *flocp, struct variable *v,
399 const char *value, unsigned int value_len,
400 int simple_value, enum variable_origin origin,
401 int append);
402
403struct variable *define_variable_in_set (const char *name, unsigned int length,
404 const char *value,
405 unsigned int value_length,
406 int duplicate_value,
407 enum variable_origin origin,
408 int recursive,
409 struct variable_set *set,
410 const struct floc *flocp);
411
412/* Define a variable in the current variable set. */
413
414#define define_variable(n,l,v,o,r) \
415 define_variable_in_set((n),(l),(v),~0U,1,(o),(r),\
416 current_variable_set_list->set,NILF)
417
418#define define_variable_vl(n,l,v,vl,dv,o,r) \
419 define_variable_in_set((n),(l),(v),(vl),(dv),(o),(r),\
420 current_variable_set_list->set,NILF)
421
422/* Define a variable with a constant name in the current variable set. */
423
424#define define_variable_cname(n,v,o,r) \
425 define_variable_in_set((n),(sizeof (n) - 1),(v),~0U,1,(o),(r),\
426 current_variable_set_list->set,NILF)
427
428/* Define a variable with a location in the current variable set. */
429
430#define define_variable_loc(n,l,v,o,r,f) \
431 define_variable_in_set((n),(l),(v),~0U,1,(o),(r),\
432 current_variable_set_list->set,(f))
433
434/* Define a variable with a location in the global variable set. */
435
436#define define_variable_global(n,l,v,o,r,f) \
437 define_variable_in_set((n),(l),(v),~0U,1,(o),(r),NULL,(f))
438
439#define define_variable_vl_global(n,l,v,vl,dv,o,r,f) \
440 define_variable_in_set((n),(l),(v),(vl),(dv),(o),(r),NULL,(f))
441
442/* Define a variable in FILE's variable set. */
443
444#define define_variable_for_file(n,l,v,o,r,f) \
445 define_variable_in_set((n),(l),(v),~0U,1,(o),(r),(f)->variables->set,NILF)
446
447#else /* !CONFIG_WITH_VALUE_LENGTH */
448
449struct variable *define_variable_in_set (const char *name, unsigned int length,
450 const char *value,
451 enum variable_origin origin,
452 int recursive,
453 struct variable_set *set,
454 const struct floc *flocp);
455
456/* Define a variable in the current variable set. */
457
458#define define_variable(n,l,v,o,r) \
459 define_variable_in_set((n),(l),(v),(o),(r),\
460 current_variable_set_list->set,NILF) /* force merge conflict */
461
462/* Define a variable with a constant name in the current variable set. */
463
464#define define_variable_cname(n,v,o,r) \
465 define_variable_in_set((n),(sizeof (n) - 1),(v),(o),(r),\
466 current_variable_set_list->set,NILF) /* force merge conflict */
467
468/* Define a variable with a location in the current variable set. */
469
470#define define_variable_loc(n,l,v,o,r,f) \
471 define_variable_in_set((n),(l),(v),(o),(r),\
472 current_variable_set_list->set,(f)) /* force merge conflict */
473
474/* Define a variable with a location in the global variable set. */
475
476#define define_variable_global(n,l,v,o,r,f) \
477 define_variable_in_set((n),(l),(v),(o),(r),NULL,(f)) /* force merge conflict */
478
479/* Define a variable in FILE's variable set. */
480
481#define define_variable_for_file(n,l,v,o,r,f) \
482 define_variable_in_set((n),(l),(v),(o),(r),(f)->variables->set,NILF) /* force merge conflict */
483
484#endif /* !CONFIG_WITH_VALUE_LENGTH */
485
486void undefine_variable_in_set (const char *name, unsigned int length,
487 enum variable_origin origin,
488 struct variable_set *set);
489
490/* Remove variable from the current variable set. */
491
492#define undefine_variable_global(n,l,o) \
493 undefine_variable_in_set((n),(l),(o),NULL)
494
495#ifdef KMK
496struct variable *
497define_variable_alias_in_set (const char *name, unsigned int length,
498 struct variable *target, enum variable_origin origin,
499 struct variable_set *set, const struct floc *flocp);
500#endif
501
502/* Warn that NAME is an undefined variable. */
503
504#define warn_undefined(n,l) do{\
505 if (warn_undefined_variables_flag) \
506 error (reading_file, \
507 _("warning: undefined variable `%.*s'"), \
508 (int)(l), (n)); \
509 }while(0)
510
511char **target_environment (struct file *file);
512
513struct pattern_var *create_pattern_var (const char *target,
514 const char *suffix);
515
516extern int export_all_variables;
517#ifdef CONFIG_WITH_STRCACHE2
518extern struct strcache2 variable_strcache;
519#endif
520
521#ifdef KMK
522# define MAKELEVEL_NAME "KMK_LEVEL"
523#else
524#define MAKELEVEL_NAME "MAKELEVEL"
525#endif
526#define MAKELEVEL_LENGTH (sizeof (MAKELEVEL_NAME) - 1)
527
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette