From 2c046c422f8bc5a879c413eba1629e3753939ac5 Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Mon, 27 Apr 2020 12:31:07 +0200 Subject: [PATCH] Add the gsl::Pointer/gsl::Owner combo to ArrayRef and SmallVector so it can diagnose this common combo. --- llvm/include/llvm/ADT/ArrayRef.h | 2 +- llvm/include/llvm/ADT/SmallVector.h | 3 ++- llvm/include/llvm/Support/Compiler.h | 8 ++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/llvm/include/llvm/ADT/ArrayRef.h b/llvm/include/llvm/ADT/ArrayRef.h index d11f8b5..5ed4d07 100644 --- a/llvm/include/llvm/ADT/ArrayRef.h +++ b/llvm/include/llvm/ADT/ArrayRef.h @@ -38,7 +38,7 @@ namespace llvm { /// This is intended to be trivially copyable, so it should be passed by /// value. template - class LLVM_NODISCARD ArrayRef { + class LLVM_GSL_POINTER LLVM_NODISCARD ArrayRef { public: using iterator = const T *; using const_iterator = const T *; diff --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h index 12a5876..5656a28 100644 --- a/llvm/include/llvm/ADT/SmallVector.h +++ b/llvm/include/llvm/ADT/SmallVector.h @@ -841,7 +841,8 @@ template struct alignas(alignof(T)) SmallVectorStorage {}; /// Note that this does not attempt to be exception safe. /// template -class SmallVector : public SmallVectorImpl, SmallVectorStorage { +class LLVM_GSL_OWNER SmallVector : public SmallVectorImpl, + SmallVectorStorage { public: SmallVector() : SmallVectorImpl(N) {} diff --git a/llvm/include/llvm/Support/Compiler.h b/llvm/include/llvm/Support/Compiler.h index cc64b54..79af6f5 100644 --- a/llvm/include/llvm/Support/Compiler.h +++ b/llvm/include/llvm/Support/Compiler.h @@ -289,6 +289,14 @@ #define LLVM_REQUIRE_CONSTANT_INITIALIZATION #endif +/// LLVM_GSL_OWNER - Apply this to owning classes like SmallVector to enable +/// lifetime warnings. +#if LLVM_HAS_CPP_ATTRIBUTE(gsl::Owner) +#define LLVM_GSL_OWNER [[gsl::Owner]] +#else +#define LLVM_GSL_OWNER +#endif + /// LLVM_GSL_POINTER - Apply this to non-owning classes like /// StringRef to enable lifetime warnings. #if LLVM_HAS_CPP_ATTRIBUTE(gsl::Pointer) -- 2.7.4