/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/MemAlloc.h" |
20 | | #include "llvm/Support/Path.h" |
21 | | #include "llvm/Support/VirtualFileSystem.h" |
22 | | #include "llvm/Support/raw_ostream.h" |
23 | | |
24 | | using namespace clang; |
25 | | using namespace llvm::sys; |
26 | | |
27 | 0 | unsigned long long clang_getBuildSessionTimestamp(void) { |
28 | 0 | return llvm::sys::toTimeT(std::chrono::system_clock::now()); |
29 | 0 | } |
30 | | |
31 | | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::vfs::YAMLVFSWriter, |
32 | | CXVirtualFileOverlay) |
33 | | |
34 | 9 | CXVirtualFileOverlay clang_VirtualFileOverlay_create(unsigned) { |
35 | 9 | return wrap(new llvm::vfs::YAMLVFSWriter()); |
36 | 9 | } |
37 | | |
38 | | enum CXErrorCode |
39 | | clang_VirtualFileOverlay_addFileMapping(CXVirtualFileOverlay VFO, |
40 | | const char *virtualPath, |
41 | 16 | const char *realPath) { |
42 | 16 | if (!VFO || !virtualPath || !realPath) |
43 | 0 | return CXError_InvalidArguments; |
44 | 16 | if (!path::is_absolute(virtualPath)) |
45 | 0 | return CXError_InvalidArguments; |
46 | 16 | if (!path::is_absolute(realPath)) |
47 | 0 | return CXError_InvalidArguments; |
48 | | |
49 | 16 | for (path::const_iterator |
50 | 16 | PI = path::begin(virtualPath), |
51 | 81 | PE = path::end(virtualPath); PI != PE; ++PI65 ) { |
52 | 66 | StringRef Comp = *PI; |
53 | 66 | if (Comp == "." || Comp == ".."65 ) |
54 | 1 | return CXError_InvalidArguments; |
55 | 66 | } |
56 | | |
57 | 15 | unwrap(VFO)->addFileMapping(virtualPath, realPath); |
58 | 15 | return CXError_Success; |
59 | 16 | } |
60 | | |
61 | | enum CXErrorCode |
62 | | clang_VirtualFileOverlay_setCaseSensitivity(CXVirtualFileOverlay VFO, |
63 | 1 | int caseSensitive) { |
64 | 1 | if (!VFO) |
65 | 0 | return CXError_InvalidArguments; |
66 | 1 | unwrap(VFO)->setCaseSensitivity(caseSensitive); |
67 | 1 | return CXError_Success; |
68 | 1 | } |
69 | | |
70 | | enum CXErrorCode |
71 | | clang_VirtualFileOverlay_writeToBuffer(CXVirtualFileOverlay VFO, unsigned, |
72 | | char **out_buffer_ptr, |
73 | 8 | unsigned *out_buffer_size) { |
74 | 8 | if (!VFO || !out_buffer_ptr || !out_buffer_size) |
75 | 0 | return CXError_InvalidArguments; |
76 | | |
77 | 8 | llvm::SmallString<256> Buf; |
78 | 8 | llvm::raw_svector_ostream OS(Buf); |
79 | 8 | unwrap(VFO)->write(OS); |
80 | | |
81 | 8 | StringRef Data = OS.str(); |
82 | 8 | *out_buffer_ptr = static_cast<char*>(llvm::safe_malloc(Data.size())); |
83 | 8 | *out_buffer_size = Data.size(); |
84 | 8 | memcpy(*out_buffer_ptr, Data.data(), Data.size()); |
85 | 8 | return CXError_Success; |
86 | 8 | } |
87 | | |
88 | 9 | void clang_free(void *buffer) { |
89 | 9 | free(buffer); |
90 | 9 | } |
91 | | |
92 | 9 | void clang_VirtualFileOverlay_dispose(CXVirtualFileOverlay VFO) { |
93 | 9 | delete unwrap(VFO); |
94 | 9 | } |
95 | | |
96 | | |
97 | | struct CXModuleMapDescriptorImpl { |
98 | | std::string ModuleName; |
99 | | std::string UmbrellaHeader; |
100 | | }; |
101 | | |
102 | 1 | CXModuleMapDescriptor clang_ModuleMapDescriptor_create(unsigned) { |
103 | 1 | return new CXModuleMapDescriptorImpl(); |
104 | 1 | } |
105 | | |
106 | | enum CXErrorCode |
107 | | clang_ModuleMapDescriptor_setFrameworkModuleName(CXModuleMapDescriptor MMD, |
108 | 1 | const char *name) { |
109 | 1 | if (!MMD || !name) |
110 | 0 | return CXError_InvalidArguments; |
111 | | |
112 | 1 | MMD->ModuleName = name; |
113 | 1 | return CXError_Success; |
114 | 1 | } |
115 | | |
116 | | enum CXErrorCode |
117 | | clang_ModuleMapDescriptor_setUmbrellaHeader(CXModuleMapDescriptor MMD, |
118 | 1 | const char *name) { |
119 | 1 | if (!MMD || !name) |
120 | 0 | return CXError_InvalidArguments; |
121 | | |
122 | 1 | MMD->UmbrellaHeader = name; |
123 | 1 | return CXError_Success; |
124 | 1 | } |
125 | | |
126 | | enum CXErrorCode |
127 | | clang_ModuleMapDescriptor_writeToBuffer(CXModuleMapDescriptor MMD, unsigned, |
128 | | char **out_buffer_ptr, |
129 | 1 | unsigned *out_buffer_size) { |
130 | 1 | if (!MMD || !out_buffer_ptr || !out_buffer_size) |
131 | 0 | return CXError_InvalidArguments; |
132 | | |
133 | 1 | llvm::SmallString<256> Buf; |
134 | 1 | llvm::raw_svector_ostream OS(Buf); |
135 | 1 | OS << "framework module " << MMD->ModuleName << " {\n"; |
136 | 1 | OS << " umbrella header \""; |
137 | 1 | OS.write_escaped(MMD->UmbrellaHeader) << "\"\n"; |
138 | 1 | OS << '\n'; |
139 | 1 | OS << " export *\n"; |
140 | 1 | OS << " module * { export * }\n"; |
141 | 1 | OS << "}\n"; |
142 | | |
143 | 1 | StringRef Data = OS.str(); |
144 | 1 | *out_buffer_ptr = static_cast<char*>(llvm::safe_malloc(Data.size())); |
145 | 1 | *out_buffer_size = Data.size(); |
146 | 1 | memcpy(*out_buffer_ptr, Data.data(), Data.size()); |
147 | 1 | return CXError_Success; |
148 | 1 | } |
149 | | |
150 | 1 | void clang_ModuleMapDescriptor_dispose(CXModuleMapDescriptor MMD) { |
151 | 1 | delete MMD; |
152 | 1 | } |