2012-06-30 19:27:57 +08:00
|
|
|
#include "clang-c/CXCompilationDatabase.h"
|
|
|
|
#include "CXString.h"
|
2012-12-04 17:25:21 +08:00
|
|
|
#include "clang/Tooling/CompilationDatabase.h"
|
2013-08-20 00:14:33 +08:00
|
|
|
#include <cstdio>
|
2012-06-30 19:27:57 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace clang::tooling;
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
2013-12-06 00:25:25 +08:00
|
|
|
// FIXME: do something more useful with the error message
|
2012-06-30 19:27:57 +08:00
|
|
|
CXCompilationDatabase
|
2012-07-04 04:38:12 +08:00
|
|
|
clang_CompilationDatabase_fromDirectory(const char *BuildDir,
|
|
|
|
CXCompilationDatabase_Error *ErrorCode)
|
2012-06-30 19:27:57 +08:00
|
|
|
{
|
|
|
|
std::string ErrorMsg;
|
|
|
|
CXCompilationDatabase_Error Err = CXCompilationDatabase_NoError;
|
|
|
|
|
2014-08-09 00:06:15 +08:00
|
|
|
std::unique_ptr<CompilationDatabase> db =
|
|
|
|
CompilationDatabase::loadFromDirectory(BuildDir, ErrorMsg);
|
2012-06-30 19:27:57 +08:00
|
|
|
|
|
|
|
if (!db) {
|
|
|
|
fprintf(stderr, "LIBCLANG TOOLING ERROR: %s\n", ErrorMsg.c_str());
|
|
|
|
Err = CXCompilationDatabase_CanNotLoadDatabase;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ErrorCode)
|
|
|
|
*ErrorCode = Err;
|
|
|
|
|
2014-08-09 00:06:15 +08:00
|
|
|
return db.release();
|
2012-06-30 19:27:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-07-04 04:38:12 +08:00
|
|
|
clang_CompilationDatabase_dispose(CXCompilationDatabase CDb)
|
2012-06-30 19:27:57 +08:00
|
|
|
{
|
|
|
|
delete static_cast<CompilationDatabase *>(CDb);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct AllocatedCXCompileCommands
|
|
|
|
{
|
|
|
|
std::vector<CompileCommand> CCmd;
|
|
|
|
|
2014-03-20 20:48:36 +08:00
|
|
|
AllocatedCXCompileCommands(std::vector<CompileCommand> Cmd)
|
|
|
|
: CCmd(std::move(Cmd)) {}
|
2012-06-30 19:27:57 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
CXCompileCommands
|
2012-07-04 04:38:12 +08:00
|
|
|
clang_CompilationDatabase_getCompileCommands(CXCompilationDatabase CDb,
|
|
|
|
const char *CompleteFileName)
|
2012-06-30 19:27:57 +08:00
|
|
|
{
|
|
|
|
if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
|
2014-03-20 20:48:36 +08:00
|
|
|
std::vector<CompileCommand> CCmd(db->getCompileCommands(CompleteFileName));
|
2012-06-30 19:27:57 +08:00
|
|
|
if (!CCmd.empty())
|
2014-03-20 20:48:36 +08:00
|
|
|
return new AllocatedCXCompileCommands(std::move(CCmd));
|
2012-06-30 19:27:57 +08:00
|
|
|
}
|
|
|
|
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2012-06-30 19:27:57 +08:00
|
|
|
}
|
|
|
|
|
2012-12-04 15:26:44 +08:00
|
|
|
CXCompileCommands
|
|
|
|
clang_CompilationDatabase_getAllCompileCommands(CXCompilationDatabase CDb) {
|
|
|
|
if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
|
2014-03-20 20:48:36 +08:00
|
|
|
std::vector<CompileCommand> CCmd(db->getAllCompileCommands());
|
2012-12-04 15:26:44 +08:00
|
|
|
if (!CCmd.empty())
|
2014-03-20 20:48:36 +08:00
|
|
|
return new AllocatedCXCompileCommands(std::move(CCmd));
|
2012-12-04 15:26:44 +08:00
|
|
|
}
|
|
|
|
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2012-12-04 15:26:44 +08:00
|
|
|
}
|
|
|
|
|
2012-06-30 19:27:57 +08:00
|
|
|
void
|
2012-07-04 04:38:12 +08:00
|
|
|
clang_CompileCommands_dispose(CXCompileCommands Cmds)
|
2012-06-30 19:27:57 +08:00
|
|
|
{
|
|
|
|
delete static_cast<AllocatedCXCompileCommands *>(Cmds);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned
|
2012-07-04 04:38:12 +08:00
|
|
|
clang_CompileCommands_getSize(CXCompileCommands Cmds)
|
2012-06-30 19:27:57 +08:00
|
|
|
{
|
|
|
|
if (!Cmds)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
AllocatedCXCompileCommands *ACC =
|
|
|
|
static_cast<AllocatedCXCompileCommands *>(Cmds);
|
|
|
|
|
|
|
|
return ACC->CCmd.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
CXCompileCommand
|
2012-07-04 04:38:12 +08:00
|
|
|
clang_CompileCommands_getCommand(CXCompileCommands Cmds, unsigned I)
|
2012-06-30 19:27:57 +08:00
|
|
|
{
|
|
|
|
if (!Cmds)
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2012-06-30 19:27:57 +08:00
|
|
|
|
|
|
|
AllocatedCXCompileCommands *ACC =
|
|
|
|
static_cast<AllocatedCXCompileCommands *>(Cmds);
|
|
|
|
|
|
|
|
if (I >= ACC->CCmd.size())
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2012-06-30 19:27:57 +08:00
|
|
|
|
|
|
|
return &ACC->CCmd[I];
|
|
|
|
}
|
|
|
|
|
|
|
|
CXString
|
2012-07-04 04:38:12 +08:00
|
|
|
clang_CompileCommand_getDirectory(CXCompileCommand CCmd)
|
2012-06-30 19:27:57 +08:00
|
|
|
{
|
|
|
|
if (!CCmd)
|
2013-02-01 22:13:32 +08:00
|
|
|
return cxstring::createNull();
|
2012-06-30 19:27:57 +08:00
|
|
|
|
|
|
|
CompileCommand *cmd = static_cast<CompileCommand *>(CCmd);
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef(cmd->Directory.c_str());
|
2012-06-30 19:27:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned
|
2012-07-04 04:38:12 +08:00
|
|
|
clang_CompileCommand_getNumArgs(CXCompileCommand CCmd)
|
2012-06-30 19:27:57 +08:00
|
|
|
{
|
|
|
|
if (!CCmd)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return static_cast<CompileCommand *>(CCmd)->CommandLine.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
CXString
|
2012-07-04 04:38:12 +08:00
|
|
|
clang_CompileCommand_getArg(CXCompileCommand CCmd, unsigned Arg)
|
2012-06-30 19:27:57 +08:00
|
|
|
{
|
|
|
|
if (!CCmd)
|
2013-02-01 22:13:32 +08:00
|
|
|
return cxstring::createNull();
|
2012-06-30 19:27:57 +08:00
|
|
|
|
|
|
|
CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd);
|
|
|
|
|
|
|
|
if (Arg >= Cmd->CommandLine.size())
|
2013-02-01 22:13:32 +08:00
|
|
|
return cxstring::createNull();
|
2012-06-30 19:27:57 +08:00
|
|
|
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef(Cmd->CommandLine[Arg].c_str());
|
2012-06-30 19:27:57 +08:00
|
|
|
}
|
|
|
|
|
2013-11-13 21:23:27 +08:00
|
|
|
unsigned
|
|
|
|
clang_CompileCommand_getNumMappedSources(CXCompileCommand CCmd)
|
|
|
|
{
|
|
|
|
if (!CCmd)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return static_cast<CompileCommand *>(CCmd)->MappedSources.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
CXString
|
|
|
|
clang_CompileCommand_getMappedSourcePath(CXCompileCommand CCmd, unsigned I)
|
|
|
|
{
|
|
|
|
if (!CCmd)
|
|
|
|
return cxstring::createNull();
|
|
|
|
|
|
|
|
CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd);
|
|
|
|
|
|
|
|
if (I >= Cmd->MappedSources.size())
|
|
|
|
return cxstring::createNull();
|
|
|
|
|
|
|
|
return cxstring::createRef(Cmd->MappedSources[I].first.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
CXString
|
|
|
|
clang_CompileCommand_getMappedSourceContent(CXCompileCommand CCmd, unsigned I)
|
|
|
|
{
|
|
|
|
if (!CCmd)
|
|
|
|
return cxstring::createNull();
|
|
|
|
|
|
|
|
CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd);
|
|
|
|
|
|
|
|
if (I >= Cmd->MappedSources.size())
|
|
|
|
return cxstring::createNull();
|
|
|
|
|
|
|
|
return cxstring::createRef(Cmd->MappedSources[I].second.c_str());
|
|
|
|
}
|
2012-06-30 19:27:57 +08:00
|
|
|
|
|
|
|
} // end: extern "C"
|