1 #include "clang-c/CXCompilationDatabase.h"
2 #include "clang/Tooling/CompilationDatabase.h"
3 #include "CXString.h"
4
5 using namespace clang;
6 using namespace clang::tooling;
7 using namespace clang::cxstring;
8
9 extern "C" {
10
11 // FIXME: do something more usefull with the error message
12 CXCompilationDatabase
clang_CompilationDatabase_fromDirectory(const char * BuildDir,CXCompilationDatabase_Error * ErrorCode)13 clang_CompilationDatabase_fromDirectory(const char *BuildDir,
14 CXCompilationDatabase_Error *ErrorCode)
15 {
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
33 void
clang_CompilationDatabase_dispose(CXCompilationDatabase CDb)34 clang_CompilationDatabase_dispose(CXCompilationDatabase CDb)
35 {
36 delete static_cast<CompilationDatabase *>(CDb);
37 }
38
39 struct AllocatedCXCompileCommands
40 {
41 std::vector<CompileCommand> CCmd;
42
AllocatedCXCompileCommandsAllocatedCXCompileCommands43 AllocatedCXCompileCommands(const std::vector<CompileCommand>& Cmd)
44 : CCmd(Cmd)
45 { }
46 };
47
48 CXCompileCommands
clang_CompilationDatabase_getCompileCommands(CXCompilationDatabase CDb,const char * CompleteFileName)49 clang_CompilationDatabase_getCompileCommands(CXCompilationDatabase CDb,
50 const char *CompleteFileName)
51 {
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
62 void
clang_CompileCommands_dispose(CXCompileCommands Cmds)63 clang_CompileCommands_dispose(CXCompileCommands Cmds)
64 {
65 delete static_cast<AllocatedCXCompileCommands *>(Cmds);
66 }
67
68 unsigned
clang_CompileCommands_getSize(CXCompileCommands Cmds)69 clang_CompileCommands_getSize(CXCompileCommands Cmds)
70 {
71 if (!Cmds)
72 return 0;
73
74 AllocatedCXCompileCommands *ACC =
75 static_cast<AllocatedCXCompileCommands *>(Cmds);
76
77 return ACC->CCmd.size();
78 }
79
80 CXCompileCommand
clang_CompileCommands_getCommand(CXCompileCommands Cmds,unsigned I)81 clang_CompileCommands_getCommand(CXCompileCommands Cmds, unsigned I)
82 {
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
95 CXString
clang_CompileCommand_getDirectory(CXCompileCommand CCmd)96 clang_CompileCommand_getDirectory(CXCompileCommand CCmd)
97 {
98 if (!CCmd)
99 return createCXString((const char*)NULL);
100
101 CompileCommand *cmd = static_cast<CompileCommand *>(CCmd);
102 return createCXString(cmd->Directory);
103 }
104
105 unsigned
clang_CompileCommand_getNumArgs(CXCompileCommand CCmd)106 clang_CompileCommand_getNumArgs(CXCompileCommand CCmd)
107 {
108 if (!CCmd)
109 return 0;
110
111 return static_cast<CompileCommand *>(CCmd)->CommandLine.size();
112 }
113
114 CXString
clang_CompileCommand_getArg(CXCompileCommand CCmd,unsigned Arg)115 clang_CompileCommand_getArg(CXCompileCommand CCmd, unsigned Arg)
116 {
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"
130