Pular para o conteúdo

Clang/LLVM - Infraestrutura Moderna de Compiladores

```bash

Ubuntu/Debian installation

sudo apt update sudo apt install clang llvm

Install specific version

sudo apt install clang-15 llvm-15 sudo apt install clang-16 llvm-16 sudo apt install clang-17 llvm-17

Install complete LLVM toolchain

sudo apt install clang llvm lld lldb

Install development packages

sudo apt install clang-tools clang-format clang-tidy

CentOS/RHEL/Fedora installation

sudo dnf install clang llvm sudo dnf install clang-tools-extra

macOS installation (Xcode includes Clang)

xcode-select —install

Or using Homebrew

brew install llvm brew install clang-format

Windows installation

Download from https://releases.llvm.org/

Or using Chocolatey

choco install llvm

Or using vcpkg

vcpkg install llvm

Verify installation

clang —version clang++ —version llvm-config —version

Check available tools

ls /usr/bin/clang* ls /usr/bin/llvm*

Set default version (Ubuntu/Debian)

sudo update-alternatives —install /usr/bin/clang clang /usr/bin/clang-15 100 sudo update-alternatives —install /usr/bin/clang++ clang++ /usr/bin/clang++-15 100

```bash
# Environment variables
export CC=clang
export CXX=clang++
export CFLAGS="-O2 -Wall"
export CXXFLAGS="-O2 -Wall -std=c++17"

# LLVM-specific environment
export LLVM_CONFIG=/usr/bin/llvm-config
export LLVM_DIR=/usr/lib/llvm-15

# Library and include paths
export LD_LIBRARY_PATH=/usr/lib/llvm-15/lib:$LD_LIBRARY_PATH
export C_INCLUDE_PATH=/usr/lib/llvm-15/include:$C_INCLUDE_PATH

# Check Clang configuration
clang -v
clang -print-targets
clang -print-supported-cpus

# Show default include paths
clang -E -v - </dev/null
clang++ -E -v - </dev/null

# Show default library paths
clang -print-search-dirs

# Show built-in defines
clang -dM -E - </dev/null

# LLVM configuration
llvm-config --version
llvm-config --prefix
llvm-config --bindir
llvm-config --libdir
llvm-config --includedir
llvm-config --cppflags
llvm-config --ldflags
llvm-config --libs
```## Instalação e Configuração
```bash
# Compile C program
clang hello.c -o hello
clang -o hello hello.c

# Compile C++ program
clang++ hello.cpp -o hello
clang++ -o hello hello.cpp

# Compile without linking (object file only)
clang -c source.c
clang++ -c source.cpp

# Compile multiple source files
clang file1.c file2.c file3.c -o program
clang++ file1.cpp file2.cpp file3.cpp -o program

# Link object files
clang file1.o file2.o file3.o -o program

# Compile with specific C standard
clang -std=c99 program.c -o program
clang -std=c11 program.c -o program
clang -std=c17 program.c -o program

# Compile with specific C++ standard
clang++ -std=c++11 program.cpp -o program
clang++ -std=c++14 program.cpp -o program
clang++ -std=c++17 program.cpp -o program
clang++ -std=c++20 program.cpp -o program
clang++ -std=c++2b program.cpp -o program

# Target-specific compilation
clang -target x86_64-linux-gnu program.c -o program
clang -target arm64-apple-macos program.c -o program
clang -target x86_64-pc-windows-msvc program.c -o program.exe

# Generate assembly output
clang -S source.c                    # Creates source.s
clang -S -o output.s source.c

# Generate LLVM IR
clang -S -emit-llvm source.c         # Creates source.ll
clang -c -emit-llvm source.c         # Creates source.bc (bitcode)

# Generate preprocessed output
clang -E source.c                    # Output to stdout
clang -E source.c -o source.i        # Save to file

# Verbose compilation
clang -v source.c -o program

# Show compilation commands
clang -### source.c -o program
```### Instalando Clang/LLVM em Diferentes Plataformas
```bash
# Generate LLVM IR (human-readable)
clang -S -emit-llvm source.c -o source.ll

# Generate LLVM bitcode (binary)
clang -c -emit-llvm source.c -o source.bc

# Optimize LLVM IR
opt -O2 source.ll -o optimized.ll
opt -O3 source.bc -o optimized.bc

# Analyze LLVM IR
llvm-dis source.bc -o source.ll      # Convert bitcode to IR
llvm-as source.ll -o source.bc       # Convert IR to bitcode

# Link LLVM bitcode files
llvm-link file1.bc file2.bc -o combined.bc

