forked from wangliu-iscas/gcc-patch
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
c++: Track lifetimes in constant evaluation [PR70331, PR96630, PR98675]
This adds rudimentary lifetime tracking in C++ constexpr contexts, allowing the compiler to report errors with using values after their backing has gone out of scope. We don't yet handle other ways of ending lifetimes (e.g. explicit destructor calls). PR c++/70331 PR c++/96630 PR c++/98675 gcc/cp/ChangeLog: * constexpr.cc (constexpr_global_ctx::put_value): Mark value as in lifetime. (constexpr_global_ctx::remove_value): Mark value as expired. (cxx_eval_call_expression): Remove comment that is no longer applicable. (non_const_var_error): Add check for expired values. (cxx_eval_constant_expression): Add checks for expired values. Forget local variables at end of bind expressions. Forget temporaries at end of cleanup points. * module.cc (trees_out::core_bools): Write out the new flag. (trees_in::core_bools): Read in the new flag. gcc/ChangeLog: * tree-core.h (struct tree_decl_common): New flag to check if value lifetime has expired. * tree.h (DECL_EXPIRED): Access the new flag. * print-tree.cc (print_node): Print the new flag. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/constexpr-ice20.C: Update error raised by test. * g++.dg/cpp1y/constexpr-lifetime1.C: New test. * g++.dg/cpp1y/constexpr-lifetime2.C: New test. * g++.dg/cpp1y/constexpr-lifetime3.C: New test. * g++.dg/cpp1y/constexpr-lifetime4.C: New test. * g++.dg/cpp1y/constexpr-lifetime5.C: New test. Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
- Loading branch information
1 parent
fcb4115
commit a072cdd
Showing
11 changed files
with
138 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// PR c++/96630 | ||
// { dg-do compile { target c++14 } } | ||
|
||
struct S { | ||
int x = 0; | ||
constexpr const int& get() const { return x; } | ||
}; | ||
|
||
constexpr const int& test() { | ||
auto local = S{}; // { dg-message "note: declared here" } | ||
return local.get(); | ||
} | ||
constexpr int x = test(); // { dg-error "accessing object outside its lifetime" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// PR c++/98675 | ||
// { dg-do compile { target c++14 } } | ||
|
||
struct S { | ||
int x = 0; | ||
constexpr const int& get() const { return x; } | ||
}; | ||
|
||
constexpr int error() { | ||
const auto& local = S{}.get(); // { dg-message "note: declared here" } | ||
return local; | ||
} | ||
constexpr int x = error(); // { dg-error "accessing object outside its lifetime" } | ||
|
||
constexpr int ok() { | ||
// temporary should only be destroyed after end of full-expression | ||
auto local = S{}.get(); | ||
return local; | ||
} | ||
constexpr int y = ok(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// PR c++/70331 | ||
// { dg-do compile { target c++14 } } | ||
|
||
constexpr int f(int i) { | ||
int *p = &i; | ||
if (i == 0) { | ||
int j = 123; // { dg-message "note: declared here" } | ||
p = &j; | ||
} | ||
return *p; | ||
} | ||
|
||
constexpr int i = f(0); // { dg-error "accessing object outside its lifetime" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// { dg-do compile { target c++14 } } | ||
|
||
constexpr const double& test() { | ||
const double& local = 3.0; // { dg-message "note: declared here" } | ||
return local; | ||
} | ||
|
||
static_assert(test() == 3.0, ""); // { dg-error "constant|accessing object outside its lifetime" } | ||
|
||
// no deference, shouldn't error | ||
static_assert((test(), true), ""); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// { dg-do compile { target c++14 } } | ||
// { dg-options "-Wno-return-local-addr" } | ||
|
||
constexpr const int& id(int x) { return x; } | ||
|
||
constexpr bool test() { | ||
const int& y = id(3); | ||
return y == 3; | ||
} | ||
|
||
constexpr bool x = test(); // { dg-error "" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters