1########################################################################
2#
3# cpp is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 2 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program; see the file COPYING.  If not, write to
15# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16#
17########################################################################
18#
19#  Project      :  File Preprocessor - cpp module
20#  Filename     :  $RCSfile: cpp.pm,v $
21#  Author       :  $Author: darren $
22#  Maintainer   :  Darren Miller: darren@cabaret.demon.co.uk
23#  File version :  $Revision: 1.5 $
24#  Last changed :  $Date: 2003/08/10 22:27:23 $
25#  Description  :  Makes filepp behave similar to cpp
26#  Licence      :  GNU copyleft
27#
28########################################################################
29# THIS IS A FILEPP MODULE, YOU NEED FILEPP TO USE IT!!!
30# usage: filepp -m cpp.pm <files>
31########################################################################
32
33package Cpp;
34
35use strict;
36
37# version number of module
38my $VERSION = '0.3.0';
39
40my $last_file    = "";
41my $last_line    = "";
42my $last_include = -1;
43
44my @SystemIncludes = ("/usr/include");
45{
46    my $include;
47    foreach $include (@SystemIncludes) {
48	Filepp::AddIncludePath($include);
49    }
50}
51
52# This will remove all C and C++ comments (done after AddFileInfo)
53Filepp::Define("REMOVE_C_COMMENTS_FIRST");
54require "c-comment.pm";
55
56########################################################################
57# This function adds cpp style information whenever the current file
58# being processed changes
59########################################################################
60sub AddFileInfo
61{
62    # take in next line
63    my $input = shift;
64    # get name of current file
65    my $current_file    = Filepp::ReplaceDefines("__FILE__");
66    my $current_line    = Filepp::ReplaceDefines("__LINE__");
67    my $current_include = Filepp::ReplaceDefines("__INCLUDE_LEVEL__");
68    # check if file has changed
69    if($current_file ne $last_file) {
70	# gcc cpp flags:
71	# `1' This indicates the start of a new file.
72	# `2' This indicates returning to a file (after having included
73	# another file).
74	# `3' This indicates that the following text comes from a system
75	# header file, so certain warnings should be suppressed.
76	# `4' This indicates that the following text should be treated as C. ?
77	my $flags = "";
78	my $last_flags = "";
79	if($last_file ne "") {
80	    # check for start of new file
81	    if($current_include > $last_include) {
82		$flags = " 1";
83	    }
84	    else { # returning
85		$flags = " 2";
86	    }
87	}
88	my $inc;
89	foreach $inc (@SystemIncludes) {
90	    if($current_file =~ /\A\"$inc/) { $flags = $flags." 3"; }
91	    if($last_file =~ /\A\"$inc/)    { $last_flags = " 3"; }
92	}
93
94	if($last_file ne "") {
95	    Filepp::Write("# ".$last_line." ".$last_file.$last_flags."\n");
96	}
97	Filepp::Write("# ".$current_line." ".$current_file.$flags."\n");
98	$last_file = $current_file;
99    }
100    # updated number of lines processed
101    $last_line = $current_line;
102    $last_include = $current_include;
103    # return the unmodified line
104    return $input;
105}
106Filepp::AddProcessor("Cpp::AddFileInfo", 1);
107Filepp::AddCloseInputFunc("Cpp::AddFileInfo");
108
109# This will quote macros such as __FILE__
110require "cmacros.pm";
111
112# This will allow macros with args to be spread over several lines
113require "blc.pm";
114
115# This will make macro replacement more cpp line
116Filepp::SetWordBoundaries(1);
117
118return 1;
119
120########################################################################
121# End of file
122########################################################################
123