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
20int llvm_test_function_attributes(void) {
21 LLVMEnablePrettyStackTrace();
22
23 LLVMContextRef C = LLVMContextCreate();
24 LLVMModuleRef M = llvm_load_module(C, false, true);
25
26 LLVMValueRef F = LLVMGetFirstFunction(M);
27 while (F) {
28 // Read attributes
29 int Idx, ParamCount;
30 for (Idx = LLVMAttributeFunctionIndex, ParamCount = LLVMCountParams(Fn: F);
31 Idx <= ParamCount; ++Idx) {
32 int AttrCount = LLVMGetAttributeCountAtIndex(F, Idx);
33 LLVMAttributeRef *Attrs = 0;
34 if (AttrCount) {
35 Attrs =
36 (LLVMAttributeRef *)malloc(size: AttrCount * sizeof(LLVMAttributeRef));
37 assert(Attrs);
38 }
39 LLVMGetAttributesAtIndex(F, Idx, Attrs);
40 free(ptr: Attrs);
41 }
42 F = LLVMGetNextFunction(Fn: F);
43 }
44
45 LLVMDisposeModule(M);
46 LLVMContextDispose(C);
47
48 return 0;
49}
50
51int llvm_test_callsite_attributes(void) {
52 LLVMEnablePrettyStackTrace();
53
54 LLVMContextRef C = LLVMContextCreate();
55 LLVMModuleRef M = llvm_load_module(C, false, true);
56
57 LLVMValueRef F = LLVMGetFirstFunction(M);
58 while (F) {
59 LLVMBasicBlockRef BB;
60 for (BB = LLVMGetFirstBasicBlock(Fn: F); BB; BB = LLVMGetNextBasicBlock(BB)) {
61 LLVMValueRef I;
62 for (I = LLVMGetFirstInstruction(BB); I; I = LLVMGetNextInstruction(Inst: I)) {
63 if (LLVMIsACallInst(Val: I)) {
64 // Read attributes
65 int Idx, ParamCount;
66 for (Idx = LLVMAttributeFunctionIndex,
67 ParamCount = LLVMCountParams(Fn: F);
68 Idx <= ParamCount; ++Idx) {
69 int AttrCount = LLVMGetCallSiteAttributeCount(C: I, Idx);
70 LLVMAttributeRef *Attrs = 0;
71 if (AttrCount) {
72 Attrs = (LLVMAttributeRef *)malloc(
73 size: AttrCount * sizeof(LLVMAttributeRef));
74 assert(Attrs);
75 }
76 LLVMGetCallSiteAttributes(C: I, Idx, Attrs);
77 free(ptr: Attrs);
78 }
79 }
80 }
81 }
82
83 F = LLVMGetNextFunction(Fn: F);
84 }
85
86 LLVMDisposeModule(M);
87 LLVMContextDispose(C);
88
89 return 0;
90}
91