Silence trailing "1 warning" message from javac when warning is silenced
soong_javac_wrapper is silencing a useless warning: warning: [options] bootstrap class path not set in conjunction with -source 1.9 but recent versions of javac have started also printing: 1 warning Read the warning count, subtract the number of silenced warnings, and reprint it if the non-silenced warning count is nonzero. Fixes: 144118634 Test: javac_wrapper_test.go Change-Id: Ie1d0a978188ab7b1c41027f718a1274608628123
This commit is contained in:
@@ -31,6 +31,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -80,10 +81,11 @@ func Main(out io.Writer, name string, args []string) (int, error) {
|
|||||||
|
|
||||||
pw.Close()
|
pw.Close()
|
||||||
|
|
||||||
|
proc := processor{}
|
||||||
// Process subprocess stdout asynchronously
|
// Process subprocess stdout asynchronously
|
||||||
errCh := make(chan error)
|
errCh := make(chan error)
|
||||||
go func() {
|
go func() {
|
||||||
errCh <- process(pr, out)
|
errCh <- proc.process(pr, out)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Wait for subprocess to finish
|
// Wait for subprocess to finish
|
||||||
@@ -117,14 +119,18 @@ func Main(out io.Writer, name string, args []string) (int, error) {
|
|||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func process(r io.Reader, w io.Writer) error {
|
type processor struct {
|
||||||
|
silencedWarnings int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (proc *processor) process(r io.Reader, w io.Writer) error {
|
||||||
scanner := bufio.NewScanner(r)
|
scanner := bufio.NewScanner(r)
|
||||||
// Some javac wrappers output the entire list of java files being
|
// Some javac wrappers output the entire list of java files being
|
||||||
// compiled on a single line, which can be very large, set the maximum
|
// compiled on a single line, which can be very large, set the maximum
|
||||||
// buffer size to 2MB.
|
// buffer size to 2MB.
|
||||||
scanner.Buffer(nil, 2*1024*1024)
|
scanner.Buffer(nil, 2*1024*1024)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
processLine(w, scanner.Text())
|
proc.processLine(w, scanner.Text())
|
||||||
}
|
}
|
||||||
err := scanner.Err()
|
err := scanner.Err()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -133,12 +139,32 @@ func process(r io.Reader, w io.Writer) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func processLine(w io.Writer, line string) {
|
func (proc *processor) processLine(w io.Writer, line string) {
|
||||||
|
for _, f := range warningFilters {
|
||||||
|
if f.MatchString(line) {
|
||||||
|
proc.silencedWarnings++
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
for _, f := range filters {
|
for _, f := range filters {
|
||||||
if f.MatchString(line) {
|
if f.MatchString(line) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if match := warningCount.FindStringSubmatch(line); match != nil {
|
||||||
|
c, err := strconv.Atoi(match[1])
|
||||||
|
if err == nil {
|
||||||
|
c -= proc.silencedWarnings
|
||||||
|
if c == 0 {
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
line = fmt.Sprintf("%d warning", c)
|
||||||
|
if c > 1 {
|
||||||
|
line += "s"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
for _, p := range colorPatterns {
|
for _, p := range colorPatterns {
|
||||||
var matched bool
|
var matched bool
|
||||||
if line, matched = applyColor(line, p.color, p.re); matched {
|
if line, matched = applyColor(line, p.color, p.re); matched {
|
||||||
@@ -170,12 +196,17 @@ var colorPatterns = []struct {
|
|||||||
{markerRe, green},
|
{markerRe, green},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var warningCount = regexp.MustCompile(`^([0-9]+) warning(s)?$`)
|
||||||
|
|
||||||
|
var warningFilters = []*regexp.Regexp{
|
||||||
|
regexp.MustCompile(`bootstrap class path not set in conjunction with -source`),
|
||||||
|
}
|
||||||
|
|
||||||
var filters = []*regexp.Regexp{
|
var filters = []*regexp.Regexp{
|
||||||
regexp.MustCompile(`Note: (Some input files|.*\.java) uses? or overrides? a deprecated API.`),
|
regexp.MustCompile(`Note: (Some input files|.*\.java) uses? or overrides? a deprecated API.`),
|
||||||
regexp.MustCompile(`Note: Recompile with -Xlint:deprecation for details.`),
|
regexp.MustCompile(`Note: Recompile with -Xlint:deprecation for details.`),
|
||||||
regexp.MustCompile(`Note: (Some input files|.*\.java) uses? unchecked or unsafe operations.`),
|
regexp.MustCompile(`Note: (Some input files|.*\.java) uses? unchecked or unsafe operations.`),
|
||||||
regexp.MustCompile(`Note: Recompile with -Xlint:unchecked for details.`),
|
regexp.MustCompile(`Note: Recompile with -Xlint:unchecked for details.`),
|
||||||
regexp.MustCompile(`bootstrap class path not set in conjunction with -source`),
|
|
||||||
|
|
||||||
regexp.MustCompile(`javadoc: warning - The old Doclet and Taglet APIs in the packages`),
|
regexp.MustCompile(`javadoc: warning - The old Doclet and Taglet APIs in the packages`),
|
||||||
regexp.MustCompile(`com.sun.javadoc, com.sun.tools.doclets and their implementations`),
|
regexp.MustCompile(`com.sun.javadoc, com.sun.tools.doclets and their implementations`),
|
||||||
|
@@ -75,13 +75,29 @@ javadoc: option --boot-class-path not allowed with target 1.9
|
|||||||
`,
|
`,
|
||||||
out: "\n",
|
out: "\n",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
in: `
|
||||||
|
warning: [options] bootstrap class path not set in conjunction with -source 1.9\n
|
||||||
|
1 warning
|
||||||
|
`,
|
||||||
|
out: "\n",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
in: `
|
||||||
|
warning: foo
|
||||||
|
warning: [options] bootstrap class path not set in conjunction with -source 1.9\n
|
||||||
|
2 warnings
|
||||||
|
`,
|
||||||
|
out: "\n\x1b[1m\x1b[35mwarning:\x1b[0m\x1b[1m foo\x1b[0m\n1 warning\n",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestJavacColorize(t *testing.T) {
|
func TestJavacColorize(t *testing.T) {
|
||||||
for i, test := range testCases {
|
for i, test := range testCases {
|
||||||
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
err := process(bytes.NewReader([]byte(test.in)), buf)
|
proc := processor{}
|
||||||
|
err := proc.process(bytes.NewReader([]byte(test.in)), buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("error: %q", err)
|
t.Errorf("error: %q", err)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user