Minos Galanakis | 2c824b4 | 2025-03-20 09:28:45 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env perl |
| 2 | |
| 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: |
| 7 | # sed -e '/EXTRACT/s/YES/NO/' doxygen/mbedtls.doxyfile | doxygen - |
| 8 | # 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. |
| 10 | # |
| 11 | # Copyright The Mbed TLS Contributors |
| 12 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 13 | |
| 14 | use warnings; |
| 15 | use strict; |
| 16 | use File::Basename; |
| 17 | |
| 18 | # C/header files in the following directories will be checked |
| 19 | my @mbedtls_directories = qw(include/mbedtls library doxygen/input); |
| 20 | my @tf_psa_crypto_directories = qw(include/psa include/tf-psa-crypto |
| 21 | drivers/builtin/include/mbedtls |
| 22 | drivers/builtin/src core doxygen/input); |
| 23 | |
| 24 | # very naive pattern to find directives: |
| 25 | # everything with a backslach except '\0' and backslash at EOL |
| 26 | my $doxy_re = qr/\\(?!0|\n)/; |
| 27 | |
| 28 | # Return an error code to the environment if a potential error in the |
| 29 | # source code is found. |
| 30 | my $exit_code = 0; |
| 31 | |
| 32 | sub check_file { |
| 33 | my ($fname) = @_; |
| 34 | open my $fh, '<', $fname or die "Failed to open '$fname': $!\n"; |
| 35 | |
| 36 | # first line of the last normal comment block, |
| 37 | # or 0 if not in a normal comment block |
| 38 | my $block_start = 0; |
| 39 | while (my $line = <$fh>) { |
| 40 | $block_start = $. if $line =~ m/\/\*(?![*!])/; |
| 41 | $block_start = 0 if $line =~ m/\*\//; |
| 42 | if ($block_start and $line =~ m/$doxy_re/) { |
| 43 | print "$fname:$block_start: directive on line $.\n"; |
| 44 | $block_start = 0; # report only one directive per block |
| 45 | $exit_code = 1; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | close $fh; |
| 50 | } |
| 51 | |
| 52 | sub check_dir { |
| 53 | my ($dirname) = @_; |
| 54 | for my $file (<$dirname/*.[ch]>) { |
| 55 | check_file($file); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | open my $project_file, "scripts/project_name.txt" or die "This script must be run from Mbed TLS or TF-PSA-Crypto root directory"; |
| 60 | my $project = <$project_file>; |
| 61 | chomp($project); |
| 62 | my @directories; |
| 63 | |
| 64 | if ($project eq "TF-PSA-Crypto") { |
| 65 | @directories = @tf_psa_crypto_directories |
| 66 | } elsif ($project eq "Mbed TLS") { |
| 67 | @directories = @mbedtls_directories |
| 68 | } |
| 69 | # Check that the script is being run from the project's root directory. |
| 70 | for my $dir (@directories) { |
| 71 | check_dir($dir) |
| 72 | } |
| 73 | |
| 74 | exit $exit_code; |
| 75 | |
| 76 | __END__ |