configure already sets TMPDIR to mixed path
[LibreOffice.git] / bin / fixincludeguards.sh
blob5e05e944205fb373bcbe5a209e93af0338807e03
1 #!/usr/bin/env bash
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.
10 # Usage:
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:
15 # #ifndef FOO_BAR_HXX
16 # #define FOO_BAR_HXX
17 # ...
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_"
25 for fn in "$@"; do
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"
51 continue
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"
57 continue
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
65 continue
68 # skip files like xmloff/source/forms/elementimport_impl.hxx
69 if [ aa"`git grep -h "#error.*directly" "$fn" | wc -l`" != "aa0" ]; then
70 continue
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"
81 ### clean up endif
82 sed -i "s/#endif\s*\(\/\/\|\/\*\)\s*\#\?\(ifndef\)\?\s*!\?\s*\(${guard_prefix}${guard}\).*/#endif \/\/ \3/g" "$fn"
85 done