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