[llvm] r230875 - AsmWriter: Escape string fields in metadata
Duncan P. N. Exon Smith
dexonsmith at apple.com
Sat Feb 28 14:20:16 PST 2015
Author: dexonsmith
Date: Sat Feb 28 16:20:16 2015
New Revision: 230875
URL: http://llvm.org/viewvc/llvm-project?rev=230875&view=rev
Log:
AsmWriter: Escape string fields in metadata
Properly escape string fields in metadata. I've added a spot-check with
direct coverage for `MDFile::getFilename()`, but we'll get more coverage
once the hierarchy is moved into place (since this comes up in various
checked-in testcases).
I've replicated the `if` logic using the `ShouldSkipEmpty` flag
(although a follow-up commit is going to change how often this flag is
specified); no NFCI other than escaping the string fields.
Added:
llvm/trunk/test/Assembler/mdfile-escaped-chars.ll
Modified:
llvm/trunk/lib/IR/AsmWriter.cpp
Modified: llvm/trunk/lib/IR/AsmWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/AsmWriter.cpp?rev=230875&r1=230874&r2=230875&view=diff
==============================================================================
--- llvm/trunk/lib/IR/AsmWriter.cpp (original)
+++ llvm/trunk/lib/IR/AsmWriter.cpp Sat Feb 28 16:20:16 2015
@@ -1369,7 +1369,7 @@ static void writeMDEnumerator(raw_ostrea
TypePrinting *, SlotTracker *, const Module *) {
Out << "!MDEnumerator(";
FieldSeparator FS;
- Out << FS << "name: \"" << N->getName() << "\"";
+ writeStringField(Out, FS, "name", N->getName(), /* ShouldSkipEmpty */ false);
Out << FS << "value: " << N->getValue();
Out << ")";
}
@@ -1379,8 +1379,7 @@ static void writeMDBasicType(raw_ostream
Out << "!MDBasicType(";
FieldSeparator FS;
writeTag(Out, FS, N);
- if (!N->getName().empty())
- Out << FS << "name: \"" << N->getName() << "\"";
+ writeStringField(Out, FS, "name", N->getName());
if (N->getSizeInBits())
Out << FS << "size: " << N->getSizeInBits();
if (N->getAlignInBits())
@@ -1415,8 +1414,7 @@ static void writeMDDerivedType(raw_ostre
Out << "!MDDerivedType(";
FieldSeparator FS;
writeTag(Out, FS, N);
- if (!N->getName().empty())
- Out << FS << "name: \"" << N->getName() << "\"";
+ writeStringField(Out, FS, "name", N->getName());
if (N->getFile()) {
Out << FS << "file: ";
writeMetadataAsOperand(Out, N->getFile(), TypePrinter, Machine,
@@ -1454,8 +1452,7 @@ static void writeMDCompositeType(raw_ost
Out << "!MDCompositeType(";
FieldSeparator FS;
writeTag(Out, FS, N);
- if (!N->getName().empty())
- Out << FS << "name: \"" << N->getName() << "\"";
+ writeStringField(Out, FS, "name", N->getName());
if (N->getFile()) {
Out << FS << "file: ";
writeMetadataAsOperand(Out, N->getFile(), TypePrinter, Machine,
@@ -1505,8 +1502,7 @@ static void writeMDCompositeType(raw_ost
writeMetadataAsOperand(Out, N->getTemplateParams(), TypePrinter, Machine,
Context);
}
- if (!N->getIdentifier().empty())
- Out << FS << "identifier: \"" << N->getIdentifier() << "\"";
+ writeStringField(Out, FS, "identifier", N->getIdentifier());
Out << ")";
}
@@ -1528,8 +1524,10 @@ static void writeMDFile(raw_ostream &Out
SlotTracker *, const Module *) {
Out << "!MDFile(";
FieldSeparator FS;
- Out << FS << "filename: \"" << N->getFilename() << "\"";
- Out << FS << "directory: \"" << N->getDirectory() << "\"";
+ writeStringField(Out, FS, "filename", N->getFilename(),
+ /* ShouldSkipEmpty */ false);
+ writeStringField(Out, FS, "directory", N->getDirectory(),
+ /* ShouldSkipEmpty */ false);
Out << ")";
}
@@ -1545,14 +1543,11 @@ static void writeMDCompileUnit(raw_ostre
Out << N->getSourceLanguage();
Out << FS << "file: ";
writeMetadataAsOperand(Out, N->getFile(), TypePrinter, Machine, Context);
- if (!N->getProducer().empty())
- Out << FS << "producer: \"" << N->getProducer() << "\"";
+ writeStringField(Out, FS, "producer", N->getProducer());
Out << FS << "isOptimized: " << (N->isOptimized() ? "true" : "false");
- if (!N->getFlags().empty())
- Out << FS << "flags: \"" << N->getFlags() << "\"";
+ writeStringField(Out, FS, "flags", N->getFlags());
Out << FS << "runtimeVersion: " << N->getRuntimeVersion();
- if (!N->getSplitDebugFilename().empty())
- Out << FS << "splitDebugFilename: \"" << N->getSplitDebugFilename() << "\"";
+ writeStringField(Out, FS, "splitDebugFilename", N->getSplitDebugFilename());
Out << FS << "emissionKind: " << N->getEmissionKind();
if (N->getEnumTypes()) {
Out << FS << "enums: ";
@@ -1589,9 +1584,8 @@ static void writeMDSubprogram(raw_ostrea
FieldSeparator FS;
Out << FS << "scope: ";
writeMetadataAsOperand(Out, N->getScope(), TypePrinter, Machine, Context);
- Out << FS << "name: \"" << N->getName() << "\"";
- if (!N->getLinkageName().empty())
- Out << FS << "linkageName: \"" << N->getLinkageName() << "\"";
+ writeStringField(Out, FS, "name", N->getName(), /* ShouldSkipEmpty */ false);
+ writeStringField(Out, FS, "linkageName", N->getLinkageName());
if (N->getFile()) {
Out << FS << "file: ";
writeMetadataAsOperand(Out, N->getFile(), TypePrinter, Machine,
@@ -1698,8 +1692,7 @@ static void writeMDNamespace(raw_ostream
Out << FS << "file: ";
writeMetadataAsOperand(Out, N->getFile(), TypePrinter, Machine, Context);
}
- if (!N->getName().empty())
- Out << FS << "name: \"" << N->getName() << "\"";
+ writeStringField(Out, FS, "name", N->getName());
if (N->getLine())
Out << FS << "line: " << N->getLine();
Out << ")";
@@ -1712,7 +1705,7 @@ static void writeMDTemplateTypeParameter
const Module *Context) {
Out << "!MDTemplateTypeParameter(";
FieldSeparator FS;
- Out << FS << "name: \"" << N->getName() << "\"";
+ writeStringField(Out, FS, "name", N->getName(), /* ShouldSkipEmpty */ false);
Out << FS << "type: ";
writeMetadataAsOperand(Out, N->getType(), TypePrinter, Machine, Context);
Out << ")";
@@ -1726,7 +1719,7 @@ static void writeMDTemplateValueParamete
Out << "!MDTemplateValueParameter(";
FieldSeparator FS;
writeTag(Out, FS, N);
- Out << FS << "name: \"" << N->getName() << "\"";
+ writeStringField(Out, FS, "name", N->getName(), /* ShouldSkipEmpty */ false);
Out << FS << "type: ";
writeMetadataAsOperand(Out, N->getType(), TypePrinter, Machine, Context);
Out << FS << "value: ";
@@ -1741,9 +1734,8 @@ static void writeMDGlobalVariable(raw_os
FieldSeparator FS;
Out << FS << "scope: ";
writeMetadataAsOperand(Out, N->getScope(), TypePrinter, Machine, Context);
- Out << FS << "name: \"" << N->getName() << "\"";
- if (!N->getLinkageName().empty())
- Out << FS << "linkageName: \"" << N->getLinkageName() << "\"";
+ writeStringField(Out, FS, "name", N->getName(), /* ShouldSkipEmpty */ false);
+ writeStringField(Out, FS, "linkageName", N->getLinkageName());
if (N->getFile()) {
Out << FS << "file: ";
writeMetadataAsOperand(Out, N->getFile(), TypePrinter, Machine,
@@ -1779,7 +1771,7 @@ static void writeMDLocalVariable(raw_ost
writeTag(Out, FS, N);
Out << FS << "scope: ";
writeMetadataAsOperand(Out, N->getScope(), TypePrinter, Machine, Context);
- Out << FS << "name: \"" << N->getName() << "\"";
+ writeStringField(Out, FS, "name", N->getName(), /* ShouldSkipEmpty */ false);
if (N->getFile()) {
Out << FS << "file: ";
writeMetadataAsOperand(Out, N->getFile(), TypePrinter, Machine,
@@ -1832,17 +1824,15 @@ static void writeMDObjCProperty(raw_ostr
const Module *Context) {
Out << "!MDObjCProperty(";
FieldSeparator FS;
- Out << FS << "name: \"" << N->getName() << "\"";
+ writeStringField(Out, FS, "name", N->getName(), /* ShouldSkipEmpty */ false);
if (N->getFile()) {
Out << FS << "file: ";
writeMetadataAsOperand(Out, N->getFile(), TypePrinter, Machine, Context);
}
if (N->getLine())
Out << FS << "line: " << N->getLine();
- if (!N->getSetterName().empty())
- Out << FS << "setter: \"" << N->getSetterName() << "\"";
- if (!N->getGetterName().empty())
- Out << FS << "getter: \"" << N->getGetterName() << "\"";
+ writeStringField(Out, FS, "setter", N->getSetterName());
+ writeStringField(Out, FS, "getter", N->getGetterName());
if (N->getAttributes())
Out << FS << "attributes: " << N->getAttributes();
if (N->getType()) {
@@ -1866,7 +1856,7 @@ static void writeMDImportedEntity(raw_os
}
if (N->getLine())
Out << FS << "line: " << N->getLine();
- Out << FS << "name: \"" << N->getName() << "\"";
+ writeStringField(Out, FS, "name", N->getName(), /* ShouldSkipEmpty */ false);
Out << ")";
}
Added: llvm/trunk/test/Assembler/mdfile-escaped-chars.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Assembler/mdfile-escaped-chars.ll?rev=230875&view=auto
==============================================================================
--- llvm/trunk/test/Assembler/mdfile-escaped-chars.ll (added)
+++ llvm/trunk/test/Assembler/mdfile-escaped-chars.ll Sat Feb 28 16:20:16 2015
@@ -0,0 +1,9 @@
+; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis | FileCheck %s
+; RUN: verify-uselistorder %s
+
+; Spot-check that metadata string fields are correctly escaped.
+
+!named = !{!0}
+
+; CHECK: !0 = !MDFile(filename: "\00\01\02\80\81\82\FD\FE\FF", directory: "/dir")
+!0 = !MDFile(filename: "\00\01\02\80\81\82\FD\FE\FF", directory: "/dir")
More information about the llvm-commits
mailing list