Repository: elliotchance/c2go (go)

Issue:
warn_unused_result extra field not handled by regex
error:
```
~/code/c2go $ go test -tags=integration ./...
panic: could not match regexp with string
^(?P<address>[0-9a-fx]+) <(?P<position>.*)>(?P<inherited> Inherited)?( warn_unused_result)?[\s]*$
0x55c2df4f93b8 <col:53> warn_unused_result ""


goroutine 74 [running]:
github.com/elliotchance/c2go/ast.groupsFromRegex(0xc00040c070, 0x61, 0xc000805987, 0x2d, 0x14)
        /home/user/code/c2go/ast/ast.go:310 +0x360
github.com/elliotchance/c2go/ast.parseWarnUnusedResultAttr(0xc000805987, 0x2d, 0x14)
        /home/user/code/c2go/ast/warn_unused_result_attr.go:13 +0x62
github.com/elliotchance/c2go/ast.Parse(0xc000805972, 0x42, 0x705495, 0x5)
        /home/user/code/c2go/ast/ast.go:286 +0x2fb2
github.com/elliotchance/c2go.convertLinesToNodes(0xc000feb9d0, 0x1b9e, 0x52da, 0x0, 0x0, 0x0)
        /home/user/code/c2go/main.go:89 +0x1a6
github.com/elliotchance/c2go.convertLinesToNodesParallel.func1.2(0xc0000280c0, 0xc00011a020, 0xc000feb9d0, 0x1b9e, 0x52da, 0x0)
        /home/user/code/c2go/main.go:121 +0x53
created by github.com/elliotchance/c2go.convertLinesToNodesParallel.func1
        /home/user/code/c2go/main.go:119 +0x1b5
```

clang:
```
~ $ clang --version
clang version 10.0.0
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm/10/bin
```

The following text was added to the warn_usued_result attribute in clang 10:
"A string literal may optionally be provided to the attribute, which will be reproduced in any resulting diagnostics. Redeclarations using different forms of the attribute (with or without the string literal or with different string literal contents) are allowed. If there are redeclarations of the entity with differing string literals, it is unspecified which one will be used by Clang in any resulting diagnostics.

"Additionally, discarded temporaries resulting from a call to a constructor marked with `[[nodiscard]]` or a constructor of a type marked `[[nodiscard]]` will also diagnose. This also applies to type conversions that use the annotated `[[nodiscard]]` constructor or result in an annotated type."
https://releases.llvm.org/10.0.0/tools/clang/docs/AttributeReference.html#nodiscard-warn-unused-result

Pull request:
Handle optional extra field in warn_unused_result
Fixes #861

<!-- 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/863)
<!-- Reviewable:end -->


