Skip to content

Instantly share code, notes, and snippets.

@acoomans
Last active December 3, 2019 16:11
Show Gist options
  • Save acoomans/1cc7cdf28d89726b9ea6bb3afa554a5e to your computer and use it in GitHub Desktop.
Save acoomans/1cc7cdf28d89726b9ea6bb3afa554a5e to your computer and use it in GitHub Desktop.
LLVM / Clang Development Notes & Cheatsheet

LLVM / Clang Cheatsheet

Links

Development

CLion

Building:

  1. Clone: git clone --depth 1 https://github.com/llvm/llvm-project.git
  2. Go to: Clion > Preferences > search "CMake"
    • Cmake options: -DLLVM_ENABLE_PROJECTS=clang
    • Generation path: [path]/build
  3. In project tree: llvm/CMakeLists.txt > right-click > Load CMake Project

Debugging:

  1. Find the right binary and arguments with clang -v [arguments]
  2. Change target configuration
  3. Set breakpoints and run debug

Patch

link

git show HEAD -U999999 > mypatch.patch

Testing

source: https://systemundertest.org/llvm/

tests (regression tests, lit)

make check-llvm

make check-clang
make clang-test  // legacy

// single test
~/src/llvm/build/bin/llvm-lit -v ~/src/llvm/tools/clang/test/
python ~/src/llvm/utils/lit/lit.py -sv --param=build_config=Debug ~/src/llvm/build/test

unittests (google test)

make check-llvm-unit

make check-clang-unit

Clang

Tools

  • clang-check: diagnostics
  • clang-format: formatting
  • clang-tidy: linter

Driver

clang -ccc-print-phases a.c -o a
	0: input, "a.c", c
	1: preprocessor, {0}, cpp-output
	2: compiler, {1}, ir
	3: backend, {2}, assembler
	4: assembler, {3}, object
	5: linker, {4}, image
	6: bind-arch, "x86_64", {5}, image

AST

clang -ast-dump

Libraries

  • interfacing:

    • libclang: low-level access to AST
    • plugins (PluginASTAction): add actions to clang
    • libtooling (FrontendActions): for building individual tools
  • reformat (libformat)

  • RecursiveASTVisitor: visit AST

  • AST matchers: match AST nodes

Building against LLVM/clang

llvm-config --cxxflags --ldflags --libs

$LLVM/build/bin/clang stat.cpp -o stat \
\
-I$LLVM/include \
-I$LLVM/build/include \
-L$LLVM/build/lib \
\
-I$LLVM/tools/clang/include \
-I$LLVM/build/tools/clang/include \
-L$LLVM/build/tools/clang/lib \
-lclangAST \
-lclangASTMatchers \
-lclangAnalysis \
-lclangBasic \
-lclangCodeGen \
-lclangDriver \
-lclangEdit \
-lclangFormat \
-lclangFrontend \
-lclangFrontendTool \
-lclangIndex \
-lclangLex \
-lclangParse \
-lclangRewrite \
-lclangSema \
-lclangSerialization \
-lclangTooling

LLDB

Building

Compiling on mac: lldb codesigning issue

Debugging LLDB

Run the build:

~/src/llvm/cmake-build-debug/bin/lldb ~/src/test

Attach:

lldb -n lldb 

Lexicon

  • Translation Unit: roughly a source file, after preprocessor includes.
  • PGO: profile-guided feedback, or FGO feedback-directed optimization. Improve performance through app profiling.
  • LTO: link-time optimization
  • thinLTO: parallelized LTO (doc, blog)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment