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