• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*===-- attributes.c - tool for testing libLLVM and llvm-c API ------------===*\
2 |*                                                                            *|
3 |* Part of the LLVM Project, under the Apache License v2.0 with LLVM          *|
4 |* Exceptions.                                                                *|
5 |* See https://llvm.org/LICENSE.txt for license information.                  *|
6 |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception                    *|
7 |*                                                                            *|
8 |*===----------------------------------------------------------------------===*|
9 |*                                                                            *|
10 |* This file implements the --test-attributes and --test-callsite-attributes  *|
11 |* commands in llvm-c-test.                                                   *|
12 |*                                                                            *|
13 \*===----------------------------------------------------------------------===*/
14 
15 #include "llvm-c-test.h"
16 
17 #include <assert.h>
18 #include <stdlib.h>
19 
llvm_test_function_attributes(void)20 int llvm_test_function_attributes(void) {
21   LLVMEnablePrettyStackTrace();
22 
23   LLVMModuleRef M = llvm_load_module(false, true);
24 
25   LLVMValueRef F = LLVMGetFirstFunction(M);
26   while (F) {
27     // Read attributes
28     int Idx, ParamCount;
29     for (Idx = LLVMAttributeFunctionIndex, ParamCount = LLVMCountParams(F);
30          Idx <= ParamCount; ++Idx) {
31       int AttrCount = LLVMGetAttributeCountAtIndex(F, Idx);
32       LLVMAttributeRef *Attrs = 0;
33       if (AttrCount) {
34         Attrs =
35             (LLVMAttributeRef *)malloc(AttrCount * sizeof(LLVMAttributeRef));
36         assert(Attrs);
37       }
38       LLVMGetAttributesAtIndex(F, Idx, Attrs);
39       free(Attrs);
40     }
41     F = LLVMGetNextFunction(F);
42   }
43 
44   LLVMDisposeModule(M);
45 
46   return 0;
47 }
48 
llvm_test_callsite_attributes(void)49 int llvm_test_callsite_attributes(void) {
50   LLVMEnablePrettyStackTrace();
51 
52   LLVMModuleRef M = llvm_load_module(false, true);
53 
54   LLVMValueRef F = LLVMGetFirstFunction(M);
55   while (F) {
56     LLVMBasicBlockRef BB;
57     for (BB = LLVMGetFirstBasicBlock(F); BB; BB = LLVMGetNextBasicBlock(BB)) {
58       LLVMValueRef I;
59       for (I = LLVMGetFirstInstruction(BB); I; I = LLVMGetNextInstruction(I)) {
60         if (LLVMIsACallInst(I)) {
61           // Read attributes
62           int Idx, ParamCount;
63           for (Idx = LLVMAttributeFunctionIndex,
64               ParamCount = LLVMCountParams(F);
65                Idx <= ParamCount; ++Idx) {
66             int AttrCount = LLVMGetCallSiteAttributeCount(I, Idx);
67             LLVMAttributeRef *Attrs = 0;
68             if (AttrCount) {
69               Attrs = (LLVMAttributeRef *)malloc(
70                   AttrCount * sizeof(LLVMAttributeRef));
71               assert(Attrs);
72             }
73             LLVMGetCallSiteAttributes(I, Idx, Attrs);
74             free(Attrs);
75           }
76         }
77       }
78     }
79 
80     F = LLVMGetNextFunction(F);
81   }
82 
83   LLVMDisposeModule(M);
84 
85   return 0;
86 }
87