llvm.org GIT mirror llvm / 8b4e833
[LLVM-C] Expose functions to create debug locations via DIBuilder. These include: * Several functions for creating an LLVMDIBuilder, * LLVMDIBuilderCreateCompileUnit, * LLVMDIBuilderCreateFile, * LLVMDIBuilderCreateDebugLocation. Patch by Harlan Haskins. Differential Revision: https://reviews.llvm.org/D32368 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@317135 91177308-0d34-0410-b5e6-96231b3b80d8 whitequark 1 year, 10 months ago
7 changed file(s) with 338 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
0 //===------------ DebugInfo.h - LLVM C API Debug Info API -----------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// This file declares the C API endpoints for generating DWARF Debug Info
10 ///
11 /// Note: This interface is experimental. It is *NOT* stable, and may be
12 /// changed without warning.
13 ///
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm-c/Core.h"
17
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21
22 /// Debug info flags.
23 typedef enum {
24 LLVMDIFlagZero = 0,
25 LLVMDIFlagPrivate = 1,
26 LLVMDIFlagProtected = 2,
27 LLVMDIFlagPublic = 3,
28 LLVMDIFlagFwdDecl = 1 << 2,
29 LLVMDIFlagAppleBlock = 1 << 3,
30 LLVMDIFlagBlockByrefStruct = 1 << 4,
31 LLVMDIFlagVirtual = 1 << 5,
32 LLVMDIFlagArtificial = 1 << 6,
33 LLVMDIFlagExplicit = 1 << 7,
34 LLVMDIFlagPrototyped = 1 << 8,
35 LLVMDIFlagObjcClassComplete = 1 << 9,
36 LLVMDIFlagObjectPointer = 1 << 10,
37 LLVMDIFlagVector = 1 << 11,
38 LLVMDIFlagStaticMember = 1 << 12,
39 LLVMDIFlagLValueReference = 1 << 13,
40 LLVMDIFlagRValueReference = 1 << 14,
41 LLVMDIFlagReserved = 1 << 15,
42 LLVMDIFlagSingleInheritance = 1 << 16,
43 LLVMDIFlagMultipleInheritance = 2 << 16,
44 LLVMDIFlagVirtualInheritance = 3 << 16,
45 LLVMDIFlagIntroducedVirtual = 1 << 18,
46 LLVMDIFlagBitField = 1 << 19,
47 LLVMDIFlagNoReturn = 1 << 20,
48 LLVMDIFlagMainSubprogram = 1 << 21,
49 LLVMDIFlagIndirectVirtualBase = (1 << 2) | (1 << 5),
50 LLVMDIFlagAccessibility = LLVMDIFlagPrivate | LLVMDIFlagProtected |
51 LLVMDIFlagPublic,
52 LLVMDIFlagPtrToMemberRep = LLVMDIFlagSingleInheritance |
53 LLVMDIFlagMultipleInheritance |
54 LLVMDIFlagVirtualInheritance
55 } LLVMDIFlags;
56
57 /// Source languages known by DWARF.
58 typedef enum {
59 LLVMDWARFSourceLanguageC89,
60 LLVMDWARFSourceLanguageC,
61 LLVMDWARFSourceLanguageAda83,
62 LLVMDWARFSourceLanguageC_plus_plus,
63 LLVMDWARFSourceLanguageCobol74,
64 LLVMDWARFSourceLanguageCobol85,
65 LLVMDWARFSourceLanguageFortran77,
66 LLVMDWARFSourceLanguageFortran90,
67 LLVMDWARFSourceLanguagePascal83,
68 LLVMDWARFSourceLanguageModula2,
69 // New in DWARF v3:
70 LLVMDWARFSourceLanguageJava,
71 LLVMDWARFSourceLanguageC99,
72 LLVMDWARFSourceLanguageAda95,
73 LLVMDWARFSourceLanguageFortran95,
74 LLVMDWARFSourceLanguagePLI,
75 LLVMDWARFSourceLanguageObjC,
76 LLVMDWARFSourceLanguageObjC_plus_plus,
77 LLVMDWARFSourceLanguageUPC,
78 LLVMDWARFSourceLanguageD,
79 // New in DWARF v4:
80 LLVMDWARFSourceLanguagePython,
81 // New in DWARF v5:
82 LLVMDWARFSourceLanguageOpenCL,
83 LLVMDWARFSourceLanguageGo,
84 LLVMDWARFSourceLanguageModula3,
85 LLVMDWARFSourceLanguageHaskell,
86 LLVMDWARFSourceLanguageC_plus_plus_03,
87 LLVMDWARFSourceLanguageC_plus_plus_11,
88 LLVMDWARFSourceLanguageOCaml,
89 LLVMDWARFSourceLanguageRust,
90 LLVMDWARFSourceLanguageC11,
91 LLVMDWARFSourceLanguageSwift,
92 LLVMDWARFSourceLanguageJulia,
93 LLVMDWARFSourceLanguageDylan,
94 LLVMDWARFSourceLanguageC_plus_plus_14,
95 LLVMDWARFSourceLanguageFortran03,
96 LLVMDWARFSourceLanguageFortran08,
97 LLVMDWARFSourceLanguageRenderScript,
98 LLVMDWARFSourceLanguageBLISS,
99 // Vendor extensions:
100 LLVMDWARFSourceLanguageMips_Assembler,
101 LLVMDWARFSourceLanguageGOOGLE_RenderScript,
102 LLVMDWARFSourceLanguageBORLAND_Delphi
103 } LLVMDWARFSourceLanguage;
104
105 /// The amount of debug information to emit.
106 typedef enum {
107 LLVMDWARFEmissionNone = 0,
108 LLVMDWARFEmissionFull,
109 LLVMDWARFEmissionLineTablesOnly
110 } LLVMDWARFEmissionKind;
111
112 /// The current debug metadata version number.
113 unsigned LLVMDebugMetadataVersion(void);
114
115 /// The version of debug metadata that's present in the provided \c Module.
116 unsigned LLVMGetModuleDebugMetadataVersion(LLVMModuleRef Module);
117
118 /// Strip debug info in the module if it exists.
119 ///
120 /// To do this, we remove all calls to the debugger intrinsics and any named
121 /// metadata for debugging. We also remove debug locations for instructions.
122 /// Return true if module is modified.
123 LLVMBool LLVMStripModuleDebugInfo(LLVMModuleRef Module);
124
125 /// Construct a builder for a module, and do not allow for unresolved nodes
126 /// attached to the module.
127 LLVMDIBuilderRef LLVMCreateDIBuilderDisallowUnresolved(LLVMModuleRef M);
128
129 /// Construct a builder for a module and collect unresolved nodes attached
130 /// to the module in order to resolve cycles during a call to
131 /// \c LLVMDIBuilderFinalize.
132 LLVMDIBuilderRef LLVMCreateDIBuilder(LLVMModuleRef M);
133
134 /// Deallocates the DIBuilder and everything it owns.
135 /// @note You must call \c LLVMDIBuilderFinalize before this
136 void LLVMDisposeDIBuilder(LLVMDIBuilderRef Builder);
137
138 /// Construct any deferred debug info descriptors.
139 void LLVMDIBuilderFinalize(LLVMDIBuilderRef Builder);
140
141 /// A CompileUnit provides an anchor for all debugging
142 /// information generated during this instance of compilation.
143 /// \param Lang Source programming language, eg.
144 /// \c LLVMDWARFSourceLanguageC99
145 /// \param File File info.
146 /// \param Producer Identify the producer of debugging information
147 /// and code. Usually this is a compiler
148 /// version string.
149 /// \param ProducerLen The length of the C string passed to \c Producer.
150 /// \param isOptimized A boolean flag which indicates whether optimization
151 /// is enabled or not.
152 /// \param Flags This string lists command line options. This
153 /// string is directly embedded in debug info
154 /// output which may be used by a tool
155 /// analyzing generated debugging information.
156 /// \param FlagsLen The length of the C string passed to \c Flags.
157 /// \param RuntimeVer This indicates runtime version for languages like
158 /// Objective-C.
159 /// \param SplitName The name of the file that we'll split debug info
160 /// out into.
161 /// \param SplitNameLen The length of the C string passed to \c SplitName.
162 /// \param Kind The kind of debug information to generate.
163 /// \param DWOId The DWOId if this is a split skeleton compile unit.
164 /// \param SplitDebugInlining Whether to emit inline debug info.
165 /// \param DebugInfoForProfiling Whether to emit extra debug info for
166 /// profile collection.
167 LLVMMetadataRef LLVMDIBuilderCreateCompileUnit(
168 LLVMDIBuilderRef Builder, LLVMDWARFSourceLanguage Lang,
169 LLVMMetadataRef FileRef, const char *Producer, size_t ProducerLen,
170 LLVMBool isOptimized, const char *Flags, size_t FlagsLen,
171 unsigned RuntimeVer, const char *SplitName, size_t SplitNameLen,
172 LLVMDWARFEmissionKind Kind, unsigned DWOId, LLVMBool SplitDebugInlining,
173 LLVMBool DebugInfoForProfiling);
174
175 /// Create a file descriptor to hold debugging information for a file.
176 /// \param Builder The DIBuilder.
177 /// \param Filename File name.
178 /// \param FilenameLen The length of the C string passed to \c Filename.
179 /// \param Directory Directory.
180 /// \param DirectoryLen The length of the C string passed to \c Directory.
181 LLVMMetadataRef
182 LLVMDIBuilderCreateFile(LLVMDIBuilderRef Builder, const char *Filename,
183 size_t FilenameLen, const char *Directory,
184 size_t DirectoryLen);
185
186 /// Creates a new DebugLocation that describes a source location.
187 /// \param Line The line in the source file.
188 /// \param Column The column in the source file.
189 /// \param Scope The scope in which the location resides.
190 /// \param InlinedAt The scope where this location was inlined, if at all.
191 /// (optional).
192 /// \note If the item to which this location is attached cannot be
193 /// attributed to a source line, pass 0 for the line and column.
194 LLVMMetadataRef
195 LLVMDIBuilderCreateDebugLocation(LLVMContextRef Ctx, unsigned Line,
196 unsigned Column, LLVMMetadataRef Scope,
197 LLVMMetadataRef InlinedAt);
198
199 #ifdef __cplusplus
200 } // end extern "C"
201 #endif
1111 //
1212 //===----------------------------------------------------------------------===//
1313
14 #include "llvm/IR/DebugInfo.h"
14 #include "llvm-c/DebugInfo.h"
15 #include "LLVMContextImpl.h"
1516 #include "llvm/ADT/DenseMap.h"
1617 #include "llvm/ADT/DenseSet.h"
1718 #include "llvm/ADT/None.h"
19 #include "llvm/ADT/STLExtras.h"
1820 #include "llvm/ADT/SmallPtrSet.h"
1921 #include "llvm/ADT/SmallVector.h"
2022 #include "llvm/ADT/StringRef.h"
2224 #include "llvm/IR/Constants.h"
2325 #include "llvm/IR/DebugInfoMetadata.h"
2426 #include "llvm/IR/DebugLoc.h"
27 #include "llvm/IR/DebugInfo.h"
28 #include "llvm/IR/DIBuilder.h"
2529 #include "llvm/IR/Function.h"
2630 #include "llvm/IR/GVMaterializer.h"
2731 #include "llvm/IR/Instruction.h"
691695 setDebugLoc(DILocation::get(
692696 Result->getContext(), 0, 0, Result->getScope(), Result->getInlinedAt()));
693697 }
698
699 //===----------------------------------------------------------------------===//
700 // LLVM C API implementations.
701 //===----------------------------------------------------------------------===//
702
703 static unsigned map_from_llvmDWARFsourcelanguage(LLVMDWARFSourceLanguage lang) {
704 switch (lang) {
705 #define HANDLE_DW_LANG(ID, NAME, VERSION, VENDOR) \
706 case LLVMDWARFSourceLanguage##NAME: return ID;
707 #include "llvm/BinaryFormat/Dwarf.def"
708 #undef HANDLE_DW_LANG
709 }
710 llvm_unreachable("Unhandled Tag");
711 }
712
713 unsigned LLVMDebugMetadataVersion() {
714 return DEBUG_METADATA_VERSION;
715 }
716
717 LLVMDIBuilderRef LLVMCreateDIBuilderDisallowUnresolved(LLVMModuleRef M) {
718 return wrap(new DIBuilder(*unwrap(M), false));
719 }
720
721 LLVMDIBuilderRef LLVMCreateDIBuilder(LLVMModuleRef M) {
722 return wrap(new DIBuilder(*unwrap(M)));
723 }
724
725 unsigned LLVMGetModuleDebugMetadataVersion(LLVMModuleRef M) {
726 return getDebugMetadataVersionFromModule(*unwrap(M));
727 }
728
729 LLVMBool LLVMStripModuleDebugInfo(LLVMModuleRef M) {
730 return StripDebugInfo(*unwrap(M));
731 }
732
733 void LLVMDisposeDIBuilder(LLVMDIBuilderRef Builder) {
734 delete unwrap(Builder);
735 }
736
737 void LLVMDIBuilderFinalize(LLVMDIBuilderRef Builder) {
738 unwrap(Builder)->finalize();
739 }
740
741 LLVMMetadataRef LLVMDIBuilderCreateCompileUnit(
742 LLVMDIBuilderRef Builder, LLVMDWARFSourceLanguage Lang,
743 LLVMMetadataRef FileRef, const char *Producer, size_t ProducerLen,
744 LLVMBool isOptimized, const char *Flags, size_t FlagsLen,
745 unsigned RuntimeVer, const char *SplitName, size_t SplitNameLen,
746 LLVMDWARFEmissionKind Kind, unsigned DWOId, LLVMBool SplitDebugInlining,
747 LLVMBool DebugInfoForProfiling) {
748 auto File = unwrap(FileRef);
749
750 return wrap(unwrap(Builder)->createCompileUnit(
751 map_from_llvmDWARFsourcelanguage(Lang), File,
752 StringRef(Producer, ProducerLen), isOptimized,
753 StringRef(Flags, FlagsLen), RuntimeVer,
754 StringRef(SplitName, SplitNameLen),
755 static_cast(Kind), DWOId,
756 SplitDebugInlining, DebugInfoForProfiling));
757 }
758
759 LLVMMetadataRef
760 LLVMDIBuilderCreateFile(LLVMDIBuilderRef Builder, const char *Filename,
761 size_t FilenameLen, const char *Directory,
762 size_t DirectoryLen) {
763 return wrap(unwrap(Builder)->createFile(StringRef(Filename, FilenameLen),
764 StringRef(Directory, DirectoryLen)));
765 }
766
767 LLVMMetadataRef
768 LLVMDIBuilderCreateDebugLocation(LLVMContextRef Ctx, unsigned Line,
769 unsigned Column, LLVMMetadataRef Scope,
770 LLVMMetadataRef InlinedAt) {
771 return wrap(DILocation::get(*unwrap(Ctx), Line, Column, unwrap(Scope),
772 unwrap(InlinedAt)));
773 }
0 ; RUN: llvm-c-test --test-dibuilder | FileCheck %s
1
2 ; CHECK: ; ModuleID = 'debuginfo.c'
3 ; CHECK-NEXT: source_filename = "debuginfo.c"
4
5 ; CHECK: !llvm.dbg.cu = !{!0}
6 ; CHECK: !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "llvm-c-test", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false)
7 ; CHECK-NEXT: !1 = !DIFile(filename: "debuginfo.c\00", directory: ".")
3737 add_llvm_tool(llvm-c-test
3838 attributes.c
3939 calc.c
40 debuginfo.c
4041 diagnostic.c
4142 disassemble.c
4243 echo.cpp
0 /*===-- debuginfo.c - tool for testing libLLVM and llvm-c API -------------===*\
1 |* *|
2 |* The LLVM Compiler Infrastructure *|
3 |* *|
4 |* This file is distributed under the University of Illinois Open Source *|
5 |* License. See LICENSE.TXT for details. *|
6 |* *|
7 |*===----------------------------------------------------------------------===*|
8 |* *|
9 |* Tests for the LLVM C DebugInfo API *|
10 |* *|
11 \*===----------------------------------------------------------------------===*/
12
13 #include "llvm-c-test.h"
14 #include "llvm-c/DebugInfo.h"
15 #include
16 #include
17
18 int llvm_test_dibuilder() {
19 LLVMModuleRef M = LLVMModuleCreateWithName("debuginfo.c");
20 LLVMDIBuilderRef DIB = LLVMCreateDIBuilder(M);
21
22 LLVMMetadataRef File = LLVMDIBuilderCreateFile(DIB, "debuginfo.c", 12,
23 ".", 1);
24
25 LLVMDIBuilderCreateCompileUnit(DIB,
26 LLVMDWARFSourceLanguageC, File,"llvm-c-test", 11, 0, NULL, 0, 0,
27 NULL, 0, LLVMDWARFEmissionFull, 0, 0, 0);
28
29 char *MStr = LLVMPrintModuleToString(M);
30 puts(MStr);
31 LLVMDisposeMessage(MStr);
32
33 LLVMDisposeDIBuilder(DIB);
34 LLVMDisposeModule(M);
35
36 return 0;
37 }
3434 // disassemble.c
3535 int llvm_disassemble(void);
3636
37 // debuginfo.c
38 int llvm_test_dibuilder(void);
39
3740 // metadata.c
3841 int llvm_add_named_metadata_operand(void);
3942 int llvm_set_metadata(void);
5454 fprintf(stderr, " * --test-diagnostic-handler\n");
5555 fprintf(stderr,
5656 " Read bitcode file form stdin with a diagnostic handler set\n\n");
57 fprintf(stderr, " * --test-dibuilder\n");
58 fprintf(stderr,
59 " Run tests for the DIBuilder C API - print generated module\n\n");
5760 }
5861
5962 int main(int argc, char **argv) {
9598 return llvm_echo();
9699 } else if (argc == 2 && !strcmp(argv[1], "--test-diagnostic-handler")) {
97100 return llvm_test_diagnostic_handler();
101 } else if (argc == 2 && !strcmp(argv[1], "--test-dibuilder")) {
102 return llvm_test_dibuilder();
98103 } else {
99104 print_usage();
100105 }