diff options
author | Francis Visoiu Mistrih <francisvm@yahoo.com> | 2017-12-18 17:38:03 +0000 |
---|---|---|
committer | Francis Visoiu Mistrih <francisvm@yahoo.com> | 2017-12-18 17:38:03 +0000 |
commit | b213b27ee3cba7d0b7ad2a45c8cbd42e59510220 (patch) | |
tree | 887a44fd8bb29d305bc2c240e44a01467ca9cc38 /llvm/lib | |
parent | 6c0858e41413de10b7194519fc6cb9d4a1eda959 (diff) | |
download | bcm5719-llvm-b213b27ee3cba7d0b7ad2a45c8cbd42e59510220.tar.gz bcm5719-llvm-b213b27ee3cba7d0b7ad2a45c8cbd42e59510220.zip |
[YAML] Add support for non-printable characters
LLVM IR function names which disable mangling start with '\01'
(https://www.llvm.org/docs/LangRef.html#identifiers).
When an identifier like "\01@abc@" gets dumped to MIR, it is quoted, but
only with single quotes.
http://www.yaml.org/spec/1.2/spec.html#id2770814:
"The allowed character range explicitly excludes the C0 control block
allowed), the surrogate block #xD800-#xDFFF, #xFFFE, and #xFFFF."
http://www.yaml.org/spec/1.2/spec.html#id2776092:
"All non-printable characters must be escaped.
[...]
Note that escape sequences are only interpreted in double-quoted scalars."
This patch adds support for printing escaped non-printable characters
between double quotes if needed.
Should also fix PR31743.
Differential Revision: https://reviews.llvm.org/D41290
llvm-svn: 320996
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/ObjectYAML/CodeViewYAMLDebugSections.cpp | 2 | ||||
-rw-r--r-- | llvm/lib/ObjectYAML/CodeViewYAMLSymbols.cpp | 6 | ||||
-rw-r--r-- | llvm/lib/ObjectYAML/CodeViewYAMLTypes.cpp | 4 | ||||
-rw-r--r-- | llvm/lib/ObjectYAML/MachOYAML.cpp | 8 | ||||
-rw-r--r-- | llvm/lib/Support/Statistic.cpp | 5 | ||||
-rw-r--r-- | llvm/lib/Support/Timer.cpp | 6 | ||||
-rw-r--r-- | llvm/lib/Support/YAMLTraits.cpp | 46 |
7 files changed, 55 insertions, 22 deletions
diff --git a/llvm/lib/ObjectYAML/CodeViewYAMLDebugSections.cpp b/llvm/lib/ObjectYAML/CodeViewYAMLDebugSections.cpp index 60b0ea28030..6debd8ab0c6 100644 --- a/llvm/lib/ObjectYAML/CodeViewYAMLDebugSections.cpp +++ b/llvm/lib/ObjectYAML/CodeViewYAMLDebugSections.cpp @@ -66,7 +66,7 @@ LLVM_YAML_IS_SEQUENCE_VECTOR(CrossModuleExport) LLVM_YAML_IS_SEQUENCE_VECTOR(YAMLCrossModuleImport) LLVM_YAML_IS_SEQUENCE_VECTOR(YAMLFrameData) -LLVM_YAML_DECLARE_SCALAR_TRAITS(HexFormattedString, false) +LLVM_YAML_DECLARE_SCALAR_TRAITS(HexFormattedString, QuotingType::None) LLVM_YAML_DECLARE_ENUM_TRAITS(DebugSubsectionKind) LLVM_YAML_DECLARE_ENUM_TRAITS(FileChecksumKind) LLVM_YAML_DECLARE_BITSET_TRAITS(LineFlags) diff --git a/llvm/lib/ObjectYAML/CodeViewYAMLSymbols.cpp b/llvm/lib/ObjectYAML/CodeViewYAMLSymbols.cpp index dbe4e2a6d6f..199a65a2870 100644 --- a/llvm/lib/ObjectYAML/CodeViewYAMLSymbols.cpp +++ b/llvm/lib/ObjectYAML/CodeViewYAMLSymbols.cpp @@ -42,8 +42,8 @@ using namespace llvm::yaml; LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(TypeIndex) // We only need to declare these, the definitions are in CodeViewYAMLTypes.cpp -LLVM_YAML_DECLARE_SCALAR_TRAITS(APSInt, false) -LLVM_YAML_DECLARE_SCALAR_TRAITS(TypeIndex, false) +LLVM_YAML_DECLARE_SCALAR_TRAITS(APSInt, QuotingType::None) +LLVM_YAML_DECLARE_SCALAR_TRAITS(TypeIndex, QuotingType::None) LLVM_YAML_DECLARE_ENUM_TRAITS(SymbolKind) LLVM_YAML_DECLARE_ENUM_TRAITS(FrameCookieKind) @@ -62,7 +62,7 @@ LLVM_YAML_DECLARE_ENUM_TRAITS(ThunkOrdinal) LLVM_YAML_STRONG_TYPEDEF(StringRef, TypeName) -LLVM_YAML_DECLARE_SCALAR_TRAITS(TypeName, true) +LLVM_YAML_DECLARE_SCALAR_TRAITS(TypeName, QuotingType::Single) StringRef ScalarTraits<TypeName>::input(StringRef S, void *V, TypeName &T) { return ScalarTraits<StringRef>::input(S, V, T.value); diff --git a/llvm/lib/ObjectYAML/CodeViewYAMLTypes.cpp b/llvm/lib/ObjectYAML/CodeViewYAMLTypes.cpp index 887328cf508..ba4ad9382ce 100644 --- a/llvm/lib/ObjectYAML/CodeViewYAMLTypes.cpp +++ b/llvm/lib/ObjectYAML/CodeViewYAMLTypes.cpp @@ -48,8 +48,8 @@ LLVM_YAML_IS_SEQUENCE_VECTOR(OneMethodRecord) LLVM_YAML_IS_SEQUENCE_VECTOR(VFTableSlotKind) LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(TypeIndex) -LLVM_YAML_DECLARE_SCALAR_TRAITS(TypeIndex, false) -LLVM_YAML_DECLARE_SCALAR_TRAITS(APSInt, false) +LLVM_YAML_DECLARE_SCALAR_TRAITS(TypeIndex, QuotingType::None) +LLVM_YAML_DECLARE_SCALAR_TRAITS(APSInt, QuotingType::None) LLVM_YAML_DECLARE_ENUM_TRAITS(TypeLeafKind) LLVM_YAML_DECLARE_ENUM_TRAITS(PointerToMemberRepresentation) diff --git a/llvm/lib/ObjectYAML/MachOYAML.cpp b/llvm/lib/ObjectYAML/MachOYAML.cpp index 85079f2605f..e00a4ea9307 100644 --- a/llvm/lib/ObjectYAML/MachOYAML.cpp +++ b/llvm/lib/ObjectYAML/MachOYAML.cpp @@ -52,7 +52,9 @@ StringRef ScalarTraits<char_16>::input(StringRef Scalar, void *, char_16 &Val) { return StringRef(); } -bool ScalarTraits<char_16>::mustQuote(StringRef S) { return needsQuotes(S); } +QuotingType ScalarTraits<char_16>::mustQuote(StringRef S) { + return needsQuotes(S); +} void ScalarTraits<uuid_t>::output(const uuid_t &Val, void *, raw_ostream &Out) { Out.write_uuid(Val); @@ -75,7 +77,9 @@ StringRef ScalarTraits<uuid_t>::input(StringRef Scalar, void *, uuid_t &Val) { return StringRef(); } -bool ScalarTraits<uuid_t>::mustQuote(StringRef S) { return needsQuotes(S); } +QuotingType ScalarTraits<uuid_t>::mustQuote(StringRef S) { + return needsQuotes(S); +} void MappingTraits<MachOYAML::FileHeader>::mapping( IO &IO, MachOYAML::FileHeader &FileHdr) { diff --git a/llvm/lib/Support/Statistic.cpp b/llvm/lib/Support/Statistic.cpp index 23718bb0e9c..544ae2d0983 100644 --- a/llvm/lib/Support/Statistic.cpp +++ b/llvm/lib/Support/Statistic.cpp @@ -168,9 +168,10 @@ void llvm::PrintStatisticsJSON(raw_ostream &OS) { const char *delim = ""; for (const Statistic *Stat : Stats.Stats) { OS << delim; - assert(!yaml::needsQuotes(Stat->getDebugType()) && + assert(yaml::needsQuotes(Stat->getDebugType()) == yaml::QuotingType::None && "Statistic group/type name is simple."); - assert(!yaml::needsQuotes(Stat->getName()) && "Statistic name is simple"); + assert(yaml::needsQuotes(Stat->getName()) == yaml::QuotingType::None && + "Statistic name is simple"); OS << "\t\"" << Stat->getDebugType() << '.' << Stat->getName() << "\": " << Stat->getValue(); delim = ",\n"; diff --git a/llvm/lib/Support/Timer.cpp b/llvm/lib/Support/Timer.cpp index 3386f2660f3..0c85faecca8 100644 --- a/llvm/lib/Support/Timer.cpp +++ b/llvm/lib/Support/Timer.cpp @@ -362,8 +362,10 @@ void TimerGroup::printAll(raw_ostream &OS) { void TimerGroup::printJSONValue(raw_ostream &OS, const PrintRecord &R, const char *suffix, double Value) { - assert(!yaml::needsQuotes(Name) && "TimerGroup name needs no quotes"); - assert(!yaml::needsQuotes(R.Name) && "Timer name needs no quotes"); + assert(yaml::needsQuotes(Name) == yaml::QuotingType::None && + "TimerGroup name needs no quotes"); + assert(yaml::needsQuotes(R.Name) == yaml::QuotingType::None && + "Timer name needs no quotes"); OS << "\t\"time." << Name << '.' << R.Name << suffix << "\": " << Value; } diff --git a/llvm/lib/Support/YAMLTraits.cpp b/llvm/lib/Support/YAMLTraits.cpp index a80adfda830..05ca40f0301 100644 --- a/llvm/lib/Support/YAMLTraits.cpp +++ b/llvm/lib/Support/YAMLTraits.cpp @@ -19,6 +19,7 @@ #include "llvm/Support/Format.h" #include "llvm/Support/LineIterator.h" #include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/Unicode.h" #include "llvm/Support/YAMLParser.h" #include "llvm/Support/raw_ostream.h" #include <algorithm> @@ -330,7 +331,7 @@ void Input::endBitSetScalar() { } } -void Input::scalarString(StringRef &S, bool) { +void Input::scalarString(StringRef &S, QuotingType) { if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) { S = SN->value(); } else { @@ -338,7 +339,7 @@ void Input::scalarString(StringRef &S, bool) { } } -void Input::blockScalarString(StringRef &S) { scalarString(S, false); } +void Input::blockScalarString(StringRef &S) { scalarString(S, QuotingType::None); } void Input::setError(HNode *hnode, const Twine &message) { assert(hnode && "HNode must not be NULL"); @@ -617,7 +618,7 @@ void Output::endBitSetScalar() { this->outputUpToEndOfLine(" ]"); } -void Output::scalarString(StringRef &S, bool MustQuote) { +void Output::scalarString(StringRef &S, QuotingType MustQuote) { this->newLineCheck(); if (S.empty()) { // Print '' for the empty string because leaving the field empty is not @@ -625,27 +626,52 @@ void Output::scalarString(StringRef &S, bool MustQuote) { this->outputUpToEndOfLine("''"); return; } - if (!MustQuote) { + if (MustQuote == QuotingType::None) { // Only quote if we must. this->outputUpToEndOfLine(S); return; } + unsigned i = 0; unsigned j = 0; unsigned End = S.size(); - output("'"); // Starting single quote. const char *Base = S.data(); + + const char *const Quote = MustQuote == QuotingType::Single ? "'" : "\""; + const char QuoteChar = MustQuote == QuotingType::Single ? '\'' : '"'; + + output(Quote); // Starting quote. + + // When using single-quoted strings, any single quote ' must be doubled to be + // escaped. + // When using double-quoted strings, print \x + hex for non-printable ASCII + // characters, and escape double quotes. while (j < End) { - // Escape a single quote by doubling it. - if (S[j] == '\'') { - output(StringRef(&Base[i], j - i + 1)); - output("'"); + if (S[j] == QuoteChar) { // Escape quotes. + output(StringRef(&Base[i], j - i)); // "flush". + if (MustQuote == QuotingType::Double) { // Print it as \" + output(StringLiteral("\\")); + output(StringRef(Quote, 1)); + } else { // Single + output(StringLiteral("''")); // Print it as '' + } + i = j + 1; + } else if (MustQuote == QuotingType::Double && + !sys::unicode::isPrintable(S[j])) { + output(StringRef(&Base[i], j - i)); // "flush" + output(StringLiteral("\\x")); + + // Output the byte 0x0F as \x0f. + auto FormattedHex = format_hex_no_prefix(S[j], 2); + Out << FormattedHex; + Column += 4; // one for the '\', one for the 'x', and two for the hex + i = j + 1; } ++j; } output(StringRef(&Base[i], j - i)); - this->outputUpToEndOfLine("'"); // Ending single quote. + this->outputUpToEndOfLine(Quote); // Ending quote. } void Output::blockScalarString(StringRef &S) { |