Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gsl::Owner/gsl::Pointer: Add implicit annotations for some std types
Summary: Hard code gsl::Owner/gsl::Pointer for std types. The paper mentions some types explicitly. Generally, all containers and their iterators are covered. For iterators, we cover both the case that they are defined as an nested class or as an typedef/using. I have started to test this implementation against some real standard library implementations, namely libc++ 7.1.0, libc++ 8.0.1rc2, libstdc++ 4.6.4, libstdc++ 4.8.5, libstdc++ 4.9.4, libstdc++ 5.4.0, libstdc++ 6.5.0, libstdc++ 7.3.0, libstdc++ 8.3.0 and libstdc++ 9.1.0. The tests are currently here https://github.com/mgehre/llvm-project/blob/lifetime-ci/lifetime-attr-test.sh https://github.com/mgehre/llvm-project/blob/lifetime-ci/lifetime-attr-test.cpp I think due to their dependency on a standard library, they are not a good fit for clang/test/. Where else could I put them? Reviewers: gribozavr, xazax.hun Subscribers: rnkovacs, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D64448 llvm-svn: 368147
- Loading branch information
Showing
6 changed files
with
268 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// RUN: %clang_cc1 -ast-dump %s | \ | ||
// RUN: FileCheck --implicit-check-not OwnerAttr --implicit-check-not PointerAttr %s | ||
|
||
// Test attribute inference for types in the standard library. | ||
namespace std { | ||
// Attributes are inferred for a (complete) class. | ||
class any { | ||
// CHECK: CXXRecordDecl {{.*}} any | ||
// CHECK: OwnerAttr {{.*}} | ||
}; | ||
|
||
// Attributes are inferred for instantiations of a complete template. | ||
template <typename T> | ||
class vector { | ||
public: | ||
class iterator {}; | ||
// CHECK: ClassTemplateDecl {{.*}} vector | ||
// CHECK: OwnerAttr {{.*}} | ||
// CHECK: CXXRecordDecl {{.*}} iterator | ||
// CHECK: PointerAttr {{.*}} | ||
// CHECK: ClassTemplateSpecializationDecl {{.*}} vector | ||
// CHECK: TemplateArgument type 'int' | ||
// CHECK: OwnerAttr | ||
// CHECK: CXXRecordDecl {{.*}} iterator | ||
// CHECK: PointerAttr {{.*}} | ||
}; | ||
static_assert(sizeof(vector<int>), ""); // Force instantiation. | ||
static_assert(sizeof(vector<int>::iterator), ""); // Force instantiation. | ||
|
||
// If std::container::iterator is a using declaration, attributes are inferred | ||
// for the underlying class. | ||
template <typename T> | ||
class __set_iterator {}; | ||
// CHECK: ClassTemplateDecl {{.*}} __set_iterator | ||
// CHECK: PointerAttr | ||
// CHECK: ClassTemplateSpecializationDecl {{.*}} __set_iterator | ||
// CHECK: TemplateArgument type 'int' | ||
// CHECK: PointerAttr | ||
|
||
template <typename T> | ||
class set { | ||
// CHECK: ClassTemplateDecl {{.*}} set | ||
// CHECK: OwnerAttr {{.*}} | ||
// CHECK: ClassTemplateSpecializationDecl {{.*}} set | ||
// CHECK: OwnerAttr {{.*}} | ||
public: | ||
using iterator = __set_iterator<T>; | ||
}; | ||
static_assert(sizeof(set<int>::iterator), ""); // Force instantiation. | ||
|
||
// If std::container::iterator is a typedef, attributes are inferred for the | ||
// underlying class. | ||
template <typename T> | ||
class __map_iterator {}; | ||
// CHECK: ClassTemplateDecl {{.*}} __map_iterator | ||
// CHECK: PointerAttr | ||
// CHECK: ClassTemplateSpecializationDecl {{.*}} __map_iterator | ||
// CHECK: TemplateArgument type 'int' | ||
// CHECK: PointerAttr | ||
|
||
template <typename T> | ||
class map { | ||
// CHECK: ClassTemplateDecl {{.*}} map | ||
// CHECK: OwnerAttr {{.*}} | ||
// CHECK: ClassTemplateSpecializationDecl {{.*}} map | ||
// CHECK: OwnerAttr {{.*}} | ||
public: | ||
typedef __map_iterator<T> iterator; | ||
}; | ||
static_assert(sizeof(map<int>::iterator), ""); // Force instantiation. | ||
|
||
// Inline namespaces are ignored when checking if | ||
// the class lives in the std namespace. | ||
inline namespace inlinens { | ||
template <typename T> | ||
class __unordered_map_iterator {}; | ||
// CHECK: ClassTemplateDecl {{.*}} __unordered_map_iterator | ||
// CHECK: PointerAttr | ||
// CHECK: ClassTemplateSpecializationDecl {{.*}} __unordered_map_iterator | ||
// CHECK: TemplateArgument type 'int' | ||
// CHECK: PointerAttr | ||
|
||
template <typename T> | ||
class unordered_map { | ||
// CHECK: ClassTemplateDecl {{.*}} unordered_map | ||
// CHECK: OwnerAttr {{.*}} | ||
// CHECK: ClassTemplateSpecializationDecl {{.*}} unordered_map | ||
// CHECK: OwnerAttr {{.*}} | ||
public: | ||
typedef __unordered_map_iterator<T> iterator; | ||
}; | ||
static_assert(sizeof(unordered_map<int>::iterator), ""); // Force instantiation. | ||
} // namespace inlinens | ||
|
||
// std::list has an implicit gsl::Owner attribute, | ||
// but explicit attributes take precedence. | ||
template <typename T> | ||
class [[gsl::Pointer]] list{}; | ||
// CHECK: ClassTemplateDecl {{.*}} list | ||
// CHECK: PointerAttr {{.*}} | ||
// CHECK: ClassTemplateSpecializationDecl {{.*}} list | ||
// CHECK: PointerAttr {{.*}} | ||
|
||
static_assert(sizeof(list<int>), ""); // Force instantiation. | ||
|
||
// Forward declared template (Owner). | ||
template < | ||
class CharT, | ||
class Traits> | ||
class basic_regex; | ||
// CHECK: ClassTemplateDecl {{.*}} basic_regex | ||
// CHECK: OwnerAttr {{.*}} | ||
|
||
// Forward declared template (Pointer). | ||
template <class T> | ||
class reference_wrapper; | ||
// CHECK: ClassTemplateDecl {{.*}} reference_wrapper | ||
// CHECK: PointerAttr {{.*}} | ||
|
||
class some_unknown_type; | ||
// CHECK: CXXRecordDecl {{.*}} some_unknown_type | ||
|
||
} // namespace std | ||
|
||
namespace user { | ||
// If a class is not in the std namespace, we don't infer the attributes. | ||
class any { | ||
}; | ||
} // namespace user |