aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2022-03-19 23:09:24 -0700
committerKaz Kylheku <kaz@kylheku.com>2022-03-19 23:09:24 -0700
commit86a3448b554e2c9963eed87a43b2db2a8156f842 (patch)
tree70662053a5a39e03e477ad85003626979c68139d
parentb627e525556c4452cb05ce8744e3f21f2bda2eb2 (diff)
downloadcppawk-86a3448b554e2c9963eed87a43b2db2a8156f842.tar.gz
cppawk-86a3448b554e2c9963eed87a43b2db2a8156f842.tar.bz2
cppawk-86a3448b554e2c9963eed87a43b2db2a8156f842.zip
Handle situations when cpp breaks a line into pieces.
There are situations in which GNU cpp breaks a single line of input into multiple lines. These are indicated by linemarkers that repeat the current line number, for instance: # 3 "file" this # 3 "file" is # 3 "file" all line three line four line five We now remove these repeat linemarkers and collapse the indicated lines back into one line, also eliminating the leading whitespace that tries to preserve the column.
-rwxr-xr-xcppawk32
-rw-r--r--testcases9
2 files changed, 38 insertions, 3 deletions
diff --git a/cppawk b/cppawk
index f7d0c33..565a445 100755
--- a/cppawk
+++ b/cppawk
@@ -70,6 +70,31 @@ die()
exit 1
}
+# Eliminate tracer lines in cpp output which continue a previous line, and
+# merge the continued lines into one. Awk needs this because spurious newlines
+# break its syntax.
+collapse()
+{
+ awk '
+ $1 == "#" { if ($2 != curline || $3 != curfile)
+ { print
+ if (line != "")
+ { printf("%s\n", line);
+ line = "" } }
+ startline = $2
+ curline = startline - 1
+ curfile = $3;
+ next }
+ 1 { if (++curline > startline)
+ { if (line != "")
+ { printf("%s\n", line)
+ line = "" } }
+ else
+ { sub(/^[ \t]+/, "") }
+ line = line $0 }
+ END { printf("%s\n", line); }'
+}
+
prepro_opts="$prepro_opts -I$selfdir/cppawk-include"
while [ $# -gt 0 ] ; do
@@ -120,7 +145,8 @@ trap 'rm -f $tmp_file' EXIT INT TERM
if [ -n "$awk_file" ] ; then
tmp_file=$(mktemp)
$delhashbang "$awk_file" | \
- eval '$prepro $incopt"$(dirname "$awk_file")" '"$prepro_opts -" > $tmp_file
+ eval '$prepro $incopt"$(dirname "$awk_file")" '"$prepro_opts -" | \
+ collapse > $tmp_file
[ $prepro_only ] \
&& cat $tmp_file \
|| eval "$awk $awk_opts -f $tmp_file -- \"\$@\""
@@ -128,10 +154,10 @@ elif [ $# -gt 0 ] ; then
tmp_file=$(mktemp)
if [ $prepro_only ] ; then
printf "%s" "$1" | $delhashbang | \
- eval '$prepro $incopt"$(pwd)"'"$prepro_opts -"
+ eval '$prepro $incopt"$(pwd)"'"$prepro_opts - | collapse"
else
printf "%s" "$1" | $delhashbang | \
- eval '$prepro $incopt"$(pwd)" '"$prepro_opts -" > $tmp_file
+ eval '$prepro $incopt"$(pwd)" '"$prepro_opts - | collapse" > $tmp_file
shift
eval "$awk $awk_opts -f $tmp_file -- \"\$@\""
fi
diff --git a/testcases b/testcases
index 091f19d..8b7dc81 100644
--- a/testcases
+++ b/testcases
@@ -161,3 +161,12 @@ abc'def
BEGIN { print __cppawk_ver >= 20220318 }'
:
1
+--
+30:
+./cppawk '
+#include <limits.h>
+BEGIN {
+ print INT_MIN, INT_MIN, INT_MIN
+}'
+:
+-2147483648 -2147483648 -2147483648