2017-11-02 06:18:52 +08:00
|
|
|
/*===-- debuginfo.c - tool for testing libLLVM and llvm-c API -------------===*\
|
|
|
|
|* *|
|
2019-01-19 16:50:56 +08:00
|
|
|
|* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
|
|
|
|
|* Exceptions. *|
|
|
|
|
|* See https://llvm.org/LICENSE.txt for license information. *|
|
|
|
|
|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *|
|
2017-11-02 06:18:52 +08:00
|
|
|
|* *|
|
|
|
|
|*===----------------------------------------------------------------------===*|
|
|
|
|
|* *|
|
|
|
|
|* Tests for the LLVM C DebugInfo API *|
|
|
|
|
|* *|
|
|
|
|
\*===----------------------------------------------------------------------===*/
|
|
|
|
|
2018-04-02 08:17:40 +08:00
|
|
|
#include "llvm-c-test.h"
|
|
|
|
#include "llvm-c/Core.h"
|
2017-11-02 06:18:52 +08:00
|
|
|
#include "llvm-c/DebugInfo.h"
|
|
|
|
#include <stdio.h>
|
2018-04-02 08:17:40 +08:00
|
|
|
#include <string.h>
|
2017-11-02 06:18:52 +08:00
|
|
|
|
2018-05-22 00:27:35 +08:00
|
|
|
static LLVMMetadataRef
|
|
|
|
declare_objc_class(LLVMDIBuilderRef DIB, LLVMMetadataRef File) {
|
|
|
|
LLVMMetadataRef Decl = LLVMDIBuilderCreateStructType(DIB, File, "TestClass", 9, File, 42, 64, 0, LLVMDIFlagObjcClassComplete, NULL, NULL, 0, 0, NULL, NULL, 0);
|
|
|
|
LLVMMetadataRef SuperDecl = LLVMDIBuilderCreateStructType(DIB, File, "TestSuperClass", 14, File, 42, 64, 0, LLVMDIFlagObjcClassComplete, NULL, NULL, 0, 0, NULL, NULL, 0);
|
|
|
|
LLVMDIBuilderCreateInheritance(DIB, Decl, SuperDecl, 0, 0, 0);
|
|
|
|
LLVMMetadataRef TestProperty =
|
|
|
|
LLVMDIBuilderCreateObjCProperty(DIB, "test", 4, File, 42, "getTest", 7, "setTest", 7, 0x20 /*copy*/ | 0x40 /*nonatomic*/, SuperDecl);
|
|
|
|
LLVMDIBuilderCreateObjCIVar(DIB, "_test", 5, File, 42, 64, 0, 64, LLVMDIFlagPublic, SuperDecl, TestProperty);
|
|
|
|
return Decl;
|
|
|
|
}
|
|
|
|
|
2017-12-16 10:54:17 +08:00
|
|
|
int llvm_test_dibuilder(void) {
|
2018-04-02 08:17:40 +08:00
|
|
|
const char *Filename = "debuginfo.c";
|
|
|
|
LLVMModuleRef M = LLVMModuleCreateWithName(Filename);
|
2017-11-02 06:18:52 +08:00
|
|
|
LLVMDIBuilderRef DIB = LLVMCreateDIBuilder(M);
|
|
|
|
|
2018-04-02 08:17:40 +08:00
|
|
|
LLVMMetadataRef File = LLVMDIBuilderCreateFile(DIB, Filename,
|
|
|
|
strlen(Filename), ".", 1);
|
2017-11-02 06:18:52 +08:00
|
|
|
|
2020-01-15 05:37:04 +08:00
|
|
|
LLVMMetadataRef CompileUnit = LLVMDIBuilderCreateCompileUnit(
|
|
|
|
DIB, LLVMDWARFSourceLanguageC, File, "llvm-c-test", 11, 0, NULL, 0, 0,
|
2020-03-05 06:12:54 +08:00
|
|
|
NULL, 0, LLVMDWARFEmissionFull, 0, 0, 0, "/", 1, "", 0);
|
2018-04-23 21:51:43 +08:00
|
|
|
|
|
|
|
LLVMMetadataRef Module =
|
|
|
|
LLVMDIBuilderCreateModule(DIB, CompileUnit,
|
|
|
|
"llvm-c-test", 11,
|
|
|
|
"", 0,
|
2020-03-04 08:05:23 +08:00
|
|
|
"/test/include/llvm-c-test.h", 27,
|
|
|
|
"", 0);
|
2018-04-23 21:51:43 +08:00
|
|
|
|
2018-04-29 06:32:07 +08:00
|
|
|
LLVMMetadataRef OtherModule =
|
|
|
|
LLVMDIBuilderCreateModule(DIB, CompileUnit,
|
|
|
|
"llvm-c-test-import", 18,
|
|
|
|
"", 0,
|
2020-03-04 08:05:23 +08:00
|
|
|
"/test/include/llvm-c-test-import.h", 34,
|
|
|
|
"", 0);
|
2018-04-29 06:32:07 +08:00
|
|
|
LLVMMetadataRef ImportedModule =
|
|
|
|
LLVMDIBuilderCreateImportedModuleFromModule(DIB, Module, OtherModule,
|
|
|
|
File, 42);
|
2018-04-29 10:01:34 +08:00
|
|
|
LLVMDIBuilderCreateImportedModuleFromAlias(DIB, Module, ImportedModule,
|
|
|
|
File, 42);
|
2018-04-29 06:32:07 +08:00
|
|
|
|
2018-05-22 00:27:35 +08:00
|
|
|
LLVMMetadataRef ClassTy = declare_objc_class(DIB, File);
|
|
|
|
LLVMMetadataRef GlobalClassValueExpr =
|
2018-10-04 02:44:53 +08:00
|
|
|
LLVMDIBuilderCreateConstantValueExpression(DIB, 0);
|
|
|
|
LLVMDIBuilderCreateGlobalVariableExpression(
|
|
|
|
DIB, Module, "globalClass", 11, "", 0, File, 1, ClassTy, true,
|
|
|
|
GlobalClassValueExpr, NULL, 0);
|
2018-05-22 00:27:35 +08:00
|
|
|
|
2018-04-02 08:17:40 +08:00
|
|
|
LLVMMetadataRef Int64Ty =
|
2018-10-04 02:44:53 +08:00
|
|
|
LLVMDIBuilderCreateBasicType(DIB, "Int64", 5, 64, 0, LLVMDIFlagZero);
|
2018-05-11 05:10:06 +08:00
|
|
|
LLVMMetadataRef Int64TypeDef =
|
2019-12-03 11:59:54 +08:00
|
|
|
LLVMDIBuilderCreateTypedef(DIB, Int64Ty, "int64_t", 7, File, 42, File, 0);
|
2018-05-11 05:10:06 +08:00
|
|
|
|
2018-04-24 06:31:49 +08:00
|
|
|
LLVMMetadataRef GlobalVarValueExpr =
|
2018-10-04 02:44:53 +08:00
|
|
|
LLVMDIBuilderCreateConstantValueExpression(DIB, 0);
|
|
|
|
LLVMDIBuilderCreateGlobalVariableExpression(
|
|
|
|
DIB, Module, "global", 6, "", 0, File, 1, Int64TypeDef, true,
|
|
|
|
GlobalVarValueExpr, NULL, 0);
|
2018-04-24 06:31:49 +08:00
|
|
|
|
|
|
|
LLVMMetadataRef NameSpace =
|
2018-10-04 02:44:53 +08:00
|
|
|
LLVMDIBuilderCreateNameSpace(DIB, Module, "NameSpace", 9, false);
|
2018-04-02 08:17:40 +08:00
|
|
|
|
|
|
|
LLVMMetadataRef StructDbgElts[] = {Int64Ty, Int64Ty, Int64Ty};
|
|
|
|
LLVMMetadataRef StructDbgTy =
|
2018-04-23 21:51:43 +08:00
|
|
|
LLVMDIBuilderCreateStructType(DIB, NameSpace, "MyStruct",
|
2018-04-02 08:17:40 +08:00
|
|
|
8, File, 0, 192, 0, 0, NULL, StructDbgElts, 3,
|
|
|
|
LLVMDWARFSourceLanguageC, NULL, "MyStruct", 8);
|
|
|
|
|
|
|
|
LLVMMetadataRef StructDbgPtrTy =
|
|
|
|
LLVMDIBuilderCreatePointerType(DIB, StructDbgTy, 192, 0, 0, "", 0);
|
|
|
|
|
|
|
|
LLVMAddNamedMetadataOperand(M, "FooType",
|
|
|
|
LLVMMetadataAsValue(LLVMGetModuleContext(M), StructDbgPtrTy));
|
|
|
|
|
2018-04-07 14:07:55 +08:00
|
|
|
|
2018-04-23 22:29:33 +08:00
|
|
|
LLVMTypeRef FooParamTys[] = {
|
|
|
|
LLVMInt64Type(),
|
|
|
|
LLVMInt64Type(),
|
|
|
|
LLVMVectorType(LLVMInt64Type(), 10),
|
|
|
|
};
|
|
|
|
LLVMTypeRef FooFuncTy = LLVMFunctionType(LLVMInt64Type(), FooParamTys, 3, 0);
|
2018-04-07 14:07:55 +08:00
|
|
|
LLVMValueRef FooFunction = LLVMAddFunction(M, "foo", FooFuncTy);
|
2018-04-23 03:24:44 +08:00
|
|
|
LLVMBasicBlockRef FooEntryBlock = LLVMAppendBasicBlock(FooFunction, "entry");
|
2018-04-07 14:07:55 +08:00
|
|
|
|
2018-04-23 22:29:33 +08:00
|
|
|
LLVMMetadataRef Subscripts[] = {
|
|
|
|
LLVMDIBuilderGetOrCreateSubrange(DIB, 0, 10),
|
|
|
|
};
|
|
|
|
LLVMMetadataRef VectorTy =
|
|
|
|
LLVMDIBuilderCreateVectorType(DIB, 64 * 10, 0,
|
|
|
|
Int64Ty, Subscripts, 1);
|
|
|
|
|
|
|
|
|
|
|
|
LLVMMetadataRef ParamTypes[] = {Int64Ty, Int64Ty, VectorTy};
|
2018-04-07 14:07:55 +08:00
|
|
|
LLVMMetadataRef FunctionTy =
|
2018-04-23 22:29:33 +08:00
|
|
|
LLVMDIBuilderCreateSubroutineType(DIB, File, ParamTypes, 3, 0);
|
2018-05-11 02:09:53 +08:00
|
|
|
|
|
|
|
LLVMMetadataRef ReplaceableFunctionMetadata =
|
|
|
|
LLVMDIBuilderCreateReplaceableCompositeType(DIB, 0x15, "foo", 3,
|
|
|
|
File, File, 42,
|
|
|
|
0, 0, 0,
|
|
|
|
LLVMDIFlagFwdDecl,
|
|
|
|
"", 0);
|
|
|
|
|
|
|
|
LLVMMetadataRef FooParamLocation =
|
|
|
|
LLVMDIBuilderCreateDebugLocation(LLVMGetGlobalContext(), 42, 0,
|
|
|
|
ReplaceableFunctionMetadata, NULL);
|
2018-04-07 14:07:55 +08:00
|
|
|
LLVMMetadataRef FunctionMetadata =
|
|
|
|
LLVMDIBuilderCreateFunction(DIB, File, "foo", 3, "foo", 3,
|
|
|
|
File, 42, FunctionTy, true, true,
|
|
|
|
42, 0, false);
|
2018-05-11 02:09:53 +08:00
|
|
|
LLVMMetadataReplaceAllUsesWith(ReplaceableFunctionMetadata, FunctionMetadata);
|
2018-04-23 22:29:33 +08:00
|
|
|
|
2018-04-23 03:24:44 +08:00
|
|
|
LLVMMetadataRef FooParamExpression =
|
|
|
|
LLVMDIBuilderCreateExpression(DIB, NULL, 0);
|
|
|
|
LLVMMetadataRef FooParamVar1 =
|
|
|
|
LLVMDIBuilderCreateParameterVariable(DIB, FunctionMetadata, "a", 1, 1, File,
|
|
|
|
42, Int64Ty, true, 0);
|
|
|
|
LLVMDIBuilderInsertDeclareAtEnd(DIB, LLVMConstInt(LLVMInt64Type(), 0, false),
|
|
|
|
FooParamVar1, FooParamExpression,
|
|
|
|
FooParamLocation, FooEntryBlock);
|
|
|
|
LLVMMetadataRef FooParamVar2 =
|
|
|
|
LLVMDIBuilderCreateParameterVariable(DIB, FunctionMetadata, "b", 1, 2, File,
|
|
|
|
42, Int64Ty, true, 0);
|
|
|
|
LLVMDIBuilderInsertDeclareAtEnd(DIB, LLVMConstInt(LLVMInt64Type(), 0, false),
|
|
|
|
FooParamVar2, FooParamExpression,
|
|
|
|
FooParamLocation, FooEntryBlock);
|
2018-04-23 22:29:33 +08:00
|
|
|
LLVMMetadataRef FooParamVar3 =
|
|
|
|
LLVMDIBuilderCreateParameterVariable(DIB, FunctionMetadata, "c", 1, 3, File,
|
|
|
|
42, VectorTy, true, 0);
|
|
|
|
LLVMDIBuilderInsertDeclareAtEnd(DIB, LLVMConstInt(LLVMInt64Type(), 0, false),
|
|
|
|
FooParamVar3, FooParamExpression,
|
|
|
|
FooParamLocation, FooEntryBlock);
|
|
|
|
|
2018-04-07 14:07:55 +08:00
|
|
|
LLVMSetSubprogram(FooFunction, FunctionMetadata);
|
|
|
|
|
|
|
|
LLVMMetadataRef FooLexicalBlock =
|
|
|
|
LLVMDIBuilderCreateLexicalBlock(DIB, FunctionMetadata, File, 42, 0);
|
|
|
|
|
2018-04-24 06:31:49 +08:00
|
|
|
LLVMBasicBlockRef FooVarBlock = LLVMAppendBasicBlock(FooFunction, "vars");
|
|
|
|
LLVMMetadataRef FooVarsLocation =
|
|
|
|
LLVMDIBuilderCreateDebugLocation(LLVMGetGlobalContext(), 43, 0,
|
|
|
|
FunctionMetadata, NULL);
|
|
|
|
LLVMMetadataRef FooVar1 =
|
|
|
|
LLVMDIBuilderCreateAutoVariable(DIB, FooLexicalBlock, "d", 1, File,
|
|
|
|
43, Int64Ty, true, 0, 0);
|
|
|
|
LLVMValueRef FooVal1 = LLVMConstInt(LLVMInt64Type(), 0, false);
|
|
|
|
LLVMMetadataRef FooVarValueExpr =
|
|
|
|
LLVMDIBuilderCreateConstantValueExpression(DIB, 0);
|
|
|
|
|
|
|
|
LLVMDIBuilderInsertDbgValueAtEnd(DIB, FooVal1, FooVar1, FooVarValueExpr,
|
|
|
|
FooVarsLocation, FooVarBlock);
|
2018-04-07 14:07:55 +08:00
|
|
|
|
2019-10-07 21:57:13 +08:00
|
|
|
LLVMMetadataRef MacroFile =
|
|
|
|
LLVMDIBuilderCreateTempMacroFile(DIB, NULL, 0, File);
|
|
|
|
LLVMDIBuilderCreateMacro(DIB, MacroFile, 0, LLVMDWARFMacinfoRecordTypeDefine,
|
|
|
|
"SIMPLE_DEFINE", 13, NULL, 0);
|
|
|
|
LLVMDIBuilderCreateMacro(DIB, MacroFile, 0, LLVMDWARFMacinfoRecordTypeDefine,
|
|
|
|
"VALUE_DEFINE", 12, "1", 1);
|
|
|
|
|
|
|
|
LLVMMetadataRef EnumeratorTestA =
|
|
|
|
LLVMDIBuilderCreateEnumerator(DIB, "Test_A", strlen("Test_A"), 0, true);
|
|
|
|
LLVMMetadataRef EnumeratorTestB =
|
|
|
|
LLVMDIBuilderCreateEnumerator(DIB, "Test_B", strlen("Test_B"), 1, true);
|
|
|
|
LLVMMetadataRef EnumeratorTestC =
|
|
|
|
LLVMDIBuilderCreateEnumerator(DIB, "Test_B", strlen("Test_C"), 2, true);
|
|
|
|
LLVMMetadataRef EnumeratorsTest[] = {EnumeratorTestA, EnumeratorTestB,
|
|
|
|
EnumeratorTestC};
|
|
|
|
LLVMMetadataRef EnumTest = LLVMDIBuilderCreateEnumerationType(
|
|
|
|
DIB, NameSpace, "EnumTest", strlen("EnumTest"), File, 0, 64, 0,
|
|
|
|
EnumeratorsTest, 3, Int64Ty);
|
|
|
|
LLVMAddNamedMetadataOperand(
|
|
|
|
M, "EnumTest", LLVMMetadataAsValue(LLVMGetModuleContext(M), EnumTest));
|
|
|
|
|
2018-04-07 14:07:55 +08:00
|
|
|
LLVMDIBuilderFinalize(DIB);
|
|
|
|
|
2017-11-02 06:18:52 +08:00
|
|
|
char *MStr = LLVMPrintModuleToString(M);
|
|
|
|
puts(MStr);
|
|
|
|
LLVMDisposeMessage(MStr);
|
|
|
|
|
|
|
|
LLVMDisposeDIBuilder(DIB);
|
|
|
|
LLVMDisposeModule(M);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|