Coverage Report

Created: 2020-10-24 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/tools/libclang/BuildSystem.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- BuildSystem.cpp - Utilities for use by build systems ---------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file implements various utilities for use by build systems.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang-c/BuildSystem.h"
14
#include "CXString.h"
15
#include "llvm/ADT/SmallString.h"
16
#include "llvm/Support/CBindingWrapping.h"
17
#include "llvm/Support/Chrono.h"
18
#include "llvm/Support/ErrorHandling.h"
19
#include "llvm/Support/Path.h"
20
#include "llvm/Support/VirtualFileSystem.h"
21
#include "llvm/Support/raw_ostream.h"
22
23
using namespace clang;
24
using namespace llvm::sys;
25
26
0
unsigned long long clang_getBuildSessionTimestamp(void) {
27
0
  return llvm::sys::toTimeT(std::chrono::system_clock::now());
28
0
}
29
30
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::vfs::YAMLVFSWriter,
31
                                   CXVirtualFileOverlay)
32
33
9
CXVirtualFileOverlay clang_VirtualFileOverlay_create(unsigned) {
34
9
  return wrap(new llvm::vfs::YAMLVFSWriter());
35
9
}
36
37
enum CXErrorCode
38
clang_VirtualFileOverlay_addFileMapping(CXVirtualFileOverlay VFO,
39
                                        const char *virtualPath,
40
16
                                        const char *realPath) {
41
16
  if (!VFO || !virtualPath || !realPath)
42
0
    return CXError_InvalidArguments;
43
16
  if (!path::is_absolute(virtualPath))
44
0
    return CXError_InvalidArguments;
45
16
  if (!path::is_absolute(realPath))
46
0
    return CXError_InvalidArguments;
47
48
16
  for (path::const_iterator
49
16
         PI = path::begin(virtualPath),
50
81
         PE = path::end(virtualPath); PI != PE; 
++PI65
) {
51
66
    StringRef Comp = *PI;
52
66
    if (Comp == "." || 
Comp == ".."65
)
53
1
      return CXError_InvalidArguments;
54
66
  }
55
56
15
  unwrap(VFO)->addFileMapping(virtualPath, realPath);
57
15
  return CXError_Success;
58
16
}
59
60
enum CXErrorCode
61
clang_VirtualFileOverlay_setCaseSensitivity(CXVirtualFileOverlay VFO,
62
1
                                            int caseSensitive) {
63
1
  if (!VFO)
64
0
    return CXError_InvalidArguments;
65
1
  unwrap(VFO)->setCaseSensitivity(caseSensitive);
66
1
  return CXError_Success;
67
1
}
68
69
enum CXErrorCode
70
clang_VirtualFileOverlay_writeToBuffer(CXVirtualFileOverlay VFO, unsigned,
71
                                       char **out_buffer_ptr,
72
8
                                       unsigned *out_buffer_size) {
73
8
  if (!VFO || !out_buffer_ptr || !out_buffer_size)
74
0
    return CXError_InvalidArguments;
75
76
8
  llvm::SmallString<256> Buf;
77
8
  llvm::raw_svector_ostream OS(Buf);
78
8
  unwrap(VFO)->write(OS);
79
80
8
  StringRef Data = OS.str();
81
8
  *out_buffer_ptr = static_cast<char*>(llvm::safe_malloc(Data.size()));
82
8
  *out_buffer_size = Data.size();
83
8
  memcpy(*out_buffer_ptr, Data.data(), Data.size());
84
8
  return CXError_Success;
85
8
}
86
87
9
void clang_free(void *buffer) {
88
9
  free(buffer);
89
9
}
90
91
9
void clang_VirtualFileOverlay_dispose(CXVirtualFileOverlay VFO) {
92
9
  delete unwrap(VFO);
93
9
}
94
95
96
struct CXModuleMapDescriptorImpl {
97
  std::string ModuleName;
98
  std::string UmbrellaHeader;
99
};
100
101
1
CXModuleMapDescriptor clang_ModuleMapDescriptor_create(unsigned) {
102
1
  return new CXModuleMapDescriptorImpl();
103
1
}
104
105
enum CXErrorCode
106
clang_ModuleMapDescriptor_setFrameworkModuleName(CXModuleMapDescriptor MMD,
107
1
                                                 const char *name) {
108
1
  if (!MMD || !name)
109
0
    return CXError_InvalidArguments;
110
111
1
  MMD->ModuleName = name;
112
1
  return CXError_Success;
113
1
}
114
115
enum CXErrorCode
116
clang_ModuleMapDescriptor_setUmbrellaHeader(CXModuleMapDescriptor MMD,
117
1
                                            const char *name) {
118
1
  if (!MMD || !name)
119
0
    return CXError_InvalidArguments;
120
121
1
  MMD->UmbrellaHeader = name;
122
1
  return CXError_Success;
123
1
}
124
125
enum CXErrorCode
126
clang_ModuleMapDescriptor_writeToBuffer(CXModuleMapDescriptor MMD, unsigned,
127
                                       char **out_buffer_ptr,
128
1
                                       unsigned *out_buffer_size) {
129
1
  if (!MMD || !out_buffer_ptr || !out_buffer_size)
130
0
    return CXError_InvalidArguments;
131
132
1
  llvm::SmallString<256> Buf;
133
1
  llvm::raw_svector_ostream OS(Buf);
134
1
  OS << "framework module " << MMD->ModuleName << " {\n";
135
1
  OS << "  umbrella header \"";
136
1
  OS.write_escaped(MMD->UmbrellaHeader) << "\"\n";
137
1
  OS << '\n';
138
1
  OS << "  export *\n";
139
1
  OS << "  module * { export * }\n";
140
1
  OS << "}\n";
141
142
1
  StringRef Data = OS.str();
143
1
  *out_buffer_ptr = static_cast<char*>(llvm::safe_malloc(Data.size()));
144
1
  *out_buffer_size = Data.size();
145
1
  memcpy(*out_buffer_ptr, Data.data(), Data.size());
146
1
  return CXError_Success;
147
1
}
148
149
1
void clang_ModuleMapDescriptor_dispose(CXModuleMapDescriptor MMD) {
150
1
  delete MMD;
151
1
}