blob: 70fab6896fb657c91b01571a901353a99933f4ad [file] [log] [blame]
fbrosson533407a2018-04-04 21:44:29 +00001#!/usr/bin/env perl
Paul Bakker9a736322012-11-14 12:39:52 +00002
3# Detect comment blocks that are likely meant to be doxygen blocks but aren't.
4#
5# More precisely, look for normal comment block containing '\'.
6# Of course one could use doxygen warnings, eg with:
Manuel Pégourié-Gonnardf234ff82015-01-22 17:01:27 +00007# sed -e '/EXTRACT/s/YES/NO/' doxygen/mbedtls.doxyfile | doxygen -
Paul Bakker9a736322012-11-14 12:39:52 +00008# but that would warn about any undocumented item, while our goal is to find
9# items that are documented, but not marked as such by mistake.
Bence Szépkúti700ee442020-05-26 00:33:31 +020010#
11# Copyright (C) 2012-2016, Arm Limited, All Rights Reserved
12#
13# This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker9a736322012-11-14 12:39:52 +000014
15use warnings;
16use strict;
17use File::Basename;
18
Manuel Pégourié-Gonnardd09a6b52015-04-09 17:19:23 +020019# C/header files in the following directories will be checked
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000020my @directories = qw(include/mbedtls library doxygen/input);
Paul Bakker9a736322012-11-14 12:39:52 +000021
22# very naive pattern to find directives:
Manuel Pégourié-Gonnardef009ff2013-09-16 13:40:25 +020023# everything with a backslach except '\0' and backslash at EOL
24my $doxy_re = qr/\\(?!0|\n)/;
Paul Bakker9a736322012-11-14 12:39:52 +000025
Andres Amaya Garciad3f0f5e2016-12-14 09:36:55 +000026# Return an error code to the environment if a potential error in the
27# source code is found.
28my $exit_code = 0;
29
Paul Bakker9a736322012-11-14 12:39:52 +000030sub check_file {
31 my ($fname) = @_;
32 open my $fh, '<', $fname or die "Failed to open '$fname': $!\n";
33
34 # first line of the last normal comment block,
35 # or 0 if not in a normal comment block
36 my $block_start = 0;
37 while (my $line = <$fh>) {
38 $block_start = $. if $line =~ m/\/\*(?![*!])/;
39 $block_start = 0 if $line =~ m/\*\//;
40 if ($block_start and $line =~ m/$doxy_re/) {
41 print "$fname:$block_start: directive on line $.\n";
42 $block_start = 0; # report only one directive per block
Andres Amaya Garciad3f0f5e2016-12-14 09:36:55 +000043 $exit_code = 1;
Paul Bakker9a736322012-11-14 12:39:52 +000044 }
45 }
46
47 close $fh;
48}
49
50sub check_dir {
51 my ($dirname) = @_;
52 for my $file (<$dirname/*.[ch]>) {
53 check_file($file);
54 }
55}
56
Andres Amaya Garciad3f0f5e2016-12-14 09:36:55 +000057# Check that the script is being run from the project's root directory.
Paul Bakker9a736322012-11-14 12:39:52 +000058for my $dir (@directories) {
Andres Amaya Garciad3f0f5e2016-12-14 09:36:55 +000059 if (! -d $dir) {
60 die "This script must be run from the mbed TLS root directory";
61 } else {
62 check_dir($dir)
63 }
Paul Bakker9a736322012-11-14 12:39:52 +000064}
65
Andres Amaya Garciad3f0f5e2016-12-14 09:36:55 +000066exit $exit_code;
67
Paul Bakker9a736322012-11-14 12:39:52 +000068__END__