Skip to content

Commit

Permalink
support inline comments in asm #include lines
Browse files Browse the repository at this point in the history
That is, the assembly line

    #include "foo.h" // bar

would make garble run into an error, as we would try to parse
the #include directive before we stripped comments,
effectively trying to unquote the string

    "foo.h" // bar

rather than just the included filename

    "foo.h"

Add test cases for asm but also cgo, while at it.

Fixes #812.
  • Loading branch information
mvdan committed Dec 25, 2023
1 parent de65196 commit bdfa619
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
26 changes: 14 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,14 +688,24 @@ func (tf *transformer) transformAsm(args []string) ([]string, error) {
for scanner.Scan() {
line := scanner.Text()

// First, handle hash directives without leading whitespaces.
// Whole-line comments might be directives, leave them in place.
// For example: //go:build race
// Any other comment, including inline ones, can be discarded entirely.
line, comment, hasComment := strings.Cut(line, "//")
if hasComment && line == "" {
buf.WriteString("//")
buf.WriteString(comment)
buf.WriteByte('\n')
continue
}

// #include "foo.h"
// Preprocessor lines to include another file.
// For example: #include "foo.h"
if quoted := strings.TrimPrefix(line, "#include"); quoted != line {
quoted = strings.TrimSpace(quoted)
path, err := strconv.Unquote(quoted)
if err != nil {
return nil, err
if err != nil { // note that strconv.Unquote errors do not include the input string
return nil, fmt.Errorf("cannot unquote %q: %v", quoted, err)
}
newPath := newHeaderPaths[path]
switch newPath {
Expand Down Expand Up @@ -734,16 +744,8 @@ func (tf *transformer) transformAsm(args []string) ([]string, error) {
continue
}

// Leave "//" comments unchanged; they might be directives.
line, comment, hasComment := strings.Cut(line, "//")

// Anything else is regular assembly; replace the names.
tf.replaceAsmNames(&buf, []byte(line))

if hasComment {
buf.WriteString("//")
buf.WriteString(comment)
}
buf.WriteByte('\n')
}
if err := scanner.Err(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion testdata/script/asm.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func main() {
-- garble_main_amd64.s --
#include "garble_define_amd64.h"

#include "extra/garble_define2_amd64.h"
#include "extra/garble_define2_amd64.h" // Inline: many·special∕asm·runes.

// A comment may include many·special∕asm·runes and it's okay.
//No space: many·special∕asm·runes.
Expand Down
2 changes: 1 addition & 1 deletion testdata/script/cgo.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import (
)

/*
#include "separate.h"
#include "separate.h" // inline comment

static int privateAdd(int a, int b) {
return a + b;
Expand Down

0 comments on commit bdfa619

Please sign in to comment.