1 #include "clang-c/CXCompilationDatabase.h"
3 #include "clang/Tooling/CompilationDatabase.h"
7 using namespace clang::tooling
;
9 // FIXME: do something more useful with the error message
11 clang_CompilationDatabase_fromDirectory(const char *BuildDir
,
12 CXCompilationDatabase_Error
*ErrorCode
)
15 CXCompilationDatabase_Error Err
= CXCompilationDatabase_NoError
;
17 std::unique_ptr
<CompilationDatabase
> db
=
18 CompilationDatabase::loadFromDirectory(BuildDir
, ErrorMsg
);
21 fprintf(stderr
, "LIBCLANG TOOLING ERROR: %s\n", ErrorMsg
.c_str());
22 Err
= CXCompilationDatabase_CanNotLoadDatabase
;
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
)) {}
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
));
52 return new AllocatedCXCompileCommands(std::move(CCmd
));
59 clang_CompilationDatabase_getAllCompileCommands(CXCompilationDatabase CDb
) {
60 if (CompilationDatabase
*db
= static_cast<CompilationDatabase
*>(CDb
)) {
61 std::vector
<CompileCommand
> CCmd(db
->getAllCompileCommands());
63 return new AllocatedCXCompileCommands(std::move(CCmd
));
70 clang_CompileCommands_dispose(CXCompileCommands Cmds
)
72 delete static_cast<AllocatedCXCompileCommands
*>(Cmds
);
76 clang_CompileCommands_getSize(CXCompileCommands Cmds
)
81 AllocatedCXCompileCommands
*ACC
=
82 static_cast<AllocatedCXCompileCommands
*>(Cmds
);
84 return ACC
->CCmd
.size();
88 clang_CompileCommands_getCommand(CXCompileCommands Cmds
, unsigned I
)
93 AllocatedCXCompileCommands
*ACC
=
94 static_cast<AllocatedCXCompileCommands
*>(Cmds
);
96 if (I
>= ACC
->CCmd
.size())
103 clang_CompileCommand_getDirectory(CXCompileCommand CCmd
)
106 return cxstring::createNull();
108 CompileCommand
*cmd
= static_cast<CompileCommand
*>(CCmd
);
109 return cxstring::createRef(cmd
->Directory
.c_str());
113 clang_CompileCommand_getFilename(CXCompileCommand CCmd
)
116 return cxstring::createNull();
118 CompileCommand
*cmd
= static_cast<CompileCommand
*>(CCmd
);
119 return cxstring::createRef(cmd
->Filename
.c_str());
123 clang_CompileCommand_getNumArgs(CXCompileCommand CCmd
)
128 return static_cast<CompileCommand
*>(CCmd
)->CommandLine
.size();
132 clang_CompileCommand_getArg(CXCompileCommand CCmd
, unsigned Arg
)
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());
146 clang_CompileCommand_getNumMappedSources(CXCompileCommand CCmd
)
148 // Left here for backward compatibility. No mapped sources exists in the C++
154 clang_CompileCommand_getMappedSourcePath(CXCompileCommand CCmd
, unsigned I
)
156 // Left here for backward compatibility. No mapped sources exists in the C++
158 return cxstring::createNull();
162 clang_CompileCommand_getMappedSourceContent(CXCompileCommand CCmd
, unsigned I
)
164 // Left here for backward compatibility. No mapped sources exists in the C++
166 return cxstring::createNull();