support inline comments in asm #include lines

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.
pull/824/head
Daniel Martí 5 months ago
parent de65196495
commit bdfa619f77

@ -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 {
@ -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 {

@ -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·specialasm·runes.
// A comment may include many·specialasm·runes and it's okay.
//No space: many·specialasm·runes.

@ -63,7 +63,7 @@ import (
)
/*
#include "separate.h"
#include "separate.h" // inline comment
static int privateAdd(int a, int b) {
return a + b;

Loading…
Cancel
Save