19

In my project there's a file enclosed in an ifdef preprocessor directive

#ifdef SOME_SYMBOL
... entire file ...
#endif

SOME_SYMBOL is defined by another file that's compiled before this one, and the code works as expected, but the static analyzer isn't aware of this symbol and so it treats SOME_SYMBOL is undefined. The entire file has no syntax highlighting and some of the analysis is just skipped (e.g. syntax error highlighting).

Is there a way to tell the analyzer to treat this symbol as defined without defining it in CMakeLists.txt?

I don't have the option of defining SOME_SYMBOL in CMakeLists.txt since the project depends on it being undefined in some compilation paths (changing this would be near impossible).

Update:
Seems like this is currently an open issue with JetBrains. See Issue CPP-2286

Neowizard
  • 2,981
  • 1
  • 21
  • 39
  • 1
    There is a chance that you need find the suitable inclusion context for the file. If you have visible status bar, you can see the 'Context:' drop down list in the right-bottom corner. Just try some. – uta Feb 03 '16 at 22:48

3 Answers3

6

Clion now has a macro which you can use to detect the IDE: https://youtrack.jetbrains.com/issue/CPP-1296#comment=27-1846360

#ifdef __JETBRAINS_IDE__
    // Stuff that only clion will see goes here
#endif

This allows you to put in defines to make clion render your code properly in cases where it can't be clever enough to figure it out.

The __JETBRAINS_IDE__ macro's value is a version string for the IDE. Specific versions of the macro exist for different Jetbrains IDEs: __CLION_IDE__, __STUDIO_IDE__ (for Android Studio), and __APPCODE_IDE__ (for AppCode).

Yay!

Chris Kitching
  • 2,559
  • 23
  • 37
4

To get syntax highlighting: Go to Settings ⇒ Editor ⇒ Colors&Fonts ⇒ C/C++ and remove all ticks for 'Conditionally non-compiled code'. This way all code will show up with the usual highlighting.

toothstone
  • 41
  • 3
  • Thanks, but I can't find any checkbox with a caption resembling "Conditionally non-compiled code". – Neowizard Mar 26 '16 at 11:38
  • Thanks, this works - it's just not displayed in the preview that highlighting is restored for these blocks. – Stecman Nov 08 '17 at 21:51
  • 3
    For the most recent 2020 CLion it is: Settings / Editor / Color_Scheme / C/C++: Remove 'Foreground' tick of 'Conditionally non-compiled code' – robsn May 19 '20 at 07:16
  • @robsn thanks, that works. However, IDE still does not let you to navigate through this code. Notably, you cannot ctrl + left click on contents. It's not clickable – SergeiK Apr 12 '21 at 10:10
0

The task has no solution for common case. But! You can find the target and related resolve context, where SOME_SYMBOL is defined.

...in the status bar you can find the Resolve Context chooser for switching between the Debug, Release, RelWithDebInfo and MinSizeRel contexts to resolve your code in the IDE with the desired definitions.

uta
  • 1,451
  • 10
  • 12