[PATCH] D91239: Update attribute example to fit the new Annotation API
Yafei Liu via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 11 00:17:10 PST 2020
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<StringLiteral>(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<Expr *, 4> 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;
}
};
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D91239.304414.patch
Type: text/x-patch
Size: 3606 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201111/2e3bb7c8/attachment.bin>
More information about the cfe-commits
mailing list