The document discusses creating a static analysis tool called "fmt_search" that checks Go code for imports of the "fmt" package. It describes generating a skeleton for the tool using the "skeleton" library, writing test code, and implementing the analysis logic to search files for "fmt" imports. The tool is tested and the results are shown, demonstrating a simple example of static analysis development.
1 of 34
Download to read offline
More Related Content
歯磨き.go Go言語の静的解析とコード生成勉強会
1. Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載?無断複製の禁止
歯磨き.go
はじめての静的解析
2. 歯磨き.goとは
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
一過性の勉強会ではなく、少しずつ無理のない範囲で
毎日の歯磨きのように続けていくことを目的とした
ゆるふわなgo勉強会です。
(もともと社内でやってたけどスピンオフしました)
3. 歯磨き.goとは
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
?社内で静的解析の勉強会を始めたので
そのアウトプットの場になれば。会が進んできたら、 社内で
作った静的解析ツールを紹介(自慢)する場に
なれば良いなとか
?社内だけだと閉塞感でると思うので社外の方用のLT
枠も用意するのでぜひぜひ登壇したい方大歓迎。
ゆるふわな会なので怖く無いよ。
4. 自己紹介
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載?無断複製の禁止
5. 自己紹介
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
山下 大輔 Daisuke Yamashita
ものづくり系エンジニア?
go言語に限らずものづくりに関わる開発は
全般的に好き。?
?
毎日使う最強のツールを作るのが目標。?
6. 何を作っているか
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
全人類が日々感じる不便さ
7. 何を作っているか
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
全人類が日々感じる不便さ
建築業界 ANDPAD
建築業界特化型ツール
8. 目次
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載?無断複製の禁止
9. 目次
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
?静的解析がなぜ必要か
?特定のパッケージ(fmt)を使っているか抽出しよう!
?レイヤードアーキテクチャでの依存方向の制約間違い?
を指摘する静的解析を作ろう!(プロジェクト独自定義)
10. 静的解析がなぜ必要か
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載?無断複製の禁止
11. 静的解析がなぜ必要か
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
要件定義 実装 QAテスト リリース
12. 静的解析がなぜ必要か
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
要件定義 実装 QAテスト リリース
右の発見ほどコスト増
シフトレフト
13. 静的解析がなぜ必要か
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
要件定義 実装 QAテスト リリース
静的解析が活躍
自動テストが活躍
14. はじめての静的解析
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載?無断複製の禁止
15. 今日作る静的解析のおしながき
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
① 特定のパッケージ(fmt)を使っているか抽出しよう!?
?
②レイヤードアーキテクチャでの依存方向の制約間違い?
を指摘する静的解析を作ろう!(プロジェクト独自定義)?
16. はじめての静的解析
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
特定のパッケージ(fmt)を使っているか抽出しよう!?
17. はじめての静的解析
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
$ go install github.com/gostaticanalysis/skeleton@latest
go 1.16以上
$ go get -u github.com/gostaticanalysis/skeleton
go 1.16未満
https://github.com/gostaticanalysis/skeleton
18. はじめての静的解析の開発手順
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
① 雛形の作成(skeleton実行するだけ)?
② テストデータの作成?
③ テスト実行?
④ Analyzerのロジックを修正?
skeletonを使った実装?
19. はじめての静的解析
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
$ skeleton -path=fmt_search
雛形を生成
20. はじめての静的解析
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
$ skeleton -path=fmt_search
雛形を生成
テストデータとし
ての
コード
開発はこちら
21. はじめての静的解析
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
$ cd fmt_search
$ go mod tidy
$ go test
22. analysis package
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
https://pkg.go.dev/golang.org/x/tools/go/analysis
静的解析をモジュール化するためのライブラリ
雛形生成で自動作成されています。
23. analysis.Analyzer
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
https://pkg.go.dev/golang.org/x/tools/go/analysis#Analyzer
// Analyzer is ...
var Analyzer = &analysis.Analyzer{
Name: "....",
Doc: doc,
Run: run,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
}
静的解析の実行単位
自動生成されており、runで渡している
関数で実際の静的解析のロジックを
作成する。
24. analysis.Pass
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
https://pkg.go.dev/golang.org/x/tools/go/analysis#Pass
func run(pass *analysis.Pass) (interface{}, error) {
// 静的解析のロジックを書く
}
run関数に引数として渡されてくやつ。
ターゲットのソースコードを静的解析するための必要な情報が入ってい
る。
25. analysis.Pass
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
https://pkg.go.dev/golang.org/x/tools/go/analysis#Pass
type Pass struct {
Fset *token.FileSet // file position information
Files []*ast.File // the abstract syntax tree of each file
Pkg *types.Package // type information about the package
Report func(Diagnostic)
...
}
26. テストデータを作成
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
fmt_search/testdata/src/a/a.go
package a
import "fmt" // want "fmt exists"
func f() {
fmt.Println("test")
}
importを検出するだけの
シンプルなもの
27. import fmtを探すコード
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
fmt_search/fmt_search.go
func run(pass *analysis.Pass) (interface{}, error) {
for _, f := range pass.Files{
for _,imp := range f.Imports{
v, err := strconv.Unquote(imp.Path.Value)
if err != nil{
return nil, err
}
if "fmt" == v {
pass.Reportf(imp.Pos(), "%s is imported.", v)
}
}
}
return nil, nil
}
“”の文字列を削除
28. 動作
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
$ go test
https://github.com/daisuke0131/fmt_search
29. プロジェクト独自定義
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
レイヤードアーキテクチャでの依存方向の制約間違い?
を指摘する静的解析を作ろう!?
(プロジェクト独自定義)?
30. レイヤードアーキテクチャ
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
依存の方向
一番外側のレイヤー
repositoryの実態とかhandlerとか
外界に依存しない、ビジネスロジック
(repositoryのIFのみに依存したい)
依存が逆転してしまっているものを検
知したい!
31. レイヤードアーキテクチャ
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
https://github.com/daisuke0131/layer
func run(pass *analysis.Pass) (interface{}, error) {
for _, f := range pass.Files{
dir := pass.Fset.File(f.Pos()).Name()
if strings.Contains(dir,"/usecase/service/"){
// infrastructureの層への依存はしない
for _,imp := range f.Imports{
v, err := strconv.Unquote(imp.Path.Value)
if err != nil{
return nil, err
}
if strings.Contains(v,"/infrastructure/") {
pass.Reportf(imp.Pos(), "%s is invalid import.", v)
}
}
}
}
return nil, nil
}
32. レイヤードアーキテクチャ
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
https://github.com/daisuke0131/layer
$ go test
33. まとめ
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載?無断複製の禁止
34. まとめ
Con?dential
Copyright ? 2020 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. 無断転載?無断複製の禁止
?ANDPADの開発でも自分たちで作った静的解析の
ツールを活用していくぞい!
?簡単な静的解析ツールの作り方を説明しました。
これくらいでも実用的なものを作れそう。