CXCompilationDatabase.cpp revision ef8225444452a1486bd721f3285301fe84643b00
1#include "clang-c/CXCompilationDatabase.h"
2#include "CXString.h"
3#include "clang/Tooling/CompilationDatabase.h"
4#include <cstdio>
5
6using namespace clang;
7using namespace clang::tooling;
8
9extern "C" {
10
11// FIXME: do something more useful with the error message
12CXCompilationDatabase
13clang_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
33void
34clang_CompilationDatabase_dispose(CXCompilationDatabase CDb)
35{
36  delete static_cast<CompilationDatabase *>(CDb);
37}
38
39struct AllocatedCXCompileCommands
40{
41  std::vector<CompileCommand> CCmd;
42
43  AllocatedCXCompileCommands(std::vector<CompileCommand> Cmd)
44      : CCmd(std::move(Cmd)) {}
45};
46
47CXCompileCommands
48clang_CompilationDatabase_getCompileCommands(CXCompilationDatabase CDb,
49                                             const char *CompleteFileName)
50{
51  if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
52    std::vector<CompileCommand> CCmd(db->getCompileCommands(CompleteFileName));
53    if (!CCmd.empty())
54      return new AllocatedCXCompileCommands(std::move(CCmd));
55  }
56
57  return nullptr;
58}
59
60CXCompileCommands
61clang_CompilationDatabase_getAllCompileCommands(CXCompilationDatabase CDb) {
62  if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
63    std::vector<CompileCommand> CCmd(db->getAllCompileCommands());
64    if (!CCmd.empty())
65      return new AllocatedCXCompileCommands(std::move(CCmd));
66  }
67
68  return nullptr;
69}
70
71void
72clang_CompileCommands_dispose(CXCompileCommands Cmds)
73{
74  delete static_cast<AllocatedCXCompileCommands *>(Cmds);
75}
76
77unsigned
78clang_CompileCommands_getSize(CXCompileCommands Cmds)
79{
80  if (!Cmds)
81    return 0;
82
83  AllocatedCXCompileCommands *ACC =
84    static_cast<AllocatedCXCompileCommands *>(Cmds);
85
86  return ACC->CCmd.size();
87}
88
89CXCompileCommand
90clang_CompileCommands_getCommand(CXCompileCommands Cmds, unsigned I)
91{
92  if (!Cmds)
93    return nullptr;
94
95  AllocatedCXCompileCommands *ACC =
96    static_cast<AllocatedCXCompileCommands *>(Cmds);
97
98  if (I >= ACC->CCmd.size())
99    return nullptr;
100
101  return &ACC->CCmd[I];
102}
103
104CXString
105clang_CompileCommand_getDirectory(CXCompileCommand CCmd)
106{
107  if (!CCmd)
108    return cxstring::createNull();
109
110  CompileCommand *cmd = static_cast<CompileCommand *>(CCmd);
111  return cxstring::createRef(cmd->Directory.c_str());
112}
113
114unsigned
115clang_CompileCommand_getNumArgs(CXCompileCommand CCmd)
116{
117  if (!CCmd)
118    return 0;
119
120  return static_cast<CompileCommand *>(CCmd)->CommandLine.size();
121}
122
123CXString
124clang_CompileCommand_getArg(CXCompileCommand CCmd, unsigned Arg)
125{
126  if (!CCmd)
127    return cxstring::createNull();
128
129  CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd);
130
131  if (Arg >= Cmd->CommandLine.size())
132    return cxstring::createNull();
133
134  return cxstring::createRef(Cmd->CommandLine[Arg].c_str());
135}
136
137unsigned
138clang_CompileCommand_getNumMappedSources(CXCompileCommand CCmd)
139{
140  if (!CCmd)
141    return 0;
142
143  return static_cast<CompileCommand *>(CCmd)->MappedSources.size();
144}
145
146CXString
147clang_CompileCommand_getMappedSourcePath(CXCompileCommand CCmd, unsigned I)
148{
149  if (!CCmd)
150    return cxstring::createNull();
151
152  CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd);
153
154  if (I >= Cmd->MappedSources.size())
155    return cxstring::createNull();
156
157  return cxstring::createRef(Cmd->MappedSources[I].first.c_str());
158}
159
160CXString
161clang_CompileCommand_getMappedSourceContent(CXCompileCommand CCmd, unsigned I)
162{
163  if (!CCmd)
164    return cxstring::createNull();
165
166  CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd);
167
168  if (I >= Cmd->MappedSources.size())
169    return cxstring::createNull();
170
171  return cxstring::createRef(Cmd->MappedSources[I].second.c_str());
172}
173
174} // end: extern "C"
175