File: warn-dangling-local.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (39 lines) | stat: -rw-r--r-- 1,804 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// RUN: %clang_cc1 -verify -std=c++11 -Wdangling-assignment-gsl %s

using T = int[];

void f() {
  int *p = &(int&)(int&&)0; // expected-warning {{temporary whose address is used as value of local variable 'p' will be destroyed at the end of the full-expression}}
  p =  &(int&)(int&&)0; // expected-warning {{object backing the pointer p will be destroyed at the end of the full-expression}}

  int *q = (int *const &)T{1, 2, 3}; // expected-warning {{temporary whose address is used as value of local variable 'q' will be destroyed at the end of the full-expression}}
  q = (int *const &)T{1, 2, 3}; // expected-warning {{object backing the pointer q will be destroyed at the end of the full-expression}}

  // FIXME: We don't warn here because the 'int*' temporary is not const, but
  // it also can't have actually changed since it was created, so we could
  // still warn.
  int *r = (int *&&)T{1, 2, 3};

  // FIXME: The wording of this warning is not quite right. There are two
  // temporaries here: an 'int* const' temporary that points to the array, and
  // is lifetime-extended, and an array temporary that the pointer temporary
  // points to, which doesn't live long enough.
  int *const &s = (int *const &)T{1, 2, 3}; // expected-warning {{temporary bound to local reference 's' will be destroyed at the end of the full-expression}}
}

// PR38355
void g() {
  const int a[] = {a[0]};
  const int b[] = {a[0]};
}

namespace std {
// std::basic_string has a hard-coded gsl::owner attr.
struct basic_string {
  const char* c_str();
};
}  // namespace std
void test(const char* a) {
  // verify we're emitting the `-Wdangling-assignment-gsl` warning.
  a = std::basic_string().c_str(); // expected-warning {{object backing the pointer a will be destroyed at the end of the full-expression}}
}