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