Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP lifetime categories #14

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions clang/include/clang/Basic/Attr.td
Original file line number Diff line number Diff line change
Expand Up @@ -2766,6 +2766,20 @@ def TypeTagForDatatype : InheritableAttr {
let Documentation = [TypeTagForDatatypeDocs];
}

def Owner : InheritableAttr {
let Spellings = [CXX11<"gsl", "Owner">];
let Subjects = SubjectList<[CXXRecord]>;
let Args = [TypeArgument<"DerefType">];
let Documentation = [Undocumented]; // FIXME
}

def Pointer : InheritableAttr {
let Subjects = SubjectList<[CXXRecord]>;
let Spellings = [CXX11<"gsl", "Pointer">];
let Args = [TypeArgument<"DerefType">];
let Documentation = [Undocumented]; // FIXME
}

// Microsoft-related attributes

def MSNoVTable : InheritableAttr, TargetSpecificAttr<TargetMicrosoftCXXABI> {
Expand Down
39 changes: 39 additions & 0 deletions clang/lib/Sema/SemaDeclAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4533,6 +4533,39 @@ static void handleSuppressAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
DiagnosticIdentifiers.size(), AL.getAttributeSpellingListIndex()));
}

static void handleLifetimeCategoryAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
// if (!checkAttributeAtLeastNumArgs(S, AL, 1))
// return;

if (checkAttrMutualExclusion<OwnerAttr>(S, D, AL))
return;
if (checkAttrMutualExclusion<PointerAttr>(S, D, AL))
return;

if (!AL.hasParsedType()) {
S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
return;
}

TypeSourceInfo *DerefTypeLoc = nullptr;
QualType ParmType = S.GetTypeFromParser(AL.getTypeArg(), &DerefTypeLoc);
assert(DerefTypeLoc && "no type source info for attribute argument");

if (ParmType->isVoidType()) {
S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << "'void'";
return;
}

if(AL.getKind() == ParsedAttr::AT_Owner) {
D->addAttr(::new (S.Context) OwnerAttr(
AL.getRange(), S.Context, DerefTypeLoc, AL.getAttributeSpellingListIndex()));
}
else {
D->addAttr(::new (S.Context) PointerAttr(
AL.getRange(), S.Context, DerefTypeLoc, AL.getAttributeSpellingListIndex()));
}
}

bool Sema::CheckCallingConvAttr(const ParsedAttr &Attrs, CallingConv &CC,
const FunctionDecl *FD) {
if (Attrs.isInvalid())
Expand Down Expand Up @@ -7110,6 +7143,12 @@ static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
case ParsedAttr::AT_Suppress:
handleSuppressAttr(S, D, AL);
break;
case ParsedAttr::AT_Owner:
handleLifetimeCategoryAttr(S, D, AL);
break;
case ParsedAttr::AT_Pointer:
handleLifetimeCategoryAttr(S, D, AL);
break;
case ParsedAttr::AT_OpenCLKernel:
handleSimpleAttribute<OpenCLKernelAttr>(S, D, AL);
break;
Expand Down
9 changes: 9 additions & 0 deletions clang/test/AST/ast-dump-attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,15 @@ namespace TestSuppress {
}
}

namespace TestLifetimeCategories {
class [[gsl::Owner(int)]] AOwner {};
// CHECK: CXXRecordDecl{{.*}} struct AOwner
// CHECK-NEXT: ObjCBridgeRelatedAttr{{.*}} NSParagraphStyle
class [[gsl::Pointer(int)]] APointer {};
// CHECK: CXXRecordDecl{{.*}} struct APointer
// CHECK-NEXT: ObjCBridgeRelatedAttr{{.*}} NSParagraphStyle
}

// Verify the order of attributes in the Ast. It must reflect the order
// in the parsed source.
int mergeAttrTest() __attribute__((deprecated)) __attribute__((warn_unused_result));
Expand Down
36 changes: 36 additions & 0 deletions clang/test/SemaCXX/attr-gsl-owner-pointer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s

int [[gsl::Owner]] i;
//expected-error@-1 {{'Owner' attribute cannot be applied to types}}
void [[gsl::Owner]] f();
//expected-error@-1 {{'Owner' attribute cannot be applied to types}}

struct S {
};

S [[gsl::Owner]] Instance;
//expected-error@-1 {{'Owner' attribute cannot be applied to types}}

class [[gsl::Owner]] OwnerMissingParameter {};
//expected-error@-1 {{'Owner' attribute takes one argument}}
class [[gsl::Pointer]] PointerMissingParameter {};
//expected-error@-1 {{'Pointer' attribute takes one argument}}


class [[gsl::Owner(7)]] OwnerDerefNoType {};
class [[gsl::Pointer("int")]] PointerDerefNoType {};


class [[gsl::Owner(int)]] [[gsl::Pointer(int)]] BothOwnerPointer {};

class [[gsl::Owner(int)]] [[gsl::Owner(int)]] DuplicateOwner {};
class [[gsl::Pointer(int)]] [[gsl::Pointer(int)]] DuplicatePointer {};

class [[gsl::Owner(int)]] [[gsl::Owner(char)]] OwnerDifferentTypes {};
class [[gsl::Pointer(int)]] [[gsl::Pointer(char)]] PointerDifferentTypes {};

class [[gsl::Owner(void)]] OwnerVoidDerefType {};
class [[gsl::Pointer(void)]] PointerVoidDerefType {};

class [[gsl::Owner(int)]] AnOwner {};
class [[gsl::Pointer(S)]] APointer {};