blob: e35ac27f79402fab7ec416a7886167da3ddfcc06 [file] [log] [blame]
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00001#include "clang-c/CXCompilationDatabase.h"
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002#include "CXString.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +00003#include "clang/Tooling/CompilationDatabase.h"
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00004
5using namespace clang;
6using namespace clang::tooling;
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00007
8extern "C" {
9
10// FIXME: do something more usefull with the error message
11CXCompilationDatabase
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +000012clang_CompilationDatabase_fromDirectory(const char *BuildDir,
13 CXCompilationDatabase_Error *ErrorCode)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000014{
15 std::string ErrorMsg;
16 CXCompilationDatabase_Error Err = CXCompilationDatabase_NoError;
17
18 CompilationDatabase *db = CompilationDatabase::loadFromDirectory(BuildDir,
19 ErrorMsg);
20
21 if (!db) {
22 fprintf(stderr, "LIBCLANG TOOLING ERROR: %s\n", ErrorMsg.c_str());
23 Err = CXCompilationDatabase_CanNotLoadDatabase;
24 }
25
26 if (ErrorCode)
27 *ErrorCode = Err;
28
29 return db;
30}
31
32void
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +000033clang_CompilationDatabase_dispose(CXCompilationDatabase CDb)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000034{
35 delete static_cast<CompilationDatabase *>(CDb);
36}
37
38struct AllocatedCXCompileCommands
39{
40 std::vector<CompileCommand> CCmd;
41
42 AllocatedCXCompileCommands(const std::vector<CompileCommand>& Cmd)
43 : CCmd(Cmd)
44 { }
45};
46
47CXCompileCommands
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +000048clang_CompilationDatabase_getCompileCommands(CXCompilationDatabase CDb,
49 const char *CompleteFileName)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000050{
51 if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
52 const std::vector<CompileCommand>
53 CCmd(db->getCompileCommands(CompleteFileName));
54 if (!CCmd.empty())
55 return new AllocatedCXCompileCommands( CCmd );
56 }
57
58 return 0;
59}
60
Argyrios Kyrtzidis7e96bfb2012-12-04 07:26:44 +000061CXCompileCommands
62clang_CompilationDatabase_getAllCompileCommands(CXCompilationDatabase CDb) {
63 if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
64 const std::vector<CompileCommand> CCmd(db->getAllCompileCommands());
65 if (!CCmd.empty())
66 return new AllocatedCXCompileCommands( CCmd );
67 }
68
69 return 0;
70}
71
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000072void
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +000073clang_CompileCommands_dispose(CXCompileCommands Cmds)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000074{
75 delete static_cast<AllocatedCXCompileCommands *>(Cmds);
76}
77
78unsigned
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +000079clang_CompileCommands_getSize(CXCompileCommands Cmds)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000080{
81 if (!Cmds)
82 return 0;
83
84 AllocatedCXCompileCommands *ACC =
85 static_cast<AllocatedCXCompileCommands *>(Cmds);
86
87 return ACC->CCmd.size();
88}
89
90CXCompileCommand
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +000091clang_CompileCommands_getCommand(CXCompileCommands Cmds, unsigned I)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000092{
93 if (!Cmds)
94 return 0;
95
96 AllocatedCXCompileCommands *ACC =
97 static_cast<AllocatedCXCompileCommands *>(Cmds);
98
99 if (I >= ACC->CCmd.size())
100 return 0;
101
102 return &ACC->CCmd[I];
103}
104
105CXString
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +0000106clang_CompileCommand_getDirectory(CXCompileCommand CCmd)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000107{
108 if (!CCmd)
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +0000109 return cxstring::createNull();
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000110
111 CompileCommand *cmd = static_cast<CompileCommand *>(CCmd);
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +0000112 return cxstring::createRef(cmd->Directory.c_str());
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000113}
114
115unsigned
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +0000116clang_CompileCommand_getNumArgs(CXCompileCommand CCmd)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000117{
118 if (!CCmd)
119 return 0;
120
121 return static_cast<CompileCommand *>(CCmd)->CommandLine.size();
122}
123
124CXString
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +0000125clang_CompileCommand_getArg(CXCompileCommand CCmd, unsigned Arg)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000126{
127 if (!CCmd)
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +0000128 return cxstring::createNull();
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000129
130 CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd);
131
132 if (Arg >= Cmd->CommandLine.size())
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +0000133 return cxstring::createNull();
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000134
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +0000135 return cxstring::createRef(Cmd->CommandLine[Arg].c_str());
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000136}
137
138
139} // end: extern "C"