]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/libclang/CXCompilationDatabase.cpp
Vendor import of clang trunk r161861:
[FreeBSD/FreeBSD.git] / tools / libclang / CXCompilationDatabase.cpp
1 #include "clang-c/CXCompilationDatabase.h"
2 #include "clang/Tooling/CompilationDatabase.h"
3 #include "CXString.h"
4
5 using namespace clang;
6 using namespace clang::tooling;
7 using namespace clang::cxstring;
8
9 extern "C" {
10
11 // FIXME: do something more usefull with the error message
12 CXCompilationDatabase
13 clang_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
33 void
34 clang_CompilationDatabase_dispose(CXCompilationDatabase CDb)
35 {
36   delete static_cast<CompilationDatabase *>(CDb);
37 }
38
39 struct AllocatedCXCompileCommands
40 {
41   std::vector<CompileCommand> CCmd;
42
43   AllocatedCXCompileCommands(const std::vector<CompileCommand>& Cmd)
44     : CCmd(Cmd)
45   { }
46 };
47
48 CXCompileCommands
49 clang_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
62 void
63 clang_CompileCommands_dispose(CXCompileCommands Cmds)
64 {
65   delete static_cast<AllocatedCXCompileCommands *>(Cmds);
66 }
67
68 unsigned
69 clang_CompileCommands_getSize(CXCompileCommands Cmds)
70 {
71   if (!Cmds)
72     return 0;
73
74   AllocatedCXCompileCommands *ACC =
75     static_cast<AllocatedCXCompileCommands *>(Cmds);
76
77   return ACC->CCmd.size();
78 }
79
80 CXCompileCommand
81 clang_CompileCommands_getCommand(CXCompileCommands Cmds, unsigned I)
82 {
83   if (!Cmds)
84     return 0;
85
86   AllocatedCXCompileCommands *ACC =
87     static_cast<AllocatedCXCompileCommands *>(Cmds);
88
89   if (I >= ACC->CCmd.size())
90     return 0;
91
92   return &ACC->CCmd[I];
93 }
94
95 CXString
96 clang_CompileCommand_getDirectory(CXCompileCommand CCmd)
97 {
98   if (!CCmd)
99     return createCXString((const char*)NULL);
100
101   CompileCommand *cmd = static_cast<CompileCommand *>(CCmd);
102   return createCXString(cmd->Directory);
103 }
104
105 unsigned
106 clang_CompileCommand_getNumArgs(CXCompileCommand CCmd)
107 {
108   if (!CCmd)
109     return 0;
110
111   return static_cast<CompileCommand *>(CCmd)->CommandLine.size();
112 }
113
114 CXString
115 clang_CompileCommand_getArg(CXCompileCommand CCmd, unsigned Arg)
116 {
117   if (!CCmd)
118     return createCXString((const char*)NULL);
119
120   CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd);
121
122   if (Arg >= Cmd->CommandLine.size())
123     return createCXString((const char*)NULL);
124
125   return createCXString(Cmd->CommandLine[Arg]);
126 }
127
128
129 } // end: extern "C"