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