[TargetVersion] Only enable on RISC-V and AArch64 (#115991)
[llvm-project.git] / clang / tools / libclang / CXCompilationDatabase.cpp
blob2ca532659d3786b7bdbef9b611c0465c76294225
1 #include "clang-c/CXCompilationDatabase.h"
2 #include "CXString.h"
3 #include "clang/Tooling/CompilationDatabase.h"
4 #include <cstdio>
6 using namespace clang;
7 using namespace clang::tooling;
9 // FIXME: do something more useful with the error message
10 CXCompilationDatabase
11 clang_CompilationDatabase_fromDirectory(const char *BuildDir,
12 CXCompilationDatabase_Error *ErrorCode)
14 std::string ErrorMsg;
15 CXCompilationDatabase_Error Err = CXCompilationDatabase_NoError;
17 std::unique_ptr<CompilationDatabase> db =
18 CompilationDatabase::loadFromDirectory(BuildDir, ErrorMsg);
20 if (!db) {
21 fprintf(stderr, "LIBCLANG TOOLING ERROR: %s\n", ErrorMsg.c_str());
22 Err = CXCompilationDatabase_CanNotLoadDatabase;
25 if (ErrorCode)
26 *ErrorCode = Err;
28 return db.release();
31 void
32 clang_CompilationDatabase_dispose(CXCompilationDatabase CDb)
34 delete static_cast<CompilationDatabase *>(CDb);
37 struct AllocatedCXCompileCommands
39 std::vector<CompileCommand> CCmd;
41 AllocatedCXCompileCommands(std::vector<CompileCommand> Cmd)
42 : CCmd(std::move(Cmd)) {}
45 CXCompileCommands
46 clang_CompilationDatabase_getCompileCommands(CXCompilationDatabase CDb,
47 const char *CompleteFileName)
49 if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
50 std::vector<CompileCommand> CCmd(db->getCompileCommands(CompleteFileName));
51 if (!CCmd.empty())
52 return new AllocatedCXCompileCommands(std::move(CCmd));
55 return nullptr;
58 CXCompileCommands
59 clang_CompilationDatabase_getAllCompileCommands(CXCompilationDatabase CDb) {
60 if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
61 std::vector<CompileCommand> CCmd(db->getAllCompileCommands());
62 if (!CCmd.empty())
63 return new AllocatedCXCompileCommands(std::move(CCmd));
66 return nullptr;
69 void
70 clang_CompileCommands_dispose(CXCompileCommands Cmds)
72 delete static_cast<AllocatedCXCompileCommands *>(Cmds);
75 unsigned
76 clang_CompileCommands_getSize(CXCompileCommands Cmds)
78 if (!Cmds)
79 return 0;
81 AllocatedCXCompileCommands *ACC =
82 static_cast<AllocatedCXCompileCommands *>(Cmds);
84 return ACC->CCmd.size();
87 CXCompileCommand
88 clang_CompileCommands_getCommand(CXCompileCommands Cmds, unsigned I)
90 if (!Cmds)
91 return nullptr;
93 AllocatedCXCompileCommands *ACC =
94 static_cast<AllocatedCXCompileCommands *>(Cmds);
96 if (I >= ACC->CCmd.size())
97 return nullptr;
99 return &ACC->CCmd[I];
102 CXString
103 clang_CompileCommand_getDirectory(CXCompileCommand CCmd)
105 if (!CCmd)
106 return cxstring::createNull();
108 CompileCommand *cmd = static_cast<CompileCommand *>(CCmd);
109 return cxstring::createRef(cmd->Directory.c_str());
112 CXString
113 clang_CompileCommand_getFilename(CXCompileCommand CCmd)
115 if (!CCmd)
116 return cxstring::createNull();
118 CompileCommand *cmd = static_cast<CompileCommand *>(CCmd);
119 return cxstring::createRef(cmd->Filename.c_str());
122 unsigned
123 clang_CompileCommand_getNumArgs(CXCompileCommand CCmd)
125 if (!CCmd)
126 return 0;
128 return static_cast<CompileCommand *>(CCmd)->CommandLine.size();
131 CXString
132 clang_CompileCommand_getArg(CXCompileCommand CCmd, unsigned Arg)
134 if (!CCmd)
135 return cxstring::createNull();
137 CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd);
139 if (Arg >= Cmd->CommandLine.size())
140 return cxstring::createNull();
142 return cxstring::createRef(Cmd->CommandLine[Arg].c_str());
145 unsigned
146 clang_CompileCommand_getNumMappedSources(CXCompileCommand CCmd)
148 // Left here for backward compatibility. No mapped sources exists in the C++
149 // backend anymore.
150 return 0;
153 CXString
154 clang_CompileCommand_getMappedSourcePath(CXCompileCommand CCmd, unsigned I)
156 // Left here for backward compatibility. No mapped sources exists in the C++
157 // backend anymore.
158 return cxstring::createNull();
161 CXString
162 clang_CompileCommand_getMappedSourceContent(CXCompileCommand CCmd, unsigned I)
164 // Left here for backward compatibility. No mapped sources exists in the C++
165 // backend anymore.
166 return cxstring::createNull();