# llvm-project **Repository Path**: xiaogvo/llvm-project ## Basic Information - **Project Name**: llvm-project - **Description**: llvm-project - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 201 - **Created**: 2026-06-30 - **Last Updated**: 2026-06-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # TrustC TrustC 是一门强调安全性与可验证性的通用编程语言,面向 C 语言生态中的系统级开发场景。它在尽量保持 C 的程序结构、函数调用约定、编译链接方式和运行时模型兼容的基础上,把内存安全、并发安全、规约描述和程序证明等能力引入编译期检查与验证流程。 本仓库是 TrustC 的编译器与工具链源码,基于 LLVM/Clang 15.0.4 扩展实现。TrustC 源文件使用 `.cbs` 扩展名,头文件使用 `.hbs` 扩展名;编译器前端仍由 Clang 驱动,并在 Lexer、Parser、Sema、代码重写、clangd 等阶段加入 TrustC 语言扩展。 ## 主要能力 TrustC 围绕“安全增强”组织语言能力,核心包括: - 内存安全:通过 `_Owned`、`_Borrow`、`&_Mut`、`&_Const` 建模资源所有权、借用关系、生命周期和释放责任,在编译期检查悬垂指针、释放后使用、重复释放和资源泄漏等问题。 - 并发安全:通过线程所有权转移、共享所有权、锁保护注解和线程安全标记,约束跨线程数据共享、同步访问和锁使用。 - 规约增强:通过规约语言描述函数、模块或数据结构应满足的前置条件、后置条件和不变式。 - 证明增强:通过证明语言描述程序性质的证明过程,并生成或辅助验证关键性质。 语言体系上,TrustC 由三层组成: - 基础语言:承载可执行的安全机制,包括所有权、借用、生命周期、安全区、成员方法、泛型、Trait、协程和空指针约束等。 - 规约语言:描述程序契约和安全性质,作为编译期检查和验证期推理的依据。 - 证明语言:描述程序性质的证明过程,与规约语言共同支撑可验证编程。 ## 构建和安装 推荐使用 Ninja 构建 LLVM 和 Clang: ```bash mkdir build && cd build cmake -G Ninja \ -S llvm \ -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" \ -DCMAKE_BUILD_TYPE=Release \ -DLLVM_USE_LINKER=lld \ -DBUILD_SHARED_LIBS=OFF \ -DLLVM_TARGETS_TO_BUILD="X86" \ -DCMAKE_INSTALL_PREFIX= ninja ``` 使用`ninja install`安装上一步构建的TrustC编译器,安装目录为 ``: ```bash ninja install ``` 常用配置项: - `ENABLE_BSC=ON`:启用 TrustC 支持,默认开启。 - `ENABLE_BSC_FUTURE=OFF`:关闭实验性 future 特性,默认关闭。 - `LLVM_ENABLE_PROJECTS="clang;clang-tools-extra"`:同时构建 Clang 和 clangd 等工具。只需要编译器时可使用 `clang`。 构建完成后,可使用: ```bash bin/clang --version bin/clangd --version ``` 可选构建: TrustC编译器项目中还提供了TrustC标准库`libcbs`,用于支持TrustC语言的开发。 ```bash cd llvm-project mkdir build_libcbs && cd build_libcbs cmake -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=/bin/clang -DCMAKE_INSTALL_PREFIX= ../libcbs ninja stdcbs ninja install ``` 安装后,可将 `/bin` 加入 `PATH`: ```bash export PATH=/bin:$PATH clang --version ``` ## 第一个 TrustC 程序 TrustC源码用`.cbs`作为文件后缀。创建 `hello.cbs`: ```c #include _Safe int main(void) { int value = 41; int *_Borrow p = &_Mut value; *p = *p + 1; _Unsafe { printf("TrustC value = %d\n", value); } return 0; } ``` 编译并运行: ```bash clang hello.cbs -o hello ./hello ``` 预期输出: ```text TrustC value = 42 ``` 这个示例展示了几个基本概念: - `_Safe` 标记受 TrustC 静态安全检查约束的函数。 - `&_Mut value` 创建对 `value` 的可写借用。 - `int *_Borrow p` 表示 `p` 只是临时访问权,不拥有 `value` 的释放责任。 - `_Unsafe { ... }` 显式标记调用 C 标准库等无法完全由 TrustC 静态验证的边界。 如果需要查看 TrustC 被重写后的 C 代码,可使用: ```bash build/bin/clang -rewrite-bsc hello.cbs -o hello.rewritten.c ``` ## 测试 开发时优先运行聚焦测试: ```bash # 单个 TrustC 编译器测试 build/bin/llvm-lit clang/test/BSC/Positive/... build/bin/llvm-lit clang/test/BSC/Negative/... # TrustC clangd/LSP 测试 build/bin/llvm-lit clang-tools-extra/clangd/test/bsc/... ``` 需要更大范围回归时,可运行: ```bash ninja -C build check-clang -j 20 ``` ## 目录参考 与 TrustC 相关的主要源码位置: - `clang/include/clang/Basic/TokenKinds.def`:TrustC/BSC 关键字定义。 - `clang/lib/Parse/BSC/`:TrustC/BSC 语法解析。 - `clang/lib/Sema/BSC/`:所有权、借用、Trait、安全区、协程、析构等语义检查。 - `clang/lib/Analysis/BSC/`:所有权、借用、空指针等分析逻辑。 - `clang/lib/Frontend/Rewrite/RewriteBSC.cpp`:TrustC 到 C 的源码重写。 - `clang-tools-extra/clangd/test/bsc/`:TrustC clangd/LSP 测试。 - `libcbs/`:TrustC/BSC 安全标准库相关源码。 ## 上游 LLVM README 参考 以下内容保留自上游 `llvm-project` README,作为 LLVM/Clang 构建和社区资料的参考。 --- # The LLVM Compiler Infrastructure This directory and its sub-directories contain the source code for LLVM, a toolkit for the construction of highly optimized compilers, optimizers, and run-time environments. The README briefly describes how to get started with building LLVM. For more information on how to contribute to the LLVM project, please take a look at the [Contributing to LLVM](https://llvm.org/docs/Contributing.html) guide. ## Getting Started with the LLVM System Taken from [here](https://llvm.org/docs/GettingStarted.html). ### Overview Welcome to the LLVM project! The LLVM project has multiple components. The core of the project is itself called "LLVM". This contains all of the tools, libraries, and header files needed to process intermediate representations and convert them into object files. Tools include an assembler, disassembler, bitcode analyzer, and bitcode optimizer. It also contains basic regression tests. C-like languages use the [Clang](http://clang.llvm.org/) frontend. This component compiles C, C++, Objective-C, and Objective-C++ code into LLVM bitcode -- and from there into object files, using LLVM. Other components include: the [libc++ C++ standard library](https://libcxx.llvm.org), the [LLD linker](https://lld.llvm.org), and more. ### Getting the Source Code and Building LLVM The LLVM Getting Started documentation may be out of date. The [Clang Getting Started](http://clang.llvm.org/get_started.html) page might have more accurate information. This is an example work-flow and configuration to get and build the LLVM source: 1. Checkout LLVM (including related sub-projects like Clang): * ``git clone https://github.com/llvm/llvm-project.git`` * Or, on windows, ``git clone --config core.autocrlf=false https://github.com/llvm/llvm-project.git`` 2. Configure and build LLVM and Clang: * ``cd llvm-project`` * ``cmake -S llvm -B build -G [options]`` Some common build system generators are: * ``Ninja`` --- for generating [Ninja](https://ninja-build.org) build files. Most llvm developers use Ninja. * ``Unix Makefiles`` --- for generating make-compatible parallel makefiles. * ``Visual Studio`` --- for generating Visual Studio projects and solutions. * ``Xcode`` --- for generating Xcode projects. Some common options: * ``-DLLVM_ENABLE_PROJECTS='...'`` and ``-DLLVM_ENABLE_RUNTIMES='...'`` --- semicolon-separated list of the LLVM sub-projects and runtimes you'd like to additionally build. ``LLVM_ENABLE_PROJECTS`` can include any of: clang, clang-tools-extra, cross-project-tests, flang, libc, libclc, lld, lldb, mlir, openmp, polly, or pstl. ``LLVM_ENABLE_RUNTIMES`` can include any of libcxx, libcxxabi, libunwind, compiler-rt, libc or openmp. Some runtime projects can be specified either in ``LLVM_ENABLE_PROJECTS`` or in ``LLVM_ENABLE_RUNTIMES``. For example, to build LLVM, Clang, libcxx, and libcxxabi, use ``-DLLVM_ENABLE_PROJECTS="clang" -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi"``. * ``-DCMAKE_INSTALL_PREFIX=directory`` --- Specify for *directory* the full path name of where you want the LLVM tools and libraries to be installed (default ``/usr/local``). Be careful if you install runtime libraries: if your system uses those provided by LLVM (like libc++ or libc++abi), you must not overwrite your system's copy of those libraries, since that could render your system unusable. In general, using something like ``/usr`` is not advised, but ``/usr/local`` is fine. * ``-DCMAKE_BUILD_TYPE=type`` --- Valid options for *type* are Debug, Release, RelWithDebInfo, and MinSizeRel. Default is Debug. * ``-DLLVM_ENABLE_ASSERTIONS=On`` --- Compile with assertion checks enabled (default is Yes for Debug builds, No for all other build types). * ``cmake --build build [-- [options] ]`` or your build system specified above directly. * The default target (i.e. ``ninja`` or ``make``) will build all of LLVM. * The ``check-all`` target (i.e. ``ninja check-all``) will run the regression tests to ensure everything is in working order. * CMake will generate targets for each tool and library, and most LLVM sub-projects generate their own ``check-`` target. * Running a serial build will be **slow**. To improve speed, try running a parallel build. That's done by default in Ninja; for ``make``, use the option ``-j NNN``, where ``NNN`` is the number of parallel jobs to run. In most cases, you get the best performance if you specify the number of CPU threads you have. On some Unix systems, you can specify this with ``-j$(nproc)``. * For more information see [CMake](https://llvm.org/docs/CMake.html). Consult the [Getting Started with LLVM](https://llvm.org/docs/GettingStarted.html#getting-started-with-llvm) page for detailed information on configuring and compiling LLVM. You can visit [Directory Layout](https://llvm.org/docs/GettingStarted.html#directory-layout) to learn about the layout of the source code tree. ## Getting in touch Join [LLVM Discourse forums](https://discourse.llvm.org/), [discord chat](https://discord.gg/xS7Z362) or #llvm IRC channel on [OFTC](https://oftc.net/). The LLVM project has adopted a [code of conduct](https://llvm.org/docs/CodeOfConduct.html) for participants to all modes of communication within the project.