2 # This file is part of the LibreOffice project.
4 # This Source Code Form is subject to the terms of the Mozilla Public
5 # License, v. 2.0. If a copy of the MPL was not distributed with this
6 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 # corrects include guards for hxx/h files automatically by its path.
11 # a) fixincludeguards.sh header.hxx
12 # b) find . -name *.hxx -or -name *.h | xargs bash ./bin/fixincludeguards.sh
14 # TODO: This doesn't fix wrong #endif comments, like:
18 # #endif // OTHER_BAR_HXX
20 # TODO: Make this portable. As it is now, it likely only works on Linux, or
21 # other platforms with a purely GNU toolset.
23 guard_prefix
="INCLUDED_"
26 # remove leading ./, if invoked with find
27 fn
=`echo "$fn" | sed 's/^.\///g'`
29 # global header in include/ top level dir:
30 # drop the project dir
31 fnfixed
=`echo $fn | sed 's,include/,,g'`
32 # add examples prefix to headers in odk/examples
33 fnfixed
=`echo $fnfixed | sed 's,odk/examples/\(cpp\|DevelopersGuide\|OLE\)/,examples_,g'`
35 # convert file path to header guard
36 guard
=`echo "$fnfixed" | sed 's/[\/\.-]/_/g' | tr 'a-z' 'A-Z'`
38 if [ aa
"`git grep -h "^\s
*#ifndef ${guard_prefix}$guard" "$fn" | wc -l`" != "aa1" ] ||
39 [ aa
"`git grep -h "^\s
*#define ${guard_prefix}$guard" "$fn" | wc -l`" != "aa1" ]; then
41 # pattern which identifies guards, common one look like
42 # _MODULE_FILE_HXX, FILE_H, FILE_INC
43 pattern
=".*\(_HXX\|_H\|_INC\|_hxx\|_h\|_inc\)"
45 ### extract guard definition
46 # head to take only the first match
47 old_guard
=`git grep -h "#ifndef $pattern" "$fn" | head -n1 | sed "s/.*\s\($pattern.*\)/\1/"`
49 if [ aa
"$old_guard" == aa
"" ]; then
50 echo -e "$fn: \e[00;31mwarning:\e[00m guard not detectable"
55 if [ aa
"`git grep -w "$old_guard" | cut -d ':' -f1 | sort -u | wc -l `" != aa
"1" ]; then
56 echo -e "$fn: \e[00;31mwarning:\e[00m $old_guard guard definition used in other files"
60 ### skip some special files...
62 # skip this comphelper stuff:
63 # INCLUDED_COMPHELPER_IMPLBASE_VAR_HXX_14
64 if [ aa
"INCLUDED_COMPHELPER_IMPLBASE_" == aa
"`echo $old_guard | sed "s
/VAR_HXX_
[0-9]\
+//g
"`" ]; then
68 # skip files like xmloff/source/forms/elementimport_impl.hxx
69 if [ aa
"`git grep -h "#error.*directly" "$fn" | wc -l`" != "aa0" ]; then
74 ### replace old guard with new scheme guard
75 echo "$fn: $old_guard"
77 # includes leading whitespace removal
78 sed -i "s/\s*${old_guard}/ ${guard_prefix}${guard}/g" "$fn"
82 sed -i "s/#endif\s*\(\/\/\|\/\*\)\s*\#\?\(ifndef\)\?\s*!\?\s*\(${guard_prefix}${guard}\).*/#endif \/\/ \3/g" "$fn"