]> granicus.if.org Git - clang/commitdiff
Currently __builtin_annotation() only annotates an i32.
authorJulien Lerouge <jlerouge@apple.com>
Sat, 28 Apr 2012 17:39:16 +0000 (17:39 +0000)
committerJulien Lerouge <jlerouge@apple.com>
Sat, 28 Apr 2012 17:39:16 +0000 (17:39 +0000)
        i32 __builtin_annotation(i32, string);

Applying it to i64 (e.g., long long) generates the following IR.

        trunc i64 {{.*}} to i32
        call i32 @llvm.annotation.i32
        zext i32 {{.*}} to i64

The redundant truncation and extension make the result difficult to use.

This patch makes __builtin_annotation() generic.

        type __builtin_annotation(type, string);

For the i64 example, it simplifies the generated IR to:

        call i64 @llvm.annotation.i64

Patch by Xi Wang!

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@155764 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/Builtins.def
include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaChecking.cpp
test/CodeGen/annotations-builtin.c
test/Sema/annotate.c

index f0b22a8703fe803465aadec69313b03e6fb26504..3bebfa3c9272abded390b7f26a2a3b1f226bc6ed 100644 (file)
@@ -882,7 +882,7 @@ LIBBUILTIN(_Block_object_dispose, "vvC*iC", "f", "Blocks.h", ALL_LANGUAGES)
 // FIXME: Also declare NSConcreteGlobalBlock and NSConcreteStackBlock.
 
 // Annotation function
-BUILTIN(__builtin_annotation, "UiUicC*", "nc")
+BUILTIN(__builtin_annotation, "v.", "tn")
 
 #undef BUILTIN
 #undef LIBBUILTIN
index cf7de93e0137f9f2defee3a2b587bb9aa2208249..887f908de8a47e858ff5221c1c52fc6be1b6e1e0 100644 (file)
@@ -5178,9 +5178,11 @@ def err_blocks_disable : Error<"blocks support disabled - compile with -fblocks"
 def err_block_returning_array_function : Error<
   "block cannot return %select{array|function}0 type %1">;
 
-// Builtin annotation string.
-def err_builtin_annotation_not_string_constant : Error<
-  "__builtin_annotation requires a non wide string constant">;
+// Builtin annotation
+def err_builtin_annotation_first_arg : Error<
+  "first argument to __builtin_annotation must be an integer">;
+def err_builtin_annotation_second_arg : Error<
+  "second argument to __builtin_annotation must be a non-wide string constant">;
 
 // CFString checking
 def err_cfstring_literal_not_string_constant : Error<
index 0d15ce24b0c34cb6654b07623d0f8a9231ddbc8d..d12f70721835938930868f8da794f407831984a2 100644 (file)
@@ -66,16 +66,31 @@ static bool checkArgCount(Sema &S, CallExpr *call, unsigned desiredArgCount) {
     << call->getArg(1)->getSourceRange();
 }
 
