Martin Roth | 0ad5fbd | 2020-12-24 12:06:38 -0700 | [diff] [blame] | 1 | #!/usr/bin/env sed -nf |
Patrick Georgi | e85e0c7c | 2010-11-12 09:46:30 +0000 | [diff] [blame] | 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 |
| 23 | s,"[^"]*",,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 Georgi | 472efa6 | 2012-02-16 20:44:20 +0100 | [diff] [blame] | 46 | |
Patrick Georgi | e85e0c7c | 2010-11-12 09:46:30 +0000 | [diff] [blame] | 47 | /^"/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 Georgi | 472efa6 | 2012-02-16 20:44:20 +0100 | [diff] [blame] | 72 | |
Patrick Georgi | e85e0c7c | 2010-11-12 09:46:30 +0000 | [diff] [blame] | 73 | /^'/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 |
| 97 | H |
| 98 | x |
| 99 | s,\n\(.[^"'/]*\).*,\1, |
| 100 | x |
| 101 | s/.[^"'/]*// |
| 102 | b loop |