Coverage Report

Created: 2020-02-18 08:44

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/tools/libclang/CXCompilationDatabase.cpp
Line
Count
Source (jump to first uncovered line)
1
#include "clang-c/CXCompilationDatabase.h"
2
#include "CXString.h"
3
#include "clang/Tooling/CompilationDatabase.h"
4
#include <cstdio>
5
6
using namespace clang;
7
using namespace clang::tooling;
8
9
// FIXME: do something more useful with the error message
10
CXCompilationDatabase
11
clang_CompilationDatabase_fromDirectory(const char *BuildDir,
12
                                        CXCompilationDatabase_Error *ErrorCode)
13
1
{
14
1
  std::string ErrorMsg;
15
1
  CXCompilationDatabase_Error Err = CXCompilationDatabase_NoError;
16
1
17
1
  std::unique_ptr<CompilationDatabase> db =
18
1
      CompilationDatabase::loadFromDirectory(BuildDir, ErrorMsg);
19
1
20
1
  if (!db) {
21
0
    fprintf(stderr, "LIBCLANG TOOLING ERROR: %s\n", ErrorMsg.c_str());
22
0
    Err = CXCompilationDatabase_CanNotLoadDatabase;
23
0
  }
24
1
25
1
  if (ErrorCode)
26
1
    *ErrorCode = Err;
27
1
28
1
  return db.release();
29
1
}
30
31
void
32
clang_CompilationDatabase_dispose(CXCompilationDatabase CDb)
33
1
{
34
1
  delete static_cast<CompilationDatabase *>(CDb);
35
1
}
36
37
struct AllocatedCXCompileCommands
38
{
39
  std::vector<CompileCommand> CCmd;
40
41
  AllocatedCXCompileCommands(std::vector<CompileCommand> Cmd)
42
1
      : CCmd(std::move(Cmd)) {}
43
};
44
45
CXCompileCommands
46
clang_CompilationDatabase_getCompileCommands(CXCompilationDatabase CDb,
47
                                             const char *CompleteFileName)
48
0
{
49
0
  if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
50
0
    std::vector<CompileCommand> CCmd(db->getCompileCommands(CompleteFileName));
51
0
    if (!CCmd.empty())
52
0
      return new AllocatedCXCompileCommands(std::move(CCmd));
53
0
  }
54
0
55
0
  return nullptr;
56
0
}
57
58
CXCompileCommands
59
1
clang_CompilationDatabase_getAllCompileCommands(CXCompilationDatabase CDb) {
60
1
  if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
61
1
    std::vector<CompileCommand> CCmd(db->getAllCompileCommands());
62
1
    if (!CCmd.empty())
63
1
      return new AllocatedCXCompileCommands(std::move(CCmd));
64
0
  }
65
0
66
0
  return nullptr;
67
0
}
68
69
void
70
clang_CompileCommands_dispose(CXCompileCommands Cmds)
71
1
{
72
1
  delete static_cast<AllocatedCXCompileCommands *>(Cmds);
73
1
}
74
75
unsigned
76
clang_CompileCommands_getSize(CXCompileCommands Cmds)
77
1
{
78
1
  if (!Cmds)
79
0
    return 0;
80
1
81
1
  AllocatedCXCompileCommands *ACC =
82
1
    static_cast<AllocatedCXCompileCommands *>(Cmds);
83
1
84
1
  return ACC->CCmd.size();
85
1
}
86
87
CXCompileCommand
88
clang_CompileCommands_getCommand(CXCompileCommands Cmds, unsigned I)
89
3
{
90
3
  if (!Cmds)
91
0
    return nullptr;
92
3
93
3
  AllocatedCXCompileCommands *ACC =
94
3
    static_cast<AllocatedCXCompileCommands *>(Cmds);
95
3
96
3
  if (I >= ACC->CCmd.size())
97
0
    return nullptr;
98
3
99
3
  return &ACC->CCmd[I];
100
3
}
101
102
CXString
103
clang_CompileCommand_getDirectory(CXCompileCommand CCmd)
104
3
{
105
3
  if (!CCmd)
106
0
    return cxstring::createNull();
107
3
108
3
  CompileCommand *cmd = static_cast<CompileCommand *>(CCmd);
109
3
  return cxstring::createRef(cmd->Directory.c_str());
110
3
}
111
112
CXString
113
clang_CompileCommand_getFilename(CXCompileCommand CCmd)
114
0
{
115
0
  if (!CCmd)
116
0
    return cxstring::createNull();
117
0
118
0
  CompileCommand *cmd = static_cast<CompileCommand *>(CCmd);
119
0
  return cxstring::createRef(cmd->Filename.c_str());
120
0
}
121
122
unsigned
123
clang_CompileCommand_getNumArgs(CXCompileCommand CCmd)
124
3
{
125
3
  if (!CCmd)
126
0
    return 0;
127
3
128
3
  return static_cast<CompileCommand *>(CCmd)->CommandLine.size();
129
3
}
130
131
CXString
132
clang_CompileCommand_getArg(CXCompileCommand CCmd, unsigned Arg)
133
20
{
134
20
  if (!CCmd)
135
0
    return cxstring::createNull();
136
20
137
20
  CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd);
138
20
139
20
  if (Arg >= Cmd->CommandLine.size())
140
0
    return cxstring::createNull();
141
20
142
20
  return cxstring::createRef(Cmd->CommandLine[Arg].c_str());
143
20
}
144
145
unsigned
146
clang_CompileCommand_getNumMappedSources(CXCompileCommand CCmd)
147
0
{
148
0
  // Left here for backward compatibility. No mapped sources exists in the C++
149
0
  // backend anymore.
150
0
  return 0;
151
0
}
152
153
CXString
154
clang_CompileCommand_getMappedSourcePath(CXCompileCommand CCmd, unsigned I)
155
0
{
156
0
  // Left here for backward compatibility. No mapped sources exists in the C++
157
0
  // backend anymore.
158
0
  return cxstring::createNull();
159
0
}
160
161
CXString
162
clang_CompileCommand_getMappedSourceContent(CXCompileCommand CCmd, unsigned I)
163
0
{
164
0
  // Left here for backward compatibility. No mapped sources exists in the C++
165
0
  // backend anymore.
166
0
  return cxstring::createNull();
167
0
}