-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcompilationdb.go
177 lines (156 loc) · 4.81 KB
/
compilationdb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package clang
// #include <stdlib.h>
// #include "go-clang.h"
// #include "clang-c/CXCompilationDatabase.h"
//
import "C"
import (
"fmt"
"unsafe"
)
/**
* A compilation database holds all information used to compile files in a
* project. For each file in the database, it can be queried for the working
* directory or the command line used for the compiler invocation.
*
* Must be freed by \c clang_CompilationDatabase_dispose
*/
type CompilationDatabase struct {
c C.CXCompilationDatabase
}
/**
* \brief Contains the results of a search in the compilation database
*
* When searching for the compile command for a file, the compilation db can
* return several commands, as the file may have been compiled with
* different options in different places of the project. This choice of compile
* commands is wrapped in this opaque data structure. It must be freed by
* \c clang_CompileCommands_dispose.
*/
type CompileCommands struct {
c C.CXCompileCommands
}
/**
* \brief Represents the command line invocation to compile a specific file.
*/
type CompileCommand struct {
c C.CXCompileCommand
}
/**
* \brief Error codes for Compilation Database
*/
type CompilationDatabaseError int
func (err CompilationDatabaseError) Error() string {
switch err {
case CompilationDatabase_CanNotLoadDatabase:
return "go-clang: can not load database"
default:
return fmt.Sprintf("go-clang: unknown compilationdatabase error (%d)", int(err))
}
}
const (
/*
* \brief No error occurred
*/
CompilationDatabase_NoError CompilationDatabaseError = C.CXCompilationDatabase_NoError
/*
* \brief Database can not be loaded
*/
CompilationDatabase_CanNotLoadDatabase = C.CXCompilationDatabase_CanNotLoadDatabase
)
/**
* \brief Creates a compilation database from the database found in directory
* buildDir. For example, CMake can output a compile_commands.json which can
* be used to build the database.
*
* It must be freed by \c clang_CompilationDatabase_dispose.
*/
func NewCompilationDatabase(builddir string) (CompilationDatabase, error) {
var db CompilationDatabase
c_dir := C.CString(builddir)
defer C.free(unsafe.Pointer(c_dir))
var c_err C.CXCompilationDatabase_Error
c_db := C.clang_CompilationDatabase_fromDirectory(c_dir, &c_err)
if c_err == C.CXCompilationDatabase_NoError {
return CompilationDatabase{c_db}, nil
}
return db, CompilationDatabaseError(c_err)
}
/**
* \brief Free the given compilation database
*/
func (db *CompilationDatabase) Dispose() {
C.clang_CompilationDatabase_dispose(db.c)
}
/**
* \brief Find the compile commands used for a file. The compile commands
* must be freed by \c clang_CompileCommands_dispose.
*/
func (db *CompilationDatabase) GetCompileCommands(fname string) CompileCommands {
c_fname := C.CString(fname)
defer C.free(unsafe.Pointer(c_fname))
c_cmds := C.clang_CompilationDatabase_getCompileCommands(db.c, c_fname)
return CompileCommands{c_cmds}
}
/**
* \brief Get all the compile commands in the given compilation database.
*/
func (db *CompilationDatabase) GetAllCompileCommands() CompileCommands {
c_cmds := C.clang_CompilationDatabase_getAllCompileCommands(db.c)
return CompileCommands{c_cmds}
}
/**
* \brief Get the number of CompileCommand we have for a file
*/
func (cmds CompileCommands) GetSize() int {
return int(C.clang_CompileCommands_getSize(cmds.c))
}
/**
* \brief Get the I'th CompileCommand for a file
*
* Note : 0 <= i < clang_CompileCommands_getSize(CXCompileCommands)
*/
func (cmds CompileCommands) GetCommand(idx int) CompileCommand {
c_cmd := C.clang_CompileCommands_getCommand(cmds.c, C.unsigned(idx))
return CompileCommand{c_cmd}
}
/**
* \brief Get the working directory where the CompileCommand was executed from
*/
func (cmd CompileCommand) GetDirectory() string {
c_str := cxstring{C.clang_CompileCommand_getDirectory(cmd.c)}
defer c_str.Dispose()
return c_str.String()
}
/**
* \brief Get the number of arguments in the compiler invocation.
*
*/
func (cmd CompileCommand) GetNumArgs() int {
return int(C.clang_CompileCommand_getNumArgs(cmd.c))
}
/**
* \brief Get the I'th argument value in the compiler invocations
*
* Invariant :
* - argument 0 is the compiler executable
*/
func (cmd CompileCommand) GetArg(idx int) string {
c_str := cxstring{C.clang_CompileCommand_getArg(cmd.c, C.unsigned(idx))}
defer c_str.Dispose()
return c_str.String()
}
// /**
// * \brief Get the number of source mappings for the compiler invocation.
// */
// func (cmd CompileCommand) GetNumMappedSources() int {
// return int(C.clang_CompileCommand_getNumMappedSources(cmd.c))
// }
// /**
// * \brief Get the I'th mapped source path for the compiler invocation.
// */
// func (cmd CompileCommand) GetMappedSourcePath(idx int) string {
// c_str := cxstring{C.clang_CompileCommand_getMappedSourcePath(cmd.c, C.unsigned(idx))}
// defer c_str.Dispose()
// return c_str.String()
// }