[llvm-dev] Adding a function attribute with an argument

Gaël Jobin via llvm-dev llvm-dev at lists.llvm.org
Thu Oct 8 01:08:05 PDT 2015


Hi Tehila,
I recently added new function attributes to clang/llvm with arguments.
What I did is the following:
1. Add your attributes to tools/clang/include/clang/Basic/Attr.td
def MYATTR : InheritableAttr {
  let Spellings = [GNU<"myattr">, CXX11<"gnu", "myattr">];
  let Args = [DefaultIntArgument<"myargument", 5>];
  let Subjects = SubjectList<[Function]>;
  let Documentation = [Undocumented];
}
2. Manage it in tools/clang/lib/CodeGen/CGCall.cpp...
void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
                                           const Decl *TargetDecl,
                                           AttributeListType &PAL,
                                           unsigned &CallingConv,
                                           bool AttrOnCallSite) {
//other code...
if (TargetDecl) {
if (TargetDecl->hasAttr<MYEXAMPLEAttr>())
	{
		auto myexample = TargetDecl->getAttr<MYEXAMPLEAttr>();
		FuncAttrs.addAttribute("myexample");
		unsigned myargument = myexample->getMyargument();


		FuncAttrs.addAttribute("myargument",
llvm::utostr(myargument));
	}
//other code...
}
//other code...
}
3. ...and in tools/clang/lib/Sema/SemaDeclAttr.cpp
static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
                                 const AttributeList &Attr,
                                 bool IncludeCXX11Attributes) {
//other code...
switch (Attr.getKind()) {
case AttributeList::AT_MYEXAMPLE:
	handleMYEXAMPLEAttr(S, D, Attr);
	break;
//other code...
}


static void handleMYEXAMPLEAttr(Sema &S, Decl *D, const AttributeList
&Attr) {
	uint32_t myargument = AVMAttr::DefaultMyargument;

	if (Attr.getNumArgs() && !checkUInt32Argument(S, Attr,
Attr.getArgAsExpr(0), myargument))
		return;

	D->addAttr(::new (S.Context)
			AVMAttr(Attr.getRange(), S.Context, myargument,
				Attr.getAttributeSpellingListIndex()));
}
Then you should have access to it in LLVM IR.
I may have forgotten some stuff but it should help you get started.
Regards,
Gael
Le jeudi 08 octobre 2015 à 10:02 +0300, Tehila Mayzels via llvm-dev a
écrit :
> Hi,
>  
> I'm trying to add a function attribute to clang/llvm.
> I need an attribute that will have an additional information of a
> variable name.
> e.g. I would like to add support for something like:
>  
> void foo() __attribute__ ((xyz(v)) {…}
>  
> such that on the IR stage I can realize that the function foo has the
> attribute xyz and an argument v that  marks variable v (which is a
> variable inside foo) for my needs.
>  
> I was able to add a function attribute without an argument, but I
> don't understand how to parse/save/analyze the argument.
>  
> I would appreciate any ideas/examples/references.
>  
> Thanks a lot,
> Tehila.
>  
>  
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20151008/dd34ead0/attachment.html>


More information about the llvm-dev mailing list