blob: 429d3bd4229bf8f351fa1692fcf2b7cfe8319e4c [file] [log] [blame]
Patrick Georgie85e0c7c2010-11-12 09:46:30 +00001#! /bin/sed -nf
2
3# Remove C and C++ comments, by Brian Hiles (brian_hiles@rocketmail.com)
4
5# Sped up (and bugfixed to some extent) by Paolo Bonzini (bonzini@gnu.org)
6# Works its way through the line, copying to hold space the text up to the
7# first special character (/, ", '). The original version went exactly a
8# character at a time, hence the greater speed of this one. But the concept
9# and especially the trick of building the line in hold space are entirely
10# merit of Brian.
11
12# Taken from http://sed.sourceforge.net/grabbag/scripts/remccoms3.sed
13# According to http://sed.sourceforge.net/grabbag/ it's in the public domain
14# Changes:
15# 2010-11-06: Remove strings
16
17:loop
18
19# This line is sufficient to remove C++ comments!
20/^\/\// s,.*,,
21
22# addition for coreboot-lint: For our purpose we don't need strings
23s,"[^"]*",,g
24
25/^$/{
26 x
27 p
28 n
29 b loop
30}
31/^"/{
32 :double
33 /^$/{
34 x
35 p
36 n
37 /^"/b break
38 b double
39 }
40
41 H
42 x
43 s,\n\(.[^\"]*\).*,\1,
44 x
45 s,.[^\"]*,,
Patrick Georgi472efa62012-02-16 20:44:20 +010046
Patrick Georgie85e0c7c2010-11-12 09:46:30 +000047 /^"/b break
48 /^\\/{
49 H
50 x
51 s,\n\(.\).*,\1,
52 x
53 s/.//
54 }
55 b double
56}
57
58/^'/{
59 :single
60 /^$/{
61 x
62 p
63 n
64 /^'/b break
65 b single
66 }
67 H
68 x
69 s,\n\(.[^\']*\).*,\1,
70 x
71 s,.[^\']*,,
Patrick Georgi472efa62012-02-16 20:44:20 +010072
Patrick Georgie85e0c7c2010-11-12 09:46:30 +000073 /^'/b break
74 /^\\/{
75 H
76 x
77 s,\n\(.\).*,\1,
78 x
79 s/.//
80 }
81 b single
82}
83
84/^\/\*/{
85 s/.//
86 :ccom
87 s,^.[^*]*,,
88 /^$/ n
89 /^\*\//{
90 s/..//
91 b loop
92 }
93 b ccom
94}
95
96:break
97H
98x
99s,\n\(.[^"'/]*\).*,\1,
100x
101s/.[^"'/]*//
102b loop