xref: /llvm-project/clang/tools/libclang/CXCompilationDatabase.cpp (revision a1928042938602aefa897d607de26515ca597ed4)
12b3c8603SArnaud A. de Grandmaison #include "clang-c/CXCompilationDatabase.h"
22b3c8603SArnaud A. de Grandmaison #include "CXString.h"
3cc0694c8SChandler Carruth #include "clang/Tooling/CompilationDatabase.h"
4ab458a13SDmitri Gribenko #include <cstdio>
52b3c8603SArnaud A. de Grandmaison 
62b3c8603SArnaud A. de Grandmaison using namespace clang;
72b3c8603SArnaud A. de Grandmaison using namespace clang::tooling;
82b3c8603SArnaud A. de Grandmaison 
9f6a24ce4SAlp Toker // FIXME: do something more useful with the error message
102b3c8603SArnaud A. de Grandmaison CXCompilationDatabase
clang_CompilationDatabase_fromDirectory(const char * BuildDir,CXCompilationDatabase_Error * ErrorCode)11fa6d73ccSArnaud A. de Grandmaison clang_CompilationDatabase_fromDirectory(const char *BuildDir,
122b3c8603SArnaud A. de Grandmaison                                         CXCompilationDatabase_Error *ErrorCode)
132b3c8603SArnaud A. de Grandmaison {
142b3c8603SArnaud A. de Grandmaison   std::string ErrorMsg;
152b3c8603SArnaud A. de Grandmaison   CXCompilationDatabase_Error Err = CXCompilationDatabase_NoError;
162b3c8603SArnaud A. de Grandmaison 
17cdba84c0SDavid Blaikie   std::unique_ptr<CompilationDatabase> db =
18cdba84c0SDavid Blaikie       CompilationDatabase::loadFromDirectory(BuildDir, ErrorMsg);
192b3c8603SArnaud A. de Grandmaison 
202b3c8603SArnaud A. de Grandmaison   if (!db) {
212b3c8603SArnaud A. de Grandmaison     fprintf(stderr, "LIBCLANG TOOLING ERROR: %s\n", ErrorMsg.c_str());
222b3c8603SArnaud A. de Grandmaison     Err = CXCompilationDatabase_CanNotLoadDatabase;
232b3c8603SArnaud A. de Grandmaison   }
242b3c8603SArnaud A. de Grandmaison 
252b3c8603SArnaud A. de Grandmaison   if (ErrorCode)
262b3c8603SArnaud A. de Grandmaison     *ErrorCode = Err;
272b3c8603SArnaud A. de Grandmaison 
28cdba84c0SDavid Blaikie   return db.release();
292b3c8603SArnaud A. de Grandmaison }
302b3c8603SArnaud A. de Grandmaison 
312b3c8603SArnaud A. de Grandmaison void
clang_CompilationDatabase_dispose(CXCompilationDatabase CDb)32fa6d73ccSArnaud A. de Grandmaison clang_CompilationDatabase_dispose(CXCompilationDatabase CDb)
332b3c8603SArnaud A. de Grandmaison {
342b3c8603SArnaud A. de Grandmaison   delete static_cast<CompilationDatabase *>(CDb);
352b3c8603SArnaud A. de Grandmaison }
362b3c8603SArnaud A. de Grandmaison 
372b3c8603SArnaud A. de Grandmaison struct AllocatedCXCompileCommands
382b3c8603SArnaud A. de Grandmaison {
392b3c8603SArnaud A. de Grandmaison   std::vector<CompileCommand> CCmd;
402b3c8603SArnaud A. de Grandmaison 
AllocatedCXCompileCommandsAllocatedCXCompileCommands41efb1eb98SBenjamin Kramer   AllocatedCXCompileCommands(std::vector<CompileCommand> Cmd)
42efb1eb98SBenjamin Kramer       : CCmd(std::move(Cmd)) {}
432b3c8603SArnaud A. de Grandmaison };
442b3c8603SArnaud A. de Grandmaison 
452b3c8603SArnaud A. de Grandmaison CXCompileCommands
clang_CompilationDatabase_getCompileCommands(CXCompilationDatabase CDb,const char * CompleteFileName)46fa6d73ccSArnaud A. de Grandmaison clang_CompilationDatabase_getCompileCommands(CXCompilationDatabase CDb,
472b3c8603SArnaud A. de Grandmaison                                              const char *CompleteFileName)
482b3c8603SArnaud A. de Grandmaison {
492b3c8603SArnaud A. de Grandmaison   if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
50efb1eb98SBenjamin Kramer     std::vector<CompileCommand> CCmd(db->getCompileCommands(CompleteFileName));
512b3c8603SArnaud A. de Grandmaison     if (!CCmd.empty())
52efb1eb98SBenjamin Kramer       return new AllocatedCXCompileCommands(std::move(CCmd));
532b3c8603SArnaud A. de Grandmaison   }
542b3c8603SArnaud A. de Grandmaison 
5569186e73SCraig Topper   return nullptr;
562b3c8603SArnaud A. de Grandmaison }
572b3c8603SArnaud A. de Grandmaison 
58251ad5e0SArgyrios Kyrtzidis CXCompileCommands
clang_CompilationDatabase_getAllCompileCommands(CXCompilationDatabase CDb)59251ad5e0SArgyrios Kyrtzidis clang_CompilationDatabase_getAllCompileCommands(CXCompilationDatabase CDb) {
60251ad5e0SArgyrios Kyrtzidis   if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
61efb1eb98SBenjamin Kramer     std::vector<CompileCommand> CCmd(db->getAllCompileCommands());
62251ad5e0SArgyrios Kyrtzidis     if (!CCmd.empty())
63efb1eb98SBenjamin Kramer       return new AllocatedCXCompileCommands(std::move(CCmd));
64251ad5e0SArgyrios Kyrtzidis   }
65251ad5e0SArgyrios Kyrtzidis 
6669186e73SCraig Topper   return nullptr;
67251ad5e0SArgyrios Kyrtzidis }
68251ad5e0SArgyrios Kyrtzidis 
692b3c8603SArnaud A. de Grandmaison void
clang_CompileCommands_dispose(CXCompileCommands Cmds)70fa6d73ccSArnaud A. de Grandmaison clang_CompileCommands_dispose(CXCompileCommands Cmds)
712b3c8603SArnaud A. de Grandmaison {
722b3c8603SArnaud A. de Grandmaison   delete static_cast<AllocatedCXCompileCommands *>(Cmds);
732b3c8603SArnaud A. de Grandmaison }
742b3c8603SArnaud A. de Grandmaison 
752b3c8603SArnaud A. de Grandmaison unsigned
clang_CompileCommands_getSize(CXCompileCommands Cmds)76fa6d73ccSArnaud A. de Grandmaison clang_CompileCommands_getSize(CXCompileCommands Cmds)
772b3c8603SArnaud A. de Grandmaison {
782b3c8603SArnaud A. de Grandmaison   if (!Cmds)
792b3c8603SArnaud A. de Grandmaison     return 0;
802b3c8603SArnaud A. de Grandmaison 
812b3c8603SArnaud A. de Grandmaison   AllocatedCXCompileCommands *ACC =
822b3c8603SArnaud A. de Grandmaison     static_cast<AllocatedCXCompileCommands *>(Cmds);
832b3c8603SArnaud A. de Grandmaison 
842b3c8603SArnaud A. de Grandmaison   return ACC->CCmd.size();
852b3c8603SArnaud A. de Grandmaison }
862b3c8603SArnaud A. de Grandmaison 
872b3c8603SArnaud A. de Grandmaison CXCompileCommand
clang_CompileCommands_getCommand(CXCompileCommands Cmds,unsigned I)88fa6d73ccSArnaud A. de Grandmaison clang_CompileCommands_getCommand(CXCompileCommands Cmds, unsigned I)
892b3c8603SArnaud A. de Grandmaison {
902b3c8603SArnaud A. de Grandmaison   if (!Cmds)
9169186e73SCraig Topper     return nullptr;
922b3c8603SArnaud A. de Grandmaison 
932b3c8603SArnaud A. de Grandmaison   AllocatedCXCompileCommands *ACC =
942b3c8603SArnaud A. de Grandmaison     static_cast<AllocatedCXCompileCommands *>(Cmds);
952b3c8603SArnaud A. de Grandmaison 
962b3c8603SArnaud A. de Grandmaison   if (I >= ACC->CCmd.size())
9769186e73SCraig Topper     return nullptr;
982b3c8603SArnaud A. de Grandmaison 
992b3c8603SArnaud A. de Grandmaison   return &ACC->CCmd[I];
1002b3c8603SArnaud A. de Grandmaison }
1012b3c8603SArnaud A. de Grandmaison 
1022b3c8603SArnaud A. de Grandmaison CXString
clang_CompileCommand_getDirectory(CXCompileCommand CCmd)103fa6d73ccSArnaud A. de Grandmaison clang_CompileCommand_getDirectory(CXCompileCommand CCmd)
1042b3c8603SArnaud A. de Grandmaison {
1052b3c8603SArnaud A. de Grandmaison   if (!CCmd)
106f98dfbaeSDmitri Gribenko     return cxstring::createNull();
1072b3c8603SArnaud A. de Grandmaison 
1082b3c8603SArnaud A. de Grandmaison   CompileCommand *cmd = static_cast<CompileCommand *>(CCmd);
1093c66b0beSDmitri Gribenko   return cxstring::createRef(cmd->Directory.c_str());
1102b3c8603SArnaud A. de Grandmaison }
1112b3c8603SArnaud A. de Grandmaison 
11274bcd21eSArgyrios Kyrtzidis CXString
clang_CompileCommand_getFilename(CXCompileCommand CCmd)11374bcd21eSArgyrios Kyrtzidis clang_CompileCommand_getFilename(CXCompileCommand CCmd)
11474bcd21eSArgyrios Kyrtzidis {
11574bcd21eSArgyrios Kyrtzidis   if (!CCmd)
11674bcd21eSArgyrios Kyrtzidis     return cxstring::createNull();
11774bcd21eSArgyrios Kyrtzidis 
11874bcd21eSArgyrios Kyrtzidis   CompileCommand *cmd = static_cast<CompileCommand *>(CCmd);
11974bcd21eSArgyrios Kyrtzidis   return cxstring::createRef(cmd->Filename.c_str());
12074bcd21eSArgyrios Kyrtzidis }
12174bcd21eSArgyrios Kyrtzidis 
1222b3c8603SArnaud A. de Grandmaison unsigned
clang_CompileCommand_getNumArgs(CXCompileCommand CCmd)123fa6d73ccSArnaud A. de Grandmaison clang_CompileCommand_getNumArgs(CXCompileCommand CCmd)
1242b3c8603SArnaud A. de Grandmaison {
1252b3c8603SArnaud A. de Grandmaison   if (!CCmd)
1262b3c8603SArnaud A. de Grandmaison     return 0;
1272b3c8603SArnaud A. de Grandmaison 
1282b3c8603SArnaud A. de Grandmaison   return static_cast<CompileCommand *>(CCmd)->CommandLine.size();
1292b3c8603SArnaud A. de Grandmaison }
1302b3c8603SArnaud A. de Grandmaison 
1312b3c8603SArnaud A. de Grandmaison CXString
clang_CompileCommand_getArg(CXCompileCommand CCmd,unsigned Arg)132fa6d73ccSArnaud A. de Grandmaison clang_CompileCommand_getArg(CXCompileCommand CCmd, unsigned Arg)
1332b3c8603SArnaud A. de Grandmaison {
1342b3c8603SArnaud A. de Grandmaison   if (!CCmd)
135f98dfbaeSDmitri Gribenko     return cxstring::createNull();
1362b3c8603SArnaud A. de Grandmaison 
1372b3c8603SArnaud A. de Grandmaison   CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd);
1382b3c8603SArnaud A. de Grandmaison 
1392b3c8603SArnaud A. de Grandmaison   if (Arg >= Cmd->CommandLine.size())
140f98dfbaeSDmitri Gribenko     return cxstring::createNull();
1412b3c8603SArnaud A. de Grandmaison 
1423c66b0beSDmitri Gribenko   return cxstring::createRef(Cmd->CommandLine[Arg].c_str());
1432b3c8603SArnaud A. de Grandmaison }
1442b3c8603SArnaud A. de Grandmaison 
1456192c93aSManuel Klimek unsigned
clang_CompileCommand_getNumMappedSources(CXCompileCommand CCmd)1466192c93aSManuel Klimek clang_CompileCommand_getNumMappedSources(CXCompileCommand CCmd)
1476192c93aSManuel Klimek {
148*a1928042SKrasimir Georgiev   // Left here for backward compatibility. No mapped sources exists in the C++
149*a1928042SKrasimir Georgiev   // backend anymore.
1506192c93aSManuel Klimek   return 0;
1516192c93aSManuel Klimek }
1526192c93aSManuel Klimek 
1536192c93aSManuel Klimek CXString
clang_CompileCommand_getMappedSourcePath(CXCompileCommand CCmd,unsigned I)1546192c93aSManuel Klimek clang_CompileCommand_getMappedSourcePath(CXCompileCommand CCmd, unsigned I)
1556192c93aSManuel Klimek {
156*a1928042SKrasimir Georgiev   // Left here for backward compatibility. No mapped sources exists in the C++
157*a1928042SKrasimir Georgiev   // backend anymore.
1586192c93aSManuel Klimek   return cxstring::createNull();
1596192c93aSManuel Klimek }
1606192c93aSManuel Klimek 
1616192c93aSManuel Klimek CXString
clang_CompileCommand_getMappedSourceContent(CXCompileCommand CCmd,unsigned I)1626192c93aSManuel Klimek clang_CompileCommand_getMappedSourceContent(CXCompileCommand CCmd, unsigned I)
1636192c93aSManuel Klimek {
164*a1928042SKrasimir Georgiev   // Left here for backward compatibility. No mapped sources exists in the C++
165*a1928042SKrasimir Georgiev   // backend anymore.
1666192c93aSManuel Klimek   return cxstring::createNull();
1676192c93aSManuel Klimek }
168