From c7be07e6362872acfca8b77c4bc4d76a68337fe2 Mon Sep 17 00:00:00 2001 From: "Duncan P. N. Exon Smith" Date: Fri, 13 Feb 2015 01:29:28 +0000 Subject: [PATCH] AsmWriter/Bitcode: MDLexicalBlock git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@229016 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Bitcode/LLVMBitCodes.h | 3 ++- lib/AsmParser/LLParser.cpp | 15 ++++++++++++- lib/Bitcode/Reader/BitcodeReader.cpp | 11 ++++++++++ lib/Bitcode/Writer/BitcodeWriter.cpp | 18 ++++++++++++---- lib/IR/AsmWriter.cpp | 21 ++++++++++++++++--- .../invalid-mdlexicalblock-missing-parent.ll | 4 ++++ test/Assembler/mdlexicalblock.ll | 16 ++++++++++++++ 7 files changed, 79 insertions(+), 9 deletions(-) create mode 100644 test/Assembler/invalid-mdlexicalblock-missing-parent.ll create mode 100644 test/Assembler/mdlexicalblock.ll diff --git a/include/llvm/Bitcode/LLVMBitCodes.h b/include/llvm/Bitcode/LLVMBitCodes.h index 1936e3434d6..b917c055edf 100644 --- a/include/llvm/Bitcode/LLVMBitCodes.h +++ b/include/llvm/Bitcode/LLVMBitCodes.h @@ -156,7 +156,8 @@ namespace bitc { METADATA_COMPOSITE_TYPE= 18, // [distinct, ...] METADATA_SUBROUTINE_TYPE=19, // [distinct, flags, types] METADATA_COMPILE_UNIT = 20, // [distinct, ...] - METADATA_SUBPROGRAM = 21 // [distinct, ...] + METADATA_SUBPROGRAM = 21, // [distinct, ...] + METADATA_LEXICAL_BLOCK = 22 // [distinct, scope, file, line, column] }; // The constants block (CONSTANTS_BLOCK_ID) describes emission for each diff --git a/lib/AsmParser/LLParser.cpp b/lib/AsmParser/LLParser.cpp index 445c7a405f7..e579b431d26 100644 --- a/lib/AsmParser/LLParser.cpp +++ b/lib/AsmParser/LLParser.cpp @@ -3466,9 +3466,22 @@ bool LLParser::ParseMDSubprogram(MDNode *&Result, bool IsDistinct) { return false; } +/// ParseMDLexicalBlock: +/// ::= !MDLexicalBlock(scope: !0, file: !2, line: 7, column: 9) bool LLParser::ParseMDLexicalBlock(MDNode *&Result, bool IsDistinct) { - return TokError("unimplemented parser"); +#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ + REQUIRED(scope, MDField, ); \ + OPTIONAL(file, MDField, ); \ + OPTIONAL(line, LineField, ); \ + OPTIONAL(column, ColumnField, ); + PARSE_MD_FIELDS(); +#undef VISIT_MD_FIELDS + + Result = GET_OR_DISTINCT( + MDLexicalBlock, (Context, scope.Val, file.Val, line.Val, column.Val)); + return false; } + bool LLParser::ParseMDLexicalBlockFile(MDNode *&Result, bool IsDistinct) { return TokError("unimplemented parser"); } diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index 12996d19e0d..22dcd2a0812 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -1468,6 +1468,17 @@ std::error_code BitcodeReader::ParseMetadata() { NextMDValueNo++); break; } + case bitc::METADATA_LEXICAL_BLOCK: { + if (Record.size() != 5) + return Error("Invalid record"); + + MDValueList.AssignValue( + GET_OR_DISTINCT(MDLexicalBlock, Record[0], + (Context, getMDOrNull(Record[1]), + getMDOrNull(Record[2]), Record[3], Record[4])), + NextMDValueNo++); + break; + } case bitc::METADATA_STRING: { std::string String(Record.begin(), Record.end()); llvm::UpgradeMDStringConstant(String); diff --git a/lib/Bitcode/Writer/BitcodeWriter.cpp b/lib/Bitcode/Writer/BitcodeWriter.cpp index f0453fff122..21dc5349cdc 100644 --- a/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -978,11 +978,21 @@ static void WriteMDSubprogram(const MDSubprogram *N, Record.clear(); } -static void WriteMDLexicalBlock(const MDLexicalBlock *, const ValueEnumerator &, - BitstreamWriter &, SmallVectorImpl &, - unsigned) { - llvm_unreachable("write not implemented"); +static void WriteMDLexicalBlock(const MDLexicalBlock *N, + const ValueEnumerator &VE, + BitstreamWriter &Stream, + SmallVectorImpl &Record, + unsigned Abbrev) { + Record.push_back(N->isDistinct()); + Record.push_back(VE.getMetadataOrNullID(N->getScope())); + Record.push_back(VE.getMetadataOrNullID(N->getFile())); + Record.push_back(N->getLine()); + Record.push_back(N->getColumn()); + + Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK, Record, Abbrev); + Record.clear(); } + static void WriteMDLexicalBlockFile(const MDLexicalBlockFile *, const ValueEnumerator &, BitstreamWriter &, SmallVectorImpl &, unsigned) { diff --git a/lib/IR/AsmWriter.cpp b/lib/IR/AsmWriter.cpp index bdcf51ba49f..7baa6edc950 100644 --- a/lib/IR/AsmWriter.cpp +++ b/lib/IR/AsmWriter.cpp @@ -1623,10 +1623,25 @@ static void writeMDSubprogram(raw_ostream &Out, const MDSubprogram *N, Out << ")"; } -static void writeMDLexicalBlock(raw_ostream &, const MDLexicalBlock *, - TypePrinting *, SlotTracker *, const Module *) { - llvm_unreachable("write not implemented"); +static void writeMDLexicalBlock(raw_ostream &Out, const MDLexicalBlock *N, + TypePrinting *TypePrinter, SlotTracker *Machine, + const Module *Context) { + Out << "!MDLexicalBlock("; + FieldSeparator FS; + Out << FS << "scope: "; + writeMetadataAsOperand(Out, N->getScope(), TypePrinter, Machine, Context); + if (N->getFile()) { + Out << FS << "file: "; + writeMetadataAsOperand(Out, N->getFile(), TypePrinter, Machine, + Context); + } + if (N->getLine()) + Out << FS << "line: " << N->getLine(); + if (N->getColumn()) + Out << FS << "column: " << N->getColumn(); + Out << ")"; } + static void writeMDLexicalBlockFile(raw_ostream &, const MDLexicalBlockFile *, TypePrinting *, SlotTracker *, const Module *) { diff --git a/test/Assembler/invalid-mdlexicalblock-missing-parent.ll b/test/Assembler/invalid-mdlexicalblock-missing-parent.ll new file mode 100644 index 00000000000..cdd12afde58 --- /dev/null +++ b/test/Assembler/invalid-mdlexicalblock-missing-parent.ll @@ -0,0 +1,4 @@ +; RUN: not llvm-as < %s -disable-output 2>&1 | FileCheck %s + +; CHECK: [[@LINE+1]]:29: error: missing required field 'scope' +!0 = !MDLexicalBlock(line: 7) diff --git a/test/Assembler/mdlexicalblock.ll b/test/Assembler/mdlexicalblock.ll new file mode 100644 index 00000000000..f41b8284747 --- /dev/null +++ b/test/Assembler/mdlexicalblock.ll @@ -0,0 +1,16 @@ +; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis | FileCheck %s +; RUN: verify-uselistorder %s + +; CHECK: !named = !{!0, !1, !2, !3, !4, !4} +!named = !{!0, !1, !2, !3, !4, !5} + +!0 = distinct !{} +!1 = !{!"path/to/file", !"/path/to/dir"} +!2 = !MDFile(filename: "path/to/file", directory: "/path/to/dir") + +; CHECK: !3 = !MDLexicalBlock(scope: !0, file: !2, line: 7, column: 35) +!3 = !MDLexicalBlock(scope: !0, file: !2, line: 7, column: 35) + +; CHECK: !4 = !MDLexicalBlock(scope: !0) +!4 = !MDLexicalBlock(scope: !0) +!5 = !MDLexicalBlock(scope: !0, file: null, line: 0, column: 0) -- 2.34.1