# Generate assembly from LLVM IR
llc source.ll -o source.s
llc source.bc -o source.s

# Generate object file from LLVM IR
llc -filetype=obj source.ll -o source.o

# Execute LLVM IR directly
lli source.ll
lli source.bc

# Profile LLVM IR execution
lli -stats source.ll
```### Configuração do LLVM e Ambiente
```bash
# No optimization (default)
clang -O0 source.c -o program

# Basic optimization
clang -O1 source.c -o program

# Standard optimization (recommended)
clang -O2 source.c -o program

# Aggressive optimization
clang -O3 source.c -o program

# Optimize for size
clang -Os source.c -o program
clang -Oz source.c -o program        # More aggressive size optimization

# Optimize for debugging
clang -Og source.c -o program

# Fast math optimizations
clang -O2 -ffast-math source.c -o program

# Link-time optimization (LTO)
clang -O2 -flto source1.c source2.c -o program

# Thin LTO (faster than full LTO)
clang -O2 -flto=thin source1.c source2.c -o program

# Profile-guided optimization
# Step 1: Compile with profiling
clang -O2 -fprofile-instr-generate source.c -o program
./program  # Run with representative input
llvm-profdata merge -output=default.profdata default.profraw
# Step 2: Compile with profile data
clang -O2 -fprofile-instr-use=default.profdata source.c -o program_optimized

# Auto-vectorization
clang -O2 -fvectorize source.c -o program
clang -O2 -fslp-vectorize source.c -o program

# Loop optimizations
clang -O2 -funroll-loops source.c -o program
```## Compilação Básica
```bash
# Architecture-specific optimization
clang -O2 -march=native source.c -o program
clang -O2 -march=skylake source.c -o program
clang -O2 -march=armv8-a source.c -o program

# CPU-specific tuning
clang -O2 -mtune=generic source.c -o program
clang -O2 -mtune=intel source.c -o program

# Show optimization remarks
clang -O2 -Rpass=loop-vectorize source.c -o program
clang -O2 -Rpass-missed=loop-vectorize source.c -o program
clang -O2 -Rpass-analysis=loop-vectorize source.c -o program

# Save optimization remarks to file
clang -O2 -fsave-optimization-record source.c -o program

# Polyhedral optimization (Polly)
clang -O3 -mllvm -polly source.c -o program
clang -O3 -mllvm -polly -mllvm -polly-vectorizer=stripmine source.c -o program

# Control flow integrity
clang -O2 -fsanitize=cfi source.c -o program

# Specific optimization passes
clang -O2 -mllvm -enable-loop-distribute source.c -o program
clang -O2 -mllvm -enable-load-pre source.c -o program

# Disable specific optimizations
clang -O2 -fno-vectorize source.c -o program
clang -O2 -fno-unroll-loops source.c -o program
clang -O2 -fno-inline source.c -o program
```### Comandos Simples de Compilação
```bash
# Basic warnings
clang -Wall source.c -o program

# Extra warnings
clang -Wextra source.c -o program

# All warnings
clang -Weverything source.c -o program

# Treat warnings as errors
clang -Werror source.c -o program

# Specific warning categories
clang -Wpedantic source.c -o program
clang -Wconversion source.c -o program
clang -Wshadow source.c -o program
clang -Wunused source.c -o program
clang -Wuninitialized source.c -o program
clang -Wformat source.c -o program
clang -Wcast-align source.c -o program
clang -Wwrite-strings source.c -o program

# Clang-specific warnings
clang -Wdocumentation source.c -o program
clang -Wthread-safety source.c -o program
clang -Wloop-analysis source.c -o program
clang -Wstring-conversion source.c -o program

# Disable specific warnings
clang -Wall -Wno-unused-variable source.c -o program
clang -Weverything -Wno-padded source.c -o program

# C++-specific warnings
clang++ -Wall -Wc++11-compat source.cpp -o program
clang++ -Wall -Wc++14-compat source.cpp -o program
clang++ -Wall -Wc++17-compat source.cpp -o program

# Show all available warnings
clang -Weverything -fsyntax-only source.c 2>&1|grep "warning:"
```### Representação Intermediária do LLVM
```bash
# Colorized output
clang -fcolor-diagnostics source.c -o program

# Show column numbers
clang -fshow-column source.c -o program

# Show source ranges
clang -fshow-source-location source.c -o program

# Caret diagnostics
clang -fcaret-diagnostics source.c -o program

# Fix-it hints
clang -fdiagnostics-fixit-info source.c -o program

# Template instantiation backtrace
clang++ -ftemplate-backtrace-limit=0 source.cpp -o program

