C and C++

AST: Examples of Normalized C/C++ ASTs

This section shows some sample C statements and their corresponding ASTs.

ASTs are shown in "list" format. Note that CodeSonar transforms some statements into one or more normalized statements with the same meaning. There is one PDG_VERTEX for each such normalized statement, each with its corresponding AST.

C/C++ Statement Normalized Statement Normalized AST (only key fields shown)
j = i + 1;
.
(c:=
    :1 (c:variable :name "j")
    :2 (c:+
        :1 (c:variable :name "i")
        :2 (c:integer-value-32 :value 1)))
j = i + (i * 2);
.
(c:=
    :1 (c:variable :name "j")
    :2 (c:+
        :1 (c:variable :name "i")
        :2 (c:*
            :1 (c:variable :name "i")
            :2 (c:integer-value-32 :value 2))))
k = &i;
.
(c:=
    :1 (c:variable :name "k")
    :2 (c:addr
        :1 (c:variable :name "i")))
j = i++;
$temp1 = i;
i = i + 1;
j = $temp1;
  1. (c:=
        :1 (c:variable :name" $temp1")
        :2 (c:variable :name "i"))
    
  2. (c:=
        :1 (c:variable :name "i")
        :2 (c:+
            :1 (c:variable :name "i")
            :2 (c:integer-value-32
                :value 1)))
    
  3. (c:=
        :1 (c:variable :name "j")
        :2 (c:variable :name "$temp1"))
    
*(k + i) = 1;
.
(c:=
    :1 (c:ptr
        :1 (c:+
            :1 (c:variable :name "k")
            :2 (c:variable :name "i")))
    :2 (c:integer-value-32 :value 1))
k[i] = 1;
*(k + i) = 1;
(c:=
    :1 (c:ptr
        :1 (c:+
            :1 (c:variable :name "k")
            :2 (c:variable :name "i")))
    :2 (c:integer-value-32 :value 1))
      
i = sizeof(unsigned);
i = 4;
(c:=
    :1 (c:variable :name "i")
    :2 (c:integer-value-32
        :value 4 ))
x.i = (short) i & 2;
x.i = (short int) i & 2;
(c:=
    :1 (c:dot
        :1 (c:variable :name "x")
        :2 (c:field :name "i"))
    :2 (c:&
        :1 (c:cast
            :1 (c:integer :integer-kind short)
            :2 (c:variable :name "i"))
        :2 (c:integer-value-32 :value 2)))

More AST Sections

General information:

ASTs Overview of Abstract Syntax Trees in CodeSonar.
AST Patterns Constructs that describe AST properties, for matching.
AST Class Index Index page for the AST class descriptions.
Tutorial Annotated example plug-ins (in each supported API language) that use AST patterns to implement a custom check.

Specific API languages:

C++ classes ast, ast_class, ast_pattern, ast_ordinal, ast_iterator
Python classes ast, ast_class, ast_pattern, ast_ordinal, ast_iterator
C C API: AST Functions and Types