Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Fix C++ synatx highlighting error in macros with extern "C" #271

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sryze
Copy link

@sryze sryze commented Apr 11, 2018

Description of the Change

Update the C++ grammar to fix syntax highlighting of extern "C" inside macros.

Example code:

#ifdef __cplusplus
    #define FOO_EXTERN extern "C"
#else
    #define FOO_EXTERN extern
#endif

Before this change:

before

After:

after

The two lines where I added else and elif are not strictly necessary for this patch to work, but since there is an endif I thought I was a good idea to have them there too. I'm not entirely sure where endif is there to be honest, this rule seems to work well without it.

@sryze
Copy link
Author

sryze commented Apr 12, 2018

I realized that this doesn't fix the bug - instead of matching meta.extern-block.cpp the extern "C" part now matches storage.modifier.c, i.e. same as plain extern (therefore the broken tests).

Including those elif and else actually helped a little, but didn't fix the issue. I checked how the "C" part affects the output of the tokenizer and found that it parses #else as invalid.illegal.stray-else.c, i.e. as far as I understand it can't find a matching #if.

[{
        value: '#',
        scopes: ['source.cpp', 'meta.preprocessor.macro.c', 'keyword.control.directive.define.c', 'punctuation.definition.directive.c']
    }, {
        value: 'define',
        scopes: ['source.cpp', 'meta.preprocessor.macro.c', 'keyword.control.directive.define.c']
    }, {
        value: ' ',
        scopes: ['source.cpp', 'meta.preprocessor.macro.c']
    }, {
        value: 'EXTERN',
        scopes: ['source.cpp', 'meta.preprocessor.macro.c', 'entity.name.function.preprocessor.c']
    }, {
        value: ' ',
        scopes: ['source.cpp', 'meta.preprocessor.macro.c']
    }, {
        value: 'extern',
        scopes: ['source.cpp', 'meta.preprocessor.macro.c', 'meta.extern-block.cpp', 'storage.modifier.cpp']
    }, {
        value: ' ',
        scopes: ['source.cpp', 'meta.preprocessor.macro.c', 'meta.extern-block.cpp']
    }, {
        value: '"',
        scopes: ['source.cpp', 'meta.preprocessor.macro.c', 'meta.extern-block.cpp', 'string.quoted.double.cpp', 'punctuation.definition.string.begin.cpp']
    }, {
        value: 'C',
        scopes: ['source.cpp', 'meta.preprocessor.macro.c', 'meta.extern-block.cpp', 'string.quoted.double.cpp']
    }, {
        value: '"',
        scopes: ['source.cpp', 'meta.preprocessor.macro.c', 'meta.extern-block.cpp', 'string.quoted.double.cpp', 'punctuation.definition.string.end.cpp']
    }]

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant