Repository: elliotchance/c2go (go)

Issue:
wide string literal not handled by regex
Error from my own test:
```
panic: could not match regexp with string
^(?P<address>[0-9a-fx]+) <(?P<position>.*)> '(?P<type>.*)'(?P<lvalue> lvalue)? (?P<value>".*")[\s]*$
0x561f897201c8 <col:16> 'wchar_t [6]' L"tes\x115t"
```
Source found on https://c-testsuite.github.io/c2go_report.html:
```c
// this file contains BMP chars encoded in UTF-8
#include <stdio.h>
#include <wchar.h>

int main()
{
    wchar_t s[] = L"hello$$你好¢¢世界€€world";
    wchar_t *p;
    for (p = s; *p; p++) printf("%04X ", (unsigned) *p);
    printf("\n");
    return 0;
}
```

Docs:
>StringLiteral - This represents a string literal expression, e.g. "foo" or L"bar" (wide strings). 

https://clang.llvm.org/doxygen/classclang_1_1StringLiteral.html

Pull request description:
Handle wide-character string literals in regex
Fixes #866

Does not add support for wide-character strings per se, only for parsing the clang AST without crashing.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/elliotchance/c2go/867)
<!-- Reviewable:end -->


Patch:
diff --git a/ast/string_literal.go b/ast/string_literal.go
index 76df79a..fd4ea83 100644
--- a/ast/string_literal.go
+++ b/ast/string_literal.go
@@ -17,7 +17,7 @@ type StringLiteral struct {
 
 func parseStringLiteral(line string) *StringLiteral {
 	groups := groupsFromRegex(
-		`<(?P<position>.*)> '(?P<type>.*)'(?P<lvalue> lvalue)? (?P<value>".*")`,
+		`<(?P<position>.*)> '(?P<type>.*)'(?P<lvalue> lvalue)? L?(?P<value>".*")`,
 		line,
 	)
 


Test patch:
diff --git a/ast/string_literal_test.go b/ast/string_literal_test.go
index bea645b..4cfabbd 100644
--- a/ast/string_literal_test.go
+++ b/ast/string_literal_test.go
@@ -30,6 +30,14 @@ func TestStringLiteral(t *testing.T) {
 			Value:      "foo",
 			ChildNodes: []Node{},
 		},
+		`0x61b80c8 <col:19> 'wchar_t [21]' lvalue L"hello$$\x4F60\x597D\242\242\x4E16\x754C\x20AC\x20ACworld"`: &StringLiteral{
+			Addr:       0x61b80c8,
+			Pos:        NewPositionFromString("col:19"),
+			Type:       "wchar_t [21]",
+			Lvalue:     true,
+			Value:      "hello$$\x4F60\x597D\242\242\x4E16\x754C\x20AC\x20ACworld",
+			ChildNodes: []Node{},
+		},
 	}
 
 	runNodeTests(t, nodes)