custom attribute crashed due to "Arg access out of range"

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

custom attribute crashed due to "Arg access out of range"

Hubert Tong via cfe-dev
I'm adding a custom attribute followed by this http://clang.llvm.org/docs/InternalsManual.html#how-to-add-an-attribute, here's my def:

def MEItem : InheritableParamAttr {
  let Spellings = [CXX11<"me", "type">, C2x<"me", "type">];
  let Args = [StringArgument<"Type", 1>, StringArgument<"Getter", 1>, StringArgument<"Setter", 1>];
  let Subjects = SubjectList<[Field]>;
  let Documentation = [Undocumented];
}

I just copied what ExternalSourceSymbol does, so the handling code is like this:

static void handleMEItemAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
  if (!checkAttributeAtLeastNumArgs(S, AL, 1))
    return;
  assert(checkAttributeAtMostNumArgs(S, AL, 3) &&
         "Invalid number of arguments in an item attribute");

  StringRef Type;
  if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(0)))
    Type = SE->getString();

  StringRef Getter;
  if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(1)))
    Getter = SE->getString();

  StringRef Setter;
  if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(2)))
    Setter = SE->getString();

  D->addAttr(::new (S.Context) MEItemAttr(S.Context, AL, Type, Getter, Setter));
}

but if I use this attribute in my code, for example [[me::type("int")]], the code crashed at AL.getArgAsExpr(1): 

clang::ArgsUnion clang::ParsedAttr::getArg(unsigned int) const: Assertion `Arg < NumArgs && "Arg access out of range!"' failed.

then I tried this: [[me::type(Type="int", Getter="xxx", Setter="xxx")]]
error: use of undeclared identifier 'Type' [[me::type(Type="int", Getter="xxx", Setter="xxx")]]

So what did I miss?

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: custom attribute crashed due to "Arg access out of range"

Hubert Tong via cfe-dev

checkAttributeAtLeastNumArgs /checkAttributeAtMostNumArgs returns false on error.  In your code, you are ensuring that there is at least 1, but no more than 3 arguments.

 

SO, you have only made sure that you have 1, 2, OR 3 arguments. 

 

When you attempt to get the 2nd argument (AL.getArgAsExpr(1), BUT only have a single argument, you get an assertion, because there IS NOT a 2nd argument.

 

In your attempt to use a designated initializer type signature (Type=”int”), you get the error because your argument list in the MEItem Attr.td entry you’ve said that you take only strings.  You’d likely have to do some custom parsing to make that syntax work.

 

From: cfe-dev <[hidden email]> On Behalf Of Yafei Liu via cfe-dev
Sent: Monday, August 10, 2020 5:23 AM
To: Clang Dev <[hidden email]>
Subject: [cfe-dev] custom attribute crashed due to "Arg access out of range"

 

I'm adding a custom attribute followed by this http://clang.llvm.org/docs/InternalsManual.html#how-to-add-an-attribute, here's my def:

 

def MEItem : InheritableParamAttr {
  let Spellings = [CXX11<"me", "type">, C2x<"me", "type">];
  let Args = [StringArgument<"Type", 1>, StringArgument<"Getter", 1>, StringArgument<"Setter", 1>];
  let Subjects = SubjectList<[Field]>;
  let Documentation = [Undocumented];
}

 

I just copied what ExternalSourceSymbol does, so the handling code is like this:

 

static void handleMEItemAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
  if (!checkAttributeAtLeastNumArgs(S, AL, 1))
    return;
  assert(checkAttributeAtMostNumArgs(S, AL, 3) &&
         "Invalid number of arguments in an item attribute");

  StringRef Type;
  if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(0)))
    Type = SE->getString();

  StringRef Getter;
  if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(1)))
    Getter = SE->getString();

  StringRef Setter;
  if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(2)))
    Setter = SE->getString();

  D->addAttr(::new (S.Context) MEItemAttr(S.Context, AL, Type, Getter, Setter));
}

 

but if I use this attribute in my code, for example [[me::type("int")]], the code crashed at AL.getArgAsExpr(1): 

 

clang::ArgsUnion clang::ParsedAttr::getArg(unsigned int) const: Assertion `Arg < NumArgs && "Arg access out of range!"' failed.

 

then I tried this: [[me::type(Type="int", Getter="xxx", Setter="xxx")]]

error: use of undeclared identifier 'Type' [[me::type(Type="int", Getter="xxx", Setter="xxx")]]

 

So what did I miss?


_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: custom attribute crashed due to "Arg access out of range"

Hubert Tong via cfe-dev
Hi Erich, do you mean that ExternalSourceSymbol has extra parsing moves to handle the syntax key="value"?

On Mon, Aug 10, 2020 at 8:57 PM Keane, Erich <[hidden email]> wrote:

checkAttributeAtLeastNumArgs /checkAttributeAtMostNumArgs returns false on error.  In your code, you are ensuring that there is at least 1, but no more than 3 arguments.

 

SO, you have only made sure that you have 1, 2, OR 3 arguments. 

 

