[PATCH] D91239: Update attribute example to fit the new Annotation API

2020-11-11 Thread Tyker via Phabricator via cfe-commits
Tyker added a comment.

I recently made it much easier to create AnnotationAttr in this context with 
https://reviews.llvm.org/rGd093401a2617d3c46aaed9eeaecf877e3ae1a9f1.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D91239/new/

https://reviews.llvm.org/D91239

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D91239: Update attribute example to fit the new Annotation API

2020-11-11 Thread Yafei Liu via Phabricator via cfe-commits
psionic12 added inline comments.



Comment at: clang/examples/Attribute/Attribute.cpp:59
+
+// First we have to create an `StringLiteralExpr`.
+StringRef AnnotationString = "example";

`AnnotationAttr` is used as an extra information in attribute plugin designs. 
Usually after handling the customized attribute, coders will add an 
`AnnotationAttr` into the AST, for later use.

Before the new API released, create an `AnnotationAttr` is not very hard.

But now `AnnotationAttr` accepts a StringLiteralExpr as it's first argument, in 
order to do the same thing I mentioned above, we have to create an 
`StringLiteralExpr` from nowhere, which looks not very elegant to me. 


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D91239/new/

https://reviews.llvm.org/D91239

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D91239: Update attribute example to fit the new Annotation API

2020-11-11 Thread Yafei Liu via Phabricator via cfe-commits
psionic12 created this revision.
psionic12 added reviewers: john.brawn, aaron.ballman, Tyker, erichkeane.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
psionic12 requested review of this revision.

Since AnnotationAttr can add extra arguments
now, update Attribute plugin example to fit this API
to make this example compiled.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D91239

Files:
  clang/examples/Attribute/Attribute.cpp
  clang/test/Frontend/plugin-attribute.cpp


Index: clang/test/Frontend/plugin-attribute.cpp
===
--- clang/test/Frontend/plugin-attribute.cpp
+++ clang/test/Frontend/plugin-attribute.cpp
@@ -6,20 +6,17 @@
 [[example]] void fn1b() { }
 [[plugin::example]] void fn1c() { }
 void fn2() __attribute__((example("somestring"))) { }
-// ATTRIBUTE: warning: 'example' attribute only applies to functions
+// ATTRIBUTE: warning: 'example' attribute only applies to functions 
[-Wignored-attributes]
 int var1 __attribute__((example("otherstring"))) = 1;
 
-// ATTRIBUTE: [[STR1_VAR:@.+]] = private unnamed_addr constant [10 x i8] 
c"example()\00"
-// ATTRIBUTE: [[STR2_VAR:@.+]] = private unnamed_addr constant [20 x i8] 
c"example(somestring)\00"
-// ATTRIBUTE: @llvm.global.annotations = 
{{.*}}@{{.*}}fn1a{{.*}}[[STR1_VAR]]{{.*}}@{{.*}}fn1b{{.*}}[[STR1_VAR]]{{.*}}@{{.*}}fn1c{{.*}}[[STR1_VAR]]{{.*}}@{{.*}}fn2{{.*}}[[STR2_VAR]]
+// ATTRIBUTE: [[STR1_VAR:@.+]] = private unnamed_addr constant [8 x i8] 
c"example\00", section "llvm.metadata"
+// ATTRIBUTE: @llvm.global.annotations = 
{{.*}}@{{.*}}fn1a{{.*}}[[STR1_VAR]]{{.*}}@{{.*}}fn1b{{.*}}[[STR1_VAR]]{{.*}}@{{.*}}fn1c{{.*}}[[STR1_VAR]]{{.*}}@{{.*}}fn2{{.*}}
 
 #ifdef BAD_ATTRIBUTE
 class Example {
   // BADATTRIBUTE: error: 'example' attribute only allowed at file scope
   void __attribute__((example)) fn3();
 };
-// BADATTRIBUTE: error: 'example' attribute requires a string
-void fn4() __attribute__((example(123))) { }
 // BADATTRIBUTE: error: 'example' attribute takes no more than 1 argument
 void fn5() __attribute__((example("a","b"))) { }
 #endif
Index: clang/examples/Attribute/Attribute.cpp
===
--- clang/examples/Attribute/Attribute.cpp
+++ clang/examples/Attribute/Attribute.cpp
@@ -55,23 +55,29 @@
   S.Diag(Attr.getLoc(), ID);
   return AttributeNotApplied;
 }
-// Check if we have an optional string argument.
-StringRef Str = "";
-if (Attr.getNumArgs() > 0) {
-  Expr *ArgExpr = Attr.getArgAsExpr(0);
-  StringLiteral *Literal =
-  dyn_cast(ArgExpr->IgnoreParenCasts());
-  if (Literal) {
-Str = Literal->getString();
-  } else {
-S.Diag(ArgExpr->getExprLoc(), diag::err_attribute_argument_type)
-<< Attr.getAttrName() << AANT_ArgumentString;
-return AttributeNotApplied;
-  }
+
+// First we have to create an `StringLiteralExpr`.
+StringRef AnnotationString = "example";
+QualType StrTy = S.Context.getConstantArrayType(
+S.Context.adjustStringLiteralBaseType(S.Context.CharTy.withConst()),
+llvm::APInt(32, AnnotationString.size()), nullptr, ArrayType::Normal,
+0);
+const auto AnnotationStringLoc = Attr.getLoc();
+auto *StringExpr =
+StringLiteral::Create(S.Context, AnnotationString, 
StringLiteral::Ascii,
+  /*Pascal*/ false, StrTy, &AnnotationStringLoc, 
1);
+
+llvm::SmallVector Args;
+Args.reserve(Attr.getNumArgs() + 1);
+
+// Add the `StringLiteralExpr` as the first argument to an `AnnotationAttr`
+Args.push_back(StringExpr);
+for (unsigned Idx = 1; Idx < Attr.getNumArgs(); Idx++) {
+  Args.push_back(Attr.getArgAsExpr(Idx));
 }
+
 // Attach an annotate attribute to the Decl.
-D->addAttr(AnnotateAttr::Create(S.Context, "example(" + Str.str() + ")",
-Attr.getRange()));
+S.AddAnnotationAttr(D, {Attr.getRange()}, AnnotationString, Args);
 return AttributeApplied;
   }
 };


Index: clang/test/Frontend/plugin-attribute.cpp
===
--- clang/test/Frontend/plugin-attribute.cpp
+++ clang/test/Frontend/plugin-attribute.cpp
@@ -6,20 +6,17 @@
 [[example]] void fn1b() { }
 [[plugin::example]] void fn1c() { }
 void fn2() __attribute__((example("somestring"))) { }
-// ATTRIBUTE: warning: 'example' attribute only applies to functions
+// ATTRIBUTE: warning: 'example' attribute only applies to functions [-Wignored-attributes]
 int var1 __attribute__((example("otherstring"))) = 1;
 
-// ATTRIBUTE: [[STR1_VAR:@.+]] = private unnamed_addr constant [10 x i8] c"example()\00"
-// ATTRIBUTE: [[STR2_VAR:@.+]] = private unnamed_addr constant [20 x i8] c"example(somestring)\00"
-// ATTRIBUTE: @llvm.global.annotations = {{.*}}@{{.*}}fn1a{{.*}}[[STR1_VAR]]{{.*}}@{{.*}}fn1b{{.*}}[[STR1_VAR]]{{.*}}@{{.*}}fn1c{{.*}}