Skip to content

Commit

Permalink
add tl expected error
Browse files Browse the repository at this point in the history
  • Loading branch information
archibate committed Aug 23, 2024
1 parent 427b4b1 commit a133f8f
Show file tree
Hide file tree
Showing 5 changed files with 2,560 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.12)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD 23)

project(main)

Expand Down
Binary file added docs/img/llvm-basic-block-branch.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 14 additions & 4 deletions docs/llvm_intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ Clang 只是 LLVM 项目中的一个前端,其负责编译 C/C++ 这类语言
- LLVM 官方仓库:https://github.com/llvm/llvm-project
- LLVM 用户文档:https://llvm.org/docs/
- LLVM 源码级文档:https://llvm.org/doxygen/
- 《Learn LLVM 17》:https://github.com/xiaoweiChen/Learn-LLVM-17
- LLVM IR 全文档:https://llvm.org/docs/LangRef.html
- 《Learn LLVM 17》:https://github.com/xiaoweiChen/Learn-LLVM-17
- 《开始学习 LLVM》:https://getting-started-with-llvm-core-libraries-zh-cn.readthedocs.io/zh-cn/latest/
- 《miniSysY 编译实验》:https://buaa-se-compiling.github.io/miniSysY-tutorial/pre/llvm.html
- A Gentle Introduction to LLVM IR:https://mcyoung.xyz/2023/08/01/llvm-ir/
- 《A Gentle Introduction to LLVM IR》:https://mcyoung.xyz/2023/08/01/llvm-ir/
- 《LLVM IR C++ API Tutorial》:https://mukulrathi.com/create-your-own-programming-language/llvm-ir-cpp-api-tutorial/

> {{ icon.warn }} 不建议按顺序全部逐个阅读完,这么多文档小彭老师都看不完。建议遇到了不熟悉的指令时,再去针对性地找到相应章节,学习。
Expand Down Expand Up @@ -1180,15 +1181,16 @@ define dso_local noundef i32 @main() #0 {

> {{ icon.story }} 如果直接 `ret i32 @i` 的话,就变成 `return &i` 的效果了。
#### 调用其他函数
#### `call` 调用其他函数

```llvm
define dso_local noundef i32 @main() #0 {
%1 = call i32 (ptr, ...) @printf(ptr noundef @.str)
ret i32 0
}
```
todo

TODO

### 轶事:LLVM IR 不跨平台

Expand Down Expand Up @@ -1429,6 +1431,14 @@ attributes #0 = { mustprogress noinline norecurse nounwind optnone sspstrong uwt
!5 = !{!"clang version 18.1.8"}
```

现在,我们用 `opt` 工具对其进行优化:

TODO

## 基本块与分支

![](img/llvm-basic-block-branch.png)

## 汇编语言(ASM)

## 汇编语言的终局:机器码
Expand Down
101 changes: 101 additions & 0 deletions examples/error_code.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include <fmt/core.h>
#include <system_error>
#include "tl-expected.hpp"
// 今日主题:现代 C++ 中的错误处理

// int : [INT_MIN, INT_MAX]
// optional<int> : [INT_MIN, INT_MAX] | {nullopt}
// variant<int, error_code> : [INT_MIN, INT_MAX] | {error_code}
// expected<int, error_code> : [INT_MIN, INT_MAX] | {error_code}

namespace mybuss {

enum class login_errc {
success = 0,
not_valid_pass,
not_login,
};

auto const &login_category() {
static const struct : std::error_category {
virtual std::string message(int val) const override {
switch ((login_errc)val) {
case login_errc::success:
return "登录成功!";
case login_errc::not_valid_pass:
return "密码不正确!";
case login_errc::not_login:
return "用户未登录!";
default:
return "未知错误!";
};
}

virtual const char *name() const noexcept override {
return "login";
}
} instance;
return instance;
}

std::error_code make_error_code(login_errc ec) {
return std::error_code((int)ec, login_category());
}

}

tl::expected<int, std::error_code> sqrt(int x) {
// 假装这是一个关于网站登录的业务函数
if (x < 0) {
return tl::unexpected{make_error_code(std::errc::argument_out_of_domain)};
}
if (x == 3) {
return tl::unexpected{make_error_code(mybuss::login_errc::not_valid_pass)};
}
if (x == 4) {
return tl::unexpected{make_error_code(mybuss::login_errc::not_login)};
}
for (int i = 0;; i++) {
if (i * i >= x) {
return i;
}
}
}

tl::expected<int, std::error_code> sqrfloor(int x) {
if (x < 1) {
return tl::unexpected{make_error_code(std::errc::invalid_argument)};
}
auto ret = sqrt(x * x);
return ret.map([&] (int x) { fmt::println("x * 2"); return x * 2; });
// 等价于:
// if (!ret.has_value()) {
// return tl::unexpected{ret.error()};
// }
// x = ret.value();
// x *= 2;
// return x;

// return ret.map_error([&] (std::error_code ec) {
// if (ec == make_error_code(mybuss::login_errc::not_login)) {
// ec = make_error_code(mybuss::login_errc::not_valid_pass);
// }
// return ec;
// });
// 等价于:
// if (!ret.has_value()) {
// if (ret.error() == make_error_code(mybuss::login_errc::not_login)) {
// ret.error() = make_error_code(mybuss::login_errc::not_valid_pass);
// }
// }
}

int main() {
auto ret = sqrfloor(3);
if (ret.has_value()) {
fmt::println("结果: {}", ret.value());
} else {
fmt::println("出错: {}", ret.error().message());
}
return 0;
}
Loading

0 comments on commit a133f8f

Please sign in to comment.