1 1.1 mrg #! /bin/sh 2 1.1 mrg 3 1.1 mrg # (C) 1998, 2007 Free Software Foundation 4 1.1 mrg # Originally by Alexandre Oliva <oliva (at] lsd.ic.unicamp.br> 5 1.1 mrg 6 1.1 mrg # This gawk/shell script is free software; you can redistribute it and/or 7 1.1 mrg # modify it under the terms of the GNU General Public License as published 8 1.1 mrg # by the Free Software Foundation; either version 3, or (at your option) 9 1.1 mrg # any later version. 10 1.1 mrg 11 1.1 mrg # Given a preprocessed C/C++ code snippet, this script will replace any 12 1.1 mrg # standard header files with an actual #include <...> directive. 13 1.1 mrg 14 1.1 mrg # Example: 15 1.1 mrg # # 1 "test.c" 16 1.1 mrg # # 1 "/usr/include/stdio.h" 1 3 17 1.1 mrg # <snip> 18 1.1 mrg # # 1 "test.c" 2 19 1.1 mrg # 20 1.1 mrg # main() { printf("Hello world!\n"); } 21 1.1 mrg 22 1.1 mrg # is replaced with 23 1.1 mrg # # 1 "test.c" 24 1.1 mrg # #include <stdio.h> 25 1.1 mrg # main() { printf("Hello world!\n"); } 26 1.1 mrg 27 1.1 mrg 28 1.1 mrg # Header files whose pathnames contain any of the following patterns 29 1.1 mrg # are considered as standard headers: usr/include, g++-include, 30 1.1 mrg # include/g++, include/c++/<version>, gcc-lib/<anything>/include. 31 1.1 mrg 32 1.1 mrg gawk ${EXCLUDEPATT+-vexclude="$EXCLUDEPATT"} \ 33 1.1 mrg ${INCLUDEPATT+-vinclude="$INCLUDEPATT"} ' 34 1.1 mrg BEGIN { 35 1.1 mrg skipping = 0; 36 1.1 mrg cppline = "^# [0-9]+ \"[^\"]*/(usr/include|g\\+\\+-include|include/g\\+\\+|include/c\\+\\+/[^/]+|gcc-lib/[^\"]+/include|gcc/include)/([^\"]+)\"( [1-4])*$" 37 1.1 mrg } 38 1.1 mrg !skipping && $0 ~ cppline && 39 1.1 mrg (exclude == "" || $3 !~ exclude) && (include == "" || $3 ~ include) { 40 1.1 mrg skipping = 1; 41 1.1.1.2 mrg printf "%s\n", "#include <" gensub(cppline, "\\2", 1, $0) ">" 42 1.1 mrg next; 43 1.1 mrg } 44 1.1 mrg skipping && /^# [0-9]+ / && $3 == lastincluded { 45 1.1 mrg skipping = 0; 46 1.1 mrg next; 47 1.1 mrg } 48 1.1 mrg !skipping && /^# [0-9]+ / { 49 1.1 mrg lastincluded = $3; 50 1.1 mrg } 51 1.1 mrg !skipping { print } 52 1.1 mrg ' ${1+"$@"} 53