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

Yafei Liu via cfe-dev cfe-dev at lists.llvm.org
Mon Aug 10 05:23:15 PDT 2020

I'm adding a custom attribute followed by this
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

static void handleMEItemAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
  if (!checkAttributeAtLeastNumArgs(S, AL, 1))
  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,

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/4fbd2f5e/attachment.html>

More information about the cfe-dev mailing list