1*e5dd7070Spatrick #include "clang-c/CXCompilationDatabase.h"
2*e5dd7070Spatrick #include "CXString.h"
3*e5dd7070Spatrick #include "clang/Tooling/CompilationDatabase.h"
4*e5dd7070Spatrick #include <cstdio>
5*e5dd7070Spatrick
6*e5dd7070Spatrick using namespace clang;
7*e5dd7070Spatrick using namespace clang::tooling;
8*e5dd7070Spatrick
9*e5dd7070Spatrick // FIXME: do something more useful with the error message
10*e5dd7070Spatrick CXCompilationDatabase
clang_CompilationDatabase_fromDirectory(const char * BuildDir,CXCompilationDatabase_Error * ErrorCode)11*e5dd7070Spatrick clang_CompilationDatabase_fromDirectory(const char *BuildDir,
12*e5dd7070Spatrick CXCompilationDatabase_Error *ErrorCode)
13*e5dd7070Spatrick {
14*e5dd7070Spatrick std::string ErrorMsg;
15*e5dd7070Spatrick CXCompilationDatabase_Error Err = CXCompilationDatabase_NoError;
16*e5dd7070Spatrick
17*e5dd7070Spatrick std::unique_ptr<CompilationDatabase> db =
18*e5dd7070Spatrick CompilationDatabase::loadFromDirectory(BuildDir, ErrorMsg);
19*e5dd7070Spatrick
20*e5dd7070Spatrick if (!db) {
21*e5dd7070Spatrick fprintf(stderr, "LIBCLANG TOOLING ERROR: %s\n", ErrorMsg.c_str());
22*e5dd7070Spatrick Err = CXCompilationDatabase_CanNotLoadDatabase;
23*e5dd7070Spatrick }
24*e5dd7070Spatrick
25*e5dd7070Spatrick if (ErrorCode)
26*e5dd7070Spatrick *ErrorCode = Err;
27*e5dd7070Spatrick
28*e5dd7070Spatrick return db.release();
29*e5dd7070Spatrick }
30*e5dd7070Spatrick
31*e5dd7070Spatrick void
clang_CompilationDatabase_dispose(CXCompilationDatabase CDb)32*e5dd7070Spatrick clang_CompilationDatabase_dispose(CXCompilationDatabase CDb)
33*e5dd7070Spatrick {
34*e5dd7070Spatrick delete static_cast<CompilationDatabase *>(CDb);
35*e5dd7070Spatrick }
36*e5dd7070Spatrick
37*e5dd7070Spatrick struct AllocatedCXCompileCommands
38*e5dd7070Spatrick {
39*e5dd7070Spatrick std::vector<CompileCommand> CCmd;
40*e5dd7070Spatrick
AllocatedCXCompileCommandsAllocatedCXCompileCommands41*e5dd7070Spatrick AllocatedCXCompileCommands(std::vector<CompileCommand> Cmd)
42*e5dd7070Spatrick : CCmd(std::move(Cmd)) {}
43*e5dd7070Spatrick };
44*e5dd7070Spatrick
45*e5dd7070Spatrick CXCompileCommands
clang_CompilationDatabase_getCompileCommands(CXCompilationDatabase CDb,const char * CompleteFileName)46*e5dd7070Spatrick clang_CompilationDatabase_getCompileCommands(CXCompilationDatabase CDb,
47*e5dd7070Spatrick const char *CompleteFileName)
48*e5dd7070Spatrick {
49*e5dd7070Spatrick if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
50*e5dd7070Spatrick std::vector<CompileCommand> CCmd(db->getCompileCommands(CompleteFileName));
51*e5dd7070Spatrick if (!CCmd.empty())
52*e5dd7070Spatrick return new AllocatedCXCompileCommands(std::move(CCmd));
53*e5dd7070Spatrick }
54*e5dd7070Spatrick
55*e5dd7070Spatrick return nullptr;
56*e5dd7070Spatrick }
57*e5dd7070Spatrick
58*e5dd7070Spatrick CXCompileCommands
clang_CompilationDatabase_getAllCompileCommands(CXCompilationDatabase CDb)59*e5dd7070Spatrick clang_CompilationDatabase_getAllCompileCommands(CXCompilationDatabase CDb) {
60*e5dd7070Spatrick if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
61*e5dd7070Spatrick std::vector<CompileCommand> CCmd(db->getAllCompileCommands());
62*e5dd7070Spatrick if (!CCmd.empty())
63*e5dd7070Spatrick return new AllocatedCXCompileCommands(std::move(CCmd));
64*e5dd7070Spatrick }
65*e5dd7070Spatrick
66*e5dd7070Spatrick return nullptr;
67*e5dd7070Spatrick }
68*e5dd7070Spatrick
69*e5dd7070Spatrick void
clang_CompileCommands_dispose(CXCompileCommands Cmds)70*e5dd7070Spatrick clang_CompileCommands_dispose(CXCompileCommands Cmds)
71*e5dd7070Spatrick {
72*e5dd7070Spatrick delete static_cast<AllocatedCXCompileCommands *>(Cmds);
73*e5dd7070Spatrick }
74*e5dd7070Spatrick
75*e5dd7070Spatrick unsigned
clang_CompileCommands_getSize(CXCompileCommands Cmds)76*e5dd7070Spatrick clang_CompileCommands_getSize(CXCompileCommands Cmds)
77*e5dd7070Spatrick {
78*e5dd7070Spatrick if (!Cmds)
79*e5dd7070Spatrick return 0;
80*e5dd7070Spatrick
81*e5dd7070Spatrick AllocatedCXCompileCommands *ACC =
82*e5dd7070Spatrick static_cast<AllocatedCXCompileCommands *>(Cmds);
83*e5dd7070Spatrick
84*e5dd7070Spatrick return ACC->CCmd.size();
85*e5dd7070Spatrick }
86*e5dd7070Spatrick
87*e5dd7070Spatrick CXCompileCommand
clang_CompileCommands_getCommand(CXCompileCommands Cmds,unsigned I)88*e5dd7070Spatrick clang_CompileCommands_getCommand(CXCompileCommands Cmds, unsigned I)
89*e5dd7070Spatrick {
90*e5dd7070Spatrick if (!Cmds)
91*e5dd7070Spatrick return nullptr;
92*e5dd7070Spatrick
93*e5dd7070Spatrick AllocatedCXCompileCommands *ACC =
94*e5dd7070Spatrick static_cast<AllocatedCXCompileCommands *>(Cmds);
95*e5dd7070Spatrick
96*e5dd7070Spatrick if (I >= ACC->CCmd.size())
97*e5dd7070Spatrick return nullptr;
98*e5dd7070Spatrick
99*e5dd7070Spatrick return &ACC->CCmd[I];
100*e5dd7070Spatrick }
101*e5dd7070Spatrick
102*e5dd7070Spatrick CXString
clang_CompileCommand_getDirectory(CXCompileCommand CCmd)103*e5dd7070Spatrick clang_CompileCommand_getDirectory(CXCompileCommand CCmd)
104*e5dd7070Spatrick {
105*e5dd7070Spatrick if (!CCmd)
106*e5dd7070Spatrick return cxstring::createNull();
107*e5dd7070Spatrick
108*e5dd7070Spatrick CompileCommand *cmd = static_cast<CompileCommand *>(CCmd);
109*e5dd7070Spatrick return cxstring::createRef(cmd->Directory.c_str());
110*e5dd7070Spatrick }
111*e5dd7070Spatrick
112*e5dd7070Spatrick CXString
clang_CompileCommand_getFilename(CXCompileCommand CCmd)113*e5dd7070Spatrick clang_CompileCommand_getFilename(CXCompileCommand CCmd)
114*e5dd7070Spatrick {
115*e5dd7070Spatrick if (!CCmd)
116*e5dd7070Spatrick return cxstring::createNull();
117*e5dd7070Spatrick
118*e5dd7070Spatrick CompileCommand *cmd = static_cast<CompileCommand *>(CCmd);
119*e5dd7070Spatrick return cxstring::createRef(cmd->Filename.c_str());
120*e5dd7070Spatrick }
121*e5dd7070Spatrick
122*e5dd7070Spatrick unsigned
clang_CompileCommand_getNumArgs(CXCompileCommand CCmd)123*e5dd7070Spatrick clang_CompileCommand_getNumArgs(CXCompileCommand CCmd)
124*e5dd7070Spatrick {
125*e5dd7070Spatrick if (!CCmd)
126*e5dd7070Spatrick return 0;
127*e5dd7070Spatrick
128*e5dd7070Spatrick return static_cast<CompileCommand *>(CCmd)->CommandLine.size();
129*e5dd7070Spatrick }
130*e5dd7070Spatrick
131*e5dd7070Spatrick CXString
clang_CompileCommand_getArg(CXCompileCommand CCmd,unsigned Arg)132*e5dd7070Spatrick clang_CompileCommand_getArg(CXCompileCommand CCmd, unsigned Arg)
133*e5dd7070Spatrick {
134*e5dd7070Spatrick if (!CCmd)
135*e5dd7070Spatrick return cxstring::createNull();
136*e5dd7070Spatrick
137*e5dd7070Spatrick CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd);
138*e5dd7070Spatrick
139*e5dd7070Spatrick if (Arg >= Cmd->CommandLine.size())
140*e5dd7070Spatrick return cxstring::createNull();
141*e5dd7070Spatrick
142*e5dd7070Spatrick return cxstring::createRef(Cmd->CommandLine[Arg].c_str());
143*e5dd7070Spatrick }
144*e5dd7070Spatrick
145*e5dd7070Spatrick unsigned
clang_CompileCommand_getNumMappedSources(CXCompileCommand CCmd)146*e5dd7070Spatrick clang_CompileCommand_getNumMappedSources(CXCompileCommand CCmd)
147*e5dd7070Spatrick {
148*e5dd7070Spatrick // Left here for backward compatibility. No mapped sources exists in the C++
149*e5dd7070Spatrick // backend anymore.
150*e5dd7070Spatrick return 0;
151*e5dd7070Spatrick }
152*e5dd7070Spatrick
153*e5dd7070Spatrick CXString
clang_CompileCommand_getMappedSourcePath(CXCompileCommand CCmd,unsigned I)154*e5dd7070Spatrick clang_CompileCommand_getMappedSourcePath(CXCompileCommand CCmd, unsigned I)
155*e5dd7070Spatrick {
156*e5dd7070Spatrick // Left here for backward compatibility. No mapped sources exists in the C++
157*e5dd7070Spatrick // backend anymore.
158*e5dd7070Spatrick return cxstring::createNull();
159*e5dd7070Spatrick }
160*e5dd7070Spatrick
161*e5dd7070Spatrick CXString
clang_CompileCommand_getMappedSourceContent(CXCompileCommand CCmd,unsigned I)162*e5dd7070Spatrick clang_CompileCommand_getMappedSourceContent(CXCompileCommand CCmd, unsigned I)
163*e5dd7070Spatrick {
164*e5dd7070Spatrick // Left here for backward compatibility. No mapped sources exists in the C++
165*e5dd7070Spatrick // backend anymore.
166*e5dd7070Spatrick return cxstring::createNull();
167*e5dd7070Spatrick }
168