From f9027f7dbeed861960ffd9b205457c216077d4de Mon Sep 17 00:00:00 2001
From: Denis Isaev <denis@golangci.com>
Date: Tue, 12 Jun 2018 15:32:34 +0300
Subject: [PATCH] Write debug logs for autogen excluding for #86

---
 pkg/commands/root.go                          |  5 ++-
 pkg/logutils/logutils.go                      | 36 +++++++++++++++++++
 .../processors/autogenerated_exclude.go       | 31 +++++++++++++---
 .../processors/autogenerated_exclude_test.go  | 24 -------------
 .../testdata/autogenerated}/autogenerated.go  |  2 +-
 .../autogenerated/{gen.go => do_not_edit.go}  |  0
 .../testdata/autogenerated/go_bindata.go      |  2 +-
 test/testdata/autogenerated/mockgen.go        |  2 +-
 .../testdata/autogenerated/protoc_gen_foo.go  |  2 +-
 9 files changed, 70 insertions(+), 34 deletions(-)
 rename {pkg/result/processors/testdata => test/testdata/autogenerated}/autogenerated.go (75%)
 rename test/testdata/autogenerated/{gen.go => do_not_edit.go} (100%)
 rename pkg/result/processors/testdata/autogenerated_alt_hdr2.go => test/testdata/autogenerated/go_bindata.go (84%)
 rename pkg/result/processors/testdata/autogenerated_alt_hdr.go => test/testdata/autogenerated/protoc_gen_foo.go (83%)

