Skip to content

[Support][YamlTraits] Add quoting for keys in textual YAML representation #88763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 29, 2024
Merged
21 changes: 14 additions & 7 deletions llvm/include/llvm/Support/YAMLTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -671,20 +671,26 @@ inline bool isBool(StringRef S) {
// (except for TAB #x9, LF #xA, and CR #xD which are allowed), DEL #x7F, the C1
// control block #x80-#x9F (except for NEL #x85 which is allowed), the surrogate
// block #xD800-#xDFFF, #xFFFE, and #xFFFF.
inline QuotingType needsQuotes(StringRef S) {
//
// Some strings are valid YAML values even unquoted, but without quotes are
// interpreted as non-string type, for instance null, boolean or numeric values.
// If ForcePreserveAsString is set, such strings are quoted.
inline QuotingType needsQuotes(StringRef S, bool ForcePreserveAsString = true) {
if (S.empty())
return QuotingType::Single;

QuotingType MaxQuotingNeeded = QuotingType::None;
if (isSpace(static_cast<unsigned char>(S.front())) ||
isSpace(static_cast<unsigned char>(S.back())))
MaxQuotingNeeded = QuotingType::Single;
if (isNull(S))
MaxQuotingNeeded = QuotingType::Single;
if (isBool(S))
MaxQuotingNeeded = QuotingType::Single;
if (isNumeric(S))
MaxQuotingNeeded = QuotingType::Single;
if (ForcePreserveAsString) {
if (isNull(S))
MaxQuotingNeeded = QuotingType::Single;
if (isBool(S))
MaxQuotingNeeded = QuotingType::Single;
if (isNumeric(S))
MaxQuotingNeeded = QuotingType::Single;
}

// 7.3.3 Plain Style
// Plain scalars must not begin with most indicators, as this would cause
Expand Down Expand Up @@ -1636,6 +1642,7 @@ class Output : public IO {

private:
void output(StringRef s);
void output(StringRef, QuotingType);
void outputUpToEndOfLine(StringRef s);
void newLineCheck(bool EmptySequence = false);
void outputNewLine();
Expand Down
80 changes: 44 additions & 36 deletions llvm/lib/Support/YAMLTraits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -718,40 +718,8 @@ void Output::scalarString(StringRef &S, QuotingType MustQuote) {
outputUpToEndOfLine("''");
return;
}
if (MustQuote == QuotingType::None) {
// Only quote if we must.
outputUpToEndOfLine(S);
return;
}

const char *const Quote = MustQuote == QuotingType::Single ? "'" : "\"";
output(Quote); // Starting quote.

// When using double-quoted strings (and only in that case), non-printable characters may be
// present, and will be escaped using a variety of unicode-scalar and special short-form
// escapes. This is handled in yaml::escape.
if (MustQuote == QuotingType::Double) {
output(yaml::escape(S, /* EscapePrintable= */ false));
outputUpToEndOfLine(Quote);
return;
}

unsigned i = 0;
unsigned j = 0;
unsigned End = S.size();
const char *Base = S.data();

// When using single-quoted strings, any single quote ' must be doubled to be escaped.
while (j < End) {
if (S[j] == '\'') { // Escape quotes.
output(StringRef(&Base[i], j - i)); // "flush".
output(StringLiteral("''")); // Print it as ''
i = j + 1;
}
++j;
}
output(StringRef(&Base[i], j - i));
outputUpToEndOfLine(Quote); // Ending quote.
output(S, MustQuote);
outputUpToEndOfLine("");
}

void Output::blockScalarString(StringRef &S) {
Expand Down Expand Up @@ -801,6 +769,46 @@ void Output::output(StringRef s) {
Out << s;
}

void Output::output(StringRef S, QuotingType MustQuote) {
if (MustQuote == QuotingType::None) {
// Only quote if we must.
output(S);
return;
}

StringLiteral Quote = MustQuote == QuotingType::Single ? StringLiteral("'")
: StringLiteral("\"");
output(Quote); // Starting quote.

// When using double-quoted strings (and only in that case), non-printable
// characters may be present, and will be escaped using a variety of
// unicode-scalar and special short-form escapes. This is handled in
// yaml::escape.
if (MustQuote == QuotingType::Double) {
output(yaml::escape(S, /* EscapePrintable= */ false));
output(Quote);
return;
}

unsigned i = 0;
unsigned j = 0;
unsigned End = S.size();
const char *Base = S.data();

// When using single-quoted strings, any single quote ' must be doubled to be
// escaped.
while (j < End) {
if (S[j] == '\'') { // Escape quotes.
output(StringRef(&Base[i], j - i)); // "flush".
output(StringLiteral("''")); // Print it as ''
i = j + 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use a StringRef with take_back incrementing? (I guess this is just moving code and shouldn't change here)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to keep it as-is as this code is just moved.

}
++j;
}
output(StringRef(&Base[i], j - i));
output(Quote); // Ending quote.
}

void Output::outputUpToEndOfLine(StringRef s) {
output(s);
if (StateStack.empty() || (!inFlowSeqAnyElement(StateStack.back()) &&
Expand Down Expand Up @@ -853,7 +861,7 @@ void Output::newLineCheck(bool EmptySequence) {
}

void Output::paddedKey(StringRef key) {
output(key);
output(key, needsQuotes(key, false));
output(":");
const char *spaces = " ";
if (key.size() < strlen(spaces))
Expand All @@ -872,7 +880,7 @@ void Output::flowKey(StringRef Key) {
Column = ColumnAtMapFlowStart;
output(" ");
}
output(Key);
output(Key, needsQuotes(Key, false));
output(": ");
}

Expand Down
4 changes: 2 additions & 2 deletions llvm/test/CodeGen/AMDGPU/amdpal-callable.ll
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ attributes #0 = { nounwind }

; GCN: amdpal.pipelines:
; GCN-NEXT: - .registers:
; GCN-NEXT: 0x2e12 (COMPUTE_PGM_RSRC1): 0xaf01ca{{$}}
; GCN-NEXT: 0x2e13 (COMPUTE_PGM_RSRC2): 0x8001{{$}}
; GCN-NEXT: '0x2e12 (COMPUTE_PGM_RSRC1)': 0xaf01ca{{$}}
; GCN-NEXT: '0x2e13 (COMPUTE_PGM_RSRC2)': 0x8001{{$}}
; GCN-NEXT: .shader_functions:
; GCN-NEXT: dynamic_stack:
; GCN-NEXT: .backend_stack_size: 0x10{{$}}
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/CodeGen/AMDGPU/amdpal-cs.ll
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
; GCN-NEXT: .entry_point: cs_amdpal
; GCN-NEXT: .scratch_memory_size: 0
; GCN: .registers:
; GCN-NEXT: 0x2e12 (COMPUTE_PGM_RSRC1):
; GCN-NEXT: 0x2e13 (COMPUTE_PGM_RSRC2):
; GCN-NEXT: '0x2e12 (COMPUTE_PGM_RSRC1)':
; GCN-NEXT: '0x2e13 (COMPUTE_PGM_RSRC2)':
; GCN-NEXT: ...
; GCN-NEXT: .end_amdgpu_pal_metadata
define amdgpu_cs half @cs_amdpal(half %arg0) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/AMDGPU/amdpal-es.ll
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
; GCN-NEXT: .entry_point: es_amdpal
; GCN-NEXT: .scratch_memory_size: 0
; GCN: .registers:
; GCN-NEXT: 0x2cca (SPI_SHADER_PGM_RSRC1_ES): 0
; GCN-NEXT: '0x2cca (SPI_SHADER_PGM_RSRC1_ES)': 0
; GCN-NEXT: ...
; GCN-NEXT: .end_amdgpu_pal_metadata
define amdgpu_es half @es_amdpal(half %arg0) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/AMDGPU/amdpal-gs.ll
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
; GCN-NEXT: .entry_point: gs_amdpal
; GCN-NEXT: .scratch_memory_size: 0
; GCN: .registers:
; GCN-NEXT: 0x2c8a (SPI_SHADER_PGM_RSRC1_GS): 0
; GCN-NEXT: '0x2c8a (SPI_SHADER_PGM_RSRC1_GS)': 0
; GCN-NEXT: ...
; GCN-NEXT: .end_amdgpu_pal_metadata
define amdgpu_gs half @gs_amdpal(half %arg0) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/AMDGPU/amdpal-hs.ll
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
; GCN-NEXT: .entry_point: hs_amdpal
; GCN-NEXT: .scratch_memory_size: 0
; GCN: .registers:
; GCN-NEXT: 0x2d0a (SPI_SHADER_PGM_RSRC1_HS): 0
; GCN-NEXT: '0x2d0a (SPI_SHADER_PGM_RSRC1_HS)': 0
; GCN-NEXT: ...
; GCN-NEXT: .end_amdgpu_pal_metadata
define amdgpu_hs half @hs_amdpal(half %arg0) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/AMDGPU/amdpal-ls.ll
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
; GCN-NEXT: .entry_point: ls_amdpal
; GCN-NEXT: .scratch_memory_size: 0
; GCN: .registers:
; GCN-NEXT: 0x2d4a (SPI_SHADER_PGM_RSRC1_LS): 0
; GCN-NEXT: '0x2d4a (SPI_SHADER_PGM_RSRC1_LS)': 0
; GCN-NEXT: ...
; GCN-NEXT: .end_amdgpu_pal_metadata
define amdgpu_ls half @ls_amdpal(half %arg0) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/AMDGPU/amdpal-msgpack-cs.ll
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
; amdpal compute shader: check for 0x2e12 (COMPUTE_PGM_RSRC1) in pal metadata
; GCN-LABEL: {{^}}cs_amdpal:
; GCN: .amdgpu_pal_metadata
; GCN: 0x2e12 (COMPUTE_PGM_RSRC1)
; GCN: '0x2e12 (COMPUTE_PGM_RSRC1)'
define amdgpu_cs half @cs_amdpal(half %arg0) {
%add = fadd half %arg0, 1.0
ret half %add
Expand Down
44 changes: 22 additions & 22 deletions llvm/test/CodeGen/AMDGPU/amdpal-msgpack-default.ll
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,64 @@
; RUN: llc -mtriple=amdgcn--amdpal -mcpu=gfx900 -verify-machineinstrs < %s | FileCheck -check-prefix=GFX9 -enable-var-scope %s

; amdpal compute shader: check for 0x2e12 (COMPUTE_PGM_RSRC1) in pal metadata
; SI-DAG: 0x2e12 (COMPUTE_PGM_RSRC1): 0x2f0000{{$}}
; VI-DAG: 0x2e12 (COMPUTE_PGM_RSRC1): 0x2f02c0{{$}}
; GFX9-DAG: 0x2e12 (COMPUTE_PGM_RSRC1): 0x2f0000{{$}}
; SI-DAG: '0x2e12 (COMPUTE_PGM_RSRC1)': 0x2f0000{{$}}
; VI-DAG: '0x2e12 (COMPUTE_PGM_RSRC1)': 0x2f02c0{{$}}
; GFX9-DAG: '0x2e12 (COMPUTE_PGM_RSRC1)': 0x2f0000{{$}}
define amdgpu_cs half @cs_amdpal(half %arg0) {
%add = fadd half %arg0, 1.0
ret half %add
}

; amdpal evaluation shader: check for 0x2cca (SPI_SHADER_PGM_RSRC1_ES) in pal metadata
; SI-DAG: 0x2cca (SPI_SHADER_PGM_RSRC1_ES): 0x2f0000{{$}}
; VI-DAG: 0x2cca (SPI_SHADER_PGM_RSRC1_ES): 0x2f02c0{{$}}
; GFX9-DAG: 0x2cca (SPI_SHADER_PGM_RSRC1_ES): 0x2f0000{{$}}
; SI-DAG: '0x2cca (SPI_SHADER_PGM_RSRC1_ES)': 0x2f0000{{$}}
; VI-DAG: '0x2cca (SPI_SHADER_PGM_RSRC1_ES)': 0x2f02c0{{$}}
; GFX9-DAG: '0x2cca (SPI_SHADER_PGM_RSRC1_ES)': 0x2f0000{{$}}
define amdgpu_es half @es_amdpal(half %arg0) {
%add = fadd half %arg0, 1.0
ret half %add
}

; amdpal geometry shader: check for 0x2c8a (SPI_SHADER_PGM_RSRC1_GS) in pal metadata
; SI-DAG: 0x2c8a (SPI_SHADER_PGM_RSRC1_GS): 0x2f0000{{$}}
; VI-DAG: 0x2c8a (SPI_SHADER_PGM_RSRC1_GS): 0x2f02c0{{$}}
; GFX9-DAG: 0x2c8a (SPI_SHADER_PGM_RSRC1_GS): 0x2f0000{{$}}
; SI-DAG: '0x2c8a (SPI_SHADER_PGM_RSRC1_GS)': 0x2f0000{{$}}
; VI-DAG: '0x2c8a (SPI_SHADER_PGM_RSRC1_GS)': 0x2f02c0{{$}}
; GFX9-DAG: '0x2c8a (SPI_SHADER_PGM_RSRC1_GS)': 0x2f0000{{$}}
define amdgpu_gs half @gs_amdpal(half %arg0) {
%add = fadd half %arg0, 1.0
ret half %add
}

; amdpal hull shader: check for 0x2d0a (SPI_SHADER_PGM_RSRC1_HS) in pal metadata
; SI-DAG: 0x2d0a (SPI_SHADER_PGM_RSRC1_HS): 0x2f0000{{$}}
; VI-DAG: 0x2d0a (SPI_SHADER_PGM_RSRC1_HS): 0x2f02c0{{$}}
; GFX9-DAG: 0x2d0a (SPI_SHADER_PGM_RSRC1_HS): 0x2f0000{{$}}
; SI-DAG: '0x2d0a (SPI_SHADER_PGM_RSRC1_HS)': 0x2f0000{{$}}
; VI-DAG: '0x2d0a (SPI_SHADER_PGM_RSRC1_HS)': 0x2f02c0{{$}}
; GFX9-DAG: '0x2d0a (SPI_SHADER_PGM_RSRC1_HS)': 0x2f0000{{$}}
define amdgpu_hs half @hs_amdpal(half %arg0) {
%add = fadd half %arg0, 1.0
ret half %add
}

; amdpal load shader: check for 0x2d4a (SPI_SHADER_PGM_RSRC1_LS) in pal metadata
; SI-DAG: 0x2d4a (SPI_SHADER_PGM_RSRC1_LS): 0x2f0000{{$}}
; VI-DAG: 0x2d4a (SPI_SHADER_PGM_RSRC1_LS): 0x2f02c0{{$}}
; GFX9-DAG: 0x2d4a (SPI_SHADER_PGM_RSRC1_LS): 0x2f0000{{$}}
; SI-DAG: '0x2d4a (SPI_SHADER_PGM_RSRC1_LS)': 0x2f0000{{$}}
; VI-DAG: '0x2d4a (SPI_SHADER_PGM_RSRC1_LS)': 0x2f02c0{{$}}
; GFX9-DAG: '0x2d4a (SPI_SHADER_PGM_RSRC1_LS)': 0x2f0000{{$}}
define amdgpu_ls half @ls_amdpal(half %arg0) {
%add = fadd half %arg0, 1.0
ret half %add
}

; amdpal pixel shader: check for 0x2c0a (SPI_SHADER_PGM_RSRC1_PS) in pal metadata
; below.
; SI-DAG: 0x2c0a (SPI_SHADER_PGM_RSRC1_PS): 0x2f0000{{$}}
; VI-DAG: 0x2c0a (SPI_SHADER_PGM_RSRC1_PS): 0x2f02c0{{$}}
; GFX9-DAG: 0x2c0a (SPI_SHADER_PGM_RSRC1_PS): 0x2f0000{{$}}
; SI-DAG: '0x2c0a (SPI_SHADER_PGM_RSRC1_PS)': 0x2f0000{{$}}
; VI-DAG: '0x2c0a (SPI_SHADER_PGM_RSRC1_PS)': 0x2f02c0{{$}}
; GFX9-DAG: '0x2c0a (SPI_SHADER_PGM_RSRC1_PS)': 0x2f0000{{$}}
define amdgpu_ps half @ps_amdpal(half %arg0) {
%add = fadd half %arg0, 1.0
ret half %add
}

; amdpal vertex shader: check for 45352 (SPI_SHADER_PGM_RSRC1_VS) in pal metadata
; SI-DAG: 0x2c4a (SPI_SHADER_PGM_RSRC1_VS): 0x2f0000{{$}}
; VI-DAG: 0x2c4a (SPI_SHADER_PGM_RSRC1_VS): 0x2f02c0{{$}}
; GFX9-DAG: 0x2c4a (SPI_SHADER_PGM_RSRC1_VS): 0x2f0000{{$}}
; SI-DAG: '0x2c4a (SPI_SHADER_PGM_RSRC1_VS)': 0x2f0000{{$}}
; VI-DAG: '0x2c4a (SPI_SHADER_PGM_RSRC1_VS)': 0x2f02c0{{$}}
; GFX9-DAG: '0x2c4a (SPI_SHADER_PGM_RSRC1_VS)': 0x2f0000{{$}}
define amdgpu_vs half @vs_amdpal(half %arg0) {
%add = fadd half %arg0, 1.0
ret half %add
Expand All @@ -75,7 +75,7 @@ define amdgpu_vs half @vs_amdpal(half %arg0) {
; - 0x123456789abcdef0
; - 0xfedcba9876543210
; .registers:
; 0x2c0b (SPI_SHADER_PGM_RSRC2_PS): 0x42000000
; '0x2c0b (SPI_SHADER_PGM_RSRC2_PS)': 0x42000000
; ...
; .end_amdgpu_pal_metadata

Expand Down
Loading
Loading