CXCompilationDatabase.cpp revision 9f9e0d2ecd4b9dd7ceeb57f662f6d8f549adca63
1#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_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(const std::vector<CompileCommand>& Cmd)
44    : CCmd(Cmd)
45  { }
46};
47
48CXCompileCommands
49clang_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
62CXCompileCommands
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
73void
74clang_CompileCommands_dispose(CXCompileCommands Cmds)
75{
76  delete static_cast<AllocatedCXCompileCommands *>(Cmds);
77}
78
79unsigned
80clang_CompileCommands_getSize(CXCompileCommands Cmds)
81{
82  if (!Cmds)
83    return 0;
84
85  AllocatedCXCompileCommands *ACC =
86    static_cast<AllocatedCXCompileCommands *>(Cmds);
87
88  return ACC->CCmd.size();
89}
90
91CXCompileCommand
92clang_CompileCommands_getCommand(CXCompileCommands Cmds, unsigned I)
93{
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
107clang_CompileCommand_getDirectory(CXCompileCommand CCmd)
108{
109  if (!CCmd)
110    return createCXString((const char*)NULL);
111
112  CompileCommand *cmd = static_cast<CompileCommand *>(CCmd);
113  return createCXString(cmd->Directory.c_str(), /*DupString=*/false);
114}
115
116unsigned
117clang_CompileCommand_getNumArgs(CXCompileCommand CCmd)
118{
119  if (!CCmd)
120    return 0;
121
122  return static_cast<CompileCommand *>(CCmd)->CommandLine.size();
123}
124
125CXString
126clang_CompileCommand_getArg(CXCompileCommand CCmd, unsigned Arg)
127{
128  if (!CCmd)
129    return createCXString((const char*)NULL);
130
131  CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd);
132
133  if (Arg >= Cmd->CommandLine.size())
134    return createCXString((const char*)NULL);
135
136  return createCXString(Cmd->CommandLine[Arg].c_str(), /*DupString=*/false);
137}
138
139
140} // end: extern "C"
141