blob: 619173c85b80c9becc7a49d83a4d751de73f30b4 [file] [log] [blame]
Josh Gaof8592a32016-07-26 18:58:27 -07001/*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "Preprocessor.h"
18
19#include <err.h>
20#include <fcntl.h>
21#include <libgen.h>
22#include <string.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <unistd.h>
26
27#include <deque>
28#include <fstream>
29#include <string>
30#include <unordered_map>
31
32#include <llvm/ADT/StringRef.h>
33#include <llvm/ADT/Twine.h>
34
35#include "Arch.h"
36#include "DeclarationDatabase.h"
37#include "versioner.h"
38
39using namespace std::string_literals;
40
41static DeclarationAvailability calculateRequiredGuard(const Declaration& declaration) {
42 // To avoid redundant macro guards, the availability calculated by this function is the set
43 // difference of 'targets marked-available' from 'targets the declaration is visible in'.
44 // For example, a declaration that is visible always and introduced in 9 would return introduced
45 // in 9, but the same declaration, except only visible in 9+ would return an empty
46 // DeclarationAvailability.
47
48 // This currently only handles __INTRODUCED_IN.
49 // TODO: Do the same for __REMOVED_IN.
50 int global_min_api_visible = 0;
51 ArchMap<int> arch_visibility;
52
53 for (const auto& it : declaration.availability) {
54 const CompilationType& type = it.first;
55
56 if (global_min_api_visible == 0 || global_min_api_visible > type.api_level) {
57 global_min_api_visible = type.api_level;
58 }
59
60 if (arch_visibility[type.arch] == 0 || arch_visibility[type.arch] > type.api_level) {
61 arch_visibility[type.arch] = type.api_level;
62 }
63 }
64
65 DeclarationAvailability decl_av;
66 if (!declaration.calculateAvailability(&decl_av)) {
67 fprintf(stderr, "versioner: failed to calculate availability while preprocessing:\n");
68 declaration.dump("", stderr, 2);
69 exit(1);
70 }
71
72 D("Calculating required guard for %s:\n", declaration.name.c_str());
73 D(" Declaration availability: %s\n", to_string(decl_av).c_str());
74
75 if (verbose) {
76 std::string arch_visibility_str;
77 for (Arch arch : supported_archs) {
78 if (arch_visibility[arch] != 0) {
79 arch_visibility_str += to_string(arch);
80 arch_visibility_str += ": ";
81 arch_visibility_str += std::to_string(arch_visibility[arch]);
82 arch_visibility_str += ", ";
83 }
84 }
85 if (!arch_visibility_str.empty()) {
86 arch_visibility_str.resize(arch_visibility_str.size() - 2);
87 }
88 D(" Declaration visibility: global = %d, arch = %s\n", global_min_api_visible,
89 arch_visibility_str.c_str());
90 }
91
92 DeclarationAvailability result = decl_av;
93 if (result.global_availability.introduced < global_min_api_visible) {
94 result.global_availability.introduced = 0;
95 }
96
97 for (Arch arch : supported_archs) {
98 if (result.arch_availability[arch].introduced < arch_visibility[arch]) {
99 result.arch_availability[arch].introduced = 0;
100 }
101 }
102
103 D(" Calculated result: %s\n", to_string(result).c_str());
104 D("\n");
105
106 return result;
107}
108
109static std::deque<std::string> readFileLines(const std::string& path) {
110 std::ifstream is(path.c_str());
111 std::deque<std::string> result;
112 std::string line;
113
114 while (std::getline(is, line)) {
115 result.push_back(std::move(line));
116 }
117
118 return result;
119}
120
121static std::string dirname(const std::string& path) {
122 std::unique_ptr<char, decltype(&free)> path_copy(strdup(path.c_str()), free);
123 return dirname(path_copy.get());
124}
125
126static bool mkdirs(const std::string& path) {
127 struct stat st;
128 if (stat(path.c_str(), &st) == 0 && S_ISDIR(st.st_mode)) {
129 return true;
130 }
131
132 std::string parent = dirname(path);
133 if (parent == path) {
134 return false;
135 }
136
137 if (!mkdirs(parent)) {
138 return false;
139 }
140
141 if (mkdir(path.c_str(), 0700) != 0) {
142 return false;
143 }
144
145 return true;
146}
147
148static void writeFileLines(const std::string& path, const std::deque<std::string>& lines) {
149 if (!mkdirs(dirname(path))) {
150 err(1, "failed to create directory '%s'", dirname(path).c_str());
151 }
152
153 std::ofstream os(path.c_str(), std::ios_base::out | std::ios_base::trunc);
154
155 for (const std::string& line : lines) {
156 os << line << "\n";
157 }
158}
159
160using GuardMap = std::map<Location, DeclarationAvailability>;
161
162static std::string generateGuardCondition(const DeclarationAvailability& avail) {
163 // Logically orred expressions that constitute the macro guard.
164 std::vector<std::string> expressions;
165 static const std::vector<std::pair<std::string, std::set<Arch>>> arch_sets = {
166 { "", supported_archs },
167 { "!defined(__LP64__)", { Arch::arm, Arch::mips, Arch::x86 } },
168 { "defined(__LP64__)", { Arch::arm64, Arch::mips64, Arch::x86_64 } },
169 { "defined(__mips__)", { Arch::mips, Arch::mips64 } },
170 };
171 std::map<Arch, std::string> individual_archs = {
172 { Arch::arm, "defined(__arm__)" },
173 { Arch::arm64, "defined(__aarch64__)" },
174 { Arch::mips, "defined(__mips__) && !defined(__LP64__)" },
175 { Arch::mips64, "defined(__mips__) && defined(__LP64__)" },
176 { Arch::x86, "defined(__i386__)" },
177 { Arch::x86_64, "defined(__x86_64__)" },
178 };
179
180 auto generate_guard = [](const std::string& arch_expr, int min_version) {
181 if (min_version == 0) {
182 return arch_expr;
183 }
184 return arch_expr + " && __ANDROID_API__ >= " + std::to_string(min_version);
185 };
186
187 D("Generating guard for availability: %s\n", to_string(avail).c_str());
188 if (!avail.global_availability.empty()) {
189 for (Arch arch : supported_archs) {
190 if (!avail.arch_availability[arch].empty()) {
191 errx(1, "attempted to generate guard with global and per-arch values: %s",
192 to_string(avail).c_str());
193 }
194 }
195
196 if (avail.global_availability.introduced == 0) {
197 fprintf(stderr, "warning: attempted to generate guard with empty availability: %s\n",
198 to_string(avail).c_str());
199 return "";
200 }
201
202 if (avail.global_availability.introduced <= 9) {
203 return "";
204 }
205
206 return "__ANDROID_API__ >= "s + std::to_string(avail.global_availability.introduced);
207 }
208
209 for (const auto& it : arch_sets) {
210 const std::string& arch_expr = it.first;
211 const std::set<Arch>& archs = it.second;
212
213 D(" Checking arch set '%s'\n", arch_expr.c_str());
214
215 int version = avail.arch_availability[*it.second.begin()].introduced;
216
217 // Assume that the entire declaration is declared __INTRODUCED_IN_FUTURE if one arch is.
218 bool future = avail.arch_availability[*it.second.begin()].future;
219
220 if (future) {
221 return "0";
222 }
223
224 // The maximum min_version of the set.
225 int max_min_version = 0;
226 for (Arch arch : archs) {
227 if (arch_min_api[arch] > max_min_version) {
228 max_min_version = arch_min_api[arch];
229 }
230
231 if (avail.arch_availability[arch].introduced != version) {
232 D(" Skipping arch set, availability for %s doesn't match %s\n",
233 to_string(*it.second.begin()).c_str(), to_string(arch).c_str());
234 goto skip;
235 }
236 }
237
238 // If all of the archs in the set have a min_api that satifies version, elide the check.
239 if (max_min_version >= version) {
240 version = 0;
241 }
242
243 expressions.emplace_back(generate_guard(arch_expr, version));
244
245 D(" Generated expression '%s'\n", expressions.rbegin()->c_str());
246
247 for (Arch arch : archs) {
248 individual_archs.erase(arch);
249 }
250
251 skip:
252 continue;
253 }
254
255 for (const auto& it : individual_archs) {
256 const std::string& arch_expr = it.second;
257 int introduced = avail.arch_availability[it.first].introduced;
258 if (introduced == 0) {
259 expressions.emplace_back(arch_expr);
260 } else {
261 expressions.emplace_back(generate_guard(arch_expr, introduced));
262 }
263 }
264
265 if (expressions.size() == 0) {
266 errx(1, "generated empty guard for availability %s", to_string(avail).c_str());
267 } else if (expressions.size() == 1) {
268 return expressions[0];
269 }
270
271 return "("s + Join(expressions, ") || (") + ")";
272}
273
274// Assumes that nothing crazy is happening (e.g. having the semicolon be in a macro)
275static FileLocation findNextSemicolon(const std::deque<std::string>& lines, FileLocation start) {
276 unsigned current_line = start.line;
277 unsigned current_column = start.column;
278 while (current_line <= lines.size()) {
279 size_t result = lines[current_line - 1].find_first_of(';', current_column - 1);
280
281 if (result != std::string::npos) {
282 FileLocation loc = {
283 .line = current_line,
284 .column = unsigned(result) + 1,
285 };
286
287 return loc;
288 }
289
290 ++current_line;
291 current_column = 0;
292 }
293
294 errx(1, "failed to find semicolon starting from %u:%u", start.line, start.column);
295}
296
297// Merge adjacent blocks with identical guards.
298static void mergeGuards(std::deque<std::string>& file_lines, GuardMap& guard_map) {
299 if (guard_map.size() < 2) {
300 return;
301 }
302
303 auto current = guard_map.begin();
304 auto next = current;
305 ++next;
306
307 while (next != guard_map.end()) {
308 if (current->second != next->second) {
309 ++current;
310 ++next;
311 continue;
312 }
313
314 // Scan from the end of current to the beginning of next.
315 bool in_block_comment = false;
316 bool valid = true;
317
318 FileLocation current_location = current->first.end;
319 FileLocation end_location = next->first.start;
320
321 auto nextLine = [&current_location]() {
322 ++current_location.line;
323 current_location.column = 1;
324 };
325
326 auto nextCol = [&file_lines, &current_location, &nextLine]() {
327 if (current_location.column == file_lines[current_location.column - 1].length()) {
328 nextLine();
329 } else {
330 ++current_location.column;
331 }
332 };
333
334 // The end location will point to the semicolon, which we don't want to read, so skip it.
335 nextCol();
336
337 while (current_location < end_location) {
338 const std::string& line = file_lines[current_location.line - 1];
339 size_t line_index = current_location.column - 1;
340
341 if (in_block_comment) {
342 size_t pos = line.find("*/", line_index);
343 if (pos == std::string::npos) {
344 D("Didn't find block comment terminator, skipping line\n");
345 nextLine();
346 continue;
347 } else {
348 D("Found block comment terminator\n");
349 in_block_comment = false;
350 current_location.column = pos + 2;
351 nextCol();
352 continue;
353 }
354 } else {
355 size_t pos = line.find_first_not_of(" \t", line_index);
356 if (pos == std::string::npos) {
357 nextLine();
358 continue;
359 }
360
361 current_location.column = pos + 1;
362 if (line[pos] != '/') {
363 D("Trailing character '%c' is not a slash: %s\n", line[pos], line.substr(pos).c_str());
364 valid = false;
365 break;
366 }
367
368 nextCol();
369 if (line.length() <= pos + 1) {
370 // Trailing slash at the end of a line?
371 D("Trailing slash at end of line\n");
372 valid = false;
373 break;
374 }
375
376 if (line[pos + 1] == '/') {
377 // C++ style comment
378 nextLine();
379 } else if (line[pos + 1] == '*') {
380 // Block comment
381 nextCol();
382 in_block_comment = true;
383 D("In a block comment\n");
384 } else {
385 // Garbage?
386 D("Unexpected output after /: %s\n", line.substr(pos).c_str());
387 valid = false;
388 break;
389 }
390 }
391 }
392
393 if (!valid) {
394 D("Not merging blocks %s and %s\n", to_string(current->first).c_str(),
395 to_string(next->first).c_str());
396 ++current;
397 ++next;
398 continue;
399 }
400
401 D("Merging blocks %s and %s\n", to_string(current->first).c_str(),
402 to_string(next->first).c_str());
403
404 Location merged = current->first;
405 merged.end = next->first.end;
406
407 DeclarationAvailability avail = current->second;
408
409 guard_map.erase(current);
410 guard_map.erase(next);
411 bool dummy;
412 std::tie(current, dummy) = guard_map.insert(std::make_pair(merged, avail));
413 next = current;
414 ++next;
415 }
416}
417
418static void rewriteFile(const std::string& output_path, std::deque<std::string>& file_lines,
419 const GuardMap& guard_map) {
420 for (auto it = guard_map.rbegin(); it != guard_map.rend(); ++it) {
421 const Location& loc = it->first;
422 const DeclarationAvailability& avail = it->second;
423
424 std::string condition = generateGuardCondition(avail);
425 if (condition.empty()) {
426 continue;
427 }
428
429 std::string prologue = "\n#if "s + condition + "\n";
430 std::string epilogue = "\n#endif /* " + condition + " */\n";
431
432 file_lines[loc.end.line - 1].insert(loc.end.column, epilogue);
433 file_lines[loc.start.line - 1].insert(loc.start.column - 1, prologue);
434 }
435
436 printf("Preprocessing %s...\n", output_path.c_str());
437 writeFileLines(output_path, file_lines);
438}
439
440bool preprocessHeaders(const std::string& dst_dir, const std::string& src_dir,
441 HeaderDatabase* database) {
442 std::unordered_map<std::string, GuardMap> guards;
443 std::unordered_map<std::string, std::deque<std::string>> file_lines;
444
445 for (const auto& symbol_it : database->symbols) {
446 const Symbol& symbol = symbol_it.second;
447
448 for (const auto& decl_it : symbol.declarations) {
449 const Location& location = decl_it.first;
450 const Declaration& decl = decl_it.second;
451
452 DeclarationAvailability macro_guard = calculateRequiredGuard(decl);
453 if (!macro_guard.empty()) {
454 guards[location.filename][location] = macro_guard;
455 }
456 }
457 }
458
459 for (const auto& file_it : guards) {
460 file_lines[file_it.first] = readFileLines(file_it.first);
461 }
462
463 for (auto& file_it : guards) {
464 llvm::StringRef file_path = file_it.first;
465 GuardMap& orig_guard_map = file_it.second;
466
467 // The end positions given to us are the end of the declaration, which is some point before the
468 // semicolon. Fix up the end positions by scanning for the next semicolon.
469 GuardMap guard_map;
470 for (const auto& it : orig_guard_map) {
471 Location loc = it.first;
472 loc.end = findNextSemicolon(file_lines[file_path], loc.end);
473 guard_map[loc] = it.second;
474 }
475
476 // TODO: Make sure that the Locations don't overlap.
477 // TODO: Merge adjacent non-identical guards.
478 mergeGuards(file_lines[file_path], guard_map);
479
480 if (!file_path.startswith(src_dir)) {
481 errx(1, "input file %s is not in %s\n", file_path.str().c_str(), src_dir.c_str());
482 }
483
484 // rel_path has a leading slash.
485 llvm::StringRef rel_path = file_path.substr(src_dir.size(), file_path.size() - src_dir.size());
486 std::string output_path = (llvm::Twine(dst_dir) + rel_path).str();
487
488 rewriteFile(output_path, file_lines[file_path], guard_map);
489 }
490
491 return true;
492}