C and C++

This section describes some of the advanced features and options that are available in SlickEdit® Core for C and C++, including language-specific formatting options, the C/C++ Beautifier, compiler settings, and preprocessing.

SlickEdit Core's default editing mode for C and C++ allows for programming in either language. If you are coding to strict ANSI C standards, you should configure the value of the macro variable def_ansic_exts to contain a space-delimited list of extensions for files you want interpreted as ANSI C. To set the macro variable, press Esc to bring up the SlickEdit Core command line, then type set-var def_ansic_exts "<extensions>", where <extensions> is the space-delimited list of extensions.

For example:

set-var def_ansic_exts "c h"

Please note that if you also code in C++ and any of these extensions are used for C++, they will be interpreted as ANSI C.

C/C++ Formatting Options

Options are available for C and C++ for changing Syntax Indent and Syntax Expansion styles. To access these options, from the main menu, click Window → SlickEdit Preferences → Languages → Application Languages → C/C++ → Formatting.

Note

Languages similar to C/C++ have similar Formatting Options screens that are not specifically documented.

The tabs on the C/C++ Formatting Options screen are described below.

Begin-End Style Tab

Use this tab to specify the brace style used by Syntax Indent and Syntax Expansion.

The top of the tab lets you choose the style you want to use (Style 1, Style 2, or Style 3). The hyperlink indicates if Adaptive Formatting is on or off for this setting. The remaining options are:

  • Insert braces immediately - When this option is selected, the template is inserted with braces.

  • Insert blank line between braces - When this option is selected, a blank line is inserted between the braces when a template expands with braces.

  • Insert function start brace on new line - When this option is selected, a function start brace is inserted after you press Enter to start a new line.

  • Apply to function braces - When this option is selected, your begin/end style is applied to braces for function definitions.

  • Quick brace/unbrace one line statements - Enables Quick Brace/Unbrace, features that allow you to convert a single line statement to a brace-enclosed block, and vice versa. See Quick Brace/Unbrace for more information.

  • Place "else" on same line as "}" - When this option is selected, SlickEdit® Core places the else keyword on the same line as }. This is typical when using brace Style 1. The following is an example of using Style 1 with an else clause:

    if (i<j) {
    } else {
    }

Indentation Tab

The Indentation tab is used to specify indenting options.

The following options are available:

  • Indent first level of code - When this option is selected, Syntax Indent indents the cursor after declarations such as functions.

  • Use continuation indent on function parameters - When this option is selected, function parameters always use the continuation indent.

    By default, we format multi-line function parameters as follows:

    myLongMethodName(firstarg,
                     secondarg,
                     thirdarg
                    );
    myLongMethodName(
        firstarg,
        secondarg,
        thirdarg
        );
              

    When this option is enabled, the format changes as follows:

    myLongMethodName(firstarg,
        secondarg,
        thirdarg
        );
    myLongMethodName(
        firstarg,
        secondarg,
        thirdarg,
        );
            
  • Indent member access specifier - (C++ only) This option controls Syntax Indent for member access specifiers (public:, private:, and protected: keywords) in C++ classes/structs. The value of this option is controlled by the def_indent_member_access_specifier configuration variable (Macro → Set Macro Variable). When the option is enabled, access specifiers are indented as follows:

    class Foo
    {
    public:
    private:
    protected:
        int value;
    };

    When the option is disabled, access specifiers are aligned along the class, for example:

    class Foo
    {
        public:
        private:
        protected:
              int value;
    };
  • Indent CASE from SWITCH - When this option is selected, Syntax Expansion places the CASE statement indented from the SWITCH statement column. The hyperlink indicates if Adaptive Formatting is on or off for this setting.

  • No space before parenthesis - When this option is selected, no space is placed between keywords (such as if, for, or while) and the open paren when Syntax Expansion occurs. For example, (if( or if (). The hyperlink indicates if Adaptive Formatting is on or off for this setting.

  • Insert padding between parentheses - When this option is selected, a space is placed after the open paren and before the close paren, providing padding for the enclosed text. For example, if () becomes if ( ). The hyperlink indicates if Adaptive Formatting is on or off for this setting.

  • Pointer style - Specify the pointer style you want to use.

Other Tab

The Other tab contains miscellaneous options.

The following option is available:

  • Main style - This option specifies the main function declaration template that is inserted. Select ANSI C/C++ if you want an old ANSI C main declaration inserted. You can define a template by using aliases, or you can write a replacement function for c_insert_main. The command find-proc c_insert_main will locate the macro source for this function.

C/C++ Beautifier

To beautify a C or C++ document, open the file you want to beautify, then from the main menu, click Format → Beautify (or use the gui_beautify command). The C/C++ Beautifier will be displayed, which allows you to make settings for how the code will be beautified.

You can use the commands c_beautify or c_beautify_selection to instantly beautify the file or the selection according to the settings on the Beautifier dialog.

Currently, this beautifier supports beautifying Slick-C® source if the statements are terminated with semicolons like C.

Note

The C#, Java, JavaScript, and Slick-C Beautifiers contain the same options and settings as the C/C++ Beautifier.

The following buttons and settings are available on the Beautifier:

  • Beautify - Beautifies current selection or buffer and closes the dialog box.

  • Reset - Restores the dialog box settings to the values that appeared when you invoked the dialog.

  • Save Settings - Saves beautify options in uformat.ini file. These settings are used by the c_beautify command.

  • Restrict to selection - When this option is selected, only lines in the selection are beautified.

  • Sync extension options - When this option is selected, the language options are updated to reflect any changes that these dialogs have in common. For example, changing the begin-end style to Style 2 will update your brace style for Syntax Expansion.

The tabs on the C/C++ Beautifier are described in the sections below.

Begin-End Style Tab

The Begin-End Style tab of the C/C++ Beautifier is pictured below.

The following options and settings are available:

  • Do not change brace style - Select this option if you do not want your brace style changed. This is useful if you are using a brace style that is not supported by SlickEdit® Core.

  • No space before paren - Determines whether a space is placed between a keyword such asif, for, or while and the open paren.

  • else on same line as } - When this option is selected, the beautifier will place } else on the same line. This is typical when using brace Style 1. The following is an example of using Style 1 with an else clause:

    if (i<j) {
    } else {
    }
  • Apply to function braces - When this option is selected, the beautifier will apply your begin/end style to braces for function definition.

