Googler | b0019ca | 2021-11-26 14:20:10 +0000 | [diff] [blame] | 1 | // Part of the Crubit project, under the Apache License v2.0 with LLVM |
| 2 | // Exceptions. See /LICENSE for license information. |
| 3 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | |
| 5 | #include "lifetime_annotations/lifetime_annotations.h" |
| 6 | |
| 7 | #include <string> |
| 8 | #include <utility> |
| 9 | |
Luca Versari | c21d92f | 2022-05-25 00:56:30 -0700 | [diff] [blame] | 10 | #include "gmock/gmock.h" |
| 11 | #include "gtest/gtest.h" |
| 12 | #include "absl/status/status.h" |
| 13 | #include "common/status_test_matchers.h" |
Marcel Hlopko | 97a68a1 | 2022-03-09 11:38:51 +0000 | [diff] [blame] | 14 | #include "lifetime_annotations/test/named_func_lifetimes.h" |
| 15 | #include "lifetime_annotations/test/run_on_code.h" |
Martin Brænne | 2af40a0 | 2022-06-27 01:28:16 -0700 | [diff] [blame] | 16 | #include "lifetime_annotations/type_lifetimes.h" |
Lukasz Anforowicz | cec7a8a | 2022-04-27 10:24:51 -0700 | [diff] [blame] | 17 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 18 | #include "clang/ASTMatchers/ASTMatchers.h" |
| 19 | #include "llvm/Support/FormatVariadic.h" |
Googler | b0019ca | 2021-11-26 14:20:10 +0000 | [diff] [blame] | 20 | |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 21 | // This file contains tests both for the "legacy" lifetime annotations |
| 22 | // (`[[clang::annotate("lifetimes", ...)]]` placed on a function declaration) |
| 23 | // and the newer annotations (`[[clang::annotate_type("lifetime", ...")]]` |
| 24 | // placed on a type). This is because we expect we may continue to use the |
| 25 | // "legacy" style of annotations in sidecar files. |
| 26 | // |
| 27 | // Some tests only test one style of annotation where testing the other style |
| 28 | // does not make sense for the particular test. |
| 29 | |
Martin Brænne | 1a207c5 | 2022-04-19 00:05:38 -0700 | [diff] [blame] | 30 | namespace clang { |
| 31 | namespace tidy { |
| 32 | namespace lifetimes { |
Googler | b0019ca | 2021-11-26 14:20:10 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | |
Luca Versari | c21d92f | 2022-05-25 00:56:30 -0700 | [diff] [blame] | 35 | using crubit::IsOkAndHolds; |
| 36 | using crubit::StatusIs; |
Googler | b0019ca | 2021-11-26 14:20:10 +0000 | [diff] [blame] | 37 | using testing::StartsWith; |
Googler | b0019ca | 2021-11-26 14:20:10 +0000 | [diff] [blame] | 38 | |
Lukasz Anforowicz | 63f10da | 2022-01-26 16:59:36 +0000 | [diff] [blame] | 39 | bool IsOverloaded(const clang::FunctionDecl* func) { |
| 40 | return !func->getDeclContext()->lookup(func->getDeclName()).isSingleResult(); |
| 41 | } |
| 42 | |
Googler | b0019ca | 2021-11-26 14:20:10 +0000 | [diff] [blame] | 43 | std::string QualifiedName(const clang::FunctionDecl* func) { |
| 44 | std::string str; |
| 45 | llvm::raw_string_ostream ostream(str); |
| 46 | func->printQualifiedName(ostream); |
Lukasz Anforowicz | 63f10da | 2022-01-26 16:59:36 +0000 | [diff] [blame] | 47 | if (IsOverloaded(func)) { |
Martin Brænne | 2af40a0 | 2022-06-27 01:28:16 -0700 | [diff] [blame] | 48 | ostream << "[" << StripAttributes(func->getType()).getAsString() << "]"; |
Lukasz Anforowicz | 63f10da | 2022-01-26 16:59:36 +0000 | [diff] [blame] | 49 | } |
Googler | b0019ca | 2021-11-26 14:20:10 +0000 | [diff] [blame] | 50 | ostream.flush(); |
| 51 | return str; |
| 52 | } |
| 53 | |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 54 | // Prepends definitions for lifetime annotation macros to the code. |
| 55 | std::string WithLifetimeMacros(absl::string_view code) { |
| 56 | std::string result = R"( |
Martin Brænne | 8bc2de3 | 2022-06-24 07:01:52 -0700 | [diff] [blame] | 57 | // TODO(mboehme): We would prefer `$(...)` to be a variadic macro that |
| 58 | // stringizes each of its macro arguments individually. This is possible but |
| 59 | // requires some contortions: https://stackoverflow.com/a/5958315 |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 60 | #define $(l) [[clang::annotate_type("lifetime", #l)]] |
Martin Brænne | 8bc2de3 | 2022-06-24 07:01:52 -0700 | [diff] [blame] | 61 | #define $2(l1, l2) [[clang::annotate_type("lifetime", #l1, #l2)]] |
| 62 | #define $3(l1, l2, l3) [[clang::annotate_type("lifetime", #l1, #l2, #l3)]] |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 63 | )"; |
| 64 | for (char l = 'a'; l <= 'z'; ++l) { |
| 65 | absl::StrAppendFormat(&result, "#define $%c $(%c)\n", l, l); |
| 66 | } |
Martin Brænne | ddf2e82 | 2022-06-27 00:43:56 -0700 | [diff] [blame] | 67 | absl::StrAppend(&result, "#define $static $(static)\n"); |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 68 | absl::StrAppend(&result, code); |
| 69 | return result; |
| 70 | } |
| 71 | |
Googler | b0019ca | 2021-11-26 14:20:10 +0000 | [diff] [blame] | 72 | class LifetimeAnnotationsTest : public testing::Test { |
| 73 | protected: |
| 74 | absl::StatusOr<NamedFuncLifetimes> GetNamedLifetimeAnnotations( |
| 75 | absl::string_view code, |
| 76 | const clang::tooling::FileContentMappings& file_contents = |
| 77 | clang::tooling::FileContentMappings()) { |
| 78 | absl::StatusOr<NamedFuncLifetimes> result; |
Googler | 656e5b2 | 2022-03-01 09:40:47 +0000 | [diff] [blame] | 79 | bool success = runOnCodeWithLifetimeHandlers( |
Googler | 1cd6687 | 2021-12-10 11:40:03 +0000 | [diff] [blame] | 80 | llvm::StringRef(code.data(), code.size()), |
Googler | b0019ca | 2021-11-26 14:20:10 +0000 | [diff] [blame] | 81 | [&result](clang::ASTContext& ast_context, |
| 82 | const LifetimeAnnotationContext& lifetime_context) { |
| 83 | using clang::ast_matchers::findAll; |
| 84 | using clang::ast_matchers::functionDecl; |
| 85 | using clang::ast_matchers::match; |
| 86 | |
| 87 | NamedFuncLifetimes named_func_lifetimes; |
| 88 | for (const auto& node : |
| 89 | match(findAll(functionDecl().bind("func")), ast_context)) { |
| 90 | if (const auto* func = |
| 91 | node.getNodeAs<clang::FunctionDecl>("func")) { |
Martin Brænne | bd003e9 | 2022-06-27 23:22:27 -0700 | [diff] [blame] | 92 | // Skip various categories of function: |
| 93 | // - Template instantiation don't contain any annotations that |
| 94 | // aren't present in the template itself, but they may contain |
| 95 | // reference-like types (which will obviously be unannotated), |
| 96 | // which will generate nuisance "lifetime elision not enabled" |
| 97 | // errors. |
| 98 | // - Implicitly defaulted functions obviously cannot contain |
| 99 | // lifetime annotations. They will need to be handled through |
| 100 | // `AnalyzeDefaultedFunction()` in analyze.cc. |
| 101 | if (func->isTemplateInstantiation() || |
| 102 | (func->isDefaulted() && !func->isExplicitlyDefaulted())) { |
| 103 | continue; |
| 104 | } |
| 105 | |
Googler | bea5dd1 | 2021-11-30 11:47:04 +0000 | [diff] [blame] | 106 | LifetimeSymbolTable symbol_table; |
Googler | b0019ca | 2021-11-26 14:20:10 +0000 | [diff] [blame] | 107 | llvm::Expected<FunctionLifetimes> func_lifetimes = |
Googler | bea5dd1 | 2021-11-30 11:47:04 +0000 | [diff] [blame] | 108 | GetLifetimeAnnotations(func, lifetime_context, &symbol_table); |
Googler | b0019ca | 2021-11-26 14:20:10 +0000 | [diff] [blame] | 109 | |
Martin Brænne | 2af40a0 | 2022-06-27 01:28:16 -0700 | [diff] [blame] | 110 | std::string new_entry; |
| 111 | if (func_lifetimes) { |
| 112 | new_entry = NameLifetimes(*func_lifetimes, symbol_table); |
| 113 | } else { |
| 114 | new_entry = absl::StrCat( |
| 115 | "ERROR: ", llvm::toString(func_lifetimes.takeError())); |
Googler | b0019ca | 2021-11-26 14:20:10 +0000 | [diff] [blame] | 116 | } |
Martin Brænne | 2af40a0 | 2022-06-27 01:28:16 -0700 | [diff] [blame] | 117 | |
Lukasz Anforowicz | 63f10da | 2022-01-26 16:59:36 +0000 | [diff] [blame] | 118 | std::string func_name = QualifiedName(func); |
Lukasz Anforowicz | 63f10da | 2022-01-26 16:59:36 +0000 | [diff] [blame] | 119 | std::optional<llvm::StringRef> old_entry = |
| 120 | named_func_lifetimes.Get(func_name); |
| 121 | if (old_entry.has_value()) { |
| 122 | if (new_entry != old_entry.value()) { |
| 123 | result = absl::UnknownError( |
| 124 | llvm::formatv( |
| 125 | "Unexpectedly different lifetimes for function '{0}'." |
| 126 | "Old: '{1}'. New: '{2}'.", |
| 127 | func_name, old_entry.value(), new_entry) |
| 128 | .str()); |
| 129 | return; |
| 130 | } |
| 131 | } else { |
| 132 | named_func_lifetimes.Add(std::move(func_name), |
| 133 | std::move(new_entry)); |
| 134 | } |
Googler | b0019ca | 2021-11-26 14:20:10 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | result = std::move(named_func_lifetimes); |
| 139 | }, |
| 140 | {}, file_contents); |
| 141 | |
Googler | 656e5b2 | 2022-03-01 09:40:47 +0000 | [diff] [blame] | 142 | if (!success) { |
| 143 | return absl::UnknownError( |
| 144 | "Error extracting lifetimes. (Compilation error?)"); |
| 145 | } |
| 146 | |
Googler | b0019ca | 2021-11-26 14:20:10 +0000 | [diff] [blame] | 147 | return result; |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | TEST_F(LifetimeAnnotationsTest, NoLifetimes) { |
| 152 | EXPECT_THAT(GetNamedLifetimeAnnotations(R"( |
| 153 | int f(int); |
| 154 | )"), |
| 155 | IsOkAndHolds(LifetimesAre({{"f", "()"}}))); |
| 156 | } |
| 157 | |
Googler | 656e5b2 | 2022-03-01 09:40:47 +0000 | [diff] [blame] | 158 | TEST_F(LifetimeAnnotationsTest, Failure_CompileError) { |
| 159 | EXPECT_THAT( |
| 160 | GetNamedLifetimeAnnotations(R"( |
| 161 | undefined f(undefined); |
| 162 | )"), |
| 163 | StatusIs(absl::StatusCode::kUnknown, |
| 164 | StartsWith("Error extracting lifetimes. (Compilation error?)"))); |
| 165 | } |
| 166 | |
Googler | b0019ca | 2021-11-26 14:20:10 +0000 | [diff] [blame] | 167 | TEST_F(LifetimeAnnotationsTest, Failure_NoAnnotationsNoLifetimeElision) { |
| 168 | EXPECT_THAT(GetNamedLifetimeAnnotations(R"( |
| 169 | int** f(int*); |
| 170 | )"), |
Martin Brænne | 2af40a0 | 2022-06-27 01:28:16 -0700 | [diff] [blame] | 171 | IsOkAndHolds(LifetimesAre( |
| 172 | {{"f", "ERROR: Lifetime elision not enabled for 'f'"}}))); |
Googler | b0019ca | 2021-11-26 14:20:10 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Googler | 6804a01 | 2022-01-05 07:04:36 +0000 | [diff] [blame] | 175 | TEST_F(LifetimeAnnotationsTest, Failure_NoOutputAnnotationNoLifetimeElision) { |
| 176 | EXPECT_THAT(GetNamedLifetimeAnnotations(R"( |
| 177 | int* f(); |
| 178 | )"), |
Martin Brænne | 2af40a0 | 2022-06-27 01:28:16 -0700 | [diff] [blame] | 179 | // We specifically want to see this error message rather than |
| 180 | // "Cannot elide output lifetimes". |
| 181 | IsOkAndHolds(LifetimesAre( |
| 182 | {{"f", "ERROR: Lifetime elision not enabled for 'f'"}}))); |
Googler | 6804a01 | 2022-01-05 07:04:36 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Googler | b0019ca | 2021-11-26 14:20:10 +0000 | [diff] [blame] | 185 | TEST_F(LifetimeAnnotationsTest, Failure_NoAnnotationsElisionPragmaInWrongFile) { |
| 186 | EXPECT_THAT(GetNamedLifetimeAnnotations(R"( |
Googler | 33f768d | 2021-11-29 10:33:49 +0000 | [diff] [blame] | 187 | #pragma clang lifetime_elision |
Googler | b0019ca | 2021-11-26 14:20:10 +0000 | [diff] [blame] | 188 | #include "header.h" |
| 189 | )", |
| 190 | {std::make_pair("header.h", R"( |
| 191 | int** f(int*); |
| 192 | )")}), |
Martin Brænne | 2af40a0 | 2022-06-27 01:28:16 -0700 | [diff] [blame] | 193 | IsOkAndHolds(LifetimesAre( |
| 194 | {{"f", "ERROR: Lifetime elision not enabled for 'f'"}}))); |
Googler | b0019ca | 2021-11-26 14:20:10 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | TEST_F(LifetimeAnnotationsTest, LifetimeElision_OneInputLifetime) { |
| 198 | EXPECT_THAT(GetNamedLifetimeAnnotations(R"( |
Googler | 33f768d | 2021-11-29 10:33:49 +0000 | [diff] [blame] | 199 | #pragma clang lifetime_elision |
Googler | b0019ca | 2021-11-26 14:20:10 +0000 | [diff] [blame] | 200 | int** f(int*); |
| 201 | )"), |
| 202 | IsOkAndHolds(LifetimesAre({{"f", "a -> (a, a)"}}))); |
| 203 | } |
| 204 | |
| 205 | TEST_F(LifetimeAnnotationsTest, LifetimeElision_NoOutputLifetimes) { |
| 206 | EXPECT_THAT(GetNamedLifetimeAnnotations(R"( |
Googler | 33f768d | 2021-11-29 10:33:49 +0000 | [diff] [blame] | 207 | #pragma clang lifetime_elision |
Googler | b0019ca | 2021-11-26 14:20:10 +0000 | [diff] [blame] | 208 | void f(int**, int *); |
| 209 | )"), |
| 210 | IsOkAndHolds(LifetimesAre({{"f", "(a, b), c"}}))); |
| 211 | } |
| 212 | |
| 213 | TEST_F(LifetimeAnnotationsTest, LifetimeElision_Templates) { |
| 214 | EXPECT_THAT(GetNamedLifetimeAnnotations(R"( |
Googler | 33f768d | 2021-11-29 10:33:49 +0000 | [diff] [blame] | 215 | #pragma clang lifetime_elision |
Googler | b0019ca | 2021-11-26 14:20:10 +0000 | [diff] [blame] | 216 | template <class T> class vector {}; |
| 217 | int* f(vector<int *>); |
| 218 | vector<int*> g(int *); |
| 219 | )"), |
| 220 | IsOkAndHolds(LifetimesAre({{"f", "a -> a"}, {"g", "a -> a"}}))); |
| 221 | } |
| 222 | |
Googler | e4d4ec4 | 2022-03-28 06:52:23 -0700 | [diff] [blame] | 223 | TEST_F(LifetimeAnnotationsTest, LifetimeElision_NestedTemplates) { |
Luca Versari | 6ee6b34 | 2022-04-05 05:40:52 -0700 | [diff] [blame] | 224 | EXPECT_THAT(GetNamedLifetimeAnnotations(R"( |
Googler | bdc5580 | 2022-03-24 11:21:37 +0000 | [diff] [blame] | 225 | #pragma clang lifetime_elision |
| 226 | template <class T> |
| 227 | struct Outer { |
| 228 | template <class U> |
| 229 | struct Inner { |
| 230 | }; |
| 231 | }; |
| 232 | void f(Outer<int *>::Inner<int *> &); |
| 233 | Outer<int *>::Inner<int *> g(int *); |
| 234 | )"), |
Luca Versari | 6ee6b34 | 2022-04-05 05:40:52 -0700 | [diff] [blame] | 235 | IsOkAndHolds(LifetimesAre( |
| 236 | {{"f", "(<a>::<b>, c)"}, {"g", "a -> <a>::<a>"}}))); |
Googler | bdc5580 | 2022-03-24 11:21:37 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Googler | fbb37d2 | 2022-03-02 15:13:53 +0000 | [diff] [blame] | 239 | TEST_F(LifetimeAnnotationsTest, LifetimeElision_LifetimeParameterizedType) { |
| 240 | EXPECT_THAT(GetNamedLifetimeAnnotations(R"( |
| 241 | #pragma clang lifetime_elision |
| 242 | struct [[clang::annotate("lifetime_params", "s")]] string_view{}; |
| 243 | string_view f(string_view); |
| 244 | )"), |
| 245 | IsOkAndHolds(LifetimesAre({{"f", "a -> a"}}))); |
| 246 | } |
| 247 | |
Googler | b0019ca | 2021-11-26 14:20:10 +0000 | [diff] [blame] | 248 | TEST_F(LifetimeAnnotationsTest, LifetimeElision_Method) { |
| 249 | EXPECT_THAT(GetNamedLifetimeAnnotations(R"( |
Googler | 33f768d | 2021-11-29 10:33:49 +0000 | [diff] [blame] | 250 | #pragma clang lifetime_elision |
Googler | b0019ca | 2021-11-26 14:20:10 +0000 | [diff] [blame] | 251 | struct S { |
| 252 | int** method(int *, int *); |
| 253 | }; |
| 254 | )"), |
Googler | 6804a01 | 2022-01-05 07:04:36 +0000 | [diff] [blame] | 255 | IsOkAndHolds(LifetimesAre({{"S::method", "a: b, c -> (a, a)"}}))); |
Googler | b0019ca | 2021-11-26 14:20:10 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Devin Jeanpierre | 108e9c0 | 2022-06-02 07:10:09 -0700 | [diff] [blame] | 258 | TEST_F(LifetimeAnnotationsTest, LifetimeElision_Destructor) { |
| 259 | EXPECT_THAT(GetNamedLifetimeAnnotations(R"cc( |
| 260 | // Note: this works even without #pragma clang lifetime_elision |
| 261 | struct S { |
| 262 | ~S(); |
| 263 | }; |
| 264 | )cc"), |
| 265 | IsOkAndHolds(LifetimesAre({{"S::~S", "a:"}}))); |
| 266 | } |
| 267 | |
Lukasz Anforowicz | 63f10da | 2022-01-26 16:59:36 +0000 | [diff] [blame] | 268 | TEST_F(LifetimeAnnotationsTest, LifetimeElision_ExplicitlyDefaultedCtor) { |
| 269 | EXPECT_THAT(GetNamedLifetimeAnnotations(R"( |
| 270 | #pragma clang lifetime_elision |
| 271 | struct S { |
| 272 | S() = default; |
| 273 | };)"), |
| 274 | IsOkAndHolds(LifetimesAre({{"S::S", "a:"}}))); |
| 275 | } |
| 276 | |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 277 | TEST_F(LifetimeAnnotationsTest, LifetimeElision_ArrayParamLifetimes) { |
| 278 | EXPECT_THAT(GetNamedLifetimeAnnotations(R"( |
| 279 | #pragma clang lifetime_elision |
| 280 | void f(int pair[2]); |
| 281 | )"), |
| 282 | IsOkAndHolds(LifetimesAre({{"f", "a"}}))); |
| 283 | } |
| 284 | |
| 285 | TEST_F(LifetimeAnnotationsTest, LifetimeElision_ArrayParamAsTypedefLifetimes) { |
| 286 | EXPECT_THAT(GetNamedLifetimeAnnotations(R"( |
| 287 | #pragma clang lifetime_elision |
Googler | 656e5b2 | 2022-03-01 09:40:47 +0000 | [diff] [blame] | 288 | typedef int Arr[2]; |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 289 | void f(Arr); |
| 290 | )"), |
| 291 | IsOkAndHolds(LifetimesAre({{"f", "a"}}))); |
| 292 | } |
| 293 | |
| 294 | TEST_F(LifetimeAnnotationsTest, LifetimeElision_FunctionPointerLifetimes) { |
| 295 | EXPECT_THAT(GetNamedLifetimeAnnotations(R"( |
| 296 | #pragma clang lifetime_elision |
| 297 | void f(void (*)()); |
| 298 | )"), |
| 299 | IsOkAndHolds(LifetimesAre({{"f", "a"}}))); |
| 300 | } |
| 301 | |
| 302 | TEST_F(LifetimeAnnotationsTest, |
| 303 | LifetimeElision_FunctionPointerAsTypedefLifetimes) { |
| 304 | EXPECT_THAT(GetNamedLifetimeAnnotations(R"( |
| 305 | #pragma clang lifetime_elision |
| 306 | typedef void (*FunctionPointer)(); |
| 307 | void f(FunctionPointer hook); |
| 308 | )"), |
| 309 | IsOkAndHolds(LifetimesAre({{"f", "a"}}))); |
| 310 | } |
| 311 | |
| 312 | TEST_F(LifetimeAnnotationsTest, LifetimeElision_FunctionReferenceLifetimes) { |
| 313 | EXPECT_THAT(GetNamedLifetimeAnnotations(R"( |
| 314 | #pragma clang lifetime_elision |
| 315 | typedef void (&FunctionReference)(); |
| 316 | void f(FunctionReference hook); |
| 317 | )"), |
| 318 | IsOkAndHolds(LifetimesAre({{"f", "a"}}))); |
| 319 | } |
| 320 | |
| 321 | TEST_F(LifetimeAnnotationsTest, |
| 322 | LifetimeElision_FunctionReferenceAsTypedefLifetimes) { |
| 323 | EXPECT_THAT(GetNamedLifetimeAnnotations(R"( |
| 324 | #pragma clang lifetime_elision |
| 325 | void f(void (&)()); |
| 326 | )"), |
| 327 | IsOkAndHolds(LifetimesAre({{"f", "a"}}))); |
| 328 | } |
| 329 | |
Googler | 2e1f0c1 | 2022-03-01 09:26:30 +0000 | [diff] [blame] | 330 | TEST_F(LifetimeAnnotationsTest, |
| 331 | LifetimeElision_PointerToMemberDoesNotGetLifetime) { |
| 332 | EXPECT_THAT(GetNamedLifetimeAnnotations(R"( |
| 333 | #pragma clang lifetime_elision |
| 334 | struct S {}; |
| 335 | void f(int S::*ptr_to_member); |
| 336 | )"), |
| 337 | IsOkAndHolds(LifetimesAre({{"f", "()"}}))); |
| 338 | } |
| 339 | |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 340 | TEST_F(LifetimeAnnotationsTest, LifetimeElision_FailureTooFewInputLifetimes) { |
| 341 | EXPECT_THAT(GetNamedLifetimeAnnotations(R"( |
| 342 | #pragma clang lifetime_elision |
| 343 | int* f(); |
| 344 | )"), |
Martin Brænne | 2af40a0 | 2022-06-27 01:28:16 -0700 | [diff] [blame] | 345 | IsOkAndHolds(LifetimesAre( |
| 346 | {{"f", |
| 347 | "ERROR: Cannot elide output lifetimes for 'f' because it " |
| 348 | "is a non-member function that does not have exactly one " |
| 349 | "input lifetime"}}))); |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | TEST_F(LifetimeAnnotationsTest, LifetimeElision_FailureTooManyInputLifetimes) { |
| 353 | EXPECT_THAT(GetNamedLifetimeAnnotations(R"( |
| 354 | #pragma clang lifetime_elision |
| 355 | int* f(int**); |
| 356 | )"), |
Martin Brænne | 2af40a0 | 2022-06-27 01:28:16 -0700 | [diff] [blame] | 357 | IsOkAndHolds(LifetimesAre( |
| 358 | {{"f", |
| 359 | "ERROR: Cannot elide output lifetimes for 'f' because it " |
| 360 | "is a non-member function that does not have exactly one " |
| 361 | "input lifetime"}}))); |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | TEST_F(LifetimeAnnotationsTest, LifetimeAnnotation_NoLifetimes) { |
| 365 | EXPECT_THAT(GetNamedLifetimeAnnotations(R"_( |
| 366 | [[clang::annotate("lifetimes", "()")]] |
| 367 | void f(int); |
| 368 | )_"), |
| 369 | IsOkAndHolds(LifetimesAre({{"f", "()"}}))); |
| 370 | } |
| 371 | |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 372 | TEST_F(LifetimeAnnotationsTest, LifetimeAnnotation_BadAttributeArgument) { |
| 373 | EXPECT_THAT( |
| 374 | GetNamedLifetimeAnnotations(WithLifetimeMacros(R"( |
| 375 | void f(int* [[clang::annotate_type("lifetime", 1)]]); |
| 376 | )")), |
Martin Brænne | 2af40a0 | 2022-06-27 01:28:16 -0700 | [diff] [blame] | 377 | IsOkAndHolds(LifetimesAre( |
| 378 | {{"f", "ERROR: cannot evaluate argument as a string literal"}}))); |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 381 | TEST_F(LifetimeAnnotationsTest, LifetimeAnnotation_Simple) { |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 382 | EXPECT_THAT(GetNamedLifetimeAnnotations(WithLifetimeMacros(R"( |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 383 | [[clang::annotate("lifetimes", "a -> a")]] |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 384 | int* f1(int*); |
| 385 | int* $a f2(int* $a); |
| 386 | )")), |
| 387 | IsOkAndHolds(LifetimesAre({{"f1", "a -> a"}, {"f2", "a -> a"}}))); |
| 388 | } |
| 389 | |
| 390 | TEST_F(LifetimeAnnotationsTest, LifetimeAnnotation_SimpleRef) { |
| 391 | EXPECT_THAT(GetNamedLifetimeAnnotations(WithLifetimeMacros(R"( |
| 392 | [[clang::annotate("lifetimes", "a -> a")]] |
| 393 | int& f1(int&); |
| 394 | int& $a f2(int& $a); |
| 395 | )")), |
| 396 | IsOkAndHolds(LifetimesAre({{"f1", "a -> a"}, {"f2", "a -> a"}}))); |
| 397 | } |
| 398 | |
| 399 | TEST_F(LifetimeAnnotationsTest, |
| 400 | LifetimeAnnotation_Invalid_MultipleLifetimesOnPointer) { |
| 401 | EXPECT_THAT( |
| 402 | GetNamedLifetimeAnnotations(WithLifetimeMacros(R"( |
Martin Brænne | 8bc2de3 | 2022-06-24 07:01:52 -0700 | [diff] [blame] | 403 | void f(int* $2(a, b)); |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 404 | )")), |
Martin Brænne | 2af40a0 | 2022-06-27 01:28:16 -0700 | [diff] [blame] | 405 | IsOkAndHolds(LifetimesAre( |
| 406 | {{"f", "ERROR: Expected a single lifetime but 2 were given"}}))); |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | TEST_F(LifetimeAnnotationsTest, LifetimeAnnotation_Static) { |
| 410 | EXPECT_THAT(GetNamedLifetimeAnnotations(WithLifetimeMacros(R"( |
| 411 | [[clang::annotate("lifetimes", "static -> static")]] |
| 412 | int* f1(int*); |
| 413 | int* $static f2(int* $static); |
| 414 | )")), |
| 415 | IsOkAndHolds(LifetimesAre( |
| 416 | {{"f1", "static -> static"}, {"f2", "static -> static"}}))); |
| 417 | } |
| 418 | |
| 419 | TEST_F(LifetimeAnnotationsTest, LifetimeAnnotation_PartialElision) { |
| 420 | EXPECT_THAT(GetNamedLifetimeAnnotations(WithLifetimeMacros(R"( |
| 421 | #pragma clang lifetime_elision |
| 422 | int* $a f(int* $a, int*, int* $a); |
| 423 | )")), |
| 424 | IsOkAndHolds(LifetimesAre({{"f", "a, b, a -> a"}}))); |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | TEST_F(LifetimeAnnotationsTest, LifetimeAnnotation_MultiplePtr) { |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 428 | EXPECT_THAT(GetNamedLifetimeAnnotations(WithLifetimeMacros(R"( |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 429 | [[clang::annotate("lifetimes", "(a, b) -> a")]] |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 430 | int* f1(int**); |
| 431 | int* $a f2(int* $a * $b); |
| 432 | )")), |
| 433 | IsOkAndHolds(LifetimesAre( |
| 434 | {{"f1", "(a, b) -> a"}, {"f2", "(a, b) -> a"}}))); |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | TEST_F(LifetimeAnnotationsTest, LifetimeAnnotation_MultipleArguments) { |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 438 | EXPECT_THAT( |
| 439 | GetNamedLifetimeAnnotations(WithLifetimeMacros(R"( |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 440 | [[clang::annotate("lifetimes", "a, b -> a")]] |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 441 | int* f1(int*, int*); |
| 442 | int* $a f2(int* $a, int* $b); |
| 443 | )")), |
| 444 | IsOkAndHolds(LifetimesAre({{"f1", "a, b -> a"}, {"f2", "a, b -> a"}}))); |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | TEST_F(LifetimeAnnotationsTest, LifetimeAnnotation_NoReturn) { |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 448 | EXPECT_THAT(GetNamedLifetimeAnnotations(WithLifetimeMacros(R"( |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 449 | [[clang::annotate("lifetimes", "a, b")]] |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 450 | void f1(int*, int*); |
| 451 | void f2(int* $a, int* $b); |
| 452 | )")), |
| 453 | IsOkAndHolds(LifetimesAre({{"f1", "a, b"}, {"f2", "a, b"}}))); |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 456 | TEST_F(LifetimeAnnotationsTest, LifetimeAnnotation_ParamWithoutLifetime) { |
| 457 | EXPECT_THAT(GetNamedLifetimeAnnotations(WithLifetimeMacros(R"( |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 458 | [[clang::annotate("lifetimes", "a, (), a -> a")]] |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 459 | int* f1(int*, int, int*); |
| 460 | int* $a f2(int* $a, int, int* $a); |
| 461 | )")), |
| 462 | IsOkAndHolds(LifetimesAre( |
| 463 | {{"f1", "a, (), a -> a"}, {"f2", "a, (), a -> a"}}))); |
| 464 | } |
| 465 | |
| 466 | TEST_F(LifetimeAnnotationsTest, LifetimeAnnotation_LifetimeParameterizedType) { |
| 467 | // Use a custom delimiter so that the `")` in the `clang::annotate` attribute |
| 468 | // below doesn't prematurely terminate the string. |
| 469 | EXPECT_THAT(GetNamedLifetimeAnnotations(WithLifetimeMacros(R"code( |
| 470 | struct [[clang::annotate("lifetime_params", "a", "b")]] S_param {}; |
| 471 | |
| 472 | [[clang::annotate("lifetimes", "([a, b]) -> ([a, b])")]] |
| 473 | S_param f1(S_param s); |
| 474 | |
Martin Brænne | 8bc2de3 | 2022-06-24 07:01:52 -0700 | [diff] [blame] | 475 | S_param $2(a, b) f2(S_param $2(a, b) s); |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 476 | )code")), |
| 477 | IsOkAndHolds(LifetimesAre({{"f1", "([a, b]) -> ([a, b])"}, |
| 478 | {"f2", "([a, b]) -> ([a, b])"}}))); |
| 479 | } |
| 480 | |
Martin Brænne | 8bc2de3 | 2022-06-24 07:01:52 -0700 | [diff] [blame] | 481 | TEST_F( |
| 482 | LifetimeAnnotationsTest, |
| 483 | LifetimeAnnotation_LifetimeParameterizedType_Invalid_WrongNumberOfLifetimes) { |
Martin Brænne | 2af40a0 | 2022-06-27 01:28:16 -0700 | [diff] [blame] | 484 | EXPECT_THAT( |
| 485 | GetNamedLifetimeAnnotations(WithLifetimeMacros(R"( |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 486 | struct [[clang::annotate("lifetime_params", "a", "b")]] S_param {}; |
| 487 | |
Martin Brænne | 8bc2de3 | 2022-06-24 07:01:52 -0700 | [diff] [blame] | 488 | void f(S_param $3(a, b, c) s); |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 489 | )")), |
Martin Brænne | 2af40a0 | 2022-06-27 01:28:16 -0700 | [diff] [blame] | 490 | IsOkAndHolds(LifetimesAre({{"f", |
| 491 | "ERROR: Type has 2 lifetime parameters but 3 " |
| 492 | "lifetime arguments were given"}}))); |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Martin Brænne | 8bc2de3 | 2022-06-24 07:01:52 -0700 | [diff] [blame] | 495 | TEST_F( |
| 496 | LifetimeAnnotationsTest, |
| 497 | LifetimeAnnotation_LifetimeParameterizedType_Invalid_MultipleAnnotateAttributes) { |
Martin Brænne | 2af40a0 | 2022-06-27 01:28:16 -0700 | [diff] [blame] | 498 | EXPECT_THAT(GetNamedLifetimeAnnotations(WithLifetimeMacros(R"( |
Martin Brænne | 8bc2de3 | 2022-06-24 07:01:52 -0700 | [diff] [blame] | 499 | struct [[clang::annotate("lifetime_params", "a", "b")]] S_param {}; |
| 500 | |
| 501 | void f(S_param $a $b s); |
| 502 | )")), |
Martin Brænne | 2af40a0 | 2022-06-27 01:28:16 -0700 | [diff] [blame] | 503 | IsOkAndHolds(LifetimesAre( |
| 504 | {{"f", |
| 505 | "ERROR: Only one `[[annotate_type(\"lifetime\", ...)]]` " |
| 506 | "attribute may be placed on a type"}}))); |
Martin Brænne | 8bc2de3 | 2022-06-24 07:01:52 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 509 | TEST_F(LifetimeAnnotationsTest, LifetimeAnnotation_Template) { |
| 510 | EXPECT_THAT(GetNamedLifetimeAnnotations(WithLifetimeMacros(R"( |
| 511 | template <class T> class vector {}; |
| 512 | |
| 513 | [[clang::annotate("lifetimes", "(a, b) -> a")]] |
| 514 | int* f1(const vector<int *> &); |
| 515 | int* $a f2(const vector<int * $a> & $b); |
| 516 | )")), |
| 517 | IsOkAndHolds(LifetimesAre( |
| 518 | {{"f1", "(a, b) -> a"}, {"f2", "(a, b) -> a"}}))); |
| 519 | } |
| 520 | |
| 521 | TEST_F(LifetimeAnnotationsTest, LifetimeAnnotation_VariadicTemplate) { |
| 522 | EXPECT_THAT(GetNamedLifetimeAnnotations(WithLifetimeMacros(R"code( |
| 523 | template <class... T> class variadic{}; |
| 524 | |
| 525 | [[clang::annotate("lifetimes", "(<a, b>, c)")]] |
| 526 | void f1(const variadic<int *, int *> &); |
| 527 | void f2(const variadic<int * $a, int * $b> & $c); |
| 528 | )code")), |
| 529 | IsOkAndHolds(LifetimesAre( |
| 530 | {{"f1", "(<a, b>, c)"}, {"f2", "(<a, b>, c)"}}))); |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Martin Brænne | 33f4f50 | 2022-06-27 01:15:15 -0700 | [diff] [blame] | 533 | TEST_F(LifetimeAnnotationsTest, LifetimeAnnotation_VariadicTemplateWithCtor) { |
Martin Brænne | 33f4f50 | 2022-06-27 01:15:15 -0700 | [diff] [blame] | 534 | EXPECT_THAT(GetNamedLifetimeAnnotations(WithLifetimeMacros(R"code( |
| 535 | template <typename... Args> struct S { S() $a {} }; |
| 536 | template <typename T, typename... Args> |
| 537 | struct S<T, Args...> { |
| 538 | S(T t, Args... args) $a {} |
| 539 | }; |
| 540 | |
Martin Brænne | bd003e9 | 2022-06-27 23:22:27 -0700 | [diff] [blame] | 541 | void target(int* $a a, int* $b b) { |
Martin Brænne | 33f4f50 | 2022-06-27 01:15:15 -0700 | [diff] [blame] | 542 | S<int*, int*> s = {a, b}; |
| 543 | } |
| 544 | )code")), |
Martin Brænne | bd003e9 | 2022-06-27 23:22:27 -0700 | [diff] [blame] | 545 | IsOkAndHolds(LifetimesAre({{"S::S<Args...>", "a:"}, |
| 546 | {"S<type-parameter-0-0, " |
| 547 | "type-parameter-0-1...>::" |
| 548 | "S<type-parameter-0-0, " |
| 549 | "type-parameter-0-1...>", |
| 550 | "a: (), ()"}, |
| 551 | {"target", "a, b"}}))); |
Martin Brænne | 33f4f50 | 2022-06-27 01:15:15 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 554 | TEST_F(LifetimeAnnotationsTest, LifetimeAnnotation_Method) { |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 555 | EXPECT_THAT( |
| 556 | GetNamedLifetimeAnnotations(WithLifetimeMacros(R"( |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 557 | struct S { |
| 558 | [[clang::annotate("lifetimes", "a: -> a")]] |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 559 | int* f1(); |
| 560 | int* $a f2() $a; |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 561 | }; |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 562 | )")), |
| 563 | IsOkAndHolds(LifetimesAre({{"S::f1", "a: -> a"}, {"S::f2", "a: -> a"}}))); |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | TEST_F(LifetimeAnnotationsTest, LifetimeAnnotation_MethodWithParam) { |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 567 | EXPECT_THAT(GetNamedLifetimeAnnotations(WithLifetimeMacros(R"( |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 568 | struct S { |
| 569 | [[clang::annotate("lifetimes", "a: b -> a")]] |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 570 | int* f1(int*); |
| 571 | int* $a f2(int* $b) $a; |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 572 | }; |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 573 | )")), |
| 574 | IsOkAndHolds(LifetimesAre( |
| 575 | {{"S::f1", "a: b -> a"}, {"S::f2", "a: b -> a"}}))); |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Luca Versari | 95ce68f | 2022-03-24 14:44:19 +0000 | [diff] [blame] | 578 | TEST_F(LifetimeAnnotationsTest, LifetimeAnnotation_MethodWithLifetimeParams) { |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 579 | EXPECT_THAT(GetNamedLifetimeAnnotations(WithLifetimeMacros(R"( |
Luca Versari | 95ce68f | 2022-03-24 14:44:19 +0000 | [diff] [blame] | 580 | struct [[clang::annotate("lifetime_params", "x", "y")]] S { |
Luca Versari | 6ee6b34 | 2022-04-05 05:40:52 -0700 | [diff] [blame] | 581 | [[clang::annotate("lifetimes", "([x, y], a): -> x")]] |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 582 | int* f1(); |
| 583 | // It's implied that the lifetime parameters of `this` are $x and $y |
| 584 | // because this is a member function on struct with those lifetime |
| 585 | // parameters. |
| 586 | // TODO(mboehme): This doesn't work yet. We need some special handling |
| 587 | // to know that in this context, the type `S` doesn't need lifetimes |
| 588 | // put on it. |
| 589 | // TODO(mboehme): How do we resolve this difference relative to the |
| 590 | // "legacy" lifetime annotations? Does this mean that they should also |
| 591 | // not include the lifetimes x and y? |
| 592 | // int* $x f2() $a; |
Luca Versari | 95ce68f | 2022-03-24 14:44:19 +0000 | [diff] [blame] | 593 | }; |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 594 | )")), |
| 595 | IsOkAndHolds(LifetimesAre({{"S::f1", "([x, y], a): -> x"}}))); |
Luca Versari | 95ce68f | 2022-03-24 14:44:19 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 598 | TEST_F(LifetimeAnnotationsTest, LifetimeAnnotation_Invalid_MissingThis) { |
Martin Brænne | 2af40a0 | 2022-06-27 01:28:16 -0700 | [diff] [blame] | 599 | EXPECT_THAT( |
| 600 | GetNamedLifetimeAnnotations(WithLifetimeMacros(R"( |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 601 | struct S { |
| 602 | [[clang::annotate("lifetimes", "-> a")]] |
| 603 | int* f(); |
| 604 | }; |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 605 | )")), |
Martin Brænne | 2af40a0 | 2022-06-27 01:28:16 -0700 | [diff] [blame] | 606 | IsOkAndHolds(LifetimesAre( |
| 607 | {{"S::f", |
| 608 | "ERROR: Invalid lifetime annotation: too few lifetimes"}}))); |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 609 | EXPECT_THAT(GetNamedLifetimeAnnotations(WithLifetimeMacros(R"( |
| 610 | struct S { |
| 611 | int* $a f(); |
| 612 | }; |
| 613 | )")), |
Martin Brænne | 2af40a0 | 2022-06-27 01:28:16 -0700 | [diff] [blame] | 614 | IsOkAndHolds(LifetimesAre( |
| 615 | {{"S::f", "ERROR: Lifetime elision not enabled for 'f'"}}))); |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | TEST_F(LifetimeAnnotationsTest, LifetimeAnnotation_Invalid_ThisOnFreeFunction) { |
Martin Brænne | 2af40a0 | 2022-06-27 01:28:16 -0700 | [diff] [blame] | 619 | EXPECT_THAT( |
| 620 | GetNamedLifetimeAnnotations(WithLifetimeMacros(R"( |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 621 | [[clang::annotate("lifetimes", "a: a -> a")]] |
| 622 | int* f(int*); |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 623 | )")), |
Martin Brænne | 2af40a0 | 2022-06-27 01:28:16 -0700 | [diff] [blame] | 624 | IsOkAndHolds(LifetimesAre( |
| 625 | {{"f", "ERROR: Invalid lifetime annotation: too many lifetimes"}}))); |
| 626 | EXPECT_THAT( |
| 627 | GetNamedLifetimeAnnotations(WithLifetimeMacros(R"( |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 628 | int* $a f(int* $a) $a; |
| 629 | )")), |
Martin Brænne | 2af40a0 | 2022-06-27 01:28:16 -0700 | [diff] [blame] | 630 | IsOkAndHolds(LifetimesAre({{"f", |
| 631 | "ERROR: Encountered a `this` lifetime on a " |
| 632 | "function with no `this` parameter"}}))); |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | TEST_F(LifetimeAnnotationsTest, LifetimeAnnotation_Invalid_WrongNumber) { |
Martin Brænne | 2af40a0 | 2022-06-27 01:28:16 -0700 | [diff] [blame] | 636 | EXPECT_THAT( |
| 637 | GetNamedLifetimeAnnotations(R"( |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 638 | [[clang::annotate("lifetimes", "a -> a")]] |
| 639 | int* f(int**); |
| 640 | )"), |
Martin Brænne | 2af40a0 | 2022-06-27 01:28:16 -0700 | [diff] [blame] | 641 | IsOkAndHolds(LifetimesAre( |
| 642 | {{"f", "ERROR: Invalid lifetime annotation: too few lifetimes"}}))); |
Googler | 8a8e82e | 2022-02-28 14:53:56 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Luca Versari | cc7e6cb | 2022-04-05 08:08:23 -0700 | [diff] [blame] | 645 | TEST_F(LifetimeAnnotationsTest, LifetimeAnnotation_Callback) { |
| 646 | EXPECT_THAT( |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 647 | GetNamedLifetimeAnnotations(WithLifetimeMacros(R"( |
Luca Versari | cc7e6cb | 2022-04-05 08:08:23 -0700 | [diff] [blame] | 648 | [[clang::annotate("lifetimes", "b, ((a -> a), static) -> b")]] |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 649 | int* f1(int*, int* (*)(int*)); |
| 650 | int* $b f2(int* $b, int* $a (* $static)(int* $a)); |
| 651 | )")), |
| 652 | IsOkAndHolds(LifetimesAre({{"f1", "b, ((a -> a), static) -> b"}, |
| 653 | {"f2", "b, ((a -> a), static) -> b"}}))); |
Luca Versari | cc7e6cb | 2022-04-05 08:08:23 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | TEST_F(LifetimeAnnotationsTest, LifetimeAnnotation_CallbackMultipleParams) { |
| 657 | EXPECT_THAT( |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 658 | GetNamedLifetimeAnnotations(WithLifetimeMacros(R"( |
Luca Versari | cc7e6cb | 2022-04-05 08:08:23 -0700 | [diff] [blame] | 659 | [[clang::annotate("lifetimes", "c, ((a, b -> a), static) -> c")]] |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 660 | int* f1(int*, int* (*)(int*, int*)); |
| 661 | int* $c f2(int* $c, int* $a (* $static)(int* $a, int* $b)); |
| 662 | )")), |
| 663 | IsOkAndHolds(LifetimesAre({{"f1", "c, ((a, b -> a), static) -> c"}, |
| 664 | {"f2", "c, ((a, b -> a), static) -> c"}}))); |
Luca Versari | cc7e6cb | 2022-04-05 08:08:23 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | TEST_F(LifetimeAnnotationsTest, LifetimeAnnotation_CallbackTmplFunc) { |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 668 | EXPECT_THAT(GetNamedLifetimeAnnotations(WithLifetimeMacros(R"( |
Luca Versari | cc7e6cb | 2022-04-05 08:08:23 -0700 | [diff] [blame] | 669 | template <typename Func> |
| 670 | struct function; |
| 671 | [[clang::annotate("lifetimes", "a, ((b -> b)) -> a")]] |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 672 | int* f1(int*, function<int*(int*)>); |
| 673 | int* $a f2(int* $a, function<int* $b(int* $b)>); |
| 674 | )")), |
| 675 | IsOkAndHolds(LifetimesAre({{"f1", "a, ((b -> b)) -> a"}, |
| 676 | {"f2", "a, ((b -> b)) -> a"}}))); |
Luca Versari | cc7e6cb | 2022-04-05 08:08:23 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | TEST_F(LifetimeAnnotationsTest, LifetimeAnnotation_MultipleCallbacks) { |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 680 | EXPECT_THAT(GetNamedLifetimeAnnotations(WithLifetimeMacros(R"( |
Luca Versari | cc7e6cb | 2022-04-05 08:08:23 -0700 | [diff] [blame] | 681 | [[clang::annotate("lifetimes", "a, ((b -> b), static), ((c -> c), static) -> a")]] |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 682 | int* f1(int*, int* (*)(int*), int* (*)(int*)); |
| 683 | int* $a f2(int* $a, int* $b (* $static)(int* $b), int* $c (* $static)(int* $c)); |
| 684 | )")), |
Luca Versari | cc7e6cb | 2022-04-05 08:08:23 -0700 | [diff] [blame] | 685 | IsOkAndHolds(LifetimesAre( |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 686 | {{"f1", "a, ((b -> b), static), ((c -> c), static) -> a"}, |
| 687 | {"f2", "a, ((b -> b), static), ((c -> c), static) -> a"}}))); |
Luca Versari | cc7e6cb | 2022-04-05 08:08:23 -0700 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | TEST_F(LifetimeAnnotationsTest, LifetimeAnnotation_ReturnFunctionPtr) { |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 691 | EXPECT_THAT(GetNamedLifetimeAnnotations(WithLifetimeMacros(R"_( |
Luca Versari | cc7e6cb | 2022-04-05 08:08:23 -0700 | [diff] [blame] | 692 | typedef int* (*FP)(int*); |
| 693 | [[clang::annotate("lifetimes", "a -> ((b -> b), static)")]] |
| 694 | FP f(int*); |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 695 | // TODO(mboehme): Need to support lifetime parameters on type aliases to |
| 696 | // be able to express this in the new syntax. |
| 697 | )_")), |
Luca Versari | cc7e6cb | 2022-04-05 08:08:23 -0700 | [diff] [blame] | 698 | IsOkAndHolds(LifetimesAre({{"f", "a -> ((b -> b), static)"}}))); |
| 699 | } |
| 700 | |
Googler | b0019ca | 2021-11-26 14:20:10 +0000 | [diff] [blame] | 701 | } // namespace |
Martin Brænne | 1a207c5 | 2022-04-19 00:05:38 -0700 | [diff] [blame] | 702 | } // namespace lifetimes |
| 703 | } // namespace tidy |
| 704 | } // namespace clang |