guard_headers.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/bin/bash
  2. # Copyright 2015 gRPC authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. set -e
  16. cd `dirname $0`/../..
  17. function process_dir {
  18. base_dir=$1
  19. prefix=$2
  20. comment_language=$3
  21. (
  22. cd $base_dir
  23. find . -name "*.h" | while read f ; do
  24. guard=${prefix}_`echo ${f#*/} | tr '[:lower:]/.-' '[:upper:]___'`
  25. if [ "$comment_language" = "c++" ] ; then
  26. comment="// $guard"
  27. else
  28. comment="/* $guard */"
  29. fi
  30. awk '
  31. BEGIN {
  32. guard = "'${guard}'";
  33. comment_language = "'${comment_language}'";
  34. }
  35. prev ~ /^#ifndef/ && !got_first_ifndef {
  36. got_first_ifndef = 1;
  37. prev = "#ifndef " guard;
  38. }
  39. prev ~ /^#define/ && !got_first_define {
  40. got_first_define = 1;
  41. prev = "#define " guard;
  42. }
  43. NR > 1 { print prev; }
  44. { prev = $0; }
  45. END {
  46. if (prev ~ /^#endif/) {
  47. if (comment_language ~ /^c$/) {
  48. print "#endif /* " guard " */";
  49. } else if (comment_language ~ /^c\+\+$/) {
  50. print "#endif // " guard;
  51. } else {
  52. print "ERROR: unknown comment language: " comment_language;
  53. }
  54. } else {
  55. print "ERROR: file does not end with #endif";
  56. }
  57. }
  58. ' "${f}" > "${f}.rewritten"
  59. mv "${f}.rewritten" "${f}"
  60. done
  61. )
  62. }
  63. process_dir include/grpc GRPC c
  64. process_dir include/grpc++ GRPCXX c++
  65. process_dir src/core GRPC_INTERNAL_CORE c
  66. process_dir src/cpp GRPC_INTERNAL_CPP c++
  67. process_dir src/compiler GRPC_INTERNAL_COMPILER c++
  68. process_dir test/core GRPC_TEST_CORE c
  69. process_dir test/cpp GRPC_TEST_CPP c++
  70. process_dir examples GRPC_EXAMPLES c++