Using the C frontend Clang
In this recipe, you will get to know how the Clang frontend can be used for different purposes.
Getting ready
You will need Clang tool.
How to do it…
Clang can be used as the high-level compiler driver. Let us show it using an example:
- Create a
hello world
C code,test.c
:$ cat test.c #include<stdio.h> int main() { printf("hello world\n"); return 0; }
- Use Clang as a compiler driver to generate the executable
a.out
file, which on execution gives the output as expected:$ clang test.c $ ./a.out hello world
Here the
test.c
file containing C code is created. Using Clang we compile it and produce an executable that on execution gives the desired result. - Clang can be used in preprocessor only mode by providing the
–E
flag. In the following example, create a C code having a #define directive defining the value of MAX and use this MAX as the size of the array you are going to create:$ cat test.c #define MAX 100 void func() { int a[MAX]; }
- Run the preprocessor using the following command, which gives the output on standard output:
$ clang test.c -E # 1 "test.c" # 1 "<built-in>" 1 # 1 "<built-in>" 3 # 308 "<built-in>" 3 # 1 "<command line>" 1 # 1 "<built-in>" 2 # 1 "test.c" 2 void func() { int a[100]; }
In the
test.c
file, which will be used in all the subsequent sections of this recipe, MAX is defined to be100
, which on preprocessing is substituted to MAX ina[MAX]
, which becomesa[100]
. - You can print the AST for the
test.c
file from the preceding example using the following command, which displays the output on standard output:| $ clang -cc1 test.c -ast-dump TranslationUnitDecl 0x3f72c50 <<invalid sloc>> <invalid sloc>|-TypedefDecl 0x3f73148 <<invalid sloc>> <invalid sloc> implicit __int128_t '__int128'|-TypedefDecl 0x3f731a8 <<invalid sloc>> <invalid sloc> implicit __uint128_t 'unsigned __int128'|-TypedefDecl 0x3f73518 <<invalid sloc>> <invalid sloc> implicit __builtin_va_list '__va_list_tag [1]'`-FunctionDecl 0x3f735b8 <test.c:3:1, line:5:1> line:3:6 func 'void ()'`-CompoundStmt 0x3f73790 <col:13, line:5:1>`-DeclStmt 0x3f73778 <line:4:1, col:11>`-VarDecl 0x3f73718 <col:1, col:10> col:5 a 'int [100]'
Here, the
–cc1
option ensures that only the compiler front-end should be run, not the driver, and it prints the AST corresponding to thetest.c
file code. - You can generate the LLVM assembly for the
test.c
file in previous examples, using the following command:|$ clang test.c -S -emit-llvm -o - |; ModuleID = 'test.c' |target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" |target triple = "x86_64-unknown-linux-gnu" | |; Function Attrs: nounwind uwtable |define void @func() #0 { |%a = alloca [100 x i32], align 16 |ret void |}
The
–S
and–emit-llvm
flag ensure the LLVM assembly is generated for thetest.c
code. - To get machine code use for the same
test.c
testcode, pass the–S
flag to Clang. It generates the output on standard output because of the option–o –
:|$ clang -S test.c -o - | .text | .file "test.c" | .globl func | .align 16, 0x90 | .type func,@function |func: # @func | .cfi_startproc |# BB#0: | pushq %rbp |.Ltmp0: | .cfi_def_cfa_offset 16 |.Ltmp1: | .cfi_offset %rbp, -16 | movq %rsp, %rbp |.Ltmp2: | .cfi_def_cfa_register %rbp | popq %rbp | retq |.Ltmp3: | .size func, .Ltmp3-func | .cfi_endproc
When the –S
flag is used alone, machine code is generated by the code generation process of the compiler. Here, on running the command, machine code is output on the standard output as we use –o –
options.
How it works...
Clang works as a preprocessor, compiler driver, frontend, and code generator in the preceding examples, thus giving the desired output as per the input flag given to it.
See also
- This was a basic introduction to how Clang can be used. There are also many other flags that can be passed to Clang, which makes it perform different operation. To see the list, use Clang
–help
.