Skip to content

Commit 37c2274

Browse files
committed
Convert compiler_services to Cpp2
Add helpful diagnostic for trying to use Cpp1 `->` syntax for dereference. Restrict `assert_not_null` to raw pointers, not STL iterators, which the comment already noted were not guaranteed to work. In the Microsoft STL under `_ITERATOR_DEBUG_LEVEL=2`, comparing a `set` or `map` iterator with a default-constructed iterator of the same type is flagged by [this test](https://github.com/microsoft/STL/blob/a62109595b6d89e08172fdf4beb75a2670fe0cc9/stl/inc/xtree#L230) as erroneously comparing objects that refer into different containers.
1 parent c84bf47 commit 37c2274

File tree

11 files changed

+301
-242
lines changed

11 files changed

+301
-242
lines changed

include/cpp2util.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,10 @@ auto assert_not_null(auto&& p CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT) -> declty
386386
{
387387
// NOTE: This "!= T{}" test may or may not work for STL iterators. The standard
388388
// doesn't guarantee that using == and != will reliably report whether an
389-
// STL iterator has the default-constructed value
390-
Null.expects(p != CPP2_TYPEOF(p){}, "dynamic null dereference attempt detected" CPP2_SOURCE_LOCATION_ARG);
389+
// STL iterator has the default-constructed value. So use it only for raw *...
390+
if constexpr (std::is_pointer_v<CPP2_TYPEOF(p)>) {
391+
Null.expects(p != CPP2_TYPEOF(p){}, "dynamic null dereference attempt detected" CPP2_SOURCE_LOCATION_ARG);
392+
}
391393
return CPP2_FORWARD(p);
392394
}
393395

regression-tests/test-results/clang-12/run-tests-clang-12.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ done
2222
rm -f *.obj *.exp *.lib
2323
printf "\nDone: %s .cpp tests compiled\n" "$count"
2424
printf "\n %s .cpp executables generated and run\n" "$exe_count"
25+
echo "$(date +"%T")"

regression-tests/test-results/gcc-10/run-tests-gcc-10.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ done
2222
rm -f *.obj *.exp *.lib
2323
printf "\nDone: %s .cpp tests compiled\n" "$count"
2424
printf "\n %s .cpp executables generated and run\n" "$exe_count"
25+
echo "$(date +"%T")"

regression-tests/test-results/msvc-2022/run-tests-msvc-2022.bat

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ del *.obj *.exp *.lib
2323
echo.
2424
echo Done: %count% .cpp tests compiled
2525
echo.
26-
echo. %exe_count% executables generated and run
26+
echo. %exe_count% executables generated and run
27+
echo %TIME%

regression-tests/test-results/run-tests.bat

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ echo. %err_count% error test cases (should not generate .cpp)
2828
echo. %total_count% total
2929
if %total_count% NEQ %count% (
3030
echo. *** MISMATCH: should equal total tests run
31-
)
31+
)
32+
echo %TIME%

regression-tests/test-results/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
cppfront compiler v0.2.1 Build 8521:1217
2+
cppfront compiler v0.2.1 Build 8522:0446
33
Copyright(c) Herb Sutter All rights reserved
44

55
SPDX-License-Identifier: CC-BY-NC-ND-4.0

source/build.info

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"8521:1217"
1+
"8522:0446"

source/cppfront.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4481,7 +4481,7 @@ class cppfront
44814481

44824482
auto found_explicit_init = false;
44834483
auto found_default_init = false;
4484-
auto stmt_pos = source_position{};
4484+
auto stmt_pos = n.position();
44854485

44864486
auto initializer = std::string{};
44874487

source/parse.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4184,17 +4184,27 @@ class parser
41844184
}
41854185
}
41864186

4187-
//G expression: // eliminated condition: - use expression:
4187+
//G expression: // eliminated 'condition:' - just use 'expression:'
41884188
//G assignment-expression
41894189
//GTODO try expression
41904190
//G
4191-
auto expression(bool allow_angle_operators = true)
4191+
auto expression(bool allow_angle_operators = true, bool check_arrow = true)
41924192
-> std::unique_ptr<expression_node>
41934193
{
41944194
auto n = std::make_unique<expression_node>();
41954195
if (!(n->expr = assignment_expression(allow_angle_operators))) {
41964196
return {};
41974197
}
4198+
4199+
if (
4200+
check_arrow
4201+
&& curr().type() == lexeme::Arrow
4202+
)
4203+
{
4204+
error("'->' is not Cpp2 deference syntax - write '*.' instead");
4205+
return {};
4206+
}
4207+
41984208
return n;
41994209
}
42004210

@@ -5073,7 +5083,7 @@ class parser
50735083
next();
50745084
}
50755085

5076-
if (auto e = expression()) {
5086+
if (auto e = expression(true, false)) {
50775087
n->expression = std::move(e);
50785088
}
50795089
else {

0 commit comments

Comments
 (0)