blob: a537c9d6f94f3403b6997774074ece7e102576bb [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
13clang_tooling_CompilationDatabase_fromDirectory(
14 const char *BuildDir,
15 CXCompilationDatabase_Error *ErrorCode)
16{
17 std::string ErrorMsg;
18 CXCompilationDatabase_Error Err = CXCompilationDatabase_NoError;
19
20 CompilationDatabase *db = CompilationDatabase::loadFromDirectory(BuildDir,
21 ErrorMsg);
22
23 if (!db) {
24 fprintf(stderr, "LIBCLANG TOOLING ERROR: %s\n", ErrorMsg.c_str());
25 Err = CXCompilationDatabase_CanNotLoadDatabase;
26 }
27
28 if (ErrorCode)
29 *ErrorCode = Err;
30
31 return db;
32}
33
34void
35clang_tooling_CompilationDatabase_dispose(CXCompilationDatabase CDb)
36{
37 delete static_cast<CompilationDatabase *>(CDb);
38}
39
40struct AllocatedCXCompileCommands
41{
42 std::vector<CompileCommand> CCmd;
43
44 AllocatedCXCompileCommands(const std::vector<CompileCommand>& Cmd)
45 : CCmd(Cmd)
46 { }
47};
48
49CXCompileCommands
50clang_tooling_CompilationDatabase_getCompileCommands(CXCompilationDatabase CDb,
51 const char *CompleteFileName)
52{
53 if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
54 const std::vector<CompileCommand>
55 CCmd(db->getCompileCommands(CompleteFileName));
56 if (!CCmd.empty())
57 return new AllocatedCXCompileCommands( CCmd );
58 }
59
60 return 0;
61}
62
63void
64clang_tooling_CompileCommands_dispose(CXCompileCommands Cmds)
65{
66 delete static_cast<AllocatedCXCompileCommands *>(Cmds);
67}
68
69unsigned
70clang_tooling_CompileCommands_getSize(CXCompileCommands Cmds)
71{
72 if (!Cmds)
73 return 0;
74
75 AllocatedCXCompileCommands *ACC =
76 static_cast<AllocatedCXCompileCommands *>(Cmds);
77
78 return ACC->CCmd.size();
79}
80
81CXCompileCommand
82clang_tooling_CompileCommands_getCommand(CXCompileCommands Cmds, unsigned I)
83{
84 if (!Cmds)
85 return 0;
86
87 AllocatedCXCompileCommands *ACC =
88 static_cast<AllocatedCXCompileCommands *>(Cmds);
89
90 if (I >= ACC->CCmd.size())
91 return 0;
92
93 return &ACC->CCmd[I];
94}
95
96CXString
97clang_tooling_CompileCommand_getDirectory(CXCompileCommand CCmd)
98{
99 if (!CCmd)
100 return createCXString((const char*)NULL);
101
102 CompileCommand *cmd = static_cast<CompileCommand *>(CCmd);
103 return createCXString(cmd->Directory);
104}
105
106unsigned
107clang_tooling_CompileCommand_getNumArgs(CXCompileCommand CCmd)
108{
109 if (!CCmd)
110 return 0;
111
112 return static_cast<CompileCommand *>(CCmd)->CommandLine.size();
113}
114
115CXString
116clang_tooling_CompileCommand_getArg(CXCompileCommand CCmd, unsigned Arg)
117{
118 if (!CCmd)
119 return createCXString((const char*)NULL);
120
121 CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd);
122
123 if (Arg >= Cmd->CommandLine.size())
124 return createCXString((const char*)NULL);
125
126 return createCXString(Cmd->CommandLine[Arg]);
127}
128
129
130} // end: extern "C"