Indenting Tab

The Indenting tab of the C/C++ Beautifier, pictured below, provides indenting parameters that you can use when working with C/C++ files in SlickEdit® Core.

The following options and settings are available:

  • Indent with tabs - When this option is selected, tab characters are used for the leading indent of lines. This value defaults to the Tabs text box setting on the language-specific Indent options screen (see Language-Specific Indent Options).

  • Indent first level of code - Do not clear this check box. When this check box is selected, the first level of code inside a function or method definition is not indented.

  • Indent CASE from SWITCH - When this option is selected, the case and default statements found inside switch statements are indented from the switch.

  • Indent access specifier - When this option is selected, specifiers are indented under the class. When not selected, specifiers are aligned directly underneath the class.

  • Indent for each level (Syntax indent) - The amount to indent for each new nesting level of code. We have put the words "Syntax indent" in parenthesis to help indicate that this field has the same value as the Syntax indent text box on the language-specific Indent options screen (see Language-Specific Indent Options). By default, we initialize this text box with your current language setup setting.

  • Tab size - The value in this field specifies the output tab size. The output tab size is only used if the option Indent with tabs is enabled on the language-specific Indent options screen (see Language-Specific Indent Options). This value defaults to the Syntax indent text box on the Indent options screen.

  • Original tab size - The value in this field specifies the size of the original expansion tab. SlickEdit Core uses the expansion size of your original file to handle reusing indent amounts from your original file. Currently the beautifier only reuses the original source files indenting for comments. This option has no effect if the original file has no tab characters.

  • Continuation indent - The value in this field specifies how much to indent lines of statements that continue to the next line. This has no effect on assignment statements or parenthesized expressions. Lines that are a continuation of an assignment statement are indented after the first equal sign. Lines that are a continuation of a parenthesized expression are indented after the open paren. Given the following example:

    unsigned
    int i;

    The result would be:

    unsigned
    <Continuation Indent>int i;
  • Align on parens - When this option is selected, the text for parenthesized expressions that spans multiple lines is aligned on the first non-blank after the parenthesis or on the parenthesis itself.

  • Align on equal - When this option is selected, the text for multi-line assignment is aligned on the first non-blank after the equals sign (=) or on the equal sign itself.

Comments Tab

The Comments tab on the C/C++ Beautifier, pictured below, contains options for setting the parameters that you want for the trailing comments.

The following options are available:

  • Indent stand alone comments - Indicates whether comments that appear on lines by themselves with no statement text to the left are indented to the current statement indent level. For example:

    /* stand alone
       comment
    */
    // another stand alone comment
    i=1;   // trailing comment
  • Indent column 1 comments - Normally comments that start in column 1 are left alone. Select this option if you want the indent for these comments to be adjusted.

  • Specific column - This text box specifies the column in which trailing comments should be placed. Trailing comments are comments that appear at the end of lines that contain statements or declarations. For example:

    // another stand alone comment
    /* stand alone
       comment
    */
    i=1;   // trailing comment
    if (x) {    /*  trailing 
                    comment.
                */
    }
  • Original absolute column - When this option is selected, trailing comments are placed at the same column as the original source file. Trailing comments are comments that appear at the end of lines that contain statements or declarations.

  • Original relative column - When this option is selected, trailing comments are indented by reusing the indent after the last character of the end of the statement or declaration of the original source file. Trailing comments are comments that are displayed at the end of lines that contain statements or declarations. For example, if the original code is as follows:

    if () {
    i=1;<four characters>//trailing comment
    i=4;<four characters>/* trailing
                            comment.
                         */
    }

    The resulting code would be:

    if () {
       i=1;<four characters>//trailing comment
       i=4;<four characters>/* trailing
           <four characters>   comment.
           <four characters>*/
    }

