jeff-hykin / better-cpp-syntax

๐Ÿ’พ The source of VS Code's C++ syntax highlighting
GNU General Public License v3.0
154 stars 29 forks source link

C syntax highlighting is wrong when a #define contains the beginning of a switch block #603

Closed alexr00 closed 3 months ago

alexr00 commented 2 years ago

Checklist

Does this issue occur when all extensions are disabled?: Yes

Steps to Reproduce:

  1. Create a new file
  2. Set the language to C
  3. Use a #define containing the beginning of a switch block, like in the following example code:
    
    #include <stdio.h>

define DISPATCH() switch (command) {

define INSTRUCTION(i) case (i):

define DISPATCH_END() }

int main(int argc, char* argv[]) { int command, a, b, r; scanf("%d %d %d", &command, &a, &b);

DISPATCH()
INSTRUCTION(0)
    r = a + b;
    break;
INSTRUCTION(1)
    r = a - b;
    break;
INSTRUCTION(2)
    r = a * b;
    break;
INSTRUCTION(3)
    r = a / b;
    break;
default:
    r = a;
DISPATCH_END()

printf("%d\n", r);
return 0;

}


4. The highlighting will be wrong. Most of the code in main should be white, but will instead be blue. The difference can be seen easily by removing the line containing `#define DISPATCH() `....

The code itself is completely valid and has thus far not failed to compile in any C compiler I have tried. As far as I can tell, it is completely standards-compliant ANSI C code.

Removing the left/opening brace from that line fixes the issue only partially. Removing `switch` fixes it completely.

Correct(ish) syntax highlighting (without the `#define DISPATCH()`...)
![image](https://user-images.githubusercontent.com/32158431/186361378-dc7a391d-f395-490d-b083-68c609c1a7fa.png)

Incorrect syntax highlighting (with the `#define DISPATCH()`...)
![image](https://user-images.githubusercontent.com/32158431/186361491-d1dc252e-0c48-4ae8-879e-9fd2a8de9594.png)

Originally from @hisahi in https://github.com/microsoft/vscode/issues/159016
jeff-hykin commented 3 months ago

should be fixed now