llvm.org GIT mirror llvm / 7db3980
Macro debug info support in LLVM IR Introduced DIMacro and DIMacroFile debug info metadata in the LLVM IR to support macros. Differential Revision: http://reviews.llvm.org/D14687 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@255245 91177308-0d34-0410-b5e6-96231b3b80d8 Amjad Aboud 4 years ago
20 changed file(s) with 511 addition(s) and 42 deletion(s). Raw diff Collapse all Expand all
37503750 """""""""""""
37513751
37523752 ``DICompileUnit`` nodes represent a compile unit. The ``enums:``,
3753 ``retainedTypes:``, ``subprograms:``, ``globals:`` and ``imports:`` fields are
3754 tuples containing the debug info to be emitted along with the compile unit,
3755 regardless of code optimizations (some nodes are only emitted if there are
3753 ``retainedTypes:``, ``subprograms:``, ``globals:``, ``imports:`` and ``macros:``
3754 fields are tuples containing the debug info to be emitted along with the compile
3755 unit, regardless of code optimizations (some nodes are only emitted if there are
37563756 references to them from instructions).
37573757
37583758 .. code-block:: llvm
37613761 isOptimized: true, flags: "-O2", runtimeVersion: 2,
37623762 splitDebugFilename: "abc.debug", emissionKind: 1,
37633763 enums: !2, retainedTypes: !3, subprograms: !4,
3764 globals: !5, imports: !6)
3764 globals: !5, imports: !6, macros: !7, dwoId: 0x0abcd)
37653765
37663766 Compile unit descriptors provide the root scope for objects declared in a
37673767 specific compilation unit. File descriptors are defined using this scope.
41264126
41274127 !2 = !DIImportedEntity(tag: DW_TAG_imported_module, name: "foo", scope: !0,
41284128 entity: !1, line: 7)
4129
4130 DIMacro
4131 """""""
4132
4133 ``DIMacro`` nodes represent definition or undefinition of a macro identifiers.
4134 The ``name:`` field is the macro identifier, followed by macro parameters when
4135 definining a function-like macro, and the ``value`` field is the token-string
4136 used to expand the macro identifier.
4137
4138 .. code-block:: llvm
4139
4140 !2 = !DIMacro(macinfo: DW_MACINFO_define, line: 7, name: "foo(x)",
4141 value: "((x) + 1)")
4142 !3 = !DIMacro(macinfo: DW_MACINFO_undef, line: 30, name: "foo")
4143
4144 DIMacroFile
4145 """""""""""
4146
4147 ``DIMacroFile`` nodes represent inclusion of source files.
4148 The ``nodes:`` field is a list of ``DIMacro`` and ``DIMacroFile`` nodes that
4149 appear in the included source file.
4150
4151 .. code-block:: llvm
4152
4153 !2 = !DIMacroFile(macinfo: DW_MACINFO_start_file, line: 7, file: !2,
4154 nodes: !3)
41294155
41304156 '``tbaa``' Metadata
41314157 ^^^^^^^^^^^^^^^^^^^
219219 METADATA_EXPRESSION = 29, // [distinct, n x element]
220220 METADATA_OBJC_PROPERTY = 30, // [distinct, name, file, line, ...]
221221 METADATA_IMPORTED_ENTITY=31, // [distinct, tag, scope, entity, line, name]
222 METADATA_MODULE=32, // [distinct, scope, name, ...]
222 METADATA_MODULE = 32, // [distinct, scope, name, ...]
223 METADATA_MACRO = 33, // [distinct, macinfo, line, name, value]
224 METADATA_MACRO_FILE = 34, // [distinct, macinfo, line, file, ...]
223225 };
224226
225227 // The constants block (CONSTANTS_BLOCK_ID) describes emission for each
948948 unsigned EmissionKind, DICompositeTypeArray EnumTypes,
949949 DITypeArray RetainedTypes, DISubprogramArray Subprograms,
950950 DIGlobalVariableArray GlobalVariables,
951 DIImportedEntityArray ImportedEntities, uint64_t DWOId,
952 StorageType Storage, bool ShouldCreate = true) {
951 DIImportedEntityArray ImportedEntities, DIMacroNodeArray Macros,
952 uint64_t DWOId, StorageType Storage, bool ShouldCreate = true) {
953953 return getImpl(Context, SourceLanguage, File,
954954 getCanonicalMDString(Context, Producer), IsOptimized,
955955 getCanonicalMDString(Context, Flags), RuntimeVersion,
956956 getCanonicalMDString(Context, SplitDebugFilename),
957957 EmissionKind, EnumTypes.get(), RetainedTypes.get(),
958958 Subprograms.get(), GlobalVariables.get(),
959 ImportedEntities.get(), DWOId, Storage, ShouldCreate);
959 ImportedEntities.get(), Macros.get(), DWOId, Storage,
960 ShouldCreate);
960961 }
961962 static DICompileUnit *
962963 getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
964965 unsigned RuntimeVersion, MDString *SplitDebugFilename,
965966 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
966967 Metadata *Subprograms, Metadata *GlobalVariables,
967 Metadata *ImportedEntities, uint64_t DWOId, StorageType Storage,
968 bool ShouldCreate = true);
968 Metadata *ImportedEntities, Metadata *Macros, uint64_t DWOId,
969 StorageType Storage, bool ShouldCreate = true);
969970
970971 TempDICompileUnit cloneImpl() const {
971972 return getTemporary(
972973 getContext(), getSourceLanguage(), getFile(), getProducer(),
973974 isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(),
974975 getEmissionKind(), getEnumTypes(), getRetainedTypes(), getSubprograms(),
975 getGlobalVariables(), getImportedEntities(), DWOId);
976 getGlobalVariables(), getImportedEntities(), getMacros(), DWOId);
976977 }
977978
978979 static void get() = delete;
986987 StringRef SplitDebugFilename, unsigned EmissionKind,
987988 DICompositeTypeArray EnumTypes, DITypeArray RetainedTypes,
988989 DISubprogramArray Subprograms, DIGlobalVariableArray GlobalVariables,
989 DIImportedEntityArray ImportedEntities, uint64_t DWOId),
990 DIImportedEntityArray ImportedEntities, DIMacroNodeArray Macros,
991 uint64_t DWOId),
990992 (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
991993 SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes, Subprograms,
992 GlobalVariables, ImportedEntities, DWOId))
994 GlobalVariables, ImportedEntities, Macros, DWOId))
993995 DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(
994996 DICompileUnit,
995997 (unsigned SourceLanguage, Metadata *File, MDString *Producer,
996998 bool IsOptimized, MDString *Flags, unsigned RuntimeVersion,
997999 MDString *SplitDebugFilename, unsigned EmissionKind, Metadata *EnumTypes,
9981000 Metadata *RetainedTypes, Metadata *Subprograms,
999 Metadata *GlobalVariables, Metadata *ImportedEntities, uint64_t DWOId),
1001 Metadata *GlobalVariables, Metadata *ImportedEntities, Metadata *Macros,
1002 uint64_t DWOId),
10001003 (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
10011004 SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes, Subprograms,
1002 GlobalVariables, ImportedEntities, DWOId))
1005 GlobalVariables, ImportedEntities, Macros, DWOId))
10031006
10041007 TempDICompileUnit clone() const { return cloneImpl(); }
10051008
10251028 DIImportedEntityArray getImportedEntities() const {
10261029 return cast_or_null(getRawImportedEntities());
10271030 }
1031 DIMacroNodeArray getMacros() const {
1032 return cast_or_null(getRawMacros());
1033 }
10281034 uint64_t getDWOId() const { return DWOId; }
10291035 void setDWOId(uint64_t DwoId) { DWOId = DwoId; }
10301036
10381044 Metadata *getRawSubprograms() const { return getOperand(6); }
10391045 Metadata *getRawGlobalVariables() const { return getOperand(7); }
10401046 Metadata *getRawImportedEntities() const { return getOperand(8); }
1047 Metadata *getRawMacros() const { return getOperand(9); }
10411048
10421049 /// \brief Replace arrays.
10431050 ///
10601067 void replaceImportedEntities(DIImportedEntityArray N) {
10611068 replaceOperandWith(8, N.get());
10621069 }
1070 void replaceMacros(DIMacroNodeArray N) { replaceOperandWith(9, N.get()); }
10631071 /// @}
10641072
10651073 static bool classof(const Metadata *MD) {
21982206 }
21992207 };
22002208
2209 /// \brief Macro Info DWARF-like metadata node.
2210 ///
2211 /// A metadata node with a DWARF macro info (i.e., a constant named
2212 /// \c DW_MACINFO_*, defined in llvm/Support/Dwarf.h). Called \a DIMacroNode
2213 /// because it's potentially used for non-DWARF output.
2214 class DIMacroNode : public MDNode {
2215 friend class LLVMContextImpl;
2216 friend class MDNode;
2217
2218 protected:
2219 DIMacroNode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned MIType,
2220 ArrayRef Ops1, ArrayRef Ops2 = None)
2221 : MDNode(C, ID, Storage, Ops1, Ops2) {
2222 assert(MIType < 1u << 16);
2223 SubclassData16 = MIType;
2224 }
2225 ~DIMacroNode() = default;
2226
2227 template Ty *getOperandAs(unsigned I) const {
2228 return cast_or_null(getOperand(I));
2229 }
2230
2231 StringRef getStringOperand(unsigned I) const {
2232 if (auto *S = getOperandAs(I))
2233 return S->getString();
2234 return StringRef();
2235 }
2236
2237 static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
2238 if (S.empty())
2239 return nullptr;
2240 return MDString::get(Context, S);
2241 }
2242
2243 public:
2244 unsigned getMacinfoType() const { return SubclassData16; }
2245
2246 static bool classof(const Metadata *MD) {
2247 switch (MD->getMetadataID()) {
2248 default:
2249 return false;
2250 case DIMacroKind:
2251 case DIMacroFileKind:
2252 return true;
2253 }
2254 }
2255 };
2256
2257 class DIMacro : public DIMacroNode {
2258 friend class LLVMContextImpl;
2259 friend class MDNode;
2260
2261 unsigned Line;
2262
2263 DIMacro(LLVMContext &C, StorageType Storage, unsigned MIType, unsigned Line,
2264 ArrayRef Ops)
2265 : DIMacroNode(C, DIMacroKind, Storage, MIType, Ops), Line(Line) {}
2266 ~DIMacro() = default;
2267
2268 static DIMacro *getImpl(LLVMContext &Context, unsigned MIType, unsigned Line,
2269 StringRef Name, StringRef Value, StorageType Storage,
2270 bool ShouldCreate = true) {
2271 return getImpl(Context, MIType, Line, getCanonicalMDString(Context, Name),
2272 getCanonicalMDString(Context, Value), Storage, ShouldCreate);
2273 }
2274 static DIMacro *getImpl(LLVMContext &Context, unsigned MIType, unsigned Line,
2275 MDString *Name, MDString *Value, StorageType Storage,
2276 bool ShouldCreate = true);
2277
2278 TempDIMacro cloneImpl() const {
2279 return getTemporary(getContext(), getMacinfoType(), getLine(), getName(),
2280 getValue());
2281 }
2282
2283 public:
2284 DEFINE_MDNODE_GET(DIMacro, (unsigned MIType, unsigned Line, StringRef Name,
2285 StringRef Value = ""),
2286 (MIType, Line, Name, Value))
2287 DEFINE_MDNODE_GET(DIMacro, (unsigned MIType, unsigned Line, MDString *Name,
2288 MDString *Value),
2289 (MIType, Line, Name, Value))
2290
2291 TempDIMacro clone() const { return cloneImpl(); }
2292
2293 unsigned getLine() const { return Line; }
2294
2295 StringRef getName() const { return getStringOperand(0); }
2296 StringRef getValue() const { return getStringOperand(1); }
2297
2298 MDString *getRawName() const { return getOperandAs(0); }
2299 MDString *getRawValue() const { return getOperandAs(1); }
2300
2301 static bool classof(const Metadata *MD) {
2302 return MD->getMetadataID() == DIMacroKind;
2303 }
2304 };
2305
2306 class DIMacroFile : public DIMacroNode {
2307 friend class LLVMContextImpl;
2308 friend class MDNode;
2309
2310 unsigned Line;
2311
2312 DIMacroFile(LLVMContext &C, StorageType Storage, unsigned MIType,
2313 unsigned Line, ArrayRef Ops)
2314 : DIMacroNode(C, DIMacroFileKind, Storage, MIType, Ops), Line(Line) {}
2315 ~DIMacroFile() = default;
2316
2317 static DIMacroFile *getImpl(LLVMContext &Context, unsigned MIType,
2318 unsigned Line, DIFile *File,
2319 DIMacroNodeArray Elements, StorageType Storage,
2320 bool ShouldCreate = true) {
2321 return getImpl(Context, MIType, Line, static_cast(File),
2322 Elements.get(), Storage, ShouldCreate);
2323 }
2324
2325 static DIMacroFile *getImpl(LLVMContext &Context, unsigned MIType,
2326 unsigned Line, Metadata *File, Metadata *Elements,
2327 StorageType Storage, bool ShouldCreate = true);
2328
2329 TempDIMacroFile cloneImpl() const {
2330 return getTemporary(getContext(), getMacinfoType(), getLine(), getFile(),
2331 getElements());
2332 }
2333
2334 public:
2335 DEFINE_MDNODE_GET(DIMacroFile, (unsigned MIType, unsigned Line, DIFile *File,
2336 DIMacroNodeArray Elements),
2337 (MIType, Line, File, Elements))
2338 DEFINE_MDNODE_GET(DIMacroFile, (unsigned MIType, unsigned Line,
2339 Metadata *File, Metadata *Elements),
2340 (MIType, Line, File, Elements))
2341
2342 TempDIMacroFile clone() const { return cloneImpl(); }
2343
2344 void replaceElements(DIMacroNodeArray Elements) {
2345 #ifndef NDEBUG
2346 for (DIMacroNode *Op : getElements())
2347 assert(std::find(Elements->op_begin(), Elements->op_end(), Op) &&
2348 "Lost a macro node during macro node list replacement");
2349 #endif
2350 replaceOperandWith(1, Elements.get());
2351 }
2352
2353 unsigned getLine() const { return Line; }
2354 DIFile *getFile() const { return cast_or_null(getRawFile()); }
2355
2356 DIMacroNodeArray getElements() const {
2357 return cast_or_null(getRawElements());
2358 }
2359
2360 Metadata *getRawFile() const { return getOperand(0); }
2361 Metadata *getRawElements() const { return getOperand(1); }
2362
2363 static bool classof(const Metadata *MD) {
2364 return MD->getMetadataID() == DIMacroFileKind;
2365 }
2366 };
2367
22012368 } // end namespace llvm
22022369
22032370 #undef DEFINE_MDNODE_GET_UNPACK_IMPL
107107 HANDLE_SPECIALIZED_MDNODE_LEAF_UNIQUABLE(DILocalVariable)
108108 HANDLE_SPECIALIZED_MDNODE_LEAF_UNIQUABLE(DIObjCProperty)
109109 HANDLE_SPECIALIZED_MDNODE_LEAF_UNIQUABLE(DIImportedEntity)
110 HANDLE_SPECIALIZED_MDNODE_BRANCH(DIMacroNode)
111 HANDLE_SPECIALIZED_MDNODE_LEAF_UNIQUABLE(DIMacro)
112 HANDLE_SPECIALIZED_MDNODE_LEAF_UNIQUABLE(DIMacroFile)
110113
111114 #undef HANDLE_METADATA
112115 #undef HANDLE_METADATA_LEAF
8282 DIImportedEntityKind,
8383 ConstantAsMetadataKind,
8484 LocalAsMetadataKind,
85 MDStringKind
85 MDStringKind,
86 DIMacroKind,
87 DIMacroFileKind
8688 };
8789
8890 protected:
624624 ///
625625 /// \li \a getTag() returns \a DW_TAG_invalid on invalid input.
626626 /// \li \a getVirtuality() returns \a DW_VIRTUALITY_invalid on invalid input.
627 /// \li \a getMacinfo() returns \a DW_MACINFO_invalid on invalid input.
627628 ///
628629 /// @{
629630 unsigned getTag(StringRef TagString);
631632 unsigned getVirtuality(StringRef VirtualityString);
632633 unsigned getLanguage(StringRef LanguageString);
633634 unsigned getAttributeEncoding(StringRef EncodingString);
635 unsigned getMacinfo(StringRef MacinfoString);
634636 /// @}
635637
636638 /// \brief Returns the symbolic string representing Val when used as a value
777777 DWKEYWORD(VIRTUALITY, DwarfVirtuality);
778778 DWKEYWORD(LANG, DwarfLang);
779779 DWKEYWORD(OP, DwarfOp);
780 DWKEYWORD(MACINFO, DwarfMacinfo);
780781 #undef DWKEYWORD
781782
782783 if (Keyword.startswith("DIFlag")) {
32783278 DwarfTagField(dwarf::Tag DefaultTag)
32793279 : MDUnsignedField(DefaultTag, dwarf::DW_TAG_hi_user) {}
32803280 };
3281 struct DwarfMacinfoTypeField : public MDUnsignedField {
3282 DwarfMacinfoTypeField() : MDUnsignedField(0, dwarf::DW_MACINFO_vendor_ext) {}
3283 DwarfMacinfoTypeField(dwarf::MacinfoRecordType DefaultType)
3284 : MDUnsignedField(DefaultType, dwarf::DW_MACINFO_vendor_ext) {}
3285 };
32813286 struct DwarfAttEncodingField : public MDUnsignedField {
32823287 DwarfAttEncodingField() : MDUnsignedField(0, dwarf::DW_ATE_hi_user) {}
32833288 };
33653370 assert(Tag <= Result.Max && "Expected valid DWARF tag");
33663371
33673372 Result.assign(Tag);
3373 Lex.Lex();
3374 return false;
3375 }
3376
3377 template <>
3378 bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
3379 DwarfMacinfoTypeField &Result) {
3380 if (Lex.getKind() == lltok::APSInt)
3381 return ParseMDField(Loc, Name, static_cast(Result));
3382
3383 if (Lex.getKind() != lltok::DwarfMacinfo)
3384 return TokError("expected DWARF macinfo type");
3385
3386 unsigned Macinfo = dwarf::getMacinfo(Lex.getStrVal());
3387 if (Macinfo == dwarf::DW_MACINFO_invalid)
3388 return TokError(
3389 "invalid DWARF macinfo type" + Twine(" '") + Lex.getStrVal() + "'");
3390 assert(Macinfo <= Result.Max && "Expected valid DWARF macinfo type");
3391
3392 Result.assign(Macinfo);
33683393 Lex.Lex();
33693394 return false;
33703395 }
37813806 /// isOptimized: true, flags: "-O2", runtimeVersion: 1,
37823807 /// splitDebugFilename: "abc.debug", emissionKind: 1,
37833808 /// enums: !1, retainedTypes: !2, subprograms: !3,
3784 /// globals: !4, imports: !5, dwoId: 0x0abcd)
3809 /// globals: !4, imports: !5, macros: !6, dwoId: 0x0abcd)
37853810 bool LLParser::ParseDICompileUnit(MDNode *&Result, bool IsDistinct) {
37863811 if (!IsDistinct)
37873812 return Lex.Error("missing 'distinct', required for !DICompileUnit");
38003825 OPTIONAL(subprograms, MDField, ); \
38013826 OPTIONAL(globals, MDField, ); \
38023827 OPTIONAL(imports, MDField, ); \
3828 OPTIONAL(macros, MDField, ); \
38033829 OPTIONAL(dwoId, MDUnsignedField, );
38043830 PARSE_MD_FIELDS();
38053831 #undef VISIT_MD_FIELDS
38073833 Result = DICompileUnit::getDistinct(
38083834 Context, language.Val, file.Val, producer.Val, isOptimized.Val, flags.Val,
38093835 runtimeVersion.Val, splitDebugFilename.Val, emissionKind.Val, enums.Val,
3810 retainedTypes.Val, subprograms.Val, globals.Val, imports.Val, dwoId.Val);
3836 retainedTypes.Val, subprograms.Val, globals.Val, imports.Val, macros.Val,
3837 dwoId.Val);
38113838 return false;
38123839 }
38133840
39023929 (Context, scope.Val, file.Val, name.Val, line.Val));
39033930 return false;
39043931 }
3932
3933 /// ParseDIMacro:
3934 /// ::= !DIMacro(macinfo: type, line: 9, name: "SomeMacro", value: "SomeValue")
3935 bool LLParser::ParseDIMacro(MDNode *&Result, bool IsDistinct) {
3936 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
3937 REQUIRED(type, DwarfMacinfoTypeField, ); \
3938 REQUIRED(line, LineField, ); \
3939 REQUIRED(name, MDStringField, ); \
3940 OPTIONAL(value, MDStringField, );
3941 PARSE_MD_FIELDS();
3942 #undef VISIT_MD_FIELDS
3943
3944 Result = GET_OR_DISTINCT(DIMacro,
3945 (Context, type.Val, line.Val, name.Val, value.Val));
3946 return false;
3947 }
3948
3949 /// ParseDIMacroFile:
3950 /// ::= !DIMacroFile(line: 9, file: !2, nodes: !3)
3951 bool LLParser::ParseDIMacroFile(MDNode *&Result, bool IsDistinct) {
3952 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
3953 OPTIONAL(type, DwarfMacinfoTypeField, (dwarf::DW_MACINFO_start_file)); \
3954 REQUIRED(line, LineField, ); \
3955 REQUIRED(file, MDField, ); \
3956 OPTIONAL(nodes, MDField, );
3957 PARSE_MD_FIELDS();
3958 #undef VISIT_MD_FIELDS
3959
3960 Result = GET_OR_DISTINCT(DIMacroFile,
3961 (Context, type.Val, line.Val, file.Val, nodes.Val));
3962 return false;
3963 }
3964
39053965
39063966 /// ParseDIModule:
39073967 /// ::= !DIModule(scope: !0, name: "SomeModule", configMacros: "-DNDEBUG",
214214 DwarfLang, // DW_LANG_foo
215215 DwarfOp, // DW_OP_foo
216216 DIFlag, // DIFlagFoo
217 DwarfMacinfo, // DW_MACINFO_foo
217218
218219 // Type valued tokens (TyVal).
219220 Type,
22132213 break;
22142214 }
22152215 case bitc::METADATA_COMPILE_UNIT: {
2216 if (Record.size() < 14 || Record.size() > 15)
2217 return error("Invalid record");
2218
2219 // Ignore Record[1], which indicates whether this compile unit is
2216 if (Record.size() < 14 || Record.size() > 16)
2217 return error("Invalid record");
2218
2219 // Ignore Record[0], which indicates whether this compile unit is
22202220 // distinct. It's always distinct.
22212221 MDValueList.assignValue(
22222222 DICompileUnit::getDistinct(
22252225 Record[6], getMDString(Record[7]), Record[8],
22262226 getMDOrNull(Record[9]), getMDOrNull(Record[10]),
22272227 getMDOrNull(Record[11]), getMDOrNull(Record[12]),
2228 getMDOrNull(Record[13]), Record.size() == 14 ? 0 : Record[14]),
2228 getMDOrNull(Record[13]),
2229 Record.size() <= 15 ? 0 : getMDOrNull(Record[15]),
2230 Record.size() <= 14 ? 0 : Record[14]),
22292231 NextMDValueNo++);
22302232 break;
22312233 }
22902292 (Context, getMDOrNull(Record[1]),
22912293 getMDOrNull(Record[2]), getMDString(Record[3]),
22922294 Record[4])),
2295 NextMDValueNo++);
2296 break;
2297 }
2298 case bitc::METADATA_MACRO: {
2299 if (Record.size() != 5)
2300 return error("Invalid record");
2301
2302 MDValueList.assignValue(
2303 GET_OR_DISTINCT(DIMacro, Record[0],
2304 (Context, Record[1], Record[2],
2305 getMDString(Record[3]), getMDString(Record[4]))),
2306 NextMDValueNo++);
2307 break;
2308 }
2309 case bitc::METADATA_MACRO_FILE: {
2310 if (Record.size() != 5)
2311 return error("Invalid record");
2312
2313 MDValueList.assignValue(
2314 GET_OR_DISTINCT(DIMacroFile, Record[0],
2315 (Context, Record[1], Record[2],
2316 getMDOrNull(Record[3]), getMDOrNull(Record[4]))),
22932317 NextMDValueNo++);
22942318 break;
22952319 }
10161016 Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables().get()));
10171017 Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities().get()));
10181018 Record.push_back(N->getDWOId());
1019 Record.push_back(VE.getMetadataOrNullID(N->getMacros().get()));
10191020
10201021 Stream.EmitRecord(bitc::METADATA_COMPILE_UNIT, Record, Abbrev);
10211022 Record.clear();
10881089 Record.push_back(N->getLine());
10891090
10901091 Stream.EmitRecord(bitc::METADATA_NAMESPACE, Record, Abbrev);
1092 Record.clear();
1093 }
1094
1095 static void WriteDIMacro(const DIMacro *N, const ValueEnumerator &VE,
1096 BitstreamWriter &Stream,
1097 SmallVectorImpl &Record, unsigned Abbrev) {
1098 Record.push_back(N->isDistinct());
1099 Record.push_back(N->getMacinfoType());
1100 Record.push_back(N->getLine());
1101 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1102 Record.push_back(VE.getMetadataOrNullID(N->getRawValue()));
1103
1104 Stream.EmitRecord(bitc::METADATA_MACRO, Record, Abbrev);
1105 Record.clear();
1106 }
1107
1108 static void WriteDIMacroFile(const DIMacroFile *N, const ValueEnumerator &VE,
1109 BitstreamWriter &Stream,
1110 SmallVectorImpl &Record,
1111 unsigned Abbrev) {
1112 Record.push_back(N->isDistinct());
1113 Record.push_back(N->getMacinfoType());
1114 Record.push_back(N->getLine());
1115 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1116 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
1117
1118 Stream.EmitRecord(bitc::METADATA_MACRO_FILE, Record, Abbrev);
10911119 Record.clear();
10921120 }
10931121
13951395 : Out(Out), TypePrinter(TypePrinter), Machine(Machine), Context(Context) {
13961396 }
13971397 void printTag(const DINode *N);
1398 void printMacinfoType(const DIMacroNode *N);
13981399 void printString(StringRef Name, StringRef Value,
13991400 bool ShouldSkipEmpty = true);
14001401 void printMetadata(StringRef Name, const Metadata *MD,
14151416 Out << Tag;
14161417 else
14171418 Out << N->getTag();
1419 }
1420
1421 void MDFieldPrinter::printMacinfoType(const DIMacroNode *N) {
1422 Out << FS << "type: ";
1423 if (const char *Type = dwarf::MacinfoString(N->getMacinfoType()))
1424 Out << Type;
1425 else
1426 Out << N->getMacinfoType();
14181427 }
14191428
14201429 void MDFieldPrinter::printString(StringRef Name, StringRef Value,
16421651 Printer.printMetadata("subprograms", N->getRawSubprograms());
16431652 Printer.printMetadata("globals", N->getRawGlobalVariables());
16441653 Printer.printMetadata("imports", N->getRawImportedEntities());
1654 Printer.printMetadata("macros", N->getRawMacros());
16451655 Printer.printInt("dwoId", N->getDWOId());
16461656 Out << ")";
16471657 }
17071717 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
17081718 Printer.printMetadata("file", N->getRawFile());
17091719 Printer.printInt("line", N->getLine());
1720 Out << ")";
1721 }
1722
1723 static void writeDIMacro(raw_ostream &Out, const DIMacro *N,
1724 TypePrinting *TypePrinter, SlotTracker *Machine,
1725 const Module *Context) {
1726 Out << "!DIMacro(";
1727 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1728 Printer.printMacinfoType(N);
1729 Printer.printInt("line", N->getLine());
1730 Printer.printString("name", N->getName());
1731 Printer.printString("value", N->getValue());
1732 Out << ")";
1733 }
1734
1735 static void writeDIMacroFile(raw_ostream &Out, const DIMacroFile *N,
1736 TypePrinting *TypePrinter, SlotTracker *Machine,
1737 const Module *Context) {
1738 Out << "!DIMacroFile(";
1739 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1740 Printer.printInt("line", N->getLine());
1741 Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
1742 Printer.printMetadata("nodes", N->getRawElements());
17101743 Out << ")";
17111744 }
17121745
147147 CUNode = DICompileUnit::getDistinct(
148148 VMContext, Lang, DIFile::get(VMContext, Filename, Directory), Producer,
149149 isOptimized, Flags, RunTimeVer, SplitName, Kind, nullptr,
150 nullptr, nullptr, nullptr, nullptr, DWOId);
150 nullptr, nullptr, nullptr, nullptr, nullptr, DWOId);
151151
152152 // Create a named metadata so that it is easier to find cu in a module.
153153 // Note that we only generate this when the caller wants to actually
314314 unsigned RuntimeVersion, MDString *SplitDebugFilename,
315315 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
316316 Metadata *Subprograms, Metadata *GlobalVariables,
317 Metadata *ImportedEntities, uint64_t DWOId,
317 Metadata *ImportedEntities, Metadata *Macros, uint64_t DWOId,
318318 StorageType Storage, bool ShouldCreate) {
319319 assert(Storage != Uniqued && "Cannot unique DICompileUnit");
320320 assert(isCanonical(Producer) && "Expected canonical MDString");
323323
324324 Metadata *Ops[] = {File, Producer, Flags, SplitDebugFilename, EnumTypes,
325325 RetainedTypes, Subprograms, GlobalVariables,
326 ImportedEntities};
326 ImportedEntities, Macros};
327327 return storeImpl(new (ArrayRef(Ops).size()) DICompileUnit(
328328 Context, Storage, SourceLanguage, IsOptimized,
329329 RuntimeVersion, EmissionKind, DWOId, Ops),
556556 Metadata *Ops[] = {Scope, Entity, Name};
557557 DEFINE_GETIMPL_STORE(DIImportedEntity, (Tag, Line), Ops);
558558 }
559
560 DIMacro *DIMacro::getImpl(LLVMContext &Context, unsigned MIType,
561 unsigned Line, MDString *Name, MDString *Value,
562 StorageType Storage, bool ShouldCreate) {
563 assert(isCanonical(Name) && "Expected canonical MDString");
564 DEFINE_GETIMPL_LOOKUP(DIMacro,
565 (MIType, Line, getString(Name), getString(Value)));
566 Metadata *Ops[] = { Name, Value };
567 DEFINE_GETIMPL_STORE(DIMacro, (MIType, Line), Ops);
568 }
569
570 DIMacroFile *DIMacroFile::getImpl(LLVMContext &Context, unsigned MIType,
571 unsigned Line, Metadata *File,
572 Metadata *Elements, StorageType Storage,
573 bool ShouldCreate) {
574 DEFINE_GETIMPL_LOOKUP(DIMacroFile,
575 (MIType, Line, File, Elements));
576 Metadata *Ops[] = { File, Elements };
577 DEFINE_GETIMPL_STORE(DIMacroFile, (MIType, Line), Ops);
578 }
579
791791 }
792792 };
793793
794 template <> struct MDNodeKeyImpl {
795 unsigned MIType;
796 unsigned Line;
797 StringRef Name;
798 StringRef Value;
799
800 MDNodeKeyImpl(unsigned MIType, unsigned Line, StringRef Name, StringRef Value)
801 : MIType(MIType), Line(Line), Name(Name), Value(Value) {}
802 MDNodeKeyImpl(const DIMacro *N)
803 : MIType(N->getMacinfoType()), Line(N->getLine()), Name(N->getName()),
804 Value(N->getValue()) {}
805
806 bool isKeyOf(const DIMacro *RHS) const {
807 return MIType == RHS->getMacinfoType() && Line == RHS->getLine() &&
808 Name == RHS->getName() && Value == RHS->getValue();
809 }
810 unsigned getHashValue() const {
811 return hash_combine(MIType, Line, Name, Value);
812 }
813 };
814
815 template <> struct MDNodeKeyImpl {
816 unsigned MIType;
817 unsigned Line;
818 Metadata *File;
819 Metadata *Elements;
820
821 MDNodeKeyImpl(unsigned MIType, unsigned Line, Metadata *File,
822 Metadata *Elements)
823 : MIType(MIType), Line(Line), File(File), Elements(Elements) {}
824 MDNodeKeyImpl(const DIMacroFile *N)
825 : MIType(N->getMacinfoType()), Line(N->getLine()), File(N->getRawFile()),
826 Elements(N->getRawElements()) {}
827
828 bool isKeyOf(const DIMacroFile *RHS) const {
829 return MIType == RHS->getMacinfoType() && Line == RHS->getLine() &&
830 File == RHS->getRawFile() && File == RHS->getRawElements();
831 }
832 unsigned getHashValue() const {
833 return hash_combine(MIType, Line, File, Elements);
834 }
835 };
836
794837 /// \brief DenseMapInfo for MDNode subclasses.
795838 template struct MDNodeInfo {
796839 typedef MDNodeKeyImpl KeyTy;
859859 "invalid composite elements", &N, N.getRawElements());
860860 Assert(isTypeRef(N, N.getRawVTableHolder()), "invalid vtable holder", &N,
861861 N.getRawVTableHolder());
862 Assert(!N.getRawElements() || isa(N.getRawElements()),
863 "invalid composite elements", &N, N.getRawElements());
864862 Assert(!hasConflictingReferenceFlags(N.getFlags()), "invalid reference flags",
865863 &N);
866864 if (auto *Params = N.getRawTemplateParams())
932930 for (Metadata *Op : N.getImportedEntities()->operands()) {
933931 Assert(Op && isa(Op), "invalid imported entity ref", &N,
934932 Op);
933 }
934 }
935 if (auto *Array = N.getRawMacros()) {
936 Assert(isa(Array), "invalid macro list", &N, Array);
937 for (Metadata *Op : N.getMacros()->operands()) {
938 Assert(Op && isa(Op), "invalid macro ref", &N, Op);
935939 }
936940 }
937941 }
985989 Assert(N.getTag() == dwarf::DW_TAG_namespace, "invalid tag", &N);
986990 if (auto *S = N.getRawScope())
987991 Assert(isa(S), "invalid scope ref", &N, S);
992 }
993
994 void Verifier::visitDIMacro(const DIMacro &N) {
995 Assert(N.getMacinfoType() == dwarf::DW_MACINFO_define ||
996 N.getMacinfoType() == dwarf::DW_MACINFO_undef,
997 "invalid macinfo type", &N);
998 Assert(!N.getName().empty(), "anonymous macro", &N);
999 }
1000
1001 void Verifier::visitDIMacroFile(const DIMacroFile &N) {
1002 Assert(N.getMacinfoType() == dwarf::DW_MACINFO_start_file,
1003 "invalid macinfo type", &N);
1004 if (auto *F = N.getRawFile())
1005 Assert(isa(F), "invalid file", &N, F);
1006
1007 if (auto *Array = N.getRawElements()) {
1008 Assert(isa(Array), "invalid macro list", &N, Array);
1009 for (Metadata *Op : N.getElements()->operands()) {
1010 Assert(Op && isa(Op), "invalid macro ref", &N, Op);
1011 }
1012 }
9881013 }
9891014
9901015 void Verifier::visitDIModule(const DIModule &N) {
472472 return nullptr;
473473 }
474474
475 unsigned llvm::dwarf::getMacinfo(StringRef MacinfoString) {
476 return StringSwitch(MacinfoString)
477 .Case("DW_MACINFO_define", DW_MACINFO_define)
478 .Case("DW_MACINFO_undef", DW_MACINFO_undef)
479 .Case("DW_MACINFO_start_file", DW_MACINFO_start_file)
480 .Case("DW_MACINFO_end_file", DW_MACINFO_end_file)
481 .Case("DW_MACINFO_vendor_ext", DW_MACINFO_vendor_ext)
482 .Default(DW_MACINFO_invalid);
483 }
484
475485 const char *llvm::dwarf::CallFrameString(unsigned Encoding) {
476486 switch (Encoding) {
477487 case DW_CFA_nop: return "DW_CFA_nop";
0 ; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis | FileCheck %s
11 ; RUN: verify-uselistorder %s
22
3 ; CHECK: !named = !{!0, !0, !1, !2, !3, !4, !5, !6, !7, !8, !8, !9, !10, !11, !12, !13, !14, !15, !16, !17, !18, !19, !20, !21, !22, !23, !24, !25, !26, !27, !27}
4 !named = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10, !11, !12, !13, !14, !15, !16, !17, !18, !19, !20, !21, !22, !23, !24, !25, !26, !27, !28, !29, !30}
3 ; CHECK: !named = !{!0, !0, !1, !2, !3, !4, !5, !6, !7, !8, !8, !9, !10, !11, !12, !13, !14, !15, !16, !17, !18, !19, !20, !21, !22, !23, !24, !25, !26, !27, !27, !28, !29, !30, !31}
4 !named = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10, !11, !12, !13, !14, !15, !16, !17, !18, !19, !20, !21, !22, !23, !24, !25, !26, !27, !28, !29, !30, !31, !32, !33, !34}
55
66 ; CHECK: !0 = !DISubrange(count: 3)
77 ; CHECK-NEXT: !1 = !DISubrange(count: 3, lowerBound: 4)
6262 !25 = !DICompositeType(tag: DW_TAG_structure_type)
6363 !26 = !DICompositeType(tag: DW_TAG_structure_type, runtimeLang: 6)
6464
65 ; !25 = !{!7, !7}
66 ; !26 = !DISubroutineType(flags: DIFlagPublic | DIFlagStaticMember, types: !25)
67 ; !27 = !DISubroutineType(types: !25)
65 ; CHECK-NEXT: !25 = !{!6, !6}
66 ; CHECK-NEXT: !26 = !DISubroutineType(flags: DIFlagPublic | DIFlagStaticMember, types: !25)
67 ; CHECK-NEXT: !27 = !DISubroutineType(types: !25)
6868 !27 = !{!7, !7}
6969 !28 = !DISubroutineType(flags: DIFlagPublic | DIFlagStaticMember, types: !27)
7070 !29 = !DISubroutineType(flags: 0, types: !27)
7171 !30 = !DISubroutineType(types: !27)
72
73 ; CHECK-NEXT: !28 = !DIMacro(type: DW_MACINFO_define, line: 9, name: "Name", value: "Value")
74 ; CHECK-NEXT: !29 = distinct !{!28}
75 ; CHECK-NEXT: !30 = !DIMacroFile(line: 9, file: !12, nodes: !29)
76 ; CHECK-NEXT: !31 = !DIMacroFile(line: 11, file: !12)
77 !31 = !DIMacro(type: DW_MACINFO_define, line: 9, name: "Name", value: "Value")
78 !32 = distinct !{!31}
79 !33 = !DIMacroFile(line: 9, file: !14, nodes: !32)
80 !34 = !DIMacroFile(type: DW_MACINFO_start_file, line: 11, file: !14)
0 ; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis | FileCheck %s
11 ; RUN: verify-uselistorder %s
22
3 ; CHECK: !named = !{!0, !1, !2, !3, !4, !5, !6, !7, !8}
4 !named = !{!0, !1, !2, !3, !4, !5, !6, !7, !8}
3 ; CHECK: !named = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9}
4 !named = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9}
55
66 !0 = distinct !{}
77 !1 = !DIFile(filename: "path/to/file", directory: "/path/to/dir")
1010 !4 = distinct !{}
1111 !5 = distinct !{}
1212 !6 = distinct !{}
13 !7 = distinct !{}
1314
14 ; CHECK: !7 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang", isOptimized: true, flags: "-O2", runtimeVersion: 2, splitDebugFilename: "abc.debug", emissionKind: 3, enums: !2, retainedTypes: !3, subprograms: !4, globals: !5, imports: !6, dwoId: 42)
15 !7 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang",
15 ; CHECK: !8 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang", isOptimized: true, flags: "-O2", runtimeVersion: 2, splitDebugFilename: "abc.debug", emissionKind: 3, enums: !2, retainedTypes: !3, subprograms: !4, globals: !5, imports: !6, macros: !7, dwoId: 42)
16 !8 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang",
1617 isOptimized: true, flags: "-O2", runtimeVersion: 2,
1718 splitDebugFilename: "abc.debug", emissionKind: 3,
1819 enums: !2, retainedTypes: !3, subprograms: !4,
19 globals: !5, imports: !6, dwoId: 42)
20 globals: !5, imports: !6, macros: !7, dwoId: 42)
2021
21 ; CHECK: !8 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, isOptimized: false, runtimeVersion: 0, emissionKind: 0)
22 !8 = distinct !DICompileUnit(language: 12, file: !1, producer: "",
22 ; CHECK: !9 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, isOptimized: false, runtimeVersion: 0, emissionKind: 0)
23 !9 = distinct !DICompileUnit(language: 12, file: !1, producer: "",
2324 isOptimized: false, flags: "", runtimeVersion: 0,
2425 splitDebugFilename: "", emissionKind: 0)
13111311 MDTuple *GlobalVariables = getTuple();
13121312 MDTuple *ImportedEntities = getTuple();
13131313 uint64_t DWOId = 0x10000000c0ffee;
1314 MDTuple *Macros = getTuple();
13141315 auto *N = DICompileUnit::getDistinct(
13151316 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
13161317 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1317 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities, DWOId);
1318 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities, Macros,
1319 DWOId);
13181320
13191321 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
13201322 EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
13301332 EXPECT_EQ(Subprograms, N->getSubprograms().get());
13311333 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
13321334 EXPECT_EQ(ImportedEntities, N->getImportedEntities().get());
1335 EXPECT_EQ(Macros, N->getMacros().get());
13331336 EXPECT_EQ(DWOId, N->getDWOId());
13341337
13351338 TempDICompileUnit Temp = N->clone();
13471350 EXPECT_EQ(Subprograms, Temp->getSubprograms().get());
13481351 EXPECT_EQ(GlobalVariables, Temp->getGlobalVariables().get());
13491352 EXPECT_EQ(ImportedEntities, Temp->getImportedEntities().get());
1353 EXPECT_EQ(Macros, Temp->getMacros().get());
13501354 EXPECT_EQ(DWOId, Temp->getDWOId());
13511355
13521356 auto *TempAddress = Temp.get();
13711375 auto *N = DICompileUnit::getDistinct(
13721376 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
13731377 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1374 RetainedTypes, nullptr, nullptr, ImportedEntities, DWOId);
1378 RetainedTypes, nullptr, nullptr, ImportedEntities, nullptr, DWOId);
13751379
13761380 auto *Subprograms = MDTuple::getDistinct(Context, None);
13771381 EXPECT_EQ(nullptr, N->getSubprograms().get());
13861390 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
13871391 N->replaceGlobalVariables(nullptr);
13881392 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
1393
1394 auto *Macros = MDTuple::getDistinct(Context, None);
1395 EXPECT_EQ(nullptr, N->getMacros().get());
1396 N->replaceMacros(Macros);
1397 EXPECT_EQ(Macros, N->getMacros().get());
1398 N->replaceMacros(nullptr);
1399 EXPECT_EQ(nullptr, N->getMacros().get());
13891400 }
13901401
13911402 typedef MetadataTest DISubprogramTest;