xref: /netbsd-src/external/apache2/llvm/dist/clang/tools/libclang/CXCompilationDatabase.cpp (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1*7330f729Sjoerg #include "clang-c/CXCompilationDatabase.h"
2*7330f729Sjoerg #include "CXString.h"
3*7330f729Sjoerg #include "clang/Tooling/CompilationDatabase.h"
4*7330f729Sjoerg #include <cstdio>
5*7330f729Sjoerg 
6*7330f729Sjoerg using namespace clang;
7*7330f729Sjoerg using namespace clang::tooling;
8*7330f729Sjoerg 
9*7330f729Sjoerg // FIXME: do something more useful with the error message
10*7330f729Sjoerg CXCompilationDatabase
clang_CompilationDatabase_fromDirectory(const char * BuildDir,CXCompilationDatabase_Error * ErrorCode)11*7330f729Sjoerg clang_CompilationDatabase_fromDirectory(const char *BuildDir,
12*7330f729Sjoerg                                         CXCompilationDatabase_Error *ErrorCode)
13*7330f729Sjoerg {
14*7330f729Sjoerg   std::string ErrorMsg;
15*7330f729Sjoerg   CXCompilationDatabase_Error Err = CXCompilationDatabase_NoError;
16*7330f729Sjoerg 
17*7330f729Sjoerg   std::unique_ptr<CompilationDatabase> db =
18*7330f729Sjoerg       CompilationDatabase::loadFromDirectory(BuildDir, ErrorMsg);
19*7330f729Sjoerg 
20*7330f729Sjoerg   if (!db) {
21*7330f729Sjoerg     fprintf(stderr, "LIBCLANG TOOLING ERROR: %s\n", ErrorMsg.c_str());
22*7330f729Sjoerg     Err = CXCompilationDatabase_CanNotLoadDatabase;
23*7330f729Sjoerg   }
24*7330f729Sjoerg 
25*7330f729Sjoerg   if (ErrorCode)
26*7330f729Sjoerg     *ErrorCode = Err;
27*7330f729Sjoerg 
28*7330f729Sjoerg   return db.release();
29*7330f729Sjoerg }
30*7330f729Sjoerg 
31*7330f729Sjoerg void
clang_CompilationDatabase_dispose(CXCompilationDatabase CDb)32*7330f729Sjoerg clang_CompilationDatabase_dispose(CXCompilationDatabase CDb)
33*7330f729Sjoerg {
34*7330f729Sjoerg   delete static_cast<CompilationDatabase *>(CDb);
35*7330f729Sjoerg }
36*7330f729Sjoerg 
37*7330f729Sjoerg struct AllocatedCXCompileCommands
38*7330f729Sjoerg {
39*7330f729Sjoerg   std::vector<CompileCommand> CCmd;
40*7330f729Sjoerg 
AllocatedCXCompileCommandsAllocatedCXCompileCommands41*7330f729Sjoerg   AllocatedCXCompileCommands(std::vector<CompileCommand> Cmd)
42*7330f729Sjoerg       : CCmd(std::move(Cmd)) {}
43*7330f729Sjoerg };
44*7330f729Sjoerg 
45*7330f729Sjoerg CXCompileCommands
clang_CompilationDatabase_getCompileCommands(CXCompilationDatabase CDb,const char * CompleteFileName)46*7330f729Sjoerg clang_CompilationDatabase_getCompileCommands(CXCompilationDatabase CDb,
47*7330f729Sjoerg                                              const char *CompleteFileName)
48*7330f729Sjoerg {
49*7330f729Sjoerg   if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
50*7330f729Sjoerg     std::vector<CompileCommand> CCmd(db->getCompileCommands(CompleteFileName));
51*7330f729Sjoerg     if (!CCmd.empty())
52*7330f729Sjoerg       return new AllocatedCXCompileCommands(std::move(CCmd));
53*7330f729Sjoerg   }
54*7330f729Sjoerg 
55*7330f729Sjoerg   return nullptr;
56*7330f729Sjoerg }
57*7330f729Sjoerg 
58*7330f729Sjoerg CXCompileCommands
clang_CompilationDatabase_getAllCompileCommands(CXCompilationDatabase CDb)59*7330f729Sjoerg clang_CompilationDatabase_getAllCompileCommands(CXCompilationDatabase CDb) {
60*7330f729Sjoerg   if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
61*7330f729Sjoerg     std::vector<CompileCommand> CCmd(db->getAllCompileCommands());
62*7330f729Sjoerg     if (!CCmd.empty())
63*7330f729Sjoerg       return new AllocatedCXCompileCommands(std::move(CCmd));
64*7330f729Sjoerg   }
65*7330f729Sjoerg 
66*7330f729Sjoerg   return nullptr;
67*7330f729Sjoerg }
68*7330f729Sjoerg 
69*7330f729Sjoerg void
clang_CompileCommands_dispose(CXCompileCommands Cmds)70*7330f729Sjoerg clang_CompileCommands_dispose(CXCompileCommands Cmds)
71*7330f729Sjoerg {
72*7330f729Sjoerg   delete static_cast<AllocatedCXCompileCommands *>(Cmds);
73*7330f729Sjoerg }
74*7330f729Sjoerg 
75*7330f729Sjoerg unsigned
clang_CompileCommands_getSize(CXCompileCommands Cmds)76*7330f729Sjoerg clang_CompileCommands_getSize(CXCompileCommands Cmds)
77*7330f729Sjoerg {
78*7330f729Sjoerg   if (!Cmds)
79*7330f729Sjoerg     return 0;
80*7330f729Sjoerg 
81*7330f729Sjoerg   AllocatedCXCompileCommands *ACC =
82*7330f729Sjoerg     static_cast<AllocatedCXCompileCommands *>(Cmds);
83*7330f729Sjoerg 
84*7330f729Sjoerg   return ACC->CCmd.size();
85*7330f729Sjoerg }
86*7330f729Sjoerg 
87*7330f729Sjoerg CXCompileCommand
clang_CompileCommands_getCommand(CXCompileCommands Cmds,unsigned I)88*7330f729Sjoerg clang_CompileCommands_getCommand(CXCompileCommands Cmds, unsigned I)
89*7330f729Sjoerg {
90*7330f729Sjoerg   if (!Cmds)
91*7330f729Sjoerg     return nullptr;
92*7330f729Sjoerg 
93*7330f729Sjoerg   AllocatedCXCompileCommands *ACC =
94*7330f729Sjoerg     static_cast<AllocatedCXCompileCommands *>(Cmds);
95*7330f729Sjoerg 
96*7330f729Sjoerg   if (I >= ACC->CCmd.size())
97*7330f729Sjoerg     return nullptr;
98*7330f729Sjoerg 
99*7330f729Sjoerg   return &ACC->CCmd[I];
100*7330f729Sjoerg }
101*7330f729Sjoerg 
102*7330f729Sjoerg CXString
clang_CompileCommand_getDirectory(CXCompileCommand CCmd)103*7330f729Sjoerg clang_CompileCommand_getDirectory(CXCompileCommand CCmd)
104*7330f729Sjoerg {
105*7330f729Sjoerg   if (!CCmd)
106*7330f729Sjoerg     return cxstring::createNull();
107*7330f729Sjoerg 
108*7330f729Sjoerg   CompileCommand *cmd = static_cast<CompileCommand *>(CCmd);
109*7330f729Sjoerg   return cxstring::createRef(cmd->Directory.c_str());
110*7330f729Sjoerg }
111*7330f729Sjoerg 
112*7330f729Sjoerg CXString
clang_CompileCommand_getFilename(CXCompileCommand CCmd)113*7330f729Sjoerg clang_CompileCommand_getFilename(CXCompileCommand CCmd)
114*7330f729Sjoerg {
115*7330f729Sjoerg   if (!CCmd)
116*7330f729Sjoerg     return cxstring::createNull();
117*7330f729Sjoerg 
118*7330f729Sjoerg   CompileCommand *cmd = static_cast<CompileCommand *>(CCmd);
119*7330f729Sjoerg   return cxstring::createRef(cmd->Filename.c_str());
120*7330f729Sjoerg }
121*7330f729Sjoerg 
122*7330f729Sjoerg unsigned
clang_CompileCommand_getNumArgs(CXCompileCommand CCmd)123*7330f729Sjoerg clang_CompileCommand_getNumArgs(CXCompileCommand CCmd)
124*7330f729Sjoerg {
125*7330f729Sjoerg   if (!CCmd)
126*7330f729Sjoerg     return 0;
127*7330f729Sjoerg 
128*7330f729Sjoerg   return static_cast<CompileCommand *>(CCmd)->CommandLine.size();
129*7330f729Sjoerg }
130*7330f729Sjoerg 
131*7330f729Sjoerg CXString
clang_CompileCommand_getArg(CXCompileCommand CCmd,unsigned Arg)132*7330f729Sjoerg clang_CompileCommand_getArg(CXCompileCommand CCmd, unsigned Arg)
133*7330f729Sjoerg {
134*7330f729Sjoerg   if (!CCmd)
135*7330f729Sjoerg     return cxstring::createNull();
136*7330f729Sjoerg 
137*7330f729Sjoerg   CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd);
138*7330f729Sjoerg 
139*7330f729Sjoerg   if (Arg >= Cmd->CommandLine.size())
140*7330f729Sjoerg     return cxstring::createNull();
141*7330f729Sjoerg 
142*7330f729Sjoerg   return cxstring::createRef(Cmd->CommandLine[Arg].c_str());
143*7330f729Sjoerg }
144*7330f729Sjoerg 
145*7330f729Sjoerg unsigned
clang_CompileCommand_getNumMappedSources(CXCompileCommand CCmd)146*7330f729Sjoerg clang_CompileCommand_getNumMappedSources(CXCompileCommand CCmd)
147*7330f729Sjoerg {
148*7330f729Sjoerg   // Left here for backward compatibility. No mapped sources exists in the C++
149*7330f729Sjoerg   // backend anymore.
150*7330f729Sjoerg   return 0;
151*7330f729Sjoerg }
152*7330f729Sjoerg 
153*7330f729Sjoerg CXString
clang_CompileCommand_getMappedSourcePath(CXCompileCommand CCmd,unsigned I)154*7330f729Sjoerg clang_CompileCommand_getMappedSourcePath(CXCompileCommand CCmd, unsigned I)
155*7330f729Sjoerg {
156*7330f729Sjoerg   // Left here for backward compatibility. No mapped sources exists in the C++
157*7330f729Sjoerg   // backend anymore.
158*7330f729Sjoerg   return cxstring::createNull();
159*7330f729Sjoerg }
160*7330f729Sjoerg 
161*7330f729Sjoerg CXString
clang_CompileCommand_getMappedSourceContent(CXCompileCommand CCmd,unsigned I)162*7330f729Sjoerg clang_CompileCommand_getMappedSourceContent(CXCompileCommand CCmd, unsigned I)
163*7330f729Sjoerg {
164*7330f729Sjoerg   // Left here for backward compatibility. No mapped sources exists in the C++
165*7330f729Sjoerg   // backend anymore.
166*7330f729Sjoerg   return cxstring::createNull();
167*7330f729Sjoerg }
168