blob: 2ca532659d3786b7bdbef9b611c0465c76294225 [file] [log] [blame]
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00001#include "clang-c/CXCompilationDatabase.h"
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00002#include "CXString.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +00003#include "clang/Tooling/CompilationDatabase.h"
Dmitri Gribenkoab458a12013-08-19 16:14:33 +00004#include <cstdio>
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00005
6using namespace clang;
7using namespace clang::tooling;
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00008
Alp Tokerf6a24ce2013-12-05 16:25:25 +00009// FIXME: do something more useful with the error message
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000010CXCompilationDatabase
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +000011clang_CompilationDatabase_fromDirectory(const char *BuildDir,
12 CXCompilationDatabase_Error *ErrorCode)
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000013{
14 std::string ErrorMsg;
15 CXCompilationDatabase_Error Err = CXCompilationDatabase_NoError;
16
David Blaikiecdba84c2014-08-08 16:06:15 +000017 std::unique_ptr<CompilationDatabase> db =
18 CompilationDatabase::loadFromDirectory(BuildDir, ErrorMsg);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000019
20 if (!db) {
21 fprintf(stderr, "LIBCLANG TOOLING ERROR: %s\n", ErrorMsg.c_str());
22 Err = CXCompilationDatabase_CanNotLoadDatabase;
23 }
24
25 if (ErrorCode)
26 *ErrorCode = Err;
27
David Blaikiecdba84c2014-08-08 16:06:15 +000028 return db.release();
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000029}
30
31void
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +000032clang_CompilationDatabase_dispose(CXCompilationDatabase CDb)
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000033{
34 delete static_cast<CompilationDatabase *>(CDb);
35}
36
37struct AllocatedCXCompileCommands
38{
39 std::vector<CompileCommand> CCmd;
40
Benjamin Kramerefb1eb92014-03-20 12:48:36 +000041 AllocatedCXCompileCommands(std::vector<CompileCommand> Cmd)
42 : CCmd(std::move(Cmd)) {}
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000043};
44
45CXCompileCommands
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +000046clang_CompilationDatabase_getCompileCommands(CXCompilationDatabase CDb,
47 const char *CompleteFileName)
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000048{
49 if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
Benjamin Kramerefb1eb92014-03-20 12:48:36 +000050 std::vector<CompileCommand> CCmd(db->getCompileCommands(CompleteFileName));
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000051 if (!CCmd.empty())
Benjamin Kramerefb1eb92014-03-20 12:48:36 +000052 return new AllocatedCXCompileCommands(std::move(CCmd));
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000053 }
54
Craig Topper69186e72014-06-08 08:38:04 +000055 return nullptr;
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000056}
57
Argyrios Kyrtzidis251ad5e2012-12-04 07:26:44 +000058CXCompileCommands
59clang_CompilationDatabase_getAllCompileCommands(CXCompilationDatabase CDb) {
60 if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
Benjamin Kramerefb1eb92014-03-20 12:48:36 +000061 std::vector<CompileCommand> CCmd(db->getAllCompileCommands());
Argyrios Kyrtzidis251ad5e2012-12-04 07:26:44 +000062 if (!CCmd.empty())
Benjamin Kramerefb1eb92014-03-20 12:48:36 +000063 return new AllocatedCXCompileCommands(std::move(CCmd));
Argyrios Kyrtzidis251ad5e2012-12-04 07:26:44 +000064 }
65
Craig Topper69186e72014-06-08 08:38:04 +000066 return nullptr;
Argyrios Kyrtzidis251ad5e2012-12-04 07:26:44 +000067}
68
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000069void
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +000070clang_CompileCommands_dispose(CXCompileCommands Cmds)
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000071{
72 delete static_cast<AllocatedCXCompileCommands *>(Cmds);
73}
74
75unsigned
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +000076clang_CompileCommands_getSize(CXCompileCommands Cmds)
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000077{
78 if (!Cmds)
79 return 0;
80
81 AllocatedCXCompileCommands *ACC =
82 static_cast<AllocatedCXCompileCommands *>(Cmds);
83
84 return ACC->CCmd.size();
85}
86
87CXCompileCommand
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +000088clang_CompileCommands_getCommand(CXCompileCommands Cmds, unsigned I)
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000089{
90 if (!Cmds)
Craig Topper69186e72014-06-08 08:38:04 +000091 return nullptr;
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000092
93 AllocatedCXCompileCommands *ACC =
94 static_cast<AllocatedCXCompileCommands *>(Cmds);
95
96 if (I >= ACC->CCmd.size())
Craig Topper69186e72014-06-08 08:38:04 +000097 return nullptr;
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000098
99 return &ACC->CCmd[I];
100}
101
102CXString
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +0000103clang_CompileCommand_getDirectory(CXCompileCommand CCmd)
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +0000104{
105 if (!CCmd)
Dmitri Gribenkof98dfba2013-02-01 14:13:32 +0000106 return cxstring::createNull();
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +0000107
108 CompileCommand *cmd = static_cast<CompileCommand *>(CCmd);
Dmitri Gribenko3c66b0b2013-02-02 00:02:12 +0000109 return cxstring::createRef(cmd->Directory.c_str());
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +0000110}
111
Argyrios Kyrtzidis74bcd212015-09-11 20:43:05 +0000112CXString
113clang_CompileCommand_getFilename(CXCompileCommand CCmd)
114{
115 if (!CCmd)
116 return cxstring::createNull();
117
118 CompileCommand *cmd = static_cast<CompileCommand *>(CCmd);
119 return cxstring::createRef(cmd->Filename.c_str());
120}
121
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +0000122unsigned
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +0000123clang_CompileCommand_getNumArgs(CXCompileCommand CCmd)
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +0000124{
125 if (!CCmd)
126 return 0;
127
128 return static_cast<CompileCommand *>(CCmd)->CommandLine.size();
129}
130
131CXString
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +0000132clang_CompileCommand_getArg(CXCompileCommand CCmd, unsigned Arg)
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +0000133{
134 if (!CCmd)
Dmitri Gribenkof98dfba2013-02-01 14:13:32 +0000135 return cxstring::createNull();
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +0000136
137 CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd);
138
139 if (Arg >= Cmd->CommandLine.size())
Dmitri Gribenkof98dfba2013-02-01 14:13:32 +0000140 return cxstring::createNull();
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +0000141
Dmitri Gribenko3c66b0b2013-02-02 00:02:12 +0000142 return cxstring::createRef(Cmd->CommandLine[Arg].c_str());
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +0000143}
144
Manuel Klimek6192c932013-11-13 13:23:27 +0000145unsigned
146clang_CompileCommand_getNumMappedSources(CXCompileCommand CCmd)
147{
Krasimir Georgieva1928042017-05-23 13:50:43 +0000148 // Left here for backward compatibility. No mapped sources exists in the C++
149 // backend anymore.
150 return 0;
Manuel Klimek6192c932013-11-13 13:23:27 +0000151}
152
153CXString
154clang_CompileCommand_getMappedSourcePath(CXCompileCommand CCmd, unsigned I)
155{
Krasimir Georgieva1928042017-05-23 13:50:43 +0000156 // Left here for backward compatibility. No mapped sources exists in the C++
157 // backend anymore.
158 return cxstring::createNull();
Manuel Klimek6192c932013-11-13 13:23:27 +0000159}
160
161CXString
162clang_CompileCommand_getMappedSourceContent(CXCompileCommand CCmd, unsigned I)
163{
Krasimir Georgieva1928042017-05-23 13:50:43 +0000164 // Left here for backward compatibility. No mapped sources exists in the C++
165 // backend anymore.
166 return cxstring::createNull();
Manuel Klimek6192c932013-11-13 13:23:27 +0000167}