brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.9 KiB · 55bbdbd Raw
74 lines · cpp
1// RUN: %clang_cc1 -fsyntax-only -Wnrvo -Wno-return-mismatch -verify %s2struct MyClass {3    int value;4    int c;5    MyClass(int v) : value(v), c(0) {}6    MyClass(const MyClass& other) : value(other.value) { c++; }7};8 9MyClass create_object(bool condition) {10    MyClass obj1(1);11    MyClass obj2(2);12    if (condition) {13        return obj1; // expected-warning{{not eliding copy on return}}14    }15    return obj2; // expected-warning{{not eliding copy on return}}16}17 18MyClass create_object2(){19    MyClass obj(1);20    return obj; // no warning21}22 23template<typename T>24T create_object3(){25    T obj(1);26    return obj; // no warning27}28 29// Known issue: if a function template uses a 30// deduced return type (i.e. auto or decltype(auto)), 31// then NRVO is not applied for any instantiation of 32// that function template 33// (see https://github.com/llvm/llvm-project/issues/95280).34template<typename T>35auto create_object4(){36    T obj(1);37    return obj; // expected-warning{{not eliding copy on return}}38}39 40template<bool F>41MyClass create_object5(){42    MyClass obj1(1);43    if constexpr (F){44        MyClass obj2(2);45        return obj2; // no warning46    }47  // Missed NRVO optimization by clang48  return obj1; // expected-warning{{not eliding copy on return}}49}50 51constexpr bool Flag = false;52 53MyClass create_object6(){54  MyClass obj1(1);55  if constexpr (Flag){56    MyClass obj2(2);57    return obj2; // expected-warning{{not eliding copy on return}}58  }59  return obj1; // no warning60}61 62void create_object7(){63    if constexpr (Flag){64        MyClass obj1(1);65        return obj1; // no warning66    }67}68 69void init_templates(){70    create_object3<MyClass>(); // no warning71    create_object4<MyClass>(); // expected-note {{in instantiation of function template specialization 'create_object4<MyClass>' requested here}}72    create_object5<false>(); // expected-note {{in instantiation of function template specialization 'create_object5<false>' requested here}}73}74