1 # Copyright (C) 2016-2024 Free Software Foundation, Inc. 2 3 # This program is free software; you can redistribute it and/or modify 4 # it under the terms of the GNU General Public License as published by 5 # the Free Software Foundation; either version 3 of the License, or 6 # (at your option) any later version. 7 # 8 # This program is distributed in the hope that it will be useful, 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 # GNU General Public License for more details. 12 # 13 # You should have received a copy of the GNU General Public License 14 # along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 # Test basic expression parsing and evaluation, without requiring a 17 # Rust compiler. This serves as a smoke test. 18 19 load_lib "rust-support.exp" 20 require allow_rust_tests 21 22 gdb_start 23 24 gdb_test_no_output "set var \$something = 27" 25 26 if {![set_lang_rust]} { 27 warning "Rust expression tests suppressed." 28 return 29 } 30 31 gdb_test "print 9__97" " = 997" 32 gdb_test "print -5" " = -5" 33 gdb_test "print +5" " = 5" 34 gdb_test "print +-+-5" " = 5" 35 gdb_test "print 3_2i32" " = 32" 36 gdb_test "print 32i64" " = 32" 37 gdb_test "print 8u8" " = 8" 38 gdb_test "print 0x1f" " = 31" 39 gdb_test "print 0o07" " = 7" 40 gdb_test "print 0o70" " = 56" 41 gdb_test "print 0b1_111" " = 15" 42 gdb_test "print 32usize" " = 32" 43 gdb_test "print 0x_4" " = 4" 44 45 gdb_test "print 'z'" " = 122 'z'" 46 gdb_test "print '\\t'" " = 9 '\\\\t'" 47 gdb_test "print '\\n'" " = 10 '\\\\n'" 48 gdb_test "print '\\r'" " = 13 '\\\\r'" 49 gdb_test "print '\\\\'" " = 92 '\\\\\\\\'" 50 gdb_test "print '\\0'" " = 0 '\\\\0'" 51 gdb_test "print '\\''" " = 39 '\\\\''" 52 gdb_test "print '\\\"'" " = 34 '\"'" 53 gdb_test "print '\\xff'" " = 255 '\\\\xff'" 54 gdb_test "print '\\xFF'" " = 255 '\\\\xff'" 55 gdb_test "print '\\u\{F0eF\}'" " = 61679 '\\\\u\\{00f0ef\\}'" 56 57 gdb_test "print b'z'" " = 122" 58 gdb_test "print b'\\xfe'" " = 254" 59 gdb_test "print b'\\t'" " = 9" 60 gdb_test "print b'\\n'" " = 10" 61 gdb_test "print b'\\r'" " = 13" 62 gdb_test "print b'\\\\'" " = 92" 63 gdb_test "print b'\\0'" " = 0" 64 gdb_test "print b'\\''" " = 39" 65 gdb_test "print b'\\\"'" " = 34" 66 gdb_test "print b'\\xff'" " = 255" 67 68 gdb_test "print 23.5" " = 23.5" 69 gdb_test "print 23.5e1" " = 235" 70 gdb_test "print 2e4" " = 20000" 71 gdb_test "print 2_E+4_f64" " = 20000" 72 gdb_test "print 5e-1" " = 0.5" 73 gdb_test "print 5e-1f32" " = 0.5" 74 75 gdb_test "print false" " = false" 76 gdb_test "print true" " = true" 77 78 gdb_test "print 1+2" " = 3" 79 gdb_test "print 1i32 + 2i32" " = 3" 80 gdb_test "print 2.0 - 1.0" " = 1" 81 gdb_test "print !false" " = true" 82 gdb_test "print !true" " = false" 83 gdb_test "print !0u8" " = 255" 84 gdb_test "print 7 * 7" " = 49" 85 gdb_test "print 7usize * 7usize" " = 49" 86 gdb_test "print 42 / 7" " = 6" 87 gdb_test "print 42 % 7" " = 0" 88 gdb_test "print 1.0 / 2.0" " = 0.5" 89 gdb_test "print 1 < 2" " = true" 90 gdb_test "print !(1 < 2)" " = false" 91 gdb_test "print 3 + 4 * 7" " = 31" 92 gdb_test "print 1 > 2" " = false" 93 gdb_test "print 1 | 2" " = 3" 94 gdb_test "print 1 & 2" " = 0" 95 gdb_test "print 3 & 2" " = 2" 96 gdb_test "print 3 ^ 2" " = 1" 97 gdb_test "print (1 < 0) || true" " = true" 98 gdb_test "print (1 > 0) && false" " = false" 99 gdb_test "print 'z' == 'z'" " = true" 100 gdb_test "print '\\u{1016f}' != 'q'" " = true" 101 gdb_test "print 32 <= 32" " = true" 102 gdb_test "print 32 >= 32" " = true" 103 gdb_test "print 1 << 5" " = 32" 104 gdb_test "print 32usize >> 5" " = 1" 105 gdb_test "ptype 32i32 as f64" "type = f64" 106 107 gdb_test "ptype 0xf9f9f9f90000" "type = i64" 108 109 gdb_test "print ()" " = \\(\\)" 110 111 gdb_test "print \[1,2,3,4\]" " = \\\[1, 2, 3, 4\\\]" 112 gdb_test "ptype \[1,2,3,4\]" "type = \\\[i32; 4\\\]" 113 gdb_test "print \[mut 1,2,3,4\]" " = \\\[1, 2, 3, 4\\\]" 114 gdb_test "print \[1,2 3" "',' or ']' expected" 115 gdb_test "print \[1 2" "',', ';', or ']' expected" 116 gdb_test "print \[23\]" " = \\\[23\\\]" 117 118 gdb_test "print b\"hi rust\"" " = b\"hi rust\"" 119 # This isn't rusty syntax yet, but that's another bug -- this is just 120 # testing that byte escapes work properly. 121 gdb_test "print b\"\\xddhi bob\"" " = b\"\\\\335hi bob\"" 122 gdb_test "print b\"has\\0nul\"" " = b\"has\\\\000nul\"" 123 124 gdb_test "print br##\"hi\"##" " = b\"hi\"" 125 gdb_test "print br##\"hi" "Unexpected EOF in string" 126 gdb_test "print br##\"hi\"" "Unexpected EOF in string" 127 gdb_test "print br##\"hi\"#" "Unexpected EOF in string" 128 129 # Test that convenience variables and functions work with the Rust 130 # parser. 131 gdb_test "print \$something" " = 27" 132 gdb_test "print \$_isvoid(\$nosuchvariable)" " = 1" 133 gdb_test "print \$_isvoid(\$something)" " = 0" 134 135 gdb_test "print \[23usize; 4\]" " = \\\[23, 23, 23, 23\\\]" 136 gdb_test "ptype \[23usize; 4\]" " = \\\[usize; 4\\\]" 137 gdb_test "print \[mut 23usize; 4\]" " = \\\[23, 23, 23, 23\\\]" 138 139 # Test lexer corner cases. 140 gdb_test "print 0x0 as *mut ()" " = \\\(\\*mut \\\(\\\)\\\) 0x0" 141 gdb_test "print 0x0 as fn(i64) -> ()" " = \\\(\\*mut fn \\\(i64\\\) -> \\\(\\\)\\\) 0x0" 142 143 # The lexer doesn't treat this as a failure, but rather as two tokens, 144 # and we error out while trying to look up 'r'. This is fine, though 145 # -- what's important is that it isn't accepted. 146 gdb_test "print r#" "No symbol 'r' in current context" 147 148 gdb_test "printf \"%d %d\\n\", 23+1, 23-1" "24 22" 149 150 gdb_test "print 5," "Syntax error near ','" 151 152 # Check expression debug works for strings. 153 gdb_test "with debug expression 1 -- print \"foo\"" \ 154 [multi_line \ 155 "Operation: OP_AGGREGATE" \ 156 " Type: &str" \ 157 " nullptr" \ 158 " Vector:" \ 159 " String: data_ptr" \ 160 " Operation: UNOP_ADDR" \ 161 " Operation: OP_STRING" \ 162 " String: foo" \ 163 " String: length" \ 164 " Operation: OP_LONG" \ 165 " Type: usize" \ 166 " Constant: 3" \ 167 "Operation: OP_LONG" \ 168 " Type: i32" \ 169 " Constant: 0" \ 170 "evaluation of this expression requires the target program to be active"] \ 171 "print a string with expression debug turned on" 172 173 # PR rust/31082 - truncating to a pointer would fail. Depending on 174 # the default host architecture, this may or may not print a warning. 175 gdb_test "print (0xffffffd00000009a as *mut u64)" \ 176 "(warning: value truncated\[\r\n\]+)?.* = \\(\\*mut u64\\) $hex" 177