blob: 7bd319ac295e2c78f8fa947ab9ed2f400153144a [file] [log] [blame]
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00001#include "clang-c/CXCompilationDatabase.h"
2#include "clang/Tooling/CompilationDatabase.h"
3#include "CXString.h"
4
5using namespace clang;
6using namespace clang::tooling;
7using namespace clang::cxstring;
8
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
62void
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +000063clang_CompileCommands_dispose(CXCompileCommands Cmds)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000064{
65 delete static_cast<AllocatedCXCompileCommands *>(Cmds);
66}
67
68unsigned
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +000069clang_CompileCommands_getSize(CXCompileCommands Cmds)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000070{
71 if (!Cmds)
72 return 0;
73
74 AllocatedCXCompileCommands *ACC =
75 static_cast<AllocatedCXCompileCommands *>(Cmds);
76
77 return ACC->CCmd.size();
78}
79
80CXCompileCommand
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +000081clang_CompileCommands_getCommand(CXCompileCommands Cmds, unsigned I)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000082{
83 if (!Cmds)
84 return 0;
85
86 AllocatedCXCompileCommands *ACC =
87 static_cast<AllocatedCXCompileCommands *>(Cmds);
88
89 if (I >= ACC->CCmd.size())
90 return 0;
91
92 return &ACC->CCmd[I];
93}
94
95CXString
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +000096clang_CompileCommand_getDirectory(CXCompileCommand CCmd)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000097{
98 if (!CCmd)
99 return createCXString((const char*)NULL);
100
101 CompileCommand *cmd = static_cast<CompileCommand *>(CCmd);
102 return createCXString(cmd->Directory);
103}
104
105unsigned
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +0000106clang_CompileCommand_getNumArgs(CXCompileCommand CCmd)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000107{
108 if (!CCmd)
109 return 0;
110
111 return static_cast<CompileCommand *>(CCmd)->CommandLine.size();
112}
113
114CXString
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +0000115clang_CompileCommand_getArg(CXCompileCommand CCmd, unsigned Arg)
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +0000116{
117 if (!CCmd)
118 return createCXString((const char*)NULL);
119
120 CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd);
121
122 if (Arg >= Cmd->CommandLine.size())
123 return createCXString((const char*)NULL);
124
125 return createCXString(Cmd->CommandLine[Arg]);
126}
127
128
129} // end: extern "C"