llvm-project/llvm/tools/llvm-c-test/attributes.c

81 lines
2.8 KiB
C
Raw Normal View History

/*===-- attributes.c - tool for testing libLLVM and llvm-c API ------------===*\
|* *|
|* The LLVM Compiler Infrastructure *|
|* *|
|* This file is distributed under the University of Illinois Open Source *|
|* License. See LICENSE.TXT for details. *|
|* *|
|*===----------------------------------------------------------------------===*|
|* *|
|* This file implements the --test-attributes and --test-callsite-attributes *|
|* commands in llvm-c-test. *|
|* *|
\*===----------------------------------------------------------------------===*/
#include "llvm-c-test.h"
Report fatal error in the case of out of memory This is the second part of recommit of r325224. The previous part was committed in r325426, which deals with C++ memory allocation. Solution for C memory allocation involved functions `llvm::malloc` and similar. This was a fragile solution because it caused ambiguity errors in some cases. In this commit the new functions have names like `llvm::safe_malloc`. The relevant part of original comment is below, updated for new function names. Analysis of fails in the case of out of memory errors can be tricky on Windows. Such error emerges at the point where memory allocation function fails, but manifests itself when null pointer is used. These two points may be distant from each other. Besides, next runs may not exhibit allocation error. In some cases memory is allocated by a call to some of C allocation functions, malloc, calloc and realloc. They are used for interoperability with C code, when allocated object has variable size and when it is necessary to avoid call of constructors. In many calls the result is not checked for null pointer. To simplify checks, new functions are defined in the namespace 'llvm': `safe_malloc`, `safe_calloc` and `safe_realloc`. They behave as corresponding standard functions but produce fatal error if allocation fails. This change replaces the standard functions like 'malloc' in the cases when the result of the allocation function is not checked for null pointer. Finally, there are plain C code, that uses malloc and similar functions. If the result is not checked, assert statement is added. Differential Revision: https://reviews.llvm.org/D43010 llvm-svn: 325551
2018-02-20 13:41:26 +08:00
#include <assert.h>
#include <stdlib.h>
int llvm_test_function_attributes(void) {
LLVMEnablePrettyStackTrace();
LLVMModuleRef M = llvm_load_module(false, true);
LLVMValueRef F = LLVMGetFirstFunction(M);
while (F) {
// Read attributes
int Idx, ParamCount;
for (Idx = LLVMAttributeFunctionIndex, ParamCount = LLVMCountParams(F);
Idx <= ParamCount; ++Idx) {
int AttrCount = LLVMGetAttributeCountAtIndex(F, Idx);
LLVMAttributeRef *Attrs =
(LLVMAttributeRef *)malloc(AttrCount * sizeof(LLVMAttributeRef));
Report fatal error in the case of out of memory This is the second part of recommit of r325224. The previous part was committed in r325426, which deals with C++ memory allocation. Solution for C memory allocation involved functions `llvm::malloc` and similar. This was a fragile solution because it caused ambiguity errors in some cases. In this commit the new functions have names like `llvm::safe_malloc`. The relevant part of original comment is below, updated for new function names. Analysis of fails in the case of out of memory errors can be tricky on Windows. Such error emerges at the point where memory allocation function fails, but manifests itself when null pointer is used. These two points may be distant from each other. Besides, next runs may not exhibit allocation error. In some cases memory is allocated by a call to some of C allocation functions, malloc, calloc and realloc. They are used for interoperability with C code, when allocated object has variable size and when it is necessary to avoid call of constructors. In many calls the result is not checked for null pointer. To simplify checks, new functions are defined in the namespace 'llvm': `safe_malloc`, `safe_calloc` and `safe_realloc`. They behave as corresponding standard functions but produce fatal error if allocation fails. This change replaces the standard functions like 'malloc' in the cases when the result of the allocation function is not checked for null pointer. Finally, there are plain C code, that uses malloc and similar functions. If the result is not checked, assert statement is added. Differential Revision: https://reviews.llvm.org/D43010 llvm-svn: 325551
2018-02-20 13:41:26 +08:00
assert(Attrs);
LLVMGetAttributesAtIndex(F, Idx, Attrs);
free(Attrs);
}
F = LLVMGetNextFunction(F);
}
LLVMDisposeModule(M);
return 0;
}
int llvm_test_callsite_attributes(void) {
LLVMEnablePrettyStackTrace();
LLVMModuleRef M = llvm_load_module(false, true);
LLVMValueRef F = LLVMGetFirstFunction(M);
while (F) {
LLVMBasicBlockRef BB;
for (BB = LLVMGetFirstBasicBlock(F); BB; BB = LLVMGetNextBasicBlock(BB)) {
LLVMValueRef I;
for (I = LLVMGetFirstInstruction(BB); I; I = LLVMGetNextInstruction(I)) {
if (LLVMIsACallInst(I)) {
// Read attributes
int Idx, ParamCount;
for (Idx = LLVMAttributeFunctionIndex,
ParamCount = LLVMCountParams(F);
Idx <= ParamCount; ++Idx) {
int AttrCount = LLVMGetCallSiteAttributeCount(I, Idx);
LLVMAttributeRef *Attrs = (LLVMAttributeRef *)malloc(
AttrCount * sizeof(LLVMAttributeRef));
Report fatal error in the case of out of memory This is the second part of recommit of r325224. The previous part was committed in r325426, which deals with C++ memory allocation. Solution for C memory allocation involved functions `llvm::malloc` and similar. This was a fragile solution because it caused ambiguity errors in some cases. In this commit the new functions have names like `llvm::safe_malloc`. The relevant part of original comment is below, updated for new function names. Analysis of fails in the case of out of memory errors can be tricky on Windows. Such error emerges at the point where memory allocation function fails, but manifests itself when null pointer is used. These two points may be distant from each other. Besides, next runs may not exhibit allocation error. In some cases memory is allocated by a call to some of C allocation functions, malloc, calloc and realloc. They are used for interoperability with C code, when allocated object has variable size and when it is necessary to avoid call of constructors. In many calls the result is not checked for null pointer. To simplify checks, new functions are defined in the namespace 'llvm': `safe_malloc`, `safe_calloc` and `safe_realloc`. They behave as corresponding standard functions but produce fatal error if allocation fails. This change replaces the standard functions like 'malloc' in the cases when the result of the allocation function is not checked for null pointer. Finally, there are plain C code, that uses malloc and similar functions. If the result is not checked, assert statement is added. Differential Revision: https://reviews.llvm.org/D43010 llvm-svn: 325551
2018-02-20 13:41:26 +08:00
assert(Attrs);
LLVMGetCallSiteAttributes(I, Idx, Attrs);
free(Attrs);
}
}
}
}
F = LLVMGetNextFunction(F);
}
LLVMDisposeModule(M);
return 0;
}