@@ -74,8 +74,13 @@ const (
cppGuard = "#if defined(__cplusplus)"
cppGuard = "#if defined(__cplusplus)"
commentStart = "/* "
commentStart = "/* "
commentEnd = " */"
commentEnd = " */"
lineComment = "// "
)
)
func isComment(line string) bool {
return strings.HasPrefix(line, commentStart) || strings.HasPrefix(line, lineComment)
}
func extractComment(lines []string, lineNo int) (comment []string, rest []string, restLineNo int, err error) {
func extractComment(lines []string, lineNo int) (comment []string, rest []string, restLineNo int, err error) {
if len(lines) == 0 {
if len(lines) == 0 {
return nil, lines, lineNo, nil
return nil, lines, lineNo, nil
@@ -84,7 +89,10 @@ func extractComment(lines []string, lineNo int) (comment []string, rest []string
restLineNo = lineNo
restLineNo = lineNo
rest = lines
rest = lines
if !strings.HasPrefix(rest[0], commentStart) {
var isBlock bool
if strings.HasPrefix(rest[0], commentStart) {
isBlock = true
} else if !strings.HasPrefix(rest[0], lineComment) {
panic("extractComment called on non-comment")
panic("extractComment called on non-comment")
}
}
commentParagraph := rest[0][len(commentStart):]
commentParagraph := rest[0][len(commentStart):]
@@ -92,25 +100,34 @@ func extractComment(lines []string, lineNo int) (comment []string, rest []string
restLineNo++
restLineNo++
for len(rest) > 0 {
for len(rest) > 0 {
i := strings.Index(commentParagraph, commentEnd)
if i >= 0 {
if i != len(commentParagraph)-len(commentEnd) {
err = fmt.Errorf("garbage after comment end on line %d", restLineNo)
if isBlock {
i := strings.Index(commentParagraph, commentEnd)
if i >= 0 {
if i != len(commentParagraph)-len(commentEnd) {
err = fmt.Errorf("garbage after comment end on line %d", restLineNo)
return
}
commentParagraph = commentParagraph[:i]
if len(commentParagraph) > 0 {
comment = append(comment, commentParagraph)
}
return
return
}
}
commentParagraph = commentParagraph[:i]
if len(commentParagraph) > 0 {
comment = append(comment, commentParagraph)
}
return
}
}
line := rest[0]
line := rest[0]
if !strings.HasPrefix(line, " *") {
err = fmt.Errorf("comment doesn't start with block prefix on line %d: %s", restLineNo, line)
if isBlock {
if !strings.HasPrefix(line, " *") {
err = fmt.Errorf("comment doesn't start with block prefix on line %d: %s", restLineNo, line)
return
}
} else if !strings.HasPrefix(line, "//") {
if len(commentParagraph) > 0 {
comment = append(comment, commentParagraph)
}
return
return
}
}
if len(line) == 2 || line[2] != '/' {
if len(line) == 2 || !isBlock || line[2] != '/' {
line = line[2:]
line = line[2:]
}
}
if strings.HasPrefix(line, " ") {
if strings.HasPrefix(line, " ") {
@@ -309,7 +326,7 @@ func (config *Config) parseHeader(path string) (*HeaderFile, error) {
}
}
oldLines = lines
oldLines = lines
if len(lines) > 0 && strings.HasPrefix(lines[0], commentStart ) {
if len(lines) > 0 && isComment(lines[0] ) {
comment, rest, restLineNo, err := extractComment(lines, lineNo)
comment, rest, restLineNo, err := extractComment(lines, lineNo)
if err != nil {
if err != nil {
return nil, err
return nil, err
@@ -345,7 +362,7 @@ func (config *Config) parseHeader(path string) (*HeaderFile, error) {
var section HeaderSection
var section HeaderSection
if strings.HasPrefix(line, commentStart ) {
if isComment(line ) {
comment, rest, restLineNo, err := extractComment(lines, lineNo)
comment, rest, restLineNo, err := extractComment(lines, lineNo)
if err != nil {
if err != nil {
return nil, err
return nil, err
@@ -380,7 +397,7 @@ func (config *Config) parseHeader(path string) (*HeaderFile, error) {
var comment []string
var comment []string
var decl string
var decl string
if strings.HasPrefix(line, commentStart ) {
if isComment(line ) {
comment, lines, lineNo, err = extractComment(lines, lineNo)
comment, lines, lineNo, err = extractComment(lines, lineNo)
if err != nil {
if err != nil {
return nil, err
return nil, err