Patch:
diff --git a/ast/asm_label_attr.go b/ast/asm_label_attr.go
index 81566d5..55e2420 100644
--- a/ast/asm_label_attr.go
+++ b/ast/asm_label_attr.go
@@ -2,27 +2,30 @@ package ast
 
 // AsmLabelAttr is a type of attribute for assembler label
 type AsmLabelAttr struct {
-	Addr         Address
-	Pos          Position
-	Inherited    bool
-	FunctionName string
-	ChildNodes   []Node
+	Addr           Address
+	Pos            Position
+	Inherited      bool
+	FunctionName   string
+	ChildNodes     []Node
+	IsLiteralLabel bool
 }
 
 func parseAsmLabelAttr(line string) *AsmLabelAttr {
 	groups := groupsFromRegex(
 		`<(?P<position>.*)>
 		(?P<inherited> Inherited)?
-		 "(?P<function>.+)"`,
+		 "(?P<function>.+)"
+		(?P<literal> IsLiteralLabel)?`,
 		line,
 	)
 
 	return &AsmLabelAttr{
-		Addr:         ParseAddress(groups["address"]),
-		Pos:          NewPositionFromString(groups["position"]),
-		Inherited:    len(groups["inherited"]) > 0,
-		FunctionName: groups["function"],
-		ChildNodes:   []Node{},
+		Addr:           ParseAddress(groups["address"]),
+		Pos:            NewPositionFromString(groups["position"]),
+		Inherited:      len(groups["inherited"]) > 0,
+		FunctionName:   groups["function"],
+		ChildNodes:     []Node{},
+		IsLiteralLabel: len(groups["literal"]) > 0,
 	}
 }
 
diff --git a/ast/warn_unused_result_attr.go b/ast/warn_unused_result_attr.go
index 7a46b29..a64c6df 100644
--- a/ast/warn_unused_result_attr.go
+++ b/ast/warn_unused_result_attr.go
@@ -10,7 +10,7 @@ type WarnUnusedResultAttr struct {
 }
 
 func parseWarnUnusedResultAttr(line string) *WarnUnusedResultAttr {
-	groups := groupsFromRegex(`<(?P<position>.*)>(?P<inherited> Inherited)?( warn_unused_result)?`, line)
+	groups := groupsFromRegex(`<(?P<position>.*)>(?P<inherited> Inherited)?( warn_unused_result)?( "")?`, line)
 
 	return &WarnUnusedResultAttr{
 		Addr:       ParseAddress(groups["address"]),


Test patch:
diff --git a/ast/asm_label_attr_test.go b/ast/asm_label_attr_test.go
index 83775c9..3b6dc23 100644
--- a/ast/asm_label_attr_test.go
+++ b/ast/asm_label_attr_test.go
@@ -7,18 +7,28 @@ import (
 func TestAsmLabelAttr(t *testing.T) {
 	nodes := map[string]Node{
 		`0x7ff26d8224e8 </usr/include/sys/cdefs.h:569:36> "_fopen"`: &AsmLabelAttr{
-			Addr:         0x7ff26d8224e8,
-			Pos:          NewPositionFromString("/usr/include/sys/cdefs.h:569:36"),
-			Inherited:    false,
-			FunctionName: "_fopen",
-			ChildNodes:   []Node{},
+			Addr:           0x7ff26d8224e8,
+			Pos:            NewPositionFromString("/usr/include/sys/cdefs.h:569:36"),
+			Inherited:      false,
+			FunctionName:   "_fopen",
+			ChildNodes:     []Node{},
+			IsLiteralLabel: false,
 		},
 		`0x7fd55a169318 </usr/include/stdio.h:325:47> Inherited "_popen"`: &AsmLabelAttr{
-			Addr:         0x7fd55a169318,
-			Pos:          NewPositionFromString("/usr/include/stdio.h:325:47"),
-			Inherited:    true,
-			FunctionName: "_popen",
-			ChildNodes:   []Node{},
+			Addr:           0x7fd55a169318,
+			Pos:            NewPositionFromString("/usr/include/stdio.h:325:47"),
+			Inherited:      true,
+			FunctionName:   "_popen",
+			ChildNodes:     []Node{},
+			IsLiteralLabel: false,
+		},
+		`0x559fea32f5f0 <line:407:94> "__isoc99_fscanf" IsLiteralLabel`: &AsmLabelAttr{
+			Addr:           0x559fea32f5f0,
+			Pos:            NewPositionFromString("line:407:94"),
+			Inherited:      false,
+			FunctionName:   "__isoc99_fscanf",
+			ChildNodes:     []Node{},
+			IsLiteralLabel: true,
 		},
 	}
 
diff --git a/ast/warn_unused_result_attr_test.go b/ast/warn_unused_result_attr_test.go
index 30f8a8a..95fb662 100644
--- a/ast/warn_unused_result_attr_test.go
+++ b/ast/warn_unused_result_attr_test.go
@@ -24,6 +24,12 @@ func TestWarnUnusedResultAttr(t *testing.T) {
 			Inherited:  true,
 			ChildNodes: []Node{},
 		},
+		`0x55c2df4f93b8 <col:53> warn_unused_result ""`: &WarnUnusedResultAttr{
+			Addr:       0x55c2df4f93b8,
+			Pos:        NewPositionFromString("col:53"),
+			Inherited:  false,
+			ChildNodes: []Node{},
+		},
 	}
 
 	runNodeTests(t, nodes)