blob: 1e4a2cd44aabbb29a3fa3664957ae316ce229b8c [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"
Dmitri Gribenko9af20d82013-08-19 16:14:33 +00004#include <cstdio>
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00005
6using namespace clang;
7using namespace clang::tooling;
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00008
9extern "C" {
10
Stephen Hines651f13c2014-04-23 16:59:28 -070011// FIXME: do something more useful with the error message
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000012CXCompilationDatabase
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +000013clang_CompilationDatabase_fromDirectory(const char *BuildDir,
14 CXCompilationDatabase_Error *ErrorCode)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000015{
16 std::string ErrorMsg;
17 CXCompilationDatabase_Error Err = CXCompilationDatabase_NoError;
18
Stephen Hines176edba2014-12-01 14:53:08 -080019 std::unique_ptr<CompilationDatabase> db =
20 CompilationDatabase::loadFromDirectory(BuildDir, ErrorMsg);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000021
22 if (!db) {
23 fprintf(stderr, "LIBCLANG TOOLING ERROR: %s\n", ErrorMsg.c_str());
24 Err = CXCompilationDatabase_CanNotLoadDatabase;
25 }
26
27 if (ErrorCode)
28 *ErrorCode = Err;
29
Stephen Hines176edba2014-12-01 14:53:08 -080030 return db.release();
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000031}
32
33void
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +000034clang_CompilationDatabase_dispose(CXCompilationDatabase CDb)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000035{
36 delete static_cast<CompilationDatabase *>(CDb);
37}
38
39struct AllocatedCXCompileCommands
40{
41 std::vector<CompileCommand> CCmd;
42
Stephen Hines651f13c2014-04-23 16:59:28 -070043 AllocatedCXCompileCommands(std::vector<CompileCommand> Cmd)
44 : CCmd(std::move(Cmd)) {}
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000045};
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)) {
Stephen Hines651f13c2014-04-23 16:59:28 -070052 std::vector<CompileCommand> CCmd(db->getCompileCommands(CompleteFileName));
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000053 if (!CCmd.empty())
Stephen Hines651f13c2014-04-23 16:59:28 -070054 return new AllocatedCXCompileCommands(std::move(CCmd));
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000055 }
56
Stephen Hinesc568f1e2014-07-21 00:47:37 -070057 return nullptr;
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000058}
59
Argyrios Kyrtzidis7e96bfb2012-12-04 07:26:44 +000060CXCompileCommands
61clang_CompilationDatabase_getAllCompileCommands(CXCompilationDatabase CDb) {
62 if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
Stephen Hines651f13c2014-04-23 16:59:28 -070063 std::vector<CompileCommand> CCmd(db->getAllCompileCommands());
Argyrios Kyrtzidis7e96bfb2012-12-04 07:26:44 +000064 if (!CCmd.empty())
Stephen Hines651f13c2014-04-23 16:59:28 -070065 return new AllocatedCXCompileCommands(std::move(CCmd));
Argyrios Kyrtzidis7e96bfb2012-12-04 07:26:44 +000066 }
67
Stephen Hinesc568f1e2014-07-21 00:47:37 -070068 return nullptr;
Argyrios Kyrtzidis7e96bfb2012-12-04 07:26:44 +000069}
70
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000071void
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +000072clang_CompileCommands_dispose(CXCompileCommands Cmds)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000073{
74 delete static_cast<AllocatedCXCompileCommands *>(Cmds);
75}
76
77unsigned
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +000078clang_CompileCommands_getSize(CXCompileCommands Cmds)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000079{
80 if (!Cmds)
81 return 0;
82
83 AllocatedCXCompileCommands *ACC =
84 static_cast<AllocatedCXCompileCommands *>(Cmds);
85
86 return ACC->CCmd.size();
87}
88
89CXCompileCommand
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +000090clang_CompileCommands_getCommand(CXCompileCommands Cmds, unsigned I)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000091{
92 if (!Cmds)
Stephen Hinesc568f1e2014-07-21 00:47:37 -070093 return nullptr;
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000094
95 AllocatedCXCompileCommands *ACC =
96 static_cast<AllocatedCXCompileCommands *>(Cmds);
97
98 if (I >= ACC->CCmd.size())
Stephen Hinesc568f1e2014-07-21 00:47:37 -070099 return nullptr;
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000100
101 return &ACC->CCmd[I];
102}
103
104CXString
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +0000105clang_CompileCommand_getDirectory(CXCompileCommand CCmd)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000106{
107 if (!CCmd)
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +0000108 return cxstring::createNull();
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000109
110 CompileCommand *cmd = static_cast<CompileCommand *>(CCmd);
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +0000111 return cxstring::createRef(cmd->Directory.c_str());
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000112}
113
114unsigned
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +0000115clang_CompileCommand_getNumArgs(CXCompileCommand CCmd)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000116{
117 if (!CCmd)
118 return 0;
119
120 return static_cast<CompileCommand *>(CCmd)->CommandLine.size();
121}
122
123CXString
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +0000124clang_CompileCommand_getArg(CXCompileCommand CCmd, unsigned Arg)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000125{
126 if (!CCmd)
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +0000127 return cxstring::createNull();
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000128
129 CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd);
130
131 if (Arg >= Cmd->CommandLine.size())
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +0000132 return cxstring::createNull();
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000133
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +0000134 return cxstring::createRef(Cmd->CommandLine[Arg].c_str());
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000135}
136
Manuel Klimekf88536a2013-11-13 13:23:27 +0000137unsigned
138clang_CompileCommand_getNumMappedSources(CXCompileCommand CCmd)
139{
140 if (!CCmd)
141 return 0;
142
143 return static_cast<CompileCommand *>(CCmd)->MappedSources.size();
144}
145
146CXString
147clang_CompileCommand_getMappedSourcePath(CXCompileCommand CCmd, unsigned I)
148{
149 if (!CCmd)
150 return cxstring::createNull();
151
152 CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd);
153
154 if (I >= Cmd->MappedSources.size())
155 return cxstring::createNull();
156
157 return cxstring::createRef(Cmd->MappedSources[I].first.c_str());
158}
159
160CXString
161clang_CompileCommand_getMappedSourceContent(CXCompileCommand CCmd, unsigned I)
162{
163 if (!CCmd)
164 return cxstring::createNull();
165
166 CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd);
167
168 if (I >= Cmd->MappedSources.size())
169 return cxstring::createNull();
170
171 return cxstring::createRef(Cmd->MappedSources[I].second.c_str());
172}
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000173
174} // end: extern "C"