Sublime Forum

C/C++ preprocessor sections not correctly colored

#1
  • Using build 3143 on both Linux & Windows
  • new -> set syntax c (or c++) and copy/paste following code:
int foo()
{
#if 0
 return 0; // disabled: OK
#elif 2
 return 0; // disabled: KO
#else
 return 0;  // enabled: KO
#endif
#if 1
 return 0; // enabled: OK
#elif 2
 return 0; // enabled: KO
#else
 return 0; // disabled: OK
#endif
 // from here on, all are enabled (seems too complex for ST)
#define SUBLIME_DEFINED
#ifdef SUBLIME_DEFINED
    return 0;
#else
    return 1;
#endif
#ifdef SUBLIME_NOT_DEFINED
    return 0;
#else
    return 1;
#endif
}

Maybe a feature request once this is correctly handled: be able to force preprocessors on (or off) per project, for the ones that are provided by compilers and thus not in source files.

Thanx for considering this issue :wink:

0 Likes

#2

ST doesn’t do any static code analysis. The so-called enabled/disabled is just a hard-coded 0/1 check.

1 Like

#3

Just to add on to this, it is impossible for Sublime’s current syntax highlighting engine to ever do this. In order to figure out the output of the preprocessor, you basically have to run the preprocessor. That would be incompatible with Sublime’s goal of providing fast syntax highlighting.

0 Likes

#4

Thanx you for for your replies.
In this case, don’t you think an intermediary solution to running the preprocessor could be to have aliases defined ? I mean to be able, in the above mentionned example, to define that SUBLIME_DEFINE is 1, and thus only the corresponding section shall be enabled ?

0 Likes

#5

Not in the syntax highlighting. From a technical standpoint, Sublime’s engine highlights languages defined by nondeterministic context-free grammars (with some restrictions). This can be done with certain efficiency guarantees. Most features of the C preprocessor are at a much higher “level”, and implementing them in syntax highlighting would require a much more powerful parsing engine that can’t meet the efficiency guarantees that we want in a syntax highlighter.

Myself, I have a wish list of improvements to the syntax engine that might let it handle some currently-unparsable constructs while compromising as little as possible on performance and complexity. But the vast majority of the C preprocessor is simply too powerful for a humble syntax highlighting engine to replicate.

0 Likes