-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a18f404
commit 209584a
Showing
13 changed files
with
150 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,8 +11,6 @@ RUN CGO_ENABLED=0 GOOS=linux go build -o /reviewbot . | |
# install staticcheck lint tools | ||
RUN GOPATH=/go go install honnef.co/go/tools/cmd/[email protected] | ||
|
||
RUN apt-get update && apt-get install -y cppcheck | ||
|
||
FROM aslan-spock-register.qiniu.io/library/ubuntu:22.04 as runner | ||
|
||
RUN apt-get update && apt-get install -y ca-certificates \ | ||
|
@@ -26,6 +24,8 @@ RUN apt-get update && apt-get install -y luarocks \ | |
&& luarocks install luacheck \ | ||
&& rm -rf /var/lib/apt/lists/* | ||
|
||
# install cppcheck lint tools | ||
RUN apt-get update && apt-get install -y cppcheck | ||
|
||
# 设置golang环境 | ||
ENV GOLANG_DOWNLOAD_URL https://go.dev/dl/go1.21.5.linux-amd64.tar.gz | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# cppcheck 语法: | ||
cppcheck [选项] [文件或路径]<br /> | ||
如果给出目录而不是文件名,则从给定的目录递归检查 *.cpp、*.cxx、*.cc、*.c++、*.c、*.ipp、*.ixx、*.tpp 和 *.txx 文件。<br /> | ||
Options:<br /> | ||
-q, --quiet 不显示进度报告。<br /> | ||
--template='<text>' 设置错误消息的格式。 可用字段: | ||
  {file}: file name<br /> | ||
  {line}: line number<br /> | ||
  {column}: column number<br /> | ||
  {message}: warning message<br /> | ||
|
||
Example usage:<br /> | ||
递归检查当前目录,不打印进度,默认启用错误检查,输出格式,文件:行:列: 报错信息<br /> | ||
cppcheck --quiet --template='{file}:{line}:{column}: {message}' ./<br /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,40 @@ | ||
package cppcheck | ||
|
||
import ( | ||
"bytes" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/google/go-github/v57/github" | ||
"github.com/qiniu/x/log" | ||
"github.com/qiniu/reviewbot/internal/linters" | ||
"github.com/qiniu/x/xlog" | ||
"github.com/reviewbot/config" | ||
"github.com/reviewbot/internal/linters" | ||
) | ||
|
||
var lintName = "cppcheck" | ||
// refer to https://cppcheck.sourceforge.io/ | ||
var linterName = "cppcheck" | ||
|
||
func init() { | ||
linters.RegisterCodeReviewHandler(lintName, cppcheckHandler) | ||
linters.RegisterPullRequestHandler(linterName, cppcheckHandler) | ||
} | ||
|
||
func cppcheckHandler(log *xlog.Logger, linterConfig config.Linter, agent linters.Agent, event github.PullRequestEvent) (map[string][]linters.LinterOutput, error) { | ||
executor, err := NewCppcheckExecutor(linterConfig.WorkDir) | ||
if err != nil { | ||
log.Errorf("init cppcheck executor failed: %v", err) | ||
return nil, err | ||
} | ||
|
||
if linters.IsEmpty(linterConfig.Args...) { | ||
linterConfig.Args = append([]string{}, "--quiet", "--template='{file}:{line}:{column}: {message}'", "./") | ||
} | ||
func cppcheckHandler(log *xlog.Logger, a linters.Agent) error { | ||
|
||
output, err := executor.Run(log, linterConfig.Args...) | ||
if err != nil { | ||
log.Errorf("cppcheck run failed: %v", err) | ||
return nil, err | ||
} | ||
|
||
parsedOutput, err := executor.Parse(log, output) | ||
if err != nil { | ||
log.Errorf("cppcheck parse output failed: %v", err) | ||
return nil, err | ||
if linters.IsEmpty(a.LinterConfig.Args...) { | ||
a.LinterConfig.Args = append([]string{}, "--quiet", "--template='{file}:{line}:{column}: {message}'", "./") | ||
} | ||
|
||
return parsedOutput, nil | ||
} | ||
|
||
// Cppcheck is an executor that knows how to execute Cppcheck commands. | ||
type Cppcheck struct { | ||
// dir is the location of this repo. | ||
dir string | ||
// cppcheck is the path to the cppcheck binary. | ||
cppcheck string | ||
return linters.GeneralHandler(log, a, func(l *xlog.Logger, output []byte) (map[string][]linters.LinterOutput, error) { | ||
return linters.Parse(log, output, cppcheckParser) | ||
}) | ||
} | ||
|
||
// NewCppcheckExecutor returns a new executor that knows how to execute cppcheck commands | ||
// TODO: with config | ||
func NewCppcheckExecutor(dir string) (linters.Linter, error) { | ||
log.Infof("cppcheck executor init") | ||
g, err := exec.LookPath("cppcheck") | ||
func cppcheckParser(line string) (*linters.LinterOutput, error) { | ||
lineResult, err := linters.GeneralLineParser(line) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Cppcheck{ | ||
dir: dir, | ||
cppcheck: g, | ||
}, nil | ||
} | ||
|
||
func (e *Cppcheck) Run(log *xlog.Logger, args ...string) ([]byte, error) { | ||
c := exec.Command(e.cppcheck, args...) | ||
c.Dir = e.dir | ||
var out bytes.Buffer | ||
c.Stdout = &out | ||
c.Stderr = &out | ||
err := c.Run() | ||
if err != nil { | ||
log.Errorf("cppcheck run with status: %v, mark and continue", err) | ||
} else { | ||
log.Infof("cppcheck succeeded") | ||
} | ||
return out.Bytes(), nil | ||
} | ||
|
||
func (e *Cppcheck) Parse(log *xlog.Logger, output []byte) (map[string][]linters.LinterOutput, error) { | ||
return linters.FormatLinterOutput(log, output, linters.FormatLinterLine) | ||
return &linters.LinterOutput{ | ||
File: strings.TrimLeft(lineResult.File, "'"), | ||
Line: lineResult.Line, | ||
Column: lineResult.Column, | ||
Message: strings.TrimRight(lineResult.Message, "'"), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cppcheck | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/qiniu/reviewbot/internal/linters" | ||
) | ||
|
||
func TestFormatCppCheckLine(t *testing.T) { | ||
tc := []struct { | ||
input string | ||
expected *linters.LinterOutput | ||
}{ | ||
{"'cppcheck_test.c:6:7: Array 'a[10]' accessed at index 10, which is out of bounds.'", &linters.LinterOutput{ | ||
File: "cppcheck_test.c", | ||
Line: 6, | ||
Column: 7, | ||
Message: "Array 'a[10]' accessed at index 10, which is out of bounds.", | ||
}}, | ||
{"'fdk-aac-0.1.4-libMpegTPDec-src/tpdec_asc.cpp:1198:23: Variable 'bitsAvailable' is reassigned a value before the old one has been used.'", &linters.LinterOutput{ | ||
File: "fdk-aac-0.1.4-libMpegTPDec-src/tpdec_asc.cpp", | ||
Line: 1198, | ||
Column: 23, | ||
Message: "Variable 'bitsAvailable' is reassigned a value before the old one has been used.", | ||
}}, | ||
{"Checking test/tpdec_adts.c", nil}, | ||
} | ||
|
||
for _, c := range tc { | ||
|
||
output, err := cppcheckParser(c.input) | ||
|
||
if output == nil { | ||
if c.expected != nil { | ||
t.Errorf("expected: %v, got: %v", c.expected, output) | ||
} | ||
continue | ||
} | ||
|
||
if c.expected == nil && output != nil { | ||
t.Errorf("expected error, got: %v", output) | ||
} | ||
|
||
if err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
} | ||
|
||
if output.File != c.expected.File || output.Line != c.expected.Line || output.Column != c.expected.Column || output.Message != c.expected.Message { | ||
t.Errorf("expected: %v, got: %v", c.expected, output) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// +build ignore go | ||
|
||
int main() | ||
{ | ||
char a[10]; | ||
a[10] = 0; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
str := "Hello, world!" | ||
fmt.Printf("%d World", str) | ||
} |
Oops, something went wrong.