blob: 76e89249785dda3077e4a264ab1c3649e2c99798 [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
11// FIXME: do something more usefull with the error message
12CXCompilationDatabase
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
19 CompilationDatabase *db = CompilationDatabase::loadFromDirectory(BuildDir,
20 ErrorMsg);
21
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
30 return db;
31}
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
43 AllocatedCXCompileCommands(const std::vector<CompileCommand>& Cmd)
44 : CCmd(Cmd)
45 { }
46};
47
48CXCompileCommands
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +000049clang_CompilationDatabase_getCompileCommands(CXCompilationDatabase CDb,
50 const char *CompleteFileName)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000051{
52 if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
53 const std::vector<CompileCommand>
54 CCmd(db->getCompileCommands(CompleteFileName));
55 if (!CCmd.empty())
56 return new AllocatedCXCompileCommands( CCmd );
57 }
58
59 return 0;
60}
61
Argyrios Kyrtzidis7e96bfb2012-12-04 07:26:44 +000062CXCompileCommands
63clang_CompilationDatabase_getAllCompileCommands(CXCompilationDatabase CDb) {
64 if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
65 const std::vector<CompileCommand> CCmd(db->getAllCompileCommands());
66 if (!CCmd.empty())
67 return new AllocatedCXCompileCommands( CCmd );
68 }
69
70 return 0;
71}
72
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000073void
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +000074clang_CompileCommands_dispose(CXCompileCommands Cmds)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000075{
76 delete static_cast<AllocatedCXCompileCommands *>(Cmds);
77}
78
79unsigned
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +000080clang_CompileCommands_getSize(CXCompileCommands Cmds)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000081{
82 if (!Cmds)
83 return 0;
84
85 AllocatedCXCompileCommands *ACC =
86 static_cast<AllocatedCXCompileCommands *>(Cmds);
87
88 return ACC->CCmd.size();
89}
90
91CXCompileCommand
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +000092clang_CompileCommands_getCommand(CXCompileCommands Cmds, unsigned I)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000093{
94 if (!Cmds)
95 return 0;
96
97 AllocatedCXCompileCommands *ACC =
98 static_cast<AllocatedCXCompileCommands *>(Cmds);
99
100 if (I >= ACC->CCmd.size())
101 return 0;
102
103 return &ACC->CCmd[I];
104}
105
106CXString
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +0000107clang_CompileCommand_getDirectory(CXCompileCommand CCmd)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000108{
109 if (!CCmd)
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +0000110 return cxstring::createNull();
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000111
112 CompileCommand *cmd = static_cast<CompileCommand *>(CCmd);
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +0000113 return cxstring::createRef(cmd->Directory.c_str());
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000114}
115
116unsigned
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +0000117clang_CompileCommand_getNumArgs(CXCompileCommand CCmd)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000118{
119 if (!CCmd)
120 return 0;
121
122 return static_cast<CompileCommand *>(CCmd)->CommandLine.size();
123}
124
125CXString
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +0000126clang_CompileCommand_getArg(CXCompileCommand CCmd, unsigned Arg)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000127{
128 if (!CCmd)
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +0000129 return cxstring::createNull();
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000130
131 CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd);
132
133 if (Arg >= Cmd->CommandLine.size())
Dmitri Gribenkodad4c1a2013-02-01 14:13:32 +0000134 return cxstring::createNull();
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000135
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +0000136 return cxstring::createRef(Cmd->CommandLine[Arg].c_str());
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000137}
138
139
140} // end: extern "C"