-/// CheckBuiltinAnnotationString - Checks that string argument to the builtin
-/// annotation is a non wide string literal.
-static bool CheckBuiltinAnnotationString(Sema &S, Expr *Arg) {
-  Arg = Arg->IgnoreParenCasts();
-  StringLiteral *Literal = dyn_cast<StringLiteral>(Arg);
+/// Check that the first argument to __builtin_annotation is an integer
+/// and the second argument is a non-wide string literal.
+static bool SemaBuiltinAnnotation(Sema &S, CallExpr *TheCall) {
+  if (checkArgCount(S, TheCall, 2))
+    return true;
+
+  // First argument should be an integer.
+  Expr *ValArg = TheCall->getArg(0);
+  QualType Ty = ValArg->getType();
+  if (!Ty->isIntegerType()) {
+    S.Diag(ValArg->getLocStart(), diag::err_builtin_annotation_first_arg)
+      << ValArg->getSourceRange();
+    return true;
+  }
+
+  // Second argument should be a constant string.
+  Expr *StrArg = TheCall->getArg(1)->IgnoreParenCasts();
+  StringLiteral *Literal = dyn_cast<StringLiteral>(StrArg);
   if (!Literal || !Literal->isAscii()) {
-    S.Diag(Arg->getLocStart(), diag::err_builtin_annotation_not_string_constant)
-      << Arg->getSourceRange();
+    S.Diag(StrArg->getLocStart(), diag::err_builtin_annotation_second_arg)
+      << StrArg->getSourceRange();
     return true;
   }
+
+  TheCall->setType(Ty);
   return false;
 }
 
@@ -256,7 +271,7 @@ Sema::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
     return SemaAtomicOpsOverloaded(move(TheCallResult), AtomicExpr::AO##ID);
 #include "clang/Basic/Builtins.def"
   case Builtin::BI__builtin_annotation:
-    if (CheckBuiltinAnnotationString(*this, TheCall->getArg(1)))
+    if (SemaBuiltinAnnotation(*this, TheCall))
       return ExprError();
     break;
   }
index 42421a0a5200f143b9cfd22f3db90e25505781a3..7938e49aa6467d2d09b97ddc012507f721eada66 100644 (file)
@@ -25,9 +25,7 @@ int main(int argc, char **argv) {
 // CHECK: call i32 @llvm.annotation.i32
 
     long long lla = __builtin_annotation(llfoo, "annotation_a");
-// CHECK: trunc i64 {{.*}} to i32
-// CHECK-NEXT: call i32 @llvm.annotation.i32
-// CHECK-NEXT: zext i32 {{.*}} to i64
+// CHECK: call i64 @llvm.annotation.i64
 
     int inta = __builtin_annotation(intfoo, "annotation_a");
 // CHECK: load i32* @intfoo
@@ -35,15 +33,11 @@ int main(int argc, char **argv) {
 // CHECK-NEXT: store
 
     short shorta =  __builtin_annotation(shortfoo, "annotation_a");
-// CHECK: sext i16 {{.*}} to i32
-// CHECK-NEXT: call i32 @llvm.annotation.i32
-// CHECK-NEXT: trunc i32 {{.*}} to i16
+// CHECK: call i16 @llvm.annotation.i16
 
     char chara = __builtin_annotation(charfoo, "annotation_a");
-// CHECK: sext i8 {{.*}} to i32
-// CHECK-NEXT: call i32 @llvm.annotation.i32
-// CHECK-NEXT: trunc i32 {{.*}} to i8
-//
+// CHECK: call i8 @llvm.annotation.i8
+
     char **arg = (char**) __builtin_annotation((int) argv, "annotation_a");
 // CHECK: ptrtoint i8** {{.*}} to
 // CHECK: call i32 @llvm.annotation.i32
index 5b2727752bbd5aafe77c37d805e912fb8ca6f7a2..ef878d4c9824f3667f403d292ff8b629261e410b 100644 (file)
@@ -4,7 +4,8 @@ void __attribute__((annotate("foo"))) foo(float *a) {
   __attribute__((annotate("bar"))) int x;
   __attribute__((annotate(1))) int y; // expected-error {{argument to annotate attribute was not a string literal}}
   __attribute__((annotate("bar", 1))) int z; // expected-error {{attribute takes one argument}}
-  int u = __builtin_annotation(z, (char*) 0); // expected-error {{__builtin_annotation requires a non wide string constant}}
-  int v = __builtin_annotation(z, (char*) L"bar"); // expected-error {{__builtin_annotation requires a non wide string constant}}
+  int u = __builtin_annotation(z, (char*) 0); // expected-error {{second argument to __builtin_annotation must be a non-wide string constant}}
+  int v = __builtin_annotation(z, (char*) L"bar"); // expected-error {{second argument to __builtin_annotation must be a non-wide string constant}}
   int w = __builtin_annotation(z, "foo");
+  float b = __builtin_annotation(*a, "foo"); // expected-error {{first argument to __builtin_annotation must be an integer}}
 }