diff --git a/src/backend/nodes/gen_node_support.pl b/src/backend/nodes/gen_node_support.pl
index dca5819f95..6816c36e2b 100644
--- a/src/backend/nodes/gen_node_support.pl
+++ b/src/backend/nodes/gen_node_support.pl
@@ -124,19 +124,31 @@ foreach my $infile (@ARGV)
 	my $supertype_field;
 
 	my $node_attrs = '';
+	my $node_attrs_lineno;
 	my @my_fields;
 	my %my_field_types;
 	my %my_field_attrs;
 
 	open my $ifh, '<', $infile or die "could not open \"$infile\": $!";
 
-	my $file_content = do { local $/; <$ifh> };
+	my $raw_file_content = do { local $/; <$ifh> };
 
-	# strip C comments
-	$file_content =~ s{/\*.*?\*/}{}gs;
+	# strip C comments, preserving newlines so we can count lines correctly
+	my $file_content = '';
+	while ($raw_file_content =~ m{^(.*?)(/\*.*?\*/)(.*)$}s)
+	{
+		$file_content .= $1;
+		my $comment = $2;
+		$raw_file_content = $3;
+		$comment =~ tr/\n//cd;
+		$file_content .= $comment;
+	}
+	$file_content .= $raw_file_content;
 
+	my $lineno = 0;
 	foreach my $line (split /\n/, $file_content)
 	{
+		$lineno++;
 		chomp $line;
 		$line =~ s/\s*$//;
 		next if $line eq '';
@@ -153,13 +165,14 @@ foreach my $infile (@ARGV)
 				$is_node_struct = 0;
 				$supertype      = undef;
 				next if $line eq '{';
-				die "$infile:$.: expected opening brace\n";
+				die "$infile:$lineno: expected opening brace\n";
 			}
 			# second line could be node attributes
 			elsif ($subline == 2
 				&& $line =~ /^\s*pg_node_attr\(([\w(), ]*)\)$/)
 			{
-				$node_attrs = $1;
+				$node_attrs        = $1;
+				$node_attrs_lineno = $lineno;
 				# hack: don't count the line
 				$subline--;
 				next;
@@ -236,7 +249,7 @@ foreach my $infile (@ARGV)
 						else
 						{
 							die
-							  "$infile:$.: unrecognized attribute \"$attr\"\n";
+							  "$infile:$node_attrs_lineno: unrecognized attribute \"$attr\"\n";
 						}
 					}
 
@@ -330,7 +343,9 @@ foreach my $infile (@ARGV)
 					# strip space between type and "*" (pointer) */
 					$type =~ s/\s+\*$/*/;
 
-					die if $type eq '';
+					die
+					  "$infile:$lineno: cannot parse data type in \"$line\"\n"
+					  if $type eq '';
 
 					my @attrs;
 					if ($attrs)
@@ -347,7 +362,7 @@ foreach my $infile (@ARGV)
 							  )
 							{
 								die
-								  "$infile:$.: unrecognized attribute \"$attr\"\n";
+								  "$infile:$lineno: unrecognized attribute \"$attr\"\n";
 							}
 						}
 					}
@@ -362,7 +377,7 @@ foreach my $infile (@ARGV)
 			{
 				if ($is_node_struct)
 				{
-					#warn "$infile:$.: could not parse \"$line\"\n";
+					#warn "$infile:$lineno: could not parse \"$line\"\n";
 				}
 			}
 		}
@@ -552,7 +567,7 @@ _equal${n}(const $n *a, const $n *b)
 			my $tt = $1;
 			if (!defined $array_size_field)
 			{
-				die "no array size defined for $n.$f of type $t";
+				die "no array size defined for $n.$f of type $t\n";
 			}
 			if ($node_type_info{$n}->{field_types}{$array_size_field} eq
 				'List*')
@@ -597,7 +612,8 @@ _equal${n}(const $n *a, const $n *b)
 		}
 		else
 		{
-			die "could not handle type \"$t\" in struct \"$n\" field \"$f\"";
+			die
+			  "could not handle type \"$t\" in struct \"$n\" field \"$f\"\n";
 		}
 	}
 
@@ -814,7 +830,7 @@ _read${n}(void)
 			}
 			if (!defined $array_size_field)
 			{
-				die "no array size defined for $n.$f of type $t";
+				die "no array size defined for $n.$f of type $t\n";
 			}
 			if ($node_type_info{$n}->{field_types}{$array_size_field} eq
 				'List*')
@@ -886,7 +902,8 @@ _read${n}(void)
 		}
 		else
 		{
-			die "could not handle type \"$t\" in struct \"$n\" field \"$f\"";
+			die
+			  "could not handle type \"$t\" in struct \"$n\" field \"$f\"\n";
 		}
 
 		# for read_as() without read_write_ignore, we have to read the value
