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

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 this:

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(AL.getArgAsExpr(0)))
Type = SE->getString();

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

StringRef Setter;
if (const auto *SE = dyn_cast_or_null(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?

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 entry you’ve said that you take only strings. You’d likely have to do some custom parsing to make that syntax work.

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

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