# Macro expansion in diagnostics
clang -fmacro-backtrace-limit=0 source.c -o program

# Diagnostic format options
clang -fdiagnostics-format=clang source.c -o program
clang -fdiagnostics-format=msvc source.c -o program

# Show option that triggered warning
clang -fdiagnostics-show-option source.c -o program

# Syntax-only check
clang -fsyntax-only source.c

# Static analysis
clang --analyze source.c
clang --analyze -Xanalyzer -analyzer-output=html source.c
```## Opções de Otimização
```bash
# Generate debug information
clang -g source.c -o program

# Different debug levels
clang -g0 source.c -o program        # No debug info
clang -g1 source.c -o program        # Minimal debug info
clang -g2 source.c -o program        # Default debug info
clang -g3 source.c -o program        # Maximum debug info

# Debug format options
clang -gdwarf-4 source.c -o program
clang -gdwarf-5 source.c -o program

# Debug with optimization
clang -O2 -g source.c -o program

# Standalone debug info
clang -g -gsplit-dwarf source.c -o program

# Compressed debug sections
clang -g -gz source.c -o program

# Debug line tables only
clang -gline-tables-only source.c -o program

# Emit debug info for used types only
clang -g -feliminate-unused-debug-types source.c -o program
```### Níveis de Otimização
```bash
# Address sanitizer (detects memory errors)
clang -g -fsanitize=address source.c -o program

# Memory sanitizer (detects uninitialized reads)
clang -g -fsanitize=memory source.c -o program

# Thread sanitizer (detects data races)
clang -g -fsanitize=thread source.c -o program

# Undefined behavior sanitizer
clang -g -fsanitize=undefined source.c -o program

# Leak sanitizer
clang -g -fsanitize=leak source.c -o program

# Control flow integrity
clang -g -fsanitize=cfi source.c -o program

# Safe stack
clang -g -fsanitize=safe-stack source.c -o program

# Kernel address sanitizer
clang -g -fsanitize=kernel-address source.c -o program

# Hardware-assisted address sanitizer
clang -g -fsanitize=hwaddress source.c -o program

# Multiple sanitizers
clang -g -fsanitize=address,undefined source.c -o program

# Sanitizer options
export ASAN_OPTIONS="detect_leaks=1:abort_on_error=1"
export MSAN_OPTIONS="print_stats=1"
export TSAN_OPTIONS="history_size=7"
export UBSAN_OPTIONS="print_stacktrace=1"

# Sanitizer coverage
clang -g -fsanitize=address -fsanitize-coverage=trace-pc-guard source.c -o program
```### Otimização Avançada
```bash
# Basic static analysis
clang --analyze source.c

# Analyze with specific checkers
clang --analyze -Xanalyzer -analyzer-checker=core source.c
clang --analyze -Xanalyzer -analyzer-checker=deadcode source.c
clang --analyze -Xanalyzer -analyzer-checker=security source.c

# List available checkers
clang -cc1 -analyzer-checker-help

# HTML output
clang --analyze -Xanalyzer -analyzer-output=html source.c

# Plist output
clang --analyze -Xanalyzer -analyzer-output=plist source.c

# Text output
clang --analyze -Xanalyzer -analyzer-output=text source.c

# Analyze entire project with scan-build
scan-build make
scan-build -o /tmp/analysis make

# Configure scan-build
scan-build -enable-checker security.insecureAPI.UncheckedReturn make
scan-build -disable-checker deadcode.DeadStores make

# Cross-translation unit analysis
clang --analyze -Xanalyzer -analyzer-config -Xanalyzer experimental-enable-naive-ctu-analysis=true source.c
```## Opções de Avisos e Diagnósticos
```bash
# Basic clang-tidy usage
clang-tidy source.c

# Specify checks
clang-tidy -checks='*' source.c
clang-tidy -checks='readability-*' source.c
clang-tidy -checks='modernize-*' source.cpp

# Apply fixes automatically
clang-tidy -fix source.c
clang-tidy -fix-errors source.c

# Configuration file (.clang-tidy)
echo "Checks: 'readability-*,modernize-*'" > .clang-tidy

# List available checks
clang-tidy -list-checks

# Explain specific check
clang-tidy -explain-config source.c

# Integration with compilation database
clang-tidy -p build_directory source.c

# Run on entire project
find . -name "*.c" -o -name "*.cpp"|xargs clang-tidy

# Parallel execution
run-clang-tidy -j$(nproc)
```### Níveis de Avisos
```bash
# List supported targets
clang -print-targets

