check-doxy-blocks.pl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env perl
  2. # Detect comment blocks that are likely meant to be doxygen blocks but aren't.
  3. #
  4. # More precisely, look for normal comment block containing '\'.
  5. # Of course one could use doxygen warnings, eg with:
  6. # sed -e '/EXTRACT/s/YES/NO/' doxygen/mbedtls.doxyfile | doxygen -
  7. # but that would warn about any undocumented item, while our goal is to find
  8. # items that are documented, but not marked as such by mistake.
  9. #
  10. # Copyright The Mbed TLS Contributors
  11. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  12. #
  13. # This file is provided under the Apache License 2.0, or the
  14. # GNU General Public License v2.0 or later.
  15. #
  16. # **********
  17. # Apache License 2.0:
  18. #
  19. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  20. # not use this file except in compliance with the License.
  21. # You may obtain a copy of the License at
  22. #
  23. # http://www.apache.org/licenses/LICENSE-2.0
  24. #
  25. # Unless required by applicable law or agreed to in writing, software
  26. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  27. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  28. # See the License for the specific language governing permissions and
  29. # limitations under the License.
  30. #
  31. # **********
  32. #
  33. # **********
  34. # GNU General Public License v2.0 or later:
  35. #
  36. # This program is free software; you can redistribute it and/or modify
  37. # it under the terms of the GNU General Public License as published by
  38. # the Free Software Foundation; either version 2 of the License, or
  39. # (at your option) any later version.
  40. #
  41. # This program is distributed in the hope that it will be useful,
  42. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  43. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  44. # GNU General Public License for more details.
  45. #
  46. # You should have received a copy of the GNU General Public License along
  47. # with this program; if not, write to the Free Software Foundation, Inc.,
  48. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  49. #
  50. # **********
  51. use warnings;
  52. use strict;
  53. use File::Basename;
  54. # C/header files in the following directories will be checked
  55. my @directories = qw(include/mbedtls library doxygen/input);
  56. # very naive pattern to find directives:
  57. # everything with a backslach except '\0' and backslash at EOL
  58. my $doxy_re = qr/\\(?!0|\n)/;
  59. # Return an error code to the environment if a potential error in the
  60. # source code is found.
  61. my $exit_code = 0;
  62. sub check_file {
  63. my ($fname) = @_;
  64. open my $fh, '<', $fname or die "Failed to open '$fname': $!\n";
  65. # first line of the last normal comment block,
  66. # or 0 if not in a normal comment block
  67. my $block_start = 0;
  68. while (my $line = <$fh>) {
  69. $block_start = $. if $line =~ m/\/\*(?![*!])/;
  70. $block_start = 0 if $line =~ m/\*\//;
  71. if ($block_start and $line =~ m/$doxy_re/) {
  72. print "$fname:$block_start: directive on line $.\n";
  73. $block_start = 0; # report only one directive per block
  74. $exit_code = 1;
  75. }
  76. }
  77. close $fh;
  78. }
  79. sub check_dir {
  80. my ($dirname) = @_;
  81. for my $file (<$dirname/*.[ch]>) {
  82. check_file($file);
  83. }
  84. }
  85. # Check that the script is being run from the project's root directory.
  86. for my $dir (@directories) {
  87. if (! -d $dir) {
  88. die "This script must be run from the mbed TLS root directory";
  89. } else {
  90. check_dir($dir)
  91. }
  92. }
  93. exit $exit_code;
  94. __END__