Other Tab

The Other tab on the C/C++ Beautifier, pictured below, contains the preprocessing and pad condition options.

The following options are available:

  • Indent preprocessing - When this option is selected, the indent before the # character of preprocessing is set to indicate the preprocessing nesting level.

  • Indent inside block - When this option is selected, preprocessing inside brace block is indented when inside preprocessing. Otherwise, preprocessing within a brace block start in column 1.

  • Indent inside special #ifndef - Many C/C++ header files starts with the following lines of code:

    #ifndef myheader_h
    #define myheader_h
     
    #endif

    When this option is selected, preprocessing inside this special #ifndef case is indented.

  • Eat spaces after # - When this option is selected, the spaces after a preprocessor #, but before the keyword (if, ifdef, else, elif, endif, etc.), are removed. This is useful for fixing old C code where the # character had to start in column 1 and spaces were used after the # to indicate the nesting level.

  • Force parens on return - When this option is selected, parentheses are added to return statements which do not have parentheses.

  • Pad condition - These options indicate if parenthesized conditional expressions should have their spacing adjusted.

Schemes Tab

The Schemes tab of the C/C++ Beautifier is pictured below.

To define a new scheme, set the various beautify options, and press the Save Scheme button. User defined schemes are stored in uformat.ini.

C/C++ Compiler Settings

In order to correctly perform full preprocessing, parsing, symbol analysis, and cross-referencing, SlickEdit® Core needs to emulate the implementation-specific parsing behavior of your compiler, including built-in functions, preset #defines, and include directories.

These properties can be specified using the C/C++ Compiler Properties preferences screen. From the main menu, click Window → SlickEdit Preferences → Languages → Application Languages → C/C++ → Compiler Properties.

The interface shows the default compiler and its associated header file and include directories, known collectively as a "configuration". Configurations can be created and modified as needed.

In the Compiler Name drop-down list, select the compiler you wish to use. If this is to be the global default compiler for all projects, click the Set Default button.

Note

It is possible to select other compilers for individual projects. In those cases, the project-specific compiler is used and overrides the global default.

SlickEdit Core ships with header files for each compiler, and the correct header file will appear in the Header File field. The header file configures the parser to emulate the compiler that is chosen in the Compiler Name field.

Creating New Configurations

There are two ways to begin a new configuration. In both cases, a dialog box will be invoked, prompting for the name of the new configuration.

  • Click Copy to copy the selected compiler configuration. This can be used as a template for creating a new configuration and makes the process of creating similar configurations more convenient.

  • Or, click Add to create a configuration from scratch or to add a newly installed compiler.

If you wish to remove the selected compiler and associated configuration from the list, click Delete. This does not delete any files from disk.

Building the Tag File

The Build tag file button on the C/C++ Compiler Properties dialog is used to build tag files from the header file found in the include directories for the selected compiler configuration. This is especially useful when new configurations are created. If you do not build the tag file here manually, it will be built on demand.

C/C++ Preprocessing

Typically your source code base will include preprocessor macros that you use in your code for portability or convenience. For performance considerations, Context Tagging® does not do full preprocessing, so macros that interfere with normal C++ syntax can cause the parser to miss symbols. For example:

   MYNAMESPACEDECL(my)
   struct MYPACKEDMACRO BinaryTree {
      MYTYPELESS data;
      MYPOINTER(BinaryTree) next;
      MYPOINTER(BinaryTree) prev;
   };
   MYPOINTER(BinaryTree) proot = MYNULL;
   MYENDNAMESPACE

This example uses the following preprocessor macros:

   #define MYNAMESPACEDECL(name)  namespace name {
   #define MYPACKEDMACRO          __packed
   #define MTYPELESS              void*
   #define MYPOINTER(t)           t*
   #define MYNULL                 ((void*)0)
   #define MYENDNAMESPACE         }

Among them, the only two that are harmless are MYTYPELESS and MYNULL, because they just create name aliases for types or constants. However, the other four are troublesome and cause the entire code snippet to be unparsable unless you configure SlickEdit® Core to be aware of these preprocessor macros. To do so, complete the following steps:

  1. From the main menu, click Window → SlickEdit Preferences → Languages and expand the Application Languages node in the tree.

  2. Depending on your language, select ANSI-C or C/C++ in the tree, then click C/C++ Preprocessing.

  3. Click New to add new preprocessing macros. Arguments are allowed; for example, mymacro(a,b,c)

  4. When finished, click OK.

  5. A prompt appears asking whether to rebuild your workspace tag file. Click Yes.

Preprocessor macros are stored in usercpp.h, located in your configuration directory. Rather than using the dialog, you can add large numbers of #defines directly to this file. You may want to make sure that your entire development team has an up-to-date copy of this configuration file once you have added all of your local preprocessor macros.

Note

The usercpp.h file should only be used for #defines and #undefs® not #includes.