diff --git a/pkg/commands/root.go b/pkg/commands/root.go
index 92e491ad..d4ac1b72 100644
--- a/pkg/commands/root.go
+++ b/pkg/commands/root.go
@@ -8,6 +8,7 @@ import (
 	"runtime/pprof"
 
 	"github.com/golangci/golangci-lint/pkg/config"
+	"github.com/golangci/golangci-lint/pkg/logutils"
 	"github.com/golangci/golangci-lint/pkg/printers"
 	"github.com/sirupsen/logrus"
 	"github.com/spf13/cobra"
@@ -16,7 +17,9 @@ import (
 
 func setupLog(isVerbose bool) {
 	log.SetFlags(0) // don't print time
-	if isVerbose {
+	if logutils.IsDebugEnabled() {
+		logrus.SetLevel(logrus.DebugLevel)
+	} else if isVerbose {
 		logrus.SetLevel(logrus.InfoLevel)
 	}
 }
diff --git a/pkg/logutils/logutils.go b/pkg/logutils/logutils.go
index 4661bd5f..affd07d5 100644
--- a/pkg/logutils/logutils.go
+++ b/pkg/logutils/logutils.go
@@ -2,6 +2,7 @@ package logutils
 
 import (
 	"os"
+	"strings"
 
 	"github.com/sirupsen/logrus"
 )
@@ -15,3 +16,38 @@ func HiddenWarnf(format string, args ...interface{}) {
 		logrus.Infof(format, args...)
 	}
 }
+
+func getEnabledDebugs() map[string]bool {
+	ret := map[string]bool{}
+	debugVar := os.Getenv("GL_DEBUG")
+	if debugVar == "" {
+		return ret
+	}
+
+	for _, tag := range strings.Split(debugVar, ",") {
+		ret[tag] = true
+	}
+
+	return ret
+}
+
+var enabledDebugs = getEnabledDebugs()
+
+type DebugFunc func(format string, args ...interface{})
+
+func nopDebugf(format string, args ...interface{}) {}
+
+func Debug(tag string) DebugFunc {
+	if !enabledDebugs[tag] {
+		return nopDebugf
+	}
+
+	return func(format string, args ...interface{}) {
+		newArgs := append([]interface{}{tag}, args...)
+		logrus.Debugf("%s: "+format, newArgs...)
+	}
+}
+
+func IsDebugEnabled() bool {
+	return len(enabledDebugs) != 0
+}
diff --git a/pkg/result/processors/autogenerated_exclude.go b/pkg/result/processors/autogenerated_exclude.go
index 276ce05e..3a12b589 100644
--- a/pkg/result/processors/autogenerated_exclude.go
+++ b/pkg/result/processors/autogenerated_exclude.go
@@ -7,9 +7,12 @@ import (
 	"strings"
 
 	"github.com/golangci/golangci-lint/pkg/lint/astcache"
+	"github.com/golangci/golangci-lint/pkg/logutils"
 	"github.com/golangci/golangci-lint/pkg/result"
 )
 
+var autogenDebugf = logutils.Debug("autogen_exclude")
+
 type ageFileSummary struct {
 	isGenerated bool
 }
@@ -62,10 +65,12 @@ func isGeneratedFileByComment(doc string) bool {
 	doc = strings.ToLower(doc)
 	for _, marker := range markers {
 		if strings.Contains(doc, marker) {
+			autogenDebugf("doc contains marker %q: file is generated", marker)
 			return true
 		}
 	}
 
+	autogenDebugf("doc of len %d doesn't contain any of markers: %s", len(doc), markers)
 	return false
 }
 
@@ -83,27 +88,43 @@ func (p *AutogeneratedExclude) getOrCreateFileSummary(i *result.Issue) (*ageFile
 		return nil, fmt.Errorf("can't parse file %s: %s", i.FilePath(), f.Err)
 	}
 
-	doc := getDoc(f.F, f.Fset)
+	autogenDebugf("file %q: astcache file is %+v", i.FilePath(), *f)
+
+	doc := getDoc(f.F, f.Fset, i.FilePath())
 
 	fs.isGenerated = isGeneratedFileByComment(doc)
+	autogenDebugf("file %q is generated: %t", i.FilePath(), fs.isGenerated)
 	return fs, nil
 }
 
-func getDoc(f *ast.File, fset *token.FileSet) string {
+func getDoc(f *ast.File, fset *token.FileSet, filePath string) string {
 	// don't use just f.Doc: e.g. mockgen leaves extra line between comment and package name
 
-	importPos := f.End()
+	var importPos token.Pos
 	if len(f.Imports) != 0 {
 		importPos = f.Imports[0].Pos()
+		autogenDebugf("file %q: search comments until first import pos %d (%s)", filePath, importPos, fset.Position(importPos))
+	} else {
+		importPos = f.End()
+		autogenDebugf("file %q: search comments until EOF pos %d (%s)", filePath, importPos, fset.Position(importPos))
 	}
 
 	var neededComments []string
 	for _, g := range f.Comments {
-		if g.Pos() < importPos && fset.Position(g.Pos()).Column == 1 {
-			neededComments = append(neededComments, g.Text())
+		pos := g.Pos()
+		filePos := fset.Position(pos)
+		text := g.Text()
+		isAllowed := pos < importPos && filePos.Column == 1
+		if isAllowed {
+			autogenDebugf("file %q: pos=%d, filePos=%s: comment %q: it's allowed", filePath, pos, filePos, text)
+			neededComments = append(neededComments, text)
+		} else {
+			autogenDebugf("file %q: pos=%d, filePos=%s: comment %q: it's NOT allowed", filePath, pos, filePos, text)
 		}
 	}
 
+	autogenDebugf("file %q: got %d allowed comments", filePath, len(neededComments))
+
 	if len(neededComments) == 0 {
 		return ""
 	}
diff --git a/pkg/result/processors/autogenerated_exclude_test.go b/pkg/result/processors/autogenerated_exclude_test.go
index a8820dcb..e0ca2364 100644
--- a/pkg/result/processors/autogenerated_exclude_test.go
+++ b/pkg/result/processors/autogenerated_exclude_test.go
@@ -1,13 +1,9 @@
 package processors
 
 import (
-	"go/token"
-	"path/filepath"
 	"strings"
 	"testing"
 
-	"github.com/golangci/golangci-lint/pkg/lint/astcache"
-	"github.com/golangci/golangci-lint/pkg/result"
 	"github.com/stretchr/testify/assert"
 )
 
@@ -66,23 +62,3 @@ func TestIsAutogeneratedDetection(t *testing.T) {
 		assert.False(t, isGenerated)
 	}
 }
-
-func TestNoIssuesInAutogeneratedFiles(t *testing.T) {
-	files := []string{
-		"autogenerated.go",
-		"autogenerated_alt_hdr.go",
-		"autogenerated_alt_hdr2.go",
-	}
-	for _, file := range files {
-		t.Run(file, func(t *testing.T) {
-			i := result.Issue{
-				Pos: token.Position{
-					Filename: filepath.Join("testdata", file),
-					Line:     4,
-				},
-			}
-			p := NewAutogeneratedExclude(astcache.NewCache())
-			processAssertEmpty(t, p, i)
-		})
-	}
-}
diff --git a/pkg/result/processors/testdata/autogenerated.go b/test/testdata/autogenerated/autogenerated.go
similarity index 75%
rename from pkg/result/processors/testdata/autogenerated.go
rename to test/testdata/autogenerated/autogenerated.go
index d4878924..bbf25c84 100644
--- a/pkg/result/processors/testdata/autogenerated.go
+++ b/test/testdata/autogenerated/autogenerated.go
@@ -1,4 +1,4 @@
 // Code generated by ... DO NOT EDIT.
-package testdata
+package p
 
 var vvv int
diff --git a/test/testdata/autogenerated/gen.go b/test/testdata/autogenerated/do_not_edit.go
similarity index 100%
rename from test/testdata/autogenerated/gen.go
rename to test/testdata/autogenerated/do_not_edit.go
diff --git a/pkg/result/processors/testdata/autogenerated_alt_hdr2.go b/test/testdata/autogenerated/go_bindata.go
similarity index 84%
rename from pkg/result/processors/testdata/autogenerated_alt_hdr2.go
rename to test/testdata/autogenerated/go_bindata.go
index 8739d8da..164e7ba1 100644
--- a/pkg/result/processors/testdata/autogenerated_alt_hdr2.go
+++ b/test/testdata/autogenerated/go_bindata.go
@@ -3,6 +3,6 @@
 // bar.baz
 // x/y.z
 // DO NOT EDIT!
-package testdata
+package p
 
 var vv int
diff --git a/test/testdata/autogenerated/mockgen.go b/test/testdata/autogenerated/mockgen.go
index 0d02e1d3..6e147bd6 100644
--- a/test/testdata/autogenerated/mockgen.go
+++ b/test/testdata/autogenerated/mockgen.go
@@ -3,4 +3,4 @@
 
 package p
 
-func unusedFunc2() {}
+func UnusedFunc2() {}
diff --git a/pkg/result/processors/testdata/autogenerated_alt_hdr.go b/test/testdata/autogenerated/protoc_gen_foo.go
similarity index 83%
rename from pkg/result/processors/testdata/autogenerated_alt_hdr.go
rename to test/testdata/autogenerated/protoc_gen_foo.go
index a88b8049..f772fabb 100644
--- a/pkg/result/processors/testdata/autogenerated_alt_hdr.go
+++ b/test/testdata/autogenerated/protoc_gen_foo.go
@@ -1,6 +1,6 @@
 // Code generated by protoc-gen-foo
 // source: bar.proto
 // DO NOT EDIT!!!
-package testdata
+package p
 
 var v int