4 * Copyright (c) 2000-2005, Darren Hiebert
6 * This source code is released for free distribution under the terms of the
7 * GNU General Public License.
9 * This module contains functions for generating tags for makefiles.
15 #include "general.h" /* must always come first */
33 static kindOption MakeKinds
[] = {
34 { TRUE
, 'm', "macro", "macros"},
35 { TRUE
, 't', "function", "targets"}
39 * FUNCTION DEFINITIONS
42 static int nextChar (void)
54 static void skipLine (void)
59 while (c
!= EOF
&& c
!= '\n');
64 static int skipToNonWhite (void)
69 while (c
!= '\n' && isspace (c
));
73 static boolean
isIdentifier (int c
)
75 return (boolean
)(c
!= '\0' && (isalnum (c
) || strchr (".-_/", c
) != NULL
));
78 static boolean
isSpecialTarget (vString
*const name
)
81 /* All special targets begin with '.'. */
82 if (vStringChar (name
, i
++) != '.') {
85 while (i
< vStringLength (name
)) {
86 char ch
= vStringChar (name
, i
++);
87 if (ch
!= '_' && !isupper (ch
))
95 static void newTarget (vString
*const name
)
97 /* Ignore GNU Make's "special targets". */
98 if (isSpecialTarget (name
))
102 makeSimpleTag (name
, MakeKinds
, K_TARGET
);
105 static void newMacro (vString
*const name
)
107 makeSimpleTag (name
, MakeKinds
, K_MACRO
);
110 static void newMacroFromDefine (vString
*const name
)
112 /* name is something like "define JAVAHPP_RULE", find the space and jump to the next char */
113 char *name_val
= strchr (vStringValue (name
), ' ');
115 if (name_val
!= NULL
) {
116 vStringCopyS (name
, name_val
+ 1);
117 makeSimpleTag (name
, MakeKinds
, K_MACRO
);
121 static void readIdentifier (const int first
, vString
*const id
)
127 while (isIdentifier (c
) || c
== ' ')
129 c_next
= nextChar ();
131 /* add the space character only if the previous and
132 * next character are valid identifiers */
133 if (isIdentifier (c_prev
) && isIdentifier (c_next
))
143 vStringTerminate (id
);
146 static void skipToMatch (const char *const pair
)
148 const int begin
= pair
[0], end
= pair
[1];
149 const unsigned long inputLineNumber
= getInputLineNumber ();
153 while (matchLevel
> 0)
164 verbose ("%s: failed to find match for '%c' at line %lu\n",
165 getInputFileName (), begin
, inputLineNumber
);
168 static void findMakeTags (void)
170 vString
*name
= vStringNew ();
171 boolean newline
= TRUE
;
172 boolean in_define
= FALSE
;
173 boolean in_rule
= FALSE
;
174 boolean variable_possible
= TRUE
;
177 while ((c
= nextChar ()) != EOF
)
185 skipLine (); /* skip rule */
191 variable_possible
= (boolean
)(!in_rule
);
196 else if (isspace (c
))
206 variable_possible
= TRUE
;
209 else if (variable_possible
&& isIdentifier (c
))
211 readIdentifier (c
, name
);
212 if (strncmp (vStringValue (name
), "endef", 5) == 0)
216 else if (strncmp (vStringValue (name
), "define", 6) == 0 &&
220 c
= skipToNonWhite ();
221 newMacroFromDefine (name
);
225 c
= skipToNonWhite ();
226 if (strchr (":?+", c
) != NULL
)
228 boolean append
= (boolean
)(c
== '+');
229 boolean was_colon
= (c
== ':');
268 variable_possible
= FALSE
;
270 vStringDelete (name
);
273 extern parserDefinition
* MakefileParser (void)
275 static const char *const patterns
[] = { "[Mm]akefile", NULL
};
276 static const char *const extensions
[] = { "mak", "mk", NULL
};
277 parserDefinition
* const def
= parserNew ("Make");
278 def
->kinds
= MakeKinds
;
279 def
->kindCount
= KIND_COUNT (MakeKinds
);
280 def
->patterns
= patterns
;
281 def
->extensions
= extensions
;
282 def
->parser
= findMakeTags
;
286 /* vi:set tabstop=4 shiftwidth=4: */