Conditional Parsing and Preprocessor Support

This section discusses features and issues that apply mainly to C/C++ and C# language files that use a prepro­cessor and have #if or #ifdef statements.

Working with Inactive Code - ifdef Support

Source Insight can recognize inactive blocks of code that are disabled at compile-time with #ifdef, #if, and #elif directives in languages that support those statements. There are project-specific and global lists of conditional identifier values. To edit the conditional constant values, select Options > Preferences: Language and click on either "Project Specific Conditions", or "Global Conditions". See: Conditional Parsing List. You can also edit the conditions using the Edit Condition command on the right-click menu of a source window. See: Edit Condition.

Conditional statements such as #if and #ifdef are only interpreted when all the condition values are speci­fied in the Edit Conditions dialog box.

By default, Source Insight ignores the conditional directives altogether. It attempts to make sense of all branches in a conditional compilation construct. Often, this works well because declarations in the condi­tional branches do not interfere with each other.

However, sometimes a tricky declaration may be broken in the middle with an #ifdef. This will often confuse Source Insight. For example:

void DoThing(

   int param1,

#ifdef ABC

   int param2)


   int param2, param3)


If you are not interested in code that is inactive, you can specify condition values. See: Conditional Parsing.

Inactive code is formatted with the Inactive Code style.

Blocks of code that are inactive are displayed in the "Inactive Code" style. For example:



Figure 3.2  The "Inactive Code" style is displayed.

Conditional Parsing

Conditional parsing applies only to languages that support conditional compilation in Source Insight: C/C++, C#, etc. See: Edit Condition.

Source Insight maintains two types of condition variable lists.

The two condition lists are combined when Source Insight parses a file. The project-specific conditions take precedence over the global conditions.

Condition Variables

Condition variables can be used in expressions in #if, #ifdef, #ifndef, and #elif statements. These are typically constant values defined in a header file, or on the compiler command line. For example:

#if VER < 3 && DEF_OPEN != 0


In this example, two condition variables are used: VER and DEF_OPEN. Each variable value can be specified using the Edit Condition command. See: Edit Condition.

Each condition variable can have any textual value. As in C and C++, any numerical value that equals zero is considered "False", and any non-zero value is "True".   

Ignoring Condition Variables

If you do not specify a variable's value, then any preprocessor statement that includes that variable is skipped and simply ignored. This is the default behavior for any #if-type statement in Source Insight. If the condition is not defined using the Edit Condition dialog, then Source Insight ignore the conditional and all branches of the #if-#else block are parsed by Source Insight.

For example:

#if VER < 3 && WINVER >= 5

   int a = 1;


   int a = 2;


If both VER and WINVER are defined using Edit Condition, then the expression in the #if statement will be evaluated, and only one of the branches will be active. However, if either of those variables are not defined in Source Insight, then both branches will be active.

Editing the Condition Variables

To edit the value of a conditional variable, right-click on it and select Edit Condition. See: Edit Condition. When you edit the condition list, Source Insight will ask you if you want to re-parse your whole project. You should make all your changes to the condition list first, and then re-parse your whole project. Until your project is re-parsed, the symbol information stored in Source Insight's symbol database will not reflect the changes you made.