[Lldb-commits] [lldb] r137247 - in /lldb/trunk: source/Expression/IRForTarget.cpp test/lang/c/strings/ test/lang/c/strings/Makefile test/lang/c/strings/TestCStrings.py test/lang/c/strings/main.c

Sean Callanan scallanan at apple.com
Wed Aug 10 14:05:52 PDT 2011


Author: spyffe
Date: Wed Aug 10 16:05:52 2011
New Revision: 137247

URL: http://llvm.org/viewvc/llvm-project?rev=137247&view=rev
Log:
Fixed a problem that prevented access to members
of string literals ("hello"[2]).  Also fixed a
problem in which empty string literals were not
being compiled correctly ((int)printf("") would
print garbage).

Added a testcase that covers both.

Added:
    lldb/trunk/test/lang/c/strings/
    lldb/trunk/test/lang/c/strings/Makefile
    lldb/trunk/test/lang/c/strings/TestCStrings.py
    lldb/trunk/test/lang/c/strings/main.c
Modified:
    lldb/trunk/source/Expression/IRForTarget.cpp

Modified: lldb/trunk/source/Expression/IRForTarget.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Expression/IRForTarget.cpp?rev=137247&r1=137246&r2=137247&view=diff
==============================================================================
--- lldb/trunk/source/Expression/IRForTarget.cpp (original)
+++ lldb/trunk/source/Expression/IRForTarget.cpp Wed Aug 10 16:05:52 2011
@@ -1634,19 +1634,42 @@
         
         Constant *gc = gv->getInitializer();
         
-        ConstantArray *gc_array = dyn_cast<ConstantArray>(gc);
+        std::string str;
         
-        if (!gc_array)
-            continue;
-        
-        if (!gc_array->isCString())
-            continue;
+        if (gc->isNullValue())
+        {
+            Type *gc_type = gc->getType();
+            
+            ArrayType *gc_array_type = dyn_cast<ArrayType>(gc_type);
+            
+            if (!gc_array_type)
+                continue;
+            
+            Type *gc_element_type = gc_array_type->getElementType();
+            
+            IntegerType *gc_integer_type = dyn_cast<IntegerType>(gc_element_type);
+            
+            if (gc_integer_type->getBitWidth() != 8)
+                continue;
+            
+            str = "";
+        }
+        else
+        {
+            ConstantArray *gc_array = dyn_cast<ConstantArray>(gc);
+
+            if (!gc_array)
+                continue;
         
-        if (log)
-            log->Printf("Found a GlobalVariable with string initializer %s", PrintValue(gc).c_str());
+            if (!gc_array->isCString())
+                continue;
         
-        std::string str = gc_array->getAsString();
+            if (log)
+                log->Printf("Found a GlobalVariable with string initializer %s", PrintValue(gc).c_str());
         
+            str = gc_array->getAsString();
+        }
+            
         offsets[gv] = m_data_allocator->GetStream().GetSize();
         
         m_data_allocator->GetStream().Write(str.c_str(), str.length() + 1);
@@ -1686,7 +1709,10 @@
                     return false;
                 }
                 
-                const_expr->replaceAllUsesWith(new_initializer);
+                Constant *bit_cast = ConstantExpr::getBitCast(new_initializer, const_expr->getOperand(0)->getType());
+                Constant *new_gep = const_expr->getWithOperandReplaced(0, bit_cast);
+                
+                const_expr->replaceAllUsesWith(new_gep);
             }
             else if (store_inst)
             {

Added: lldb/trunk/test/lang/c/strings/Makefile
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/lang/c/strings/Makefile?rev=137247&view=auto
==============================================================================
--- lldb/trunk/test/lang/c/strings/Makefile (added)
+++ lldb/trunk/test/lang/c/strings/Makefile Wed Aug 10 16:05:52 2011
@@ -0,0 +1,5 @@
+LEVEL = ../../../make
+
+C_SOURCES := main.c
+
+include $(LEVEL)/Makefile.rules

Added: lldb/trunk/test/lang/c/strings/TestCStrings.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/lang/c/strings/TestCStrings.py?rev=137247&view=auto
==============================================================================
--- lldb/trunk/test/lang/c/strings/TestCStrings.py (added)
+++ lldb/trunk/test/lang/c/strings/TestCStrings.py Wed Aug 10 16:05:52 2011
@@ -0,0 +1,57 @@
+"""
+Tests that C strings work as expected in expressions
+"""
+
+from lldbtest import *
+
+class CStringsTestCase(TestBase):
+    
+    mydir = os.path.join("lang", "c", "strings")
+    
+    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
+    def test_with_dsym_and_run_command(self):
+        """Tests that C strings work as expected in expressions"""
+        self.buildDsym()
+        self.static_method_commands()
+
+    def test_with_dwarf_and_run_command(self):
+        """Tests that C strings work as expected in expressions"""
+        self.buildDwarf()
+        self.static_method_commands()
+
+    def setUp(self):
+        TestBase.setUp(self)
+    
+    def set_breakpoint(self, line):
+        self.expect("breakpoint set -f main.c -l %d" % line,
+                    BREAKPOINT_CREATED,
+                    startstr = "Breakpoint created")
+    
+    def static_method_commands(self):
+        """Tests that C strings work as expected in expressions"""
+        self.runCmd("file a.out", CURRENT_EXECUTABLE_SET)
+
+        self.set_breakpoint(line_number('main.c', '// breakpoint 1'))
+
+        self.runCmd("process launch", RUN_SUCCEEDED)
+
+        self.expect("expression -- a[2]",
+                    startstr = "(char) $0 = 'c'")
+
+        self.expect("expression -- z[2]",
+                    startstr = "(const char) $1 = 'x'")
+
+        self.expect("expression -- (int)strlen(\"hello\")",
+                    startstr = "(int) $2 = 5")
+
+        self.expect("expression -- \"world\"[2]",
+                    startstr = "(const char) $3 = 'r'")
+
+        self.expect("expression -- \"\"[0]",
+                    startstr = "(const char) $4 = '\\0'")
+
+if __name__ == '__main__':
+    import atexit
+    lldb.SBDebugger.Initialize()
+    atexit.register(lambda: lldb.SBDebugger.Terminate())
+    unittest2.main()

Added: lldb/trunk/test/lang/c/strings/main.c
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/lang/c/strings/main.c?rev=137247&view=auto
==============================================================================
--- lldb/trunk/test/lang/c/strings/main.c (added)
+++ lldb/trunk/test/lang/c/strings/main.c Wed Aug 10 16:05:52 2011
@@ -0,0 +1,18 @@
+//===-- main.c ----------------------------------------------------*- C -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include <stdio.h>
+
+int main()
+{
+  const char a[] = "abcde";
+  const char *z = "vwxyz";
+  
+  printf("%s %s", a, z); // breakpoint 1
+}





More information about the lldb-commits mailing list