[cfe-dev] custom attribute crashed due to "Arg access out of range"

Keane, Erich via cfe-dev cfe-dev at lists.llvm.org
Mon Aug 10 05:57:05 PDT 2020


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 <cfe-dev-bounces at lists.llvm.org> On Behalf Of Yafei Liu via cfe-dev
Sent: Monday, August 10, 2020 5:23 AM
To: Clang Dev <cfe-dev at lists.llvm.org>
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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20200810/5f148f82/attachment.html>


More information about the cfe-dev mailing list