Skip to content
pi-c Artifact [4511780717]
Login

Artifact [4511780717]

Artifact 4511780717360fbd7501d14e69e16440d06e63aa4228efbbeb53e2f9567ceb73:


# Prints "NAME KIND FILE" for each name a pic source file defines at file scope that two files
# could not both define in one translation unit: static functions and variables, typedefs, and
# struct, union and enum tags and enumerators. Works on pic's style (definitions start in column
# 0, one declaration per line, a struct body's closing "}" in column 0).
# Usage: awk -f amalgam-names.awk FILE.c...
function out(name, kind) {
	if (name == "" || name in done) return
	done[name] = 1
	print name, kind, FILENAME
}
FNR == 1 { delete done; depth = 0; inenum = 0; intd = 0 }
{
	line = $0
	sub(/\/\*.*\*\//, "", line)
	sub(/\/\/.*$/, "", line)
}
/^#/ { next }
inenum {
	if (line ~ /^[ \t]*[A-Za-z_][A-Za-z0-9_]*/) {
		e = line; sub(/^[ \t]*/, "", e); sub(/[^A-Za-z0-9_].*$/, "", e)
		if (e != "") out(e, "enumerator")
	}
	if (line ~ /^}/) {
		inenum = 0
		if (intd) { t = line; gsub(/[};, \t]/, "", t); out(t, "typedef") }
	}
	next
}
line ~ /^typedef enum/ || line ~ /^(static )?(const )?enum [A-Za-z_0-9]* *\{/ {
	if (match(line, /enum [A-Za-z_0-9]+/)) out(substr(line, RSTART + 5, RLENGTH - 5), "tag")
	intd = (line ~ /^typedef/)
	if (line ~ /\}/) next
	inenum = 1; next
}
line ~ /^typedef struct [A-Za-z_0-9]+ [A-Za-z_0-9]+;/ { n = line; sub(/;.*/, "", n); sub(/^.* /, "", n); out(n, "typedef"); next }
line ~ /^typedef .*;$/ {
	n = line; sub(/;.*$/, "", n)
	if (n ~ /\(\*/) { sub(/^.*\(\*/, "", n); sub(/\).*$/, "", n) } else { sub(/\[.*$/, "", n); sub(/^.*[ \*]/, "", n) }
	out(n, "typedef"); next
}
line ~ /^typedef (struct|union)/ { if (match(line, /(struct|union) [A-Za-z_0-9]+/)) { t = substr(line, RSTART, RLENGTH); sub(/^[a-z]+ /, "", t); out(t, "tag") } td = 1; next }
line ~ /^(struct|union) [A-Za-z_0-9]+ *\{/ { t = line; sub(/^[a-z]+ /, "", t); sub(/ *\{.*$/, "", t); out(t, "tag"); next }
td && line ~ /^} *[A-Za-z_][A-Za-z0-9_]*;/ { t = line; gsub(/[};, \t]/, "", t); out(t, "typedef"); td = 0; next }
line ~ /^static / {
	if (line ~ /^static (const )?(struct|union|enum) *\{/ || line ~ /^static (const )?struct [A-Za-z_0-9]+ *\{/) { sbody = 1; next }
	n = line
	if (n ~ /\(/ && n !~ /^[^(]*=/) { sub(/\(.*$/, "", n); sub(/^.*[ \*]/, "", n); out(n, "static") ; next }
	sub(/[=;\[].*$/, "", n); sub(/[ \t]+$/, "", n); sub(/^.*[ \*]/, "", n); out(n, "static"); next
}
sbody && line ~ /^} *[A-Za-z_][A-Za-z0-9_]*[\[;= ]/ { t = line; sub(/^} */, "", t); sub(/[\[;= ].*$/, "", t); out(t, "static"); sbody = 0 }