Skip to content

A Walkthrough the Code

Elliot Chance edited this page May 5, 2017 · 4 revisions

This wiki page is a starting point for anyone that is interested in understanding how c2go works internally or are interested in learning the code base to work on issues.

This page is still a work in progress.

Overview

The basic process is this:

  1. The clang program preprocesses the C input file and produces a temporary file, /tmp/pp.c. This file contains a lot more source code because all the of the #include directives have been evaluated.
  2. The clang program is used again to parse the pp.c file and produce an AST. This is the coloured output you will see below.
  3. The clang output (which is text) is converted by the ast package into a real AST.
  4. The transpiler package converts the AST into Go's internal AST.
  5. The Go AST is rendered into Go code.

The Clang AST

The clang program parses C and produces text that represents the internal clang AST, here is an example of the output:

clang output


Random notes:

This text (once the colours have been removed) ...

The first thing to understand is where the input is coming from. c2go does not parse the C code itself, instead it uses clang to parse the C and output an AST. The AST describes C code in a machine readable format.

C2go reads this input and translates the text into its own structure.

The AST is then transpiled into a Go AST.

The Codebase Documentation can be found here.

  • ast - contains the code for parsing the clang AST output and building the AST that will be transpiled. Each file in the package represents a different AST node and has an associated test file.
  • darwin
  • linux
  • noarch
  • transpiler
  • util
Clone this wiki locally