1#	$NetBSD: genlintstub.awk,v 1.9 2005/12/11 12:24:29 christos Exp $
2#
3# Copyright 2001 Wasabi Systems, Inc.
4# All rights reserved.
5#
6# Written by Perry E. Metzger for Wasabi Systems, Inc.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16# 3. All advertising materials mentioning features or use of this software
17#    must display the following acknowledgement:
18#      This product includes software developed for the NetBSD Project by
19#      Wasabi Systems, Inc.
20# 4. The name of Wasabi Systems, Inc. may not be used to endorse
21#    or promote products derived from this software without specific prior
22#    written permission.
23#
24# THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
25# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
28# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34# POSSIBILITY OF SUCH DAMAGE.
35#
36
37# This awk script is used by kernel Makefiles to construct C lint
38# stubs automatically from properly formatted comments in .S files. In
39# general, a .S file should have a special comment for anything with
40# something like an ENTRY designation. The special formats are:
41#
42# /* LINTSTUB: Empty */
43# This is used as an indicator that the file contains no stubs at
44# all. It generates a /* LINTED */ comment to quiet lint.
45#
46# /* LINTSTUB: Func: type function(args); */
47# type must be void, int or long. A return is faked up for ints and longs.
48# Semicolon is optional.
49#
50# /* LINTSTUB: Var: type variable, variable; */
51# This is often appropriate for assembly bits that the rest of the
52# kernel has declared as char * and such, like various bits of
53# trampoline code.
54#
55# /* LINTSTUB: include foo */
56# Turns into a literal `#include foo' line in the source. Useful for
57# making sure the stubs are checked against system prototypes like
58# systm.h, cpu.h, etc., and to make sure that various types are
59# properly declared.
60#
61# /* LINTSTUB: Ignore */
62# This is used as an indicator to humans (and possible future
63# automatic tools) that the entry is only used internally by other .S
64# files and does not need a stub. You want this so you know you
65# haven't just forgotten to put a stub in for something and you are
66# *deliberately* ignoring it.
67
68# LINTSTUBs are also accepted inside multiline comments, e.g.
69#
70# /*
71#  * LINTSTUB: include <foo>
72#  * LINTSTUB: include "bar"
73#  */
74#
75# /*
76#  * LINTSTUB: Func: type function(args)
77#  *    Some descriptive comment about the function.
78#  */
79
80BEGIN {
81	printf "/* DO NOT EDIT! DO NOT EDIT! DO NOT EDIT! */\n";
82	printf "/* DO NOT EDIT! DO NOT EDIT! DO NOT EDIT! */\n";
83	printf "/* This file was automatically generated. */\n";
84	printf "/* see genlintstub.awk for details.       */\n";
85	printf "/* This file was automatically generated. */\n";
86	printf "/* DO NOT EDIT! DO NOT EDIT! DO NOT EDIT! */\n";
87	printf "/* DO NOT EDIT! DO NOT EDIT! DO NOT EDIT! */\n";
88	printf "\n\n";
89
90	nerrors = 0;
91}
92
93function error(msg) {
94	printf "ERROR:%d: %s: \"%s\"\n", NR, msg, $0 > "/dev/stderr";
95	++nerrors;
96}
97
98END {
99	if (nerrors > 0)
100		exit 1;
101}
102
103
104# Check if $i contains semicolon or "*/" comment terminator.  If it
105# does, strip them and the rest of the word away and return 1 to
106# signal that no more words on the line are to be processed.
107
108function process_word(i) {
109	if ($i ~ /;/) {
110		sub(";.*$", "", $i);
111		return 1;
112	}
113	else if ($i ~ /\*\//) {
114		sub("\\*\\/.*$", "", $i);
115		return 1;
116	}
117	else if (i == NF)
118		return 1;
119	else
120		return 0;
121}
122
123
124/^[\/ ]\* LINTSTUB: Func:/ {
125	if (NF < 5) {
126		error("bad 'Func' declaration");
127		next;
128	}
129	if (($4 == "int") || ($4 == "long"))
130		retflag = 1;
131	else if ($4 == "void")
132		retflag = 0;
133	else {
134		error("type is not int, long or void");
135		next;
136	}
137	printf "/* ARGSUSED */\n%s", $4;
138	for (i = 5; i <= NF; ++i) {
139		if (process_word(i)) {
140			printf " %s\n", $i;
141			break;
142		}
143		else
144			printf " %s", $i;
145	}
146	print "{";
147	if (retflag)
148		print "\treturn(0);";
149	print "}\n";
150	next;
151}
152
153/^[\/ ]\* LINTSTUB: Var:/ {
154	if (NF < 4) {
155		error("bad 'Var' declaration");
156		next;
157	}
158	for (i = 4; i <= NF; ++i) {
159		if (process_word(i)) {
160			printf " %s;\n", $i;
161			break;
162		}
163		else
164			printf " %s", $i;
165	}
166	next;
167}
168
169/^[\/ ]\* LINTSTUB: include[ \t]+/ {
170	if (NF < 4) {
171		error("bad 'include' directive");
172		next;
173	}
174	sub("\\*\\/.*$", "", $4);
175	printf "#include %s\n", $4;
176	next;
177}
178
179/^[\/ ]\* LINTSTUB: Empty($|[^_0-9A-Za-z])/ {
180	printf "/* LINTED (empty translation unit) */\n";
181	next;
182}
183
184/^[\/ ]\* LINTSTUB: Ignore($|[^_0-9A-Za-z])/ {
185	next;
186}
187
188/^[\/ ]\* LINTSTUBS:/ {
189	error("LINTSTUB, not LINTSTUBS");
190	next;
191}
192
193/^[\/ ]\* LINTSTUB:/ {
194	error("unrecognized");
195	next;
196}
197