Skip to content
Snippets Groups Projects
kernel-doc 66.2 KiB
Newer Older
sub process_export_file($) {
    my ($orig_file) = @_;
    my $file = map_filename($orig_file);

    if (!open(IN,"<$file")) {
	print STDERR "Error: Cannot open file $file\n";
	++$errors;
	return;
    }

    while (<IN>) {
	if (/$export_symbol/) {
	    next if (defined($nosymbol_table{$2}));
#
# Parsers for the various processing states.
#
# STATE_NORMAL: looking for the /** to begin everything.
#
sub process_normal() {
    if (/$doc_start/o) {
	$state = STATE_NAME;	# next line is always the function name
	$in_doc_sect = 0;
	$declaration_start_line = $. + 1;
    }
}

#
# STATE_NAME: Looking for the "name - description" line
#
sub process_name($$) {
    my $file = shift;
    my $identifier;
    my $descr;

    if (/$doc_block/o) {
	$state = STATE_DOCBLOCK;
	$contents = "";

	if ( $1 eq "" ) {
	    $section = $section_intro;
	} else {
	    $section = $1;
	}
    }
    elsif (/$doc_decl/o) {
	$identifier = $1;
	if (/\s*([\w\s]+?)(\(\))?\s*-/) {
	$state = STATE_BODY;
	# if there's no @param blocks need to set up default section
	# here
	$contents = "";
	$section = $section_default;
	$new_start_line = $. + 1;
	if (/-(.*)/) {
	    # strip leading/trailing/multiple spaces
	    $descr= $1;
	    $descr =~ s/^\s*//;
	    $descr =~ s/\s*$//;
	    $descr =~ s/\s+/ /g;
	    $declaration_purpose = $descr;
	    $state = STATE_BODY_MAYBE;
	} else {
	    $declaration_purpose = "";
	}

	if (($declaration_purpose eq "") && $verbose) {
	    print STDERR "${file}:$.: warning: missing initial short description on line:\n";
	    print STDERR $_;
	    ++$warnings;
	}

	if ($identifier =~ m/^struct\b/) {
	} elsif ($identifier =~ m/^union\b/) {
	} elsif ($identifier =~ m/^enum\b/) {
	} elsif ($identifier =~ m/^typedef\b/) {
	    $decl_type = 'typedef';
	} else {
	    $decl_type = 'function';
	}

	if ($verbose) {
	    print STDERR "${file}:$.: info: Scanning doc for $identifier\n";
	}
    } else {
	print STDERR "${file}:$.: warning: Cannot understand $_ on line $.",
	    " - I thought it was a doc line\n";
	++$warnings;
	$state = STATE_NORMAL;
    }
}

#
# STATE_BODY and STATE_BODY_MAYBE: the bulk of a kerneldoc comment.
#
sub process_body($$) {
    my $file = shift;

    # Until all named variable macro parameters are
    # documented using the bare name (`x`) rather than with
    # dots (`x...`), strip the dots:
    if ($section =~ /\w\.\.\.$/) {
	$section =~ s/\.\.\.$//;

	if ($verbose) {
	    print STDERR "${file}:$.: warning: Variable macro arguments should be documented without dots\n";
	    ++$warnings;
	}
    }

    if ($state == STATE_BODY_WITH_BLANK_LINE && /^\s*\*\s?\S/) {
	dump_section($file, $section, $contents);
	$section = $section_default;
    if (/$doc_sect/i) { # case insensitive for supported section names
	$newsection = $1;
	$newcontents = $2;

	# map the supported section names to the canonical names
	if ($newsection =~ m/^description$/i) {
	    $newsection = $section_default;
	} elsif ($newsection =~ m/^context$/i) {
	    $newsection = $section_context;
	} elsif ($newsection =~ m/^returns?$/i) {
	    $newsection = $section_return;
	} elsif ($newsection =~ m/^\@return$/) {
	    # special: @return is a section, not a param description
	    $newsection = $section_return;
	}

	if (($contents ne "") && ($contents ne "\n")) {
	    if (!$in_doc_sect && $verbose) {
		print STDERR "${file}:$.: warning: contents before sections\n";
		++$warnings;
	    }
	    dump_section($file, $section, $contents);
	    $section = $section_default;
	}

	$in_doc_sect = 1;
	$state = STATE_BODY;
	$contents = $newcontents;
	$new_start_line = $.;
	while (substr($contents, 0, 1) eq " ") {
	    $contents = substr($contents, 1);
	}
	if ($contents ne "") {
	    $contents .= "\n";
	}
	$section = $newsection;
	$leading_space = undef;
    } elsif (/$doc_end/) {
	if (($contents ne "") && ($contents ne "\n")) {
	    dump_section($file, $section, $contents);
	    $section = $section_default;
	    $contents = "";
	}
	# look for doc_com + <text> + doc_end:
	if ($_ =~ m'\s*\*\s*[a-zA-Z_0-9:\.]+\*/') {
	    print STDERR "${file}:$.: warning: suspicious ending line: $_";
	    ++$warnings;
	}

	$prototype = "";
	$state = STATE_PROTO;
	$brcount = 0;
        $new_start_line = $. + 1;
    } elsif (/$doc_content/) {
	if ($1 eq "") {
		dump_section($file, $section, $contents);
		$section = $section_default;
		$contents = "";
		$new_start_line = $.;
		if ($section ne $section_default) {
		    $state = STATE_BODY_WITH_BLANK_LINE;
		} else {
		    $state = STATE_BODY;
		}
		$contents .= "\n";
	    }
	} elsif ($state == STATE_BODY_MAYBE) {
	    # Continued declaration purpose
	    chomp($declaration_purpose);
	    $declaration_purpose .= " " . $1;
	    $declaration_purpose =~ s/\s+/ /g;
	} else {
	    my $cont = $1;
	    if ($section =~ m/^@/ || $section eq $section_context) {
		if (!defined $leading_space) {
		    if ($cont =~ m/^(\s+)/) {
			$leading_space = $1;
		    } else {
			$leading_space = "";
		    }
		}
		$cont =~ s/^$leading_space//;
	    }
	    $contents .= $cont . "\n";
	}
    } else {
	# i dont know - bad line?  ignore.
	print STDERR "${file}:$.: warning: bad line: $_";
	++$warnings;
    }
}


#
# STATE_PROTO: reading a function/whatever prototype.
#
sub process_proto($$) {
    my $file = shift;

    if (/$doc_inline_oneline/) {
	$section = $1;
	$contents = $2;
	if ($contents ne "") {
	    $contents .= "\n";
	    dump_section($file, $section, $contents);
	    $section = $section_default;
	    $contents = "";
	}
    } elsif (/$doc_inline_start/) {
	$state = STATE_INLINE;
	$inline_doc_state = STATE_INLINE_NAME;
    } elsif ($decl_type eq 'function') {
	process_proto_function($_, $file);
    } else {
	process_proto_type($_, $file);
    }
}

#
# STATE_DOCBLOCK: within a DOC: block.
#
sub process_docblock($$) {
    my $file = shift;

    if (/$doc_end/) {
	dump_doc_section($file, $section, $contents);
	$section = $section_default;
	$contents = "";
	$function = "";
	%parameterdescs = ();
	%parametertypes = ();
	@parameterlist = ();
	%sections = ();
	@sectionlist = ();
	$prototype = "";
	$state = STATE_NORMAL;
    } elsif (/$doc_content/) {
	if ( $1 eq "" )	{
	    $contents .= $blankline;
	} else {
	    $contents .= $1 . "\n";
	}
    }
}

#
# STATE_INLINE: docbook comments within a prototype.
#
sub process_inline($$) {
    my $file = shift;

    # First line (state 1) needs to be a @parameter
    if ($inline_doc_state == STATE_INLINE_NAME && /$doc_inline_sect/o) {
	$section = $1;
	$contents = $2;
	$new_start_line = $.;
	if ($contents ne "") {
	    while (substr($contents, 0, 1) eq " ") {
		$contents = substr($contents, 1);
	    }
	    $contents .= "\n";
	}
	$inline_doc_state = STATE_INLINE_TEXT;
	# Documentation block end */
    } elsif (/$doc_inline_end/) {
	if (($contents ne "") && ($contents ne "\n")) {
	    dump_section($file, $section, $contents);
	    $section = $section_default;
	    $contents = "";
	}
	$state = STATE_PROTO;
	$inline_doc_state = STATE_INLINE_NA;
	# Regular text
    } elsif (/$doc_content/) {
	if ($inline_doc_state == STATE_INLINE_TEXT) {
	    $contents .= $1 . "\n";
	    # nuke leading blank lines
	    if ($contents =~ /^\s*$/) {
		$contents = "";
	    }
	} elsif ($inline_doc_state == STATE_INLINE_NAME) {
	    $inline_doc_state = STATE_INLINE_ERROR;
	    print STDERR "${file}:$.: warning: ";
	    print STDERR "Incorrect use of kernel-doc format: $_";
	    ++$warnings;
	}
    }
}

sub process_file($) {
    my $file;
    my $initial_section_counter = $section_counter;
    my ($orig_file) = @_;

    $file = map_filename($orig_file);

    if (!open(IN_FILE,"<$file")) {
Linus Torvalds's avatar
Linus Torvalds committed
	print STDERR "Error: Cannot open file $file\n";
	++$errors;
	return;
    }

Linus Torvalds's avatar
Linus Torvalds committed
    $section_counter = 0;
	while (s/\\\s*$//) {
	# Replace tabs by spaces
        while ($_ =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {};
	# Hand this line to the appropriate state handler
	if ($state == STATE_NORMAL) {
	} elsif ($state == STATE_NAME) {
	    process_name($file, $_);
	} elsif ($state == STATE_BODY || $state == STATE_BODY_MAYBE ||
		 $state == STATE_BODY_WITH_BLANK_LINE) {
	} elsif ($state == STATE_INLINE) { # scanning for inline parameters
	} elsif ($state == STATE_PROTO) {
	    process_proto($file, $_);
	} elsif ($state == STATE_DOCBLOCK) {
Linus Torvalds's avatar
Linus Torvalds committed
    }

    # Make sure we got something interesting.
    if ($initial_section_counter == $section_counter && $
	output_mode ne "none") {
	if ($output_selection == OUTPUT_INCLUDE) {
	    print STDERR "${file}:1: warning: '$_' not found\n"
		for keys %function_table;
	else {
	    print STDERR "${file}:1: warning: no structured comments found\n";
Linus Torvalds's avatar
Linus Torvalds committed
    }
Linus Torvalds's avatar
Linus Torvalds committed
}
if ($output_mode eq "rst") {
	get_sphinx_version() if (!$sphinx_major);
}

$kernelversion = get_kernel_version();

# generate a sequence of code that will splice in highlighting information
# using the s// operator.
for (my $k = 0; $k < @highlights; $k++) {
    my $pattern = $highlights[$k][0];
    my $result = $highlights[$k][1];
#   print STDERR "scanning pattern:$pattern, highlight:($result)\n";
    $dohighlight .=  "\$contents =~ s:$pattern:$result:gs;\n";
}

# Read the file that maps relative names to absolute names for
# separate source and object directories and for shadow trees.
if (open(SOURCE_MAP, "<.tmp_filelist.txt")) {
	my ($relname, $absname);
	while(<SOURCE_MAP>) {
		chop();
		($relname, $absname) = (split())[0..1];
		$relname =~ s:^/+::;
		$source_map{$relname} = $absname;
	}
	close(SOURCE_MAP);
}

if ($output_selection == OUTPUT_EXPORTED ||
    $output_selection == OUTPUT_INTERNAL) {
    foreach (@export_file_list) {
	chomp;
	process_export_file($_);
    }
}

foreach (@ARGV) {
    chomp;
    process_file($_);
}
if ($verbose && $errors) {
  print STDERR "$errors errors\n";
}
if ($verbose && $warnings) {
  print STDERR "$warnings warnings\n";
}

if ($Werror && $warnings) {
    print STDERR "$warnings warnings as Errors\n";
    exit($warnings);
} else {
    exit($output_mode eq "none" ? 0 : $errors)
}