When you attempt to get the 2nd argument (AL.getArgAsExpr(1), BUT only have a single argument, you get an assertion, because there IS NOT a 2nd argument.

 

In your attempt to use a designated initializer type signature (Type=”int”), you get the error because your argument list in the MEItem Attr.td entry you’ve said that you take only strings.  You’d likely have to do some custom parsing to make that syntax work.

 

From: cfe-dev <[hidden email]> On Behalf Of Yafei Liu via cfe-dev
Sent: Monday, August 10, 2020 5:23 AM
To: Clang Dev <[hidden email]>
Subject: [cfe-dev] custom attribute crashed due to "Arg access out of range"

 

I'm adding a custom attribute followed by this http://clang.llvm.org/docs/InternalsManual.html#how-to-add-an-attribute, here's my def:

 

def MEItem : InheritableParamAttr {
  let Spellings = [CXX11<"me", "type">, C2x<"me", "type">];
  let Args = [StringArgument<"Type", 1>, StringArgument<"Getter", 1>, StringArgument<"Setter", 1>];
  let Subjects = SubjectList<[Field]>;
  let Documentation = [Undocumented];
}

 

I just copied what ExternalSourceSymbol does, so the handling code is like this:

 

static void handleMEItemAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
  if (!checkAttributeAtLeastNumArgs(S, AL, 1))
    return;
  assert(checkAttributeAtMostNumArgs(S, AL, 3) &&
         "Invalid number of arguments in an item attribute");

  StringRef Type;
  if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(0)))
    Type = SE->getString();

  StringRef Getter;
  if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(1)))
    Getter = SE->getString();

  StringRef Setter;
  if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(2)))
    Setter = SE->getString();

  D->addAttr(::new (S.Context) MEItemAttr(S.Context, AL, Type, Getter, Setter));
}

 

but if I use this attribute in my code, for example [[me::type("int")]], the code crashed at AL.getArgAsExpr(1): 

 

clang::ArgsUnion clang::ParsedAttr::getArg(unsigned int) const: Assertion `Arg < NumArgs && "Arg access out of range!"' failed.

 

then I tried this: [[me::type(Type="int", Getter="xxx", Setter="xxx")]]

error: use of undeclared identifier 'Type' [[me::type(Type="int", Getter="xxx", Setter="xxx")]]

 

So what did I miss?


_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: custom attribute crashed due to "Arg access out of range"

Hubert Tong via cfe-dev

Yes, it does.  In ParseDecl.cpp you can see the ParseExternalSourceSymbolAttribute function that does that.

 

From: Yafei Liu <[hidden email]>
Sent: Monday, August 10, 2020 6:53 PM
To: Keane, Erich <[hidden email]>
Cc: [hidden email]
Subject: Re: [cfe-dev] custom attribute crashed due to "Arg access out of range"

 

Hi Erich, do you mean that ExternalSourceSymbol has extra parsing moves to handle the syntax key="value"?

 

On Mon, Aug 10, 2020 at 8:57 PM Keane, Erich <[hidden email]> wrote:

checkAttributeAtLeastNumArgs /checkAttributeAtMostNumArgs returns false on error.  In your code, you are ensuring that there is at least 1, but no more than 3 arguments.

 

SO, you have only made sure that you have 1, 2, OR 3 arguments. 

 

When you attempt to get the 2nd argument (AL.getArgAsExpr(1), BUT only have a single argument, you get an assertion, because there IS NOT a 2nd argument.

 

In your attempt to use a designated initializer type signature (Type=”int”), you get the error because your argument list in the MEItem Attr.td entry you’ve said that you take only strings.  You’d likely have to do some custom parsing to make that syntax work.

 

From: cfe-dev <[hidden email]> On Behalf Of Yafei Liu via cfe-dev
Sent: Monday, August 10, 2020 5:23 AM
To: Clang Dev <[hidden email]>
Subject: [cfe-dev] custom attribute crashed due to "Arg access out of range"

 

I'm adding a custom attribute followed by this http://clang.llvm.org/docs/InternalsManual.html#how-to-add-an-attribute, here's my def:

 

def MEItem : InheritableParamAttr {
  let Spellings = [CXX11<"me", "type">, C2x<"me", "type">];
  let Args = [StringArgument<"Type", 1>, StringArgument<"Getter", 1>, StringArgument<"Setter", 1>];
  let Subjects = SubjectList<[Field]>;
  let Documentation = [Undocumented];
}

 

I just copied what ExternalSourceSymbol does, so the handling code is like this:

 

static void handleMEItemAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
  if (!checkAttributeAtLeastNumArgs(S, AL, 1))
    return;
  assert(checkAttributeAtMostNumArgs(S, AL, 3) &&
         "Invalid number of arguments in an item attribute");

  StringRef Type;
  if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(0)))
    Type = SE->getString();

  StringRef Getter;
  if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(1)))
    Getter = SE->getString();

  StringRef Setter;
  if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(2)))
    Setter = SE->getString();

  D->addAttr(::new (S.Context) MEItemAttr(S.Context, AL, Type, Getter, Setter));
}

 

but if I use this attribute in my code, for example [[me::type("int")]], the code crashed at AL.getArgAsExpr(1): 

 

clang::ArgsUnion clang::ParsedAttr::getArg(unsigned int) const: Assertion `Arg < NumArgs && "Arg access out of range!"' failed.

 

then I tried this: [[me::type(Type="int", Getter="xxx", Setter="xxx")]]

error: use of undeclared identifier 'Type' [[me::type(Type="int", Getter="xxx", Setter="xxx")]]

 

So what did I miss?


_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev