THE RANT / THE SCHPLOG
Schmorp's POD Blog a.k.a. THE RANT
a.k.a. the blog that cannot decide on a name

This document was first published 2016-01-24 14:23:54, and last modified 2016-01-26 05:11:01.

Tidbits - fixing VIM XS syntax highlighting

This is a minor thing - after upgrading my Debian to Jessie, I started noticing that the syntax highlighting for XS files sometimes marked C functions in red, as if it was a syntax error.

I thought this is just one more minor bug: syntax highlighting is difficult, and while VIM is rather good at it, it frequently fails, especially with Perl, and even in cases where it could do better (Perl cannot, in general, by syntax-highlighted perfectly).

However, over time a pattern emerged - memcpy, strlen and so on. I decided to investigate and found that this is deliberate:

syn keyword xsSuperseded atof atol calloc clearerr exit fclose feof ferror
syn keyword xsSuperseded fflush fgetc fgetpos fgets fopen fprintf fputc fputs
syn keyword xsSuperseded fread free freopen fseek fsetpos fwrite getc getenv
syn keyword xsSuperseded isalnum isalpha iscntrl isdigit isgraph islower
syn keyword xsSuperseded isprint ispunct isspace isupper isxdigit malloc
syn keyword xsSuperseded memcpy memmove memset printf putc rand realloc
syn keyword xsSuperseded rewind setenv sprintf srand stderr stdin stdout
syn keyword xsSuperseded strcat strcmp strcpy strdup strlen strncat strncmp
syn keyword xsSuperseded strncpy strstr strtod strtol strtoul system tolower
syn keyword xsSuperseded toupper ungetc

Aha, so the clueless author of the syntax highlighting thinks some C functions are superseded.

It's not hard to spot the pattern - these are all C functions operating on C strings, char * or FILE *, which have a perl equivalent when operating on perl scalars. So the author thinks that C as a language, somehow seems to be superseded, which is of course a fatal mistake - XS is C, so there is no point in somehow ruling out the language.

Otherwise, I cannot imagine where the "superseded" term should originate - sure, for perl scalars, SvCUR is normally the right way get the length of a perl scalar, and Newx is a way to access the perl memory allocator, but sure enough, strlen is still the only correct way to get the length of a C string, and malloc is still the only correct way to get memory that is later freed by free, e.g. in the library you are currently writing an XS interface for. In fact, malloc might be the only legal way to allocate memory in another thread, as Newx is not, in general, thread safe.

Ok, so how do we fix it?

Fixing it is as easy as, oh my, patching the xs.vim syntax highlighting file, as the author didn't make it configurable.

Since I didn't like this approach, I put some overrides in my .vimrc (thanks to Aristoteles Pagaltzis, who provided a better version than the one I used originally):

if &t_Co > 1
   syntax on
   hi link xsSuperseded Normal
endif

Only the hi line is needed, the rest is just there to enable syntax highlighting on colour terminals in general.

The above disables error highlighting for all functions listed in the xsSuperseded group. If you want to keep some (I didn't check whether there are any real superseded functions in there), you will probably have to edit the xs.vim file directly.