diff options
author | Thomas Arnhold <thomas@arnhold.org> | 2013-10-22 05:35:49 +0200 |
---|---|---|
committer | Caolán McNamara <caolanm@redhat.com> | 2013-10-23 10:26:06 +0000 |
commit | b009e8fd4fce06d9abae8aaac8750ece7df212e4 (patch) | |
tree | 452fdf0407278dc35c6e7a86191ef12e9889b442 /bin | |
parent | da18612bf67b1f7caf9d94f4d8cb7fbd626e09f2 (diff) |
fixincludeguards.sh: unify all include guards
The include guard gets generated based on the path of the header file.
INCLUDED_FOO_BAR_HXX for global headers in include/
INCLUDED_FOO_SOURCE_BAR_BAZ_HXX for anything else.
See fdo#68849.
Change-Id: Ia250e7c99cef3cb5bb0f9d4dc758ef2da3eec0a8
Reviewed-on: https://gerrit.libreoffice.org/6386
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
Tested-by: Caolán McNamara <caolanm@redhat.com>
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/fixincludeguards.sh | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/bin/fixincludeguards.sh b/bin/fixincludeguards.sh new file mode 100755 index 000000000000..ec47edf428f8 --- /dev/null +++ b/bin/fixincludeguards.sh @@ -0,0 +1,74 @@ +#!/bin/bash +# This file is part of the LibreOffice project. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +# corrects include guards for hxx/h files automatically by its path. + +# Usage: +# a) fixincludeguards.sh header.hxx +# b) find . -name *.hxx -or -name *.h | xargs sh fixincludeguards.sh + +guard_prefix="INCLUDED_" + +for fn in "$@"; do + # remove leading ./, if invoked with find + fn=`echo "$fn" | sed 's/^.\///g'` + + # global header in include/ top level dir: + # drop the project dir + fnfixed=`echo $fn | sed 's/include\///g'` + + # convert file path to header guard + guard=`echo "$fnfixed" | sed 's/[\/\.-]/_/g' | tr 'a-z' 'A-Z'` + + if [ aa"`git grep -h "^\s*#ifndef ${guard_prefix}$guard" "$fn" | wc -l`" != "aa1" ] || + [ aa"`git grep -h "^\s*#define ${guard_prefix}$guard" "$fn" | wc -l`" != "aa1" ]; then + + # pattern which identifies guards, common one look like + # _XMLOFF_ANIMEXP_HXX, BENTOID_H, IXFOBJECT_INC + pattern=".*\(_HXX\|_H\|_INC\)" + + ### extract guard definition + # head to take only the first match + old_guard=`git grep -h "#ifndef $pattern" "$fn" | head -n1 | sed "s/.*\s\($pattern.*\)/\1/"` + + if [ aa"$old_guard" == aa"" ]; then + echo -e "$fn: \e[00;31mwarning:\e[00m guard not detectable" + continue + fi + + + if [ aa"`git grep -w "$old_guard" | cut -d ':' -f1 | sort -u | wc -l `" != aa"1" ]; then + echo -e "$fn: \e[00;31mwarning:\e[00m guard definition used in other files" + continue + fi + + ### skip some special files... + + # skip this comphelper stuff: + # INCLUDED_COMPHELPER_IMPLBASE_VAR_HXX_14 + if [ aa"INCLUDED_COMPHELPER_IMPLBASE_" == aa"`echo $old_guard | sed "s/VAR_HXX_[0-9]\+//g"`" ]; then + continue + fi + + # skip files like xmloff/source/forms/elementimport_impl.hxx + if [ aa"`git grep -h "#error.*directly" "$fn" | wc -l`" != "aa0" ]; then + continue + fi + + + ### replace old guard with new scheme guard + echo "$fn: $old_guard" + + # includes leading whitespace removal + sed -i "s/\s*${old_guard}/ ${guard_prefix}${guard}/g" "$fn" + + + ### clean up endif + sed -i "s/#endif\s*\(\/\/\|\/\*\)\s*\#\?\(ifndef\)\?\s*\(${guard_prefix}${guard}\).*/#endif \/\/ \3/g" "$fn" + + fi +done |