JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
C and C++


Authoring Compiler Models

CodeSonar ships with compiler models for many industry standard compilers. In cases where none of these models are suitable, a custom compiler model may be required.

Compiler models can be authored in C++ or Python.

Note: STk compiler models are no longer supported. If you have previously implemented a custom STk compiler model and need assistance converting it to C++ or Python, contact CodeSecure.



Do you need a custom compiler model?

In most cases you will not need a custom compiler model. Before reading the rest of this section, check the following to see it that there is a simpler solution for your build.

You do not need a custom compiler model if:

CodeSonar recognizes your compiler and builds the project correctly.
In this case you don't need to do anything special.
Your compiler is based on an existing model but is named differently:
Specify which of the existing models CodeSonar should use, as described in section Mapping Compiler Names to Models.

Even if your compiler is not directly based on any of the existing models, it may be similar enough to an existing model to work with no modification. Therefore, you should try mapping your compiler name to each model in turn to see if the build works before resorting to a custom model. Try the gcc model first: it is the most flexible, versatile, and tolerant.

Your compiler is based on an existing model but you need to prepend or append flags to the command line.
Specify the flags using CFLAGS_PREPEND and CFLAGS_APPEND in the configuration file.

You need a custom compiler model if:

There is no existing compiler model for your compiler.
The available compiler models are listed in section Compiler Models.
Your build system has requirements that are not covered by the compiler defaults.
(And cannot be accommodated by appending command line flags.)

If your project compiles correctly under your normal build system but you get errors when you run the CodeSonar build system, you may need to modify the compiler model.

Overview

The compiler model is responsible for obtaining all relevant information from a native compiler invocation and translating this information to individual compilation units for the CodeSonar front end.

Typical tasks for a compiler model include the following.

The process for creating a new compiler model can be divided into five steps.

The remainder of this page describes these steps, with links to the sections about authoring compiler models in C++ and authoring compiler models in Python when the details are language-specific.

Note

In this section (and throughout this manual), $CSONAR indicates the CodeSonar installation directory.

A. Determine Behaviors

Compiler models need to account for the following. The compiler should have documentation that provides the necssary information.

You will also need to choose a name for your compiler model. This is the name that you will use in COMPILER_MODELS rules for mapping compiler executables to this model. The model name can be the same as the compiler executable name (without file extension), but does not have to be.

B. Implement the Model

You can implement the model in either C++ or Python. Detailed implementation information is provided in separate sections:

C++ Implementing a compiler model in C++
Python Implementing a compiler model in Python

In both cases, compiler models are implemented as classes that must must...

Illustrated example compiler models are provided:

C. Build (if necessary) and Install

Compiler models implemented in C++ must be built into shared libraries before installation; models implemented in Python are installed directly. In both cases, you will also need to set up one or more COMPILER_MODELS rules to map the relevant native compiler executables to your model.

C++ Build and Install
Python Installing the Model

D. Create Configuration Templates

When you use your new model to build a CodeSonar project, CodeSonar will load a corresponding project-compiler configuration file to control aspects of the process, creating it if necessary.

The process for creating a new compiler model therefore includes establishing whether or not you need to provide specific compiler template configuration files, and implementing them if necessary.

For a new compiler model called cmname:

  1. Evaluate the configuration file needs for cmname.
    1. What languages L can be compiled with the model?
    2. What are the possible target sizes S?
    3. [If applicable] Will different ABI keys A correspond to different configuration needs?
    4. With respect to your answers to the previous questions, what compiler template configuration files cmname[.A].L.S will you need to provide?
    For example, suppose cmname only compiles C++ (not C), and can target both 32-bit and 64-bit architectures, and that you don't intend to use ABI keys to designate specific configuration needs. Then we need two compiler template configuration files.
    C++ cmname.c++.32.conf and cmname.c++.64.conf.
    Python pya_cmname.c++.32.conf and pya_cmname.c++.64.conf.
  2. Once you have determined which language/size/ABI key combinations you need to address, create initial compiler template configuration files by copying the corresponding project-compiler defaults.
    For example:
    C++
    cp $CSONAR/csurf/compiler_confs/unknown.c++.32.conf $CSONAR/csurf/compiler_confs/cmname.c++.32.conf
    cp $CSONAR/csurf/compiler_confs/unknown.c++.64.conf $CSONAR/csurf/compiler_confs/cmname.c++.64.conf
    Python
    cp $CSONAR/csurf/compiler_confs/unknown.c++.32.conf $CSONAR/csurf/compiler_confs/pya_cmname.c++.32.conf
    cp $CSONAR/csurf/compiler_confs/unknown.c++.64.conf $CSONAR/csurf/compiler_confs/pya_cmname.c++.64.conf
  3. Inspect your new templates and make any required changes and additions.
    Note that you may need to modify the templates further if testing reveals any issues with your model. In particular, you may wish to specify one or more of the following.

E. Test the Model

Suppose you have created a model mycompilermodel for native compiler mycompiler, where the path to the native compiler executable is path/to/compilers/mycomp/bin/mycompiler. To test the model, proceed as follows.

  1. Create a sample source file testcm.cpp for testing. The source file should:
  2. Choose a set of command arguments to test. We will refer to this set as <testflags> in the next step. Note that there may be some flags that you need to include every time. For example, if your compiler uses a flag (such as -c) to indicate a file for compilation, you will always need to include that flag.
  3. On a command line, run
    rm -rf CMT*
    then
    codesonar compile -TCMT -CCMT -invoke-compiler no -transparent yes
    -compiler-location /path/to/compilers/mycomp/bin/mycompiler mycompilermodel
    -conf-file myproject.CSURF_COMPILER_CONF_MARKER <testflags> testcm.cpp
  4. Examine the output from the codesonar compile command.

    If there are parse errors, then the compiler model is doing something wrong. Use the error messages to determine what constructs are being handled incorrectly, and amend your model (or configuration templates) to address the problems.

  5. Repeat from step 2) with a new set of <testflags>, continuing until you are satisfied that you have tested the model thoroughly.

 

 

To report problems with this documentation, please visit https://support.codesecure.com/.