# Cross-compile for different architectures
clang -target arm-linux-gnueabihf source.c -o program_arm
clang -target aarch64-linux-gnu source.c -o program_arm64
clang -target x86_64-pc-windows-msvc source.c -o program.exe
clang -target wasm32-unknown-unknown source.c -o program.wasm

# Specify system root
clang -target arm-linux-gnueabihf --sysroot=/path/to/sysroot source.c -o program

# Cross-compilation with specific CPU
clang -target arm-linux-gnueabihf -mcpu=cortex-a9 source.c -o program

# WebAssembly compilation
clang -target wasm32-unknown-unknown -nostdlib -Wl,--no-entry source.c -o program.wasm

# Emscripten (WebAssembly with JavaScript runtime)
emcc source.c -o program.html
emcc source.c -o program.js

# Android cross-compilation
clang -target armv7a-linux-androideabi21 source.c -o program_android

# iOS cross-compilation
clang -target arm64-apple-ios source.c -o program_ios
```### Diagnósticos Aprimorados
```bash
# Bare metal compilation
clang -target arm-none-eabi -mcpu=cortex-m4 -mthumb source.c -o program.elf

# Specify floating-point ABI
clang -target arm-none-eabi -mfloat-abi=hard -mfpu=fpv4-sp-d16 source.c -o program.elf

# Link with custom linker script
clang -target arm-none-eabi -T linker_script.ld source.c -o program.elf

# Generate different output formats
llvm-objcopy -O binary program.elf program.bin
llvm-objcopy -O ihex program.elf program.hex

# Size analysis
llvm-size program.elf

# Disassembly
llvm-objdump -d program.elf

# Symbol table
llvm-nm program.elf

# Common embedded flags
clang -target arm-none-eabi -mcpu=cortex-m4 -mthumb \
    -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Os \
    -ffunction-sections -fdata-sections \
    -Wl,--gc-sections source.c -o program.elf
```## Depuração e Sanitizadores
```bash
# LLVM profiling
llvm-profdata merge -output=merged.profdata *.profraw
llvm-profdata show merged.profdata
llvm-cov show program -instr-profile=merged.profdata

# Code coverage
clang -fprofile-instr-generate -fcoverage-mapping source.c -o program
./program
llvm-profdata merge -sparse default.profraw -o default.profdata
llvm-cov show program -instr-profile=default.profdata

# Generate coverage report
llvm-cov report program -instr-profile=default.profdata
llvm-cov export program -instr-profile=default.profdata -format=lcov > coverage.lcov

# Symbol analysis
llvm-nm program
llvm-readobj -symbols program
llvm-readelf -s program

# Binary analysis
llvm-objdump -d program
llvm-objdump -t program
llvm-objdump -h program

# Archive manipulation
llvm-ar rcs libmylib.a file1.o file2.o
llvm-ranlib libmylib.a

# String extraction
llvm-strings program
```### Informações de Depuração
```bash
# Optimization remarks
clang -O2 -Rpass=.* source.c -o program 2> remarks.txt
clang -O2 -Rpass-missed=.* source.c -o program 2> missed.txt
clang -O2 -Rpass-analysis=.* source.c -o program 2> analysis.txt

# YAML optimization records
clang -O2 -fsave-optimization-record source.c -o program
opt-viewer optimization-record.opt.yaml

# LLVM IR optimization
opt -O2 -print-before-all -print-after-all source.ll -o optimized.ll

# Pass analysis
opt -analyze -basicaa -aa-eval source.ll
opt -analyze -loops source.ll
opt -analyze -domtree source.ll

# Machine code analysis
llc -print-machineinstrs source.ll -o source.s

# Register allocation analysis
llc -debug-only=regalloc source.ll -o source.s
```### Sanitizadores
```bash
# Code formatting
clang-format source.c
clang-format -i source.c              # In-place formatting
clang-format -style=Google source.c
clang-format -style=LLVM source.c
clang-format -style=Mozilla source.c

# Generate .clang-format file
clang-format -style=Google -dump-config > .clang-format

# Include-what-you-use
include-what-you-use source.c
iwyu_tool.py -p build_directory

# Clang-based refactoring
clang-rename -old-name=oldFunction -new-name=newFunction source.c
clang-apply-replacements replacements_directory

# AST dumping
clang -Xclang -ast-dump source.c
clang -Xclang -ast-dump=json source.c

# Token dumping
clang -Xclang -dump-tokens source.c

# Preprocessor output
clang -E -dM source.c               # Show macros
clang -E -dI source.c               # Show includes
```## Análise Estática