Skip to content

Commit

Permalink
Convert compiler_services to Cpp2
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hsutter authored and zaucy committed Dec 5, 2023
1 parent 65cd5da commit ee79371
Show file tree
Hide file tree
Showing 11 changed files with 301 additions and 242 deletions.
6 changes: 4 additions & 2 deletions include/cpp2util.h
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,10 @@ auto assert_not_null(auto&& p CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT) -> declty
{
// NOTE: This "!= T{}" test may or may not work for STL iterators. The standard
// doesn't guarantee that using == and != will reliably report whether an
// STL iterator has the default-constructed value
Null.expects(p != CPP2_TYPEOF(p){}, "dynamic null dereference attempt detected" CPP2_SOURCE_LOCATION_ARG);
// STL iterator has the default-constructed value. So use it only for raw *...
if constexpr (std::is_pointer_v<CPP2_TYPEOF(p)>) {
Null.expects(p != CPP2_TYPEOF(p){}, "dynamic null dereference attempt detected" CPP2_SOURCE_LOCATION_ARG);
}
return CPP2_FORWARD(p);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ done
rm -f *.obj *.exp *.lib
printf "\nDone: %s .cpp tests compiled\n" "$count"
printf "\n %s .cpp executables generated and run\n" "$exe_count"
echo "$(date +"%T")"
1 change: 1 addition & 0 deletions regression-tests/test-results/gcc-10/run-tests-gcc-10.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ done
rm -f *.obj *.exp *.lib
printf "\nDone: %s .cpp tests compiled\n" "$count"
printf "\n %s .cpp executables generated and run\n" "$exe_count"
echo "$(date +"%T")"
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ del *.obj *.exp *.lib
echo.
echo Done: %count% .cpp tests compiled
echo.
echo. %exe_count% executables generated and run
echo. %exe_count% executables generated and run
echo %TIME%
3 changes: 2 additions & 1 deletion regression-tests/test-results/run-tests.bat
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ echo. %err_count% error test cases (should not generate .cpp)
echo. %total_count% total
if %total_count% NEQ %count% (
echo. *** MISMATCH: should equal total tests run
)
)
echo %TIME%
2 changes: 1 addition & 1 deletion regression-tests/test-results/version
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

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

SPDX-License-Identifier: CC-BY-NC-ND-4.0
Expand Down
2 changes: 1 addition & 1 deletion source/build.info
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"8521:1217"
"8522:0446"
2 changes: 1 addition & 1 deletion source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4481,7 +4481,7 @@ class cppfront

auto found_explicit_init = false;
auto found_default_init = false;
auto stmt_pos = source_position{};
auto stmt_pos = n.position();

auto initializer = std::string{};

Expand Down
16 changes: 13 additions & 3 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -4184,17 +4184,27 @@ class parser
}
}

//G expression: // eliminated condition: - use expression:
//G expression: // eliminated 'condition:' - just use 'expression:'
//G assignment-expression
//GTODO try expression
//G
auto expression(bool allow_angle_operators = true)
auto expression(bool allow_angle_operators = true, bool check_arrow = true)
-> std::unique_ptr<expression_node>
{
auto n = std::make_unique<expression_node>();
if (!(n->expr = assignment_expression(allow_angle_operators))) {
return {};
}

if (
check_arrow
&& curr().type() == lexeme::Arrow
)
{
error("'->' is not Cpp2 deference syntax - write '*.' instead");
return {};
}

return n;
}

Expand Down Expand Up @@ -5073,7 +5083,7 @@ class parser
next();
}

if (auto e = expression()) {
if (auto e = expression(true, false)) {
n->expression = std::move(e);
}
else {
Expand Down
Loading

0 comments on commit ee79371

Please sign in to comment.