@@ -24,26 +24,29 @@ import (
2424 "github.com/arduino/go-paths-helper"
2525)
2626
27- type compilationCommand struct {
27+ // CompilationDatabase keeps track of all the compile commands run by the builder
28+ type CompilationDatabase struct {
29+ contents []CompilationCommand
30+ filename * paths.Path
31+ }
32+
33+ // CompilationCommand keeps track of a single run of a compile command
34+ type CompilationCommand struct {
2835 Directory string `json:"directory"`
2936 Arguments []string `json:"arguments"`
3037 File string `json:"file"`
3138}
3239
33- type CompilationDatabase struct {
34- contents []compilationCommand
35- filename * paths.Path
36- }
37-
40+ // NewCompilationDatabase creates an empty CompilationDatabase
3841func NewCompilationDatabase (filename * paths.Path ) * CompilationDatabase {
3942 return & CompilationDatabase {
4043 filename : filename ,
4144 }
4245}
4346
44- func ( db * CompilationDatabase ) UpdateFile ( complete bool ) {
45- // TODO: Read any existing file and use its contents for any
46- // kept files, or any files not in db.contents if !complete.
47+ // SaveToFile save the CompilationDatabase to file as a clangd-compatible compile_commands.json,
48+ // see https://clang.llvm.org/docs/JSONCompilationDatabase.html
49+ func ( db * CompilationDatabase ) SaveToFile () {
4750 if jsonContents , err := json .MarshalIndent (db .contents , "" , " " ); err != nil {
4851 fmt .Printf ("Error serializing compilation database: %s" , err )
4952 return
@@ -52,31 +55,27 @@ func (db *CompilationDatabase) UpdateFile(complete bool) {
5255 }
5356}
5457
55- func ( db * CompilationDatabase ) dirForCommand (command * exec.Cmd ) string {
58+ func dirForCommand (command * exec.Cmd ) string {
5659 // This mimics what Cmd.Run also does: Use Dir if specified,
5760 // current directory otherwise
5861 if command .Dir != "" {
5962 return command .Dir
60- } else {
61- dir , err := os .Getwd ()
62- if err != nil {
63- fmt .Printf ("Error getting current directory for compilation database: %s" , err )
64- return ""
65- }
66- return dir
6763 }
64+ dir , err := os .Getwd ()
65+ if err != nil {
66+ fmt .Printf ("Error getting current directory for compilation database: %s" , err )
67+ return ""
68+ }
69+ return dir
6870}
6971
70- func (db * CompilationDatabase ) ReplaceEntry (filename * paths.Path , command * exec.Cmd ) {
71- entry := compilationCommand {
72- Directory : db .dirForCommand (command ),
72+ // Add adds a new CompilationDatabase entry
73+ func (db * CompilationDatabase ) Add (target * paths.Path , command * exec.Cmd ) {
74+ entry := CompilationCommand {
75+ Directory : dirForCommand (command ),
7376 Arguments : command .Args ,
74- File : filename .String (),
77+ File : target .String (),
7578 }
7679
7780 db .contents = append (db .contents , entry )
7881}
79-
80- func (db * CompilationDatabase ) KeepEntry (filename * paths.Path ) {
81- // TODO
82- }
0 commit comments