Skip to content

Commit f64ef36

Browse files
committed
feat: Merge branch 'access-check'
2 parents 0c5472e + 8043503 commit f64ef36

File tree

9 files changed

+516
-30
lines changed

9 files changed

+516
-30
lines changed

.github/workflows/main.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ jobs:
1010
compiler: ['g++-12', 'g++-11', 'g++-10', 'g++-9']
1111
standard: ['11', '14', '17', '20']
1212
precompile: ['ON', 'OFF']
13+
betafeature: ['ON', 'OFF']
1314
steps:
1415
- name: Get number of CPU cores
1516
uses: SimenB/github-actions-cpu-cores@v2
@@ -25,7 +26,7 @@ jobs:
2526
sudo apt-get install ${{ matrix.compiler }}
2627
- name: Configure
2728
run: |
28-
cmake -B build/ -DCMAKE_CXX_COMPILER=${{ matrix.compiler }} -DCMAKE_CXX_STANDARD=${{ matrix.standard }} -DTOML11_BUILD_TESTS=ON -DTOML11_PRECOMPILE=${{ matrix.precompile }}
29+
cmake -B build/ -DCMAKE_CXX_COMPILER=${{ matrix.compiler }} -DCMAKE_CXX_STANDARD=${{ matrix.standard }} -DTOML11_BUILD_TESTS=ON -DTOML11_PRECOMPILE=${{ matrix.precompile }} -DTOML11_ENABLE_ACCESS_CHECK=${{ matrix.betafeature }}
2930
- name: Build
3031
run: |
3132
cmake --build build/ -j${{ steps.cpu-cores.outputs.count }}
@@ -141,6 +142,7 @@ jobs:
141142
matrix:
142143
standard: ['11', '14', '17', '20']
143144
precompile: ['ON', 'OFF']
145+
betafeature: ['ON', 'OFF']
144146
steps:
145147
- name: Get number of CPU cores
146148
uses: SimenB/github-actions-cpu-cores@v2
@@ -151,7 +153,7 @@ jobs:
151153
submodules: true
152154
- name: Configure
153155
run: |
154-
cmake -B build/ -DCMAKE_CXX_STANDARD=${{ matrix.standard }} -DTOML11_BUILD_TESTS=ON -DTOML11_PRECOMPILE=${{ matrix.precompile }}
156+
cmake -B build/ -DCMAKE_CXX_STANDARD=${{ matrix.standard }} -DTOML11_BUILD_TESTS=ON -DTOML11_PRECOMPILE=${{ matrix.precompile }} -DTOML11_ENABLE_ACCESS_CHECK=${{ matrix.betafeature }}
155157
- name: Build
156158
run: |
157159
cmake --build build/ -j${{ steps.cpu-cores.outputs.count }}
@@ -190,6 +192,7 @@ jobs:
190192
standard: ['11', '14', '17', '20']
191193
config: ['Release', 'Debug']
192194
precompile: ['ON', 'OFF']
195+
betafeature: ['ON', 'OFF']
193196
steps:
194197
- name: Get number of CPU cores
195198
uses: SimenB/github-actions-cpu-cores@v2
@@ -202,7 +205,7 @@ jobs:
202205
- name: Configure
203206
shell: cmd
204207
run: |
205-
cmake -B build/ -G "NMake Makefiles" -DTOML11_BUILD_TESTS=ON -DCMAKE_CXX_STANDARD=${{ matrix.standard }} -DTOML11_PRECOMPILE=${{ matrix.precompile }}
208+
cmake -B build/ -G "NMake Makefiles" -DTOML11_BUILD_TESTS=ON -DCMAKE_CXX_STANDARD=${{ matrix.standard }} -DTOML11_PRECOMPILE=${{ matrix.precompile }} -DTOML11_ENABLE_ACCESS_CHECK=${{ matrix.betafeature }}
206209
- name: Build
207210
run: |
208211
cmake --build ./build --config "${{ matrix.config }}" -j${{ steps.cpu-cores.outputs.count }}

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ project(toml11 LANGUAGES CXX VERSION "${TOML11_VERSION_MAJOR}.${TOML11_VERSION_M
1717
include(CTest) # to use ${BUILD_TESTING}
1818

1919
option(TOML11_PRECOMPILE "precompile toml11 library" OFF)
20+
option(TOML11_ENABLE_ACCESS_CHECK "enable access check feature (beta)" OFF)
2021

2122
include(CMakeDependentOption)
2223
cmake_policy(PUSH)

docs/content.en/docs/features/value.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,3 +834,76 @@ struct bar
834834
}
835835
};
836836
```
837+
838+
# Checking Whether a Value Has Been Accessed
839+
840+
{{% hint warning %}}
841+
842+
This feature is not enabled by default. To use it, you need to define `TOML11_ENABLE_ACCESS_CHECK`.
843+
Additionally, since this feature introduces extra processing on parsed values, it may impact runtime performance.
844+
845+
{{% /hint %}}
846+
847+
When compiled with the `TOML11_ENABLE_ACCESS_CHECK` macro defined, the `toml::value` class gains an additional method: `bool accessed() const`.
848+
This allows you to check whether a value has been accessed after parsing.
849+
850+
```console
851+
$ g++ -std=c++17 -O2 -DTOML11_ENABLE_ACCESS_CHECK -I/path/to/toml11/include main.cpp
852+
```
853+
854+
or
855+
856+
```console
857+
$ cmake -B ./build -DTOML11_ENABLE_ACCESS_CHECK=ON
858+
```
859+
860+
or
861+
862+
```cmake
863+
CPMAddPackage(
864+
NAME toml11
865+
GITHUB_REPOSITORY "ToruNiina/toml11"
866+
VERSION 4.4.0
867+
OPTIONS "CMAKE_CXX_STANDARD 17" "TOML11_PRECOMPILE ON" "TOML11_ENABLE_ACCESS_CHECK ON"
868+
)
869+
```
870+
871+
This feature allows users to implement code that warns about values defined in a table but never used.
872+
873+
```cpp
874+
#include <toml.hpp>
875+
876+
namespace yours
877+
{
878+
879+
Config read_config(const toml::value& input)
880+
{
881+
const auto cfg = read_your_config(input);
882+
883+
for (const auto& [k, v] : input.as_table())
884+
{
885+
if (!v.accessed())
886+
{
887+
std::cerr << toml::format_error("value defined but not used",
888+
v.source_location(), "not used");
889+
}
890+
}
891+
return cfg;
892+
}
893+
} // namespace yours
894+
```
895+
896+
This feature is useful when a value is mistakenly defined under the wrong name but is never accessed. For example:
897+
898+
```toml
899+
# The correct key is "reactions"
900+
# reactions = [ ":+1:", "star" ]
901+
902+
# This key is incorrect and will not be read
903+
reaction = [ ":+1:", "star" ]
904+
```
905+
906+
If this file is read using the above code, `read_your_config` will search for `reactions`. Since it is not defined, it will process `reactions` as an empty array.
907+
In this case, `input.at("reaction").accessed()` will be `false`, allowing it to be detected as an error.
908+
909+

docs/content.ja/docs/features/value.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,3 +863,71 @@ struct bar
863863
}
864864
};
865865
```
866+
867+
# 値がアクセス済みかどうかチェックする
868+
869+
{{% hint warning %}}
870+
871+
この機能はデフォルトでは有効化されず、使用する際には`TOML11_ENABLE_ACCESS_CHECK`を定義する必要があります。
872+
また、この機能はパースした値に対して追加の処理を行うため、実行時パフォーマンスが低下する可能性があります。
873+
874+
{{% /hint %}}
875+
876+
`TOML11_ENABLE_ACCESS_CHECK`マクロを定義してコンパイルすると、`toml::value``bool accessed() const`メソッドが追加され、パース後にその値にアクセスしたかどうかが確認できるようになります。
877+
878+
```console
879+
$ g++ -std=c++17 -O2 -DTOML11_ENABLE_ACCESS_CHECK -I/path/to/toml11/include main.cpp
880+
```
881+
882+
```console
883+
$ cmake -B ./build -DTOML11_ENABLE_ACCESS_CHECK=ON
884+
```
885+
886+
```cmake
887+
CPMAddPackage(
888+
NAME toml11
889+
GITHUB_REPOSITORY "ToruNiina/toml11"
890+
VERSION 4.4.0
891+
OPTIONS "CMAKE_CXX_STANDARD 17" "TOML11_PRECOMPILE ON" "TOML11_ENABLE_ACCESS_CHECK ON"
892+
)
893+
```
894+
895+
この機能によって、テーブル内に定義されているものの使用されなかった値についての警告を表示することが可能になります。
896+
897+
```cpp
898+
#include <toml.hpp>
899+
900+
namespace yours
901+
{
902+
903+
Config read_config(const toml::value& v)
904+
{
905+
const auto cfg = read_your_config(input);
906+
907+
for(const auto& [k, v] : input.as_table())
908+
{
909+
if( ! v.accessed())
910+
{
911+
std::cerr << toml::format_error("value defined but not used",
912+
v.source_location(), "not used");
913+
}
914+
}
915+
return cfg;
916+
}
917+
} // yours
918+
```
919+
920+
この機能は、必要な場合のみ定義されるような値を、名称を間違えて定義してしまった際に役に立つでしょう。
921+
例えば、
922+
923+
```toml
924+
# 正しくは reactions
925+
# reactions = [ ":+1:", "star" ]
926+
927+
# 名前が違うので読み込めない
928+
reaction = [ ":+1:", "star" ]
929+
```
930+
931+
このファイルを上記のコードで読んだ場合、`read_your_config``reactions`を探し、定義されていなかったので空の配列として処理するでしょう。
932+
その場合、`reaction``accessed()``true`にならないため、エラーとして検出できます。
933+

include/toml11/parser.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3426,6 +3426,11 @@ parse_file(location& loc, context<TC>& ctx)
34263426
{
34273427
return err(std::move(ctx.errors()));
34283428
}
3429+
3430+
#ifdef TOML11_ENABLE_ACCESS_CHECK
3431+
detail::unset_access_flag_recursively(root);
3432+
#endif
3433+
34293434
return ok(std::move(root));
34303435
}
34313436

0 commit comments

Comments
 (0)