1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
|
//===--- IndexRecord.cpp --------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/Index/IndexRecord.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTPrinter.h"
#include "swift/AST/Decl.h"
#include "swift/AST/DiagnosticsFrontend.h"
#include "swift/AST/Expr.h"
#include "swift/AST/Module.h"
#include "swift/AST/ModuleLoader.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/Pattern.h"
#include "swift/AST/SourceFile.h"
#include "swift/AST/Stmt.h"
#include "swift/AST/Types.h"
#include "swift/Basic/PathRemapper.h"
#include "swift/ClangImporter/ClangModule.h"
#include "swift/IDE/ModuleInterfacePrinting.h"
#include "swift/Index/Index.h"
#include "clang/Basic/FileManager.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Index/IndexRecordWriter.h"
#include "clang/Index/IndexUnitWriter.h"
#include "clang/Index/IndexingAction.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Serialization/ASTReader.h"
#include "llvm/Support/Path.h"
using namespace swift;
using namespace swift::index;
using clang::index::IndexUnitWriter;
using clang::index::IndexRecordWriter;
using clang::index::SymbolRole;
using clang::index::SymbolRoleSet;
//===----------------------------------------------------------------------===//
// Index data collection and record writing
//===----------------------------------------------------------------------===//
namespace {
class SymbolTracker {
public:
struct SymbolRelation {
size_t symbolIndex;
SymbolRoleSet roles;
llvm::hash_code hash() const { return llvm::hash_combine(symbolIndex, roles); }
};
struct SymbolOccurrence {
size_t symbolIndex;
SymbolRoleSet roles;
unsigned line;
unsigned column;
SmallVector<SymbolRelation, 3> related;
llvm::hash_code hash() const {
auto hash = llvm::hash_combine(symbolIndex, roles, line, column);
for (auto &relation : related) {
hash = llvm::hash_combine(hash, relation.hash());
}
return hash;
}
};
struct Symbol {
StringRef name;
StringRef USR;
StringRef group;
SymbolInfo symInfo;
unsigned isTestCandidate : 1;
llvm::hash_code hash() const {
return llvm::hash_combine(
name, USR, group,
static_cast<unsigned>(symInfo.Kind),
static_cast<unsigned>(symInfo.SubKind),
symInfo.Properties, isTestCandidate);
}
};
Symbol *getSymbol(size_t index) {
assert(index < symbols.size());
return &symbols[index];
}
ArrayRef<SymbolOccurrence> getOccurrences() {
if (!sorted) {
std::stable_sort(occurrences.begin(), occurrences.end(),
[](const SymbolOccurrence &a, const SymbolOccurrence& b) {
if (a.line < b.line)
return true;
if (b.line < a.line)
return false;
return a.column < b.column;
});
sorted = true;
}
return occurrences;
}
size_t addSymbol(const IndexRelation &indexSym) {
auto pair = USRToSymbol.insert(std::make_pair(indexSym.USR.data(),
symbols.size()));
if (pair.second) {
Symbol symbol{indexSym.name,
indexSym.USR,
indexSym.group,
indexSym.symInfo,
0};
recordHash = llvm::hash_combine(recordHash, symbol.hash());
symbols.push_back(std::move(symbol));
}
return pair.first->second;
}
void addOccurrence(const IndexSymbol &indexOccur) {
sorted = false;
SmallVector<SymbolRelation, 3> relations;
for(IndexRelation indexRel: indexOccur.Relations) {
relations.push_back({addSymbol(indexRel), indexRel.roles});
}
occurrences.push_back({/*symbolIndex=*/addSymbol(indexOccur),
indexOccur.roles,
indexOccur.line,
indexOccur.column,
std::move(relations)});
recordHash = llvm::hash_combine(recordHash, occurrences.back().hash());
}
llvm::hash_code hashRecord() const { return recordHash; }
private:
llvm::DenseMap<const char *, size_t> USRToSymbol;
std::vector<Symbol> symbols;
std::vector<SymbolOccurrence> occurrences;
bool sorted = false;
llvm::hash_code recordHash = 0;
};
class IndexRecordingConsumer : public IndexDataConsumer {
SymbolTracker record;
// Keep a USR map to uniquely identify Decls.
// FIXME: if we just passed the original Decl * through we could use that,
// which would also let us avoid producing the USR/Name/etc. for decls unless
// we actually need it (once per Decl instead of once per occurrence).
std::vector<IndexSymbol> symbolStack;
bool includeLocals;
std::function<void(SymbolTracker &)> onFinish;
public:
IndexRecordingConsumer(bool includeLocals,
std::function<void(SymbolTracker &)> onFinish)
: includeLocals(includeLocals), onFinish(std::move(onFinish)) {}
void failed(StringRef error) override {
// FIXME: expose errors?
}
bool startDependency(StringRef name, StringRef path, bool isClangModule, bool isSystem) override {
return true;
}
bool finishDependency(bool isClangModule) override { return true; }
Action startSourceEntity(const IndexSymbol &symbol) override {
symbolStack.push_back(symbol);
return Action::Continue;
}
bool finishSourceEntity(SymbolInfo sym, SymbolRoleSet roles) override {
IndexSymbol symbol = std::move(symbolStack.back());
symbolStack.pop_back();
assert(!symbol.USR.empty());
record.addOccurrence(symbol);
return true;
}
void finish() override { onFinish(record); }
bool indexLocals() override { return includeLocals; }
};
class StdlibGroupsIndexRecordingConsumer : public IndexDataConsumer {
llvm::StringMap<std::unique_ptr<SymbolTracker>> TrackerByGroup;
// Keep a USR map to uniquely identify Decls.
// FIXME: if we just passed the original Decl * through we could use that,
// which would also let us avoid producing the USR/Name/etc. for decls unless
// we actually need it (once per Decl instead of once per occurrence).
std::vector<IndexSymbol> symbolStack;
std::function<bool(StringRef groupName, SymbolTracker &)> onFinish;
public:
StdlibGroupsIndexRecordingConsumer(std::function<bool(StringRef groupName, SymbolTracker &)> onFinish)
: onFinish(std::move(onFinish)) {}
void failed(StringRef error) override {
// FIXME: expose errors?
}
bool startDependency(StringRef name, StringRef path, bool isClangModule, bool isSystem) override {
return true;
}
bool finishDependency(bool isClangModule) override { return true; }
Action startSourceEntity(const IndexSymbol &symbol) override {
symbolStack.push_back(symbol);
return Action::Continue;
}
bool finishSourceEntity(SymbolInfo sym, SymbolRoleSet roles) override {
IndexSymbol symbol = std::move(symbolStack.back());
symbolStack.pop_back();
assert(!symbol.USR.empty());
StringRef groupName = findGroupForSymbol(symbol);
auto &tracker = TrackerByGroup[groupName];
if (!tracker) {
tracker = std::make_unique<SymbolTracker>();
}
tracker->addOccurrence(symbol);
return true;
}
void finish() override {
SmallVector<std::pair<StringRef, SymbolTracker *>, 0> SortedGroups;
for (auto &entry : TrackerByGroup) {
SortedGroups.emplace_back(entry.first(), entry.second.get());
}
llvm::sort(SortedGroups, llvm::less_first());
for (auto &pair : SortedGroups) {
StringRef groupName = pair.first;
SymbolTracker *tracker = pair.second;
bool cont = onFinish(groupName, *tracker);
if (!cont)
break;
}
}
private:
StringRef findGroupForSymbol(const IndexSymbol &sym);
};
} // end anonymous namespace
static StringRef findGroupNameForDecl(const Decl *D) {
if (!D || isa<ModuleDecl>(D) || isa<TopLevelCodeDecl>(D))
return StringRef();
auto groupNameOpt = D->getGroupName();
if (groupNameOpt)
return *groupNameOpt;
return findGroupNameForDecl(D->getDeclContext()->getInnermostDeclarationDeclContext());
}
StringRef StdlibGroupsIndexRecordingConsumer::findGroupForSymbol(const IndexSymbol &sym) {
bool isDeclOrDef = sym.roles & ((SymbolRoleSet)SymbolRole::Declaration | (SymbolRoleSet)SymbolRole::Definition);
if (isDeclOrDef) {
if (!sym.group.empty())
return sym.group;
return findGroupNameForDecl(sym.decl);
}
for (auto &rel : sym.Relations) {
if (!rel.group.empty())
return rel.group;
if (rel.decl)
return findGroupNameForDecl(rel.decl);
}
llvm_unreachable("did not find group name for reference");
}
static bool writeRecord(SymbolTracker &record, std::string Filename,
std::string indexStorePath, DiagnosticEngine *diags,
std::string &outRecordFile) {
IndexRecordWriter recordWriter(indexStorePath);
std::string error;
auto result = recordWriter.beginRecord(
Filename, record.hashRecord(), error, &outRecordFile);
switch (result) {
case IndexRecordWriter::Result::Failure:
diags->diagnose(SourceLoc(), diag::error_write_index_record, error);
return true;
case IndexRecordWriter::Result::AlreadyExists:
return false;
case IndexRecordWriter::Result::Success:
break;
}
for (auto &occurrence : record.getOccurrences()) {
SmallVector<clang::index::writer::SymbolRelation, 3> relations;
for(SymbolTracker::SymbolRelation symbolRelation: occurrence.related) {
relations.push_back({record.getSymbol(symbolRelation.symbolIndex), symbolRelation.roles});
}
recordWriter.addOccurrence(
record.getSymbol(occurrence.symbolIndex), occurrence.roles,
occurrence.line, occurrence.column, relations);
}
result = recordWriter.endRecord(error,
[&](clang::index::writer::OpaqueDecl opaqueSymbol,
SmallVectorImpl<char> &scratch) {
auto *symbol = static_cast<const SymbolTracker::Symbol *>(opaqueSymbol);
clang::index::writer::Symbol result;
result.SymInfo = symbol->symInfo;
result.Name = symbol->name;
result.USR = symbol->USR;
result.CodeGenName = ""; // FIXME
return result;
});
if (result == IndexRecordWriter::Result::Failure) {
diags->diagnose(SourceLoc(), diag::error_write_index_record, error);
return true;
}
return false;
}
static std::unique_ptr<IndexRecordingConsumer>
makeRecordingConsumer(std::string Filename, std::string indexStorePath,
bool includeLocals, DiagnosticEngine *diags,
std::string *outRecordFile,
bool *outFailed) {
return std::make_unique<IndexRecordingConsumer>(includeLocals,
[=](SymbolTracker &record) {
*outFailed = writeRecord(record, Filename, indexStorePath, diags,
*outRecordFile);
});
}
static bool
recordSourceFile(SourceFile *SF, StringRef indexStorePath,
bool includeLocals, DiagnosticEngine &diags,
llvm::function_ref<void(StringRef, StringRef)> callback) {
std::string recordFile;
bool failed = false;
auto consumer =
makeRecordingConsumer(SF->getFilename().str(), indexStorePath.str(),
includeLocals, &diags, &recordFile, &failed);
indexSourceFile(SF, *consumer);
if (!failed && !recordFile.empty())
callback(recordFile, SF->getFilename());
return failed;
}
//===----------------------------------------------------------------------===//
// Index unit file writing
//===----------------------------------------------------------------------===//
// Used to get std::string pointers to pass as writer::OpaqueModule.
namespace {
class StringScratchSpace {
std::vector<std::unique_ptr<std::string>> StrsCreated;
public:
const std::string *createString(StringRef str) {
StrsCreated.emplace_back(std::make_unique<std::string>(str));
return StrsCreated.back().get();
}
};
}
static clang::index::writer::ModuleInfo
getModuleInfoFromOpaqueModule(clang::index::writer::OpaqueModule mod,
SmallVectorImpl<char> &Scratch) {
clang::index::writer::ModuleInfo info;
info.Name = *static_cast<const std::string*>(mod);
return info;
}
static bool
emitDataForSwiftSerializedModule(ModuleDecl *module,
StringRef indexStorePath,
bool indexClangModules,
bool indexSystemModules,
bool skipStdlib,
bool includeLocals,
bool explicitModulebuild,
StringRef targetTriple,
const clang::CompilerInstance &clangCI,
DiagnosticEngine &diags,
IndexUnitWriter &parentUnitWriter,
const PathRemapper &pathRemapper,
SourceFile *initialFile);
static void
appendSymbolicInterfaceToIndexStorePath(SmallVectorImpl<char> &resultingPath) {
llvm::sys::path::append(resultingPath, "interfaces");
}
static llvm::Error initSymbolicInterfaceStorePath(StringRef storePath) {
SmallString<128> subPath = storePath;
appendSymbolicInterfaceToIndexStorePath(subPath);
std::error_code ec = llvm::sys::fs::create_directories(subPath);
if (ec)
return llvm::errorCodeToError(ec);
return llvm::Error::success();
}
static void appendSymbolicInterfaceClangModuleFilename(
StringRef filePath, SmallVectorImpl<char> &resultingPath) {
llvm::sys::path::append(resultingPath, llvm::sys::path::filename(filePath));
StringRef extension = ".symbolicswiftinterface";
resultingPath.append(extension.begin(), extension.end());
}
// FIXME (Alex): Share code with IndexUnitWriter in LLVM after refactoring it.
static llvm::Expected<bool>
isFileUpToDateForOutputFile(StringRef filePath, StringRef timeCompareFilePath) {
auto makeError = [](StringRef path, std::error_code ec) -> llvm::Error {
std::string error;
llvm::raw_string_ostream(error)
<< "could not access path '" << path << "': " << ec.message();
return llvm::createStringError(ec, error.c_str());
};
llvm::sys::fs::file_status unitStat;
if (std::error_code ec = llvm::sys::fs::status(filePath, unitStat)) {
if (ec != std::errc::no_such_file_or_directory)
return makeError(filePath, ec);
return false;
}
if (timeCompareFilePath.empty())
return true;
llvm::sys::fs::file_status compareStat;
if (std::error_code ec =
llvm::sys::fs::status(timeCompareFilePath, compareStat)) {
if (ec != std::errc::no_such_file_or_directory)
return makeError(timeCompareFilePath, ec);
return true;
}
// Return true (unit is up-to-date) if the file to compare is older than the
// unit file.
return compareStat.getLastModificationTime() <=
unitStat.getLastModificationTime();
}
/// Emit the symbolic swift interface file for an imported Clang module into the
/// index store directory.
///
/// The swift interface file is emitted only when it doesn't exist yet, or when
/// the PCM for the Clang module has been updated.
///
/// System modules without the 'cplusplus' requirement are not emitted.
static void emitSymbolicInterfaceForClangModule(
ClangModuleUnit *clangModUnit, ModuleDecl *M,
const clang::Module *clangModule, StringRef indexStorePath,
const clang::CompilerInstance &clangCI, DiagnosticEngine &diags) {
if (!M->getASTContext().LangOpts.EnableCXXInterop)
return;
// Skip system modules without an explicit 'cplusplus' requirement.
bool isSystem = clangModUnit->isSystemModule();
if (isSystem && !importer::requiresCPlusPlus(clangModule))
return;
// Make sure the `interfaces` directory is created.
if (auto err = initSymbolicInterfaceStorePath(indexStorePath)) {
llvm::handleAllErrors(std::move(err), [&](const llvm::ECError &ec) {
diags.diagnose(SourceLoc(), diag::error_create_symbolic_interfaces_dir,
ec.convertToErrorCode().message());
});
return;
}
// Determine the output name for the symbolic interface file.
clang::serialization::ModuleFile *ModFile =
clangCI.getASTReader()->getModuleManager().lookup(
clangModule->getASTFile());
assert(ModFile && "no module file loaded for module ?");
SmallString<128> interfaceOutputPath = indexStorePath;
appendSymbolicInterfaceToIndexStorePath(interfaceOutputPath);
appendSymbolicInterfaceClangModuleFilename(ModFile->FileName,
interfaceOutputPath);
// Check if the symbolic interface file is already up to date.
std::string error;
auto upToDate =
isFileUpToDateForOutputFile(interfaceOutputPath, ModFile->FileName);
if (!upToDate) {
llvm::handleAllErrors(
upToDate.takeError(), [&](const llvm::StringError &ec) {
diags.diagnose(SourceLoc(),
diag::error_symbolic_interfaces_failed_status_check,
ec.getMessage());
});
return;
}
if (M->getASTContext().LangOpts.EnableIndexingSystemModuleRemarks) {
diags.diagnose(SourceLoc(), diag::remark_emitting_symbolic_interface_module,
interfaceOutputPath, *upToDate);
}
if (*upToDate)
return;
// Output the interface to a temporary file first.
SmallString<128> tempOutputPath = interfaceOutputPath;
tempOutputPath += "-%%%%%%%%";
int tempFD;
if (llvm::sys::fs::createUniqueFile(tempOutputPath.str(), tempFD,
tempOutputPath)) {
llvm::raw_string_ostream errOS(error);
errOS << "failed to create temporary file: " << tempOutputPath;
diags.diagnose(SourceLoc(), diag::error_write_symbolic_interface,
errOS.str());
return;
}
llvm::raw_fd_ostream os(tempFD, /*shouldClose=*/true);
StreamPrinter printer(os);
ide::printSymbolicSwiftClangModuleInterface(M, printer, clangModule);
os.close();
if (os.has_error()) {
llvm::raw_string_ostream errOS(error);
errOS << "failed to write '" << tempOutputPath
<< "': " << os.error().message();
diags.diagnose(SourceLoc(), diag::error_write_symbolic_interface,
errOS.str());
os.clear_error();
llvm::sys::fs::remove(tempOutputPath);
return;
}
// Move the resulting output to the destination symbolic interface file.
std::error_code ec = llvm::sys::fs::rename(
/*from=*/tempOutputPath, /*to=*/interfaceOutputPath);
if (ec) {
llvm::raw_string_ostream errOS(error);
errOS << "failed to rename '" << tempOutputPath << "' to '"
<< interfaceOutputPath << "': " << ec.message();
diags.diagnose(SourceLoc(), diag::error_write_symbolic_interface,
errOS.str());
llvm::sys::fs::remove(tempOutputPath);
return;
}
}
static void emitTransitiveClangSymbolicInterfacesForSwiftModuleImports(
ArrayRef<ImportedModule> imports, StringRef indexStorePath,
const clang::CompilerInstance &clangCI, DiagnosticEngine &diags) {
auto &fileMgr = clangCI.getFileManager();
for (auto &import : imports) {
ModuleDecl *mod = import.importedModule;
if (mod->isOnoneSupportModule())
continue; // ignore the Onone support library.
if (mod->isSwiftShimsModule())
continue;
for (auto *FU : mod->getFiles()) {
switch (FU->getKind()) {
default:
break;
case FileUnitKind::SerializedAST:
case FileUnitKind::DWARFModule:
case FileUnitKind::ClangModule: {
auto *LFU = cast<LoadedFile>(FU);
if (auto F = fileMgr.getFile(LFU->getFilename())) {
if (FU->getKind() == FileUnitKind::ClangModule) {
auto clangModUnit = cast<ClangModuleUnit>(LFU);
if (auto clangMod = clangModUnit->getUnderlyingClangModule()) {
// Emit the symbolic interface file in addition to index data.
emitSymbolicInterfaceForClangModule(
clangModUnit, mod, clangMod, indexStorePath, clangCI, diags);
}
}
// FIXME: We should keep recursing here into other Swift modules.
}
}
}
}
}
}
static void addModuleDependencies(ArrayRef<ImportedModule> imports,
StringRef indexStorePath,
bool indexClangModules,
bool indexSystemModules,
bool skipStdlib,
bool includeLocals,
bool explicitModuleBuild,
StringRef targetTriple,
const clang::CompilerInstance &clangCI,
DiagnosticEngine &diags,
IndexUnitWriter &unitWriter,
StringScratchSpace &moduleNameScratch,
const PathRemapper &pathRemapper,
SourceFile *initialFile = nullptr) {
auto &fileMgr = clangCI.getFileManager();
for (auto &import : imports) {
ModuleDecl *mod = import.importedModule;
if (mod->isOnoneSupportModule())
continue; // ignore the Onone support library.
if (mod->isSwiftShimsModule())
continue;
for (auto *FU : mod->getFiles()) {
switch (FU->getKind()) {
case FileUnitKind::Source:
case FileUnitKind::Builtin:
case FileUnitKind::Synthesized:
break;
case FileUnitKind::SerializedAST:
case FileUnitKind::DWARFModule:
case FileUnitKind::ClangModule: {
auto *LFU = cast<LoadedFile>(FU);
// This should really be returned from emitting index data, rather
// than guessing which is used for the output file here.
StringRef modulePath;
if (FU->getKind() == FileUnitKind::ClangModule) {
modulePath = LFU->getFilename();
} else {
modulePath = LFU->getSourceFilename();
}
auto F = fileMgr.getFile(modulePath);
if (!F)
break;
// Use module real name for unit writer in case module aliasing
// is used. For example, if a file being indexed has `import Foo`
// and `-module-alias Foo=Bar` is passed, treat Foo as an alias
// and Bar as the real module name as its dependency.
StringRef moduleName = mod->getRealName().str();
bool withoutUnitName = true;
if (FU->getKind() == FileUnitKind::ClangModule) {
auto clangModUnit = cast<ClangModuleUnit>(LFU);
bool shouldIndexModule =
indexClangModules &&
(!mod->isNonUserModule() || indexSystemModules);
withoutUnitName = !shouldIndexModule;
if (auto clangMod = clangModUnit->getUnderlyingClangModule()) {
moduleName = clangMod->getTopLevelModuleName();
// FIXME: clang's -Rremarks do not seem to go through Swift's
// diagnostic emitter.
if (shouldIndexModule)
clang::index::emitIndexDataForModuleFile(clangMod,
clangCI, unitWriter);
// Emit the symbolic interface file in addition to index data.
if (indexClangModules)
emitSymbolicInterfaceForClangModule(clangModUnit, mod, clangMod,
indexStorePath, clangCI,
diags);
}
} else {
// Serialized AST file.
// Only index distributed system modules, and the stdlib.
// We don't officially support binary swift modules, so normally
// the index data for user modules would get generated while
// building them.
if (mod->isNonUserModule() && indexSystemModules &&
(!skipStdlib || !mod->isStdlibModule())) {
emitDataForSwiftSerializedModule(mod, indexStorePath,
indexClangModules,
indexSystemModules, skipStdlib,
includeLocals,
explicitModuleBuild,
targetTriple,
clangCI, diags,
unitWriter,
pathRemapper,
initialFile);
withoutUnitName = false;
}
// If this is a cross-import overlay, make sure we use the name of
// the underlying module instead.
if (auto *declaring = mod->getDeclaringModuleIfCrossImportOverlay())
moduleName = declaring->getNameStr();
// Emit symbolic interface files for any re-exported Clang modules
// from this Swift module.
if (mod->getASTContext().LangOpts.EnableCXXInterop) {
SmallVector<ImportedModule, 4> imports;
mod->getImportedModules(imports,
ModuleDecl::ImportFilterKind::Exported);
if (indexClangModules)
emitTransitiveClangSymbolicInterfacesForSwiftModuleImports(
imports, indexStorePath, clangCI, diags);
}
}
clang::index::writer::OpaqueModule opaqMod =
moduleNameScratch.createString(moduleName);
unitWriter.addASTFileDependency(*F, mod->isNonUserModule(), opaqMod,
withoutUnitName);
break;
}
}
}
}
}
/// \returns true if an error occurred.
static bool
emitDataForSwiftSerializedModule(ModuleDecl *module,
StringRef indexStorePath,
bool indexClangModules,
bool indexSystemModules,
bool skipStdlib,
bool includeLocals,
bool explicitModuleBuild,
StringRef targetTriple,
const clang::CompilerInstance &clangCI,
DiagnosticEngine &diags,
IndexUnitWriter &parentUnitWriter,
const PathRemapper &pathRemapper,
SourceFile *initialFile) {
StringRef filename = module->getModuleSourceFilename();
std::string moduleName = module->getNameStr().str();
// If this is a cross-import overlay, make sure we use the name of the
// underlying module instead.
if (ModuleDecl *declaring = module->getDeclaringModuleIfCrossImportOverlay())
moduleName = declaring->getNameStr().str();
std::string error;
auto isUptodateOpt = parentUnitWriter.isUnitUpToDateForOutputFile(
/*FilePath=*/filename,
/*TimeCompareFilePath=*/filename, error);
if (!isUptodateOpt.has_value()) {
diags.diagnose(SourceLoc(), diag::error_index_failed_status_check, error);
return true;
}
if (*isUptodateOpt)
return false;
// Reload resilient modules from swiftinterface to avoid indexing
// internal details.
bool skipIndexingModule = false;
// Note, we are unable to reload from interface on an explicit module build
if (!explicitModuleBuild &&
module->getResilienceStrategy() == ResilienceStrategy::Resilient &&
!module->isBuiltFromInterface() &&
!module->isStdlibModule()) {
module->getASTContext().setIgnoreAdjacentModules(true);
ImportPath::Module::Builder builder(module->getName());
ASTContext &ctx = module->getASTContext();
auto reloadedModule = ctx.getModule(builder.get(),
/*AllowMemoryCached=*/false);
if (reloadedModule) {
module = reloadedModule;
} else {
// If we can't rebuild from the swiftinterface, don't index this module.
skipIndexingModule = true;
}
}
if (module->getASTContext().LangOpts.EnableIndexingSystemModuleRemarks) {
diags.diagnose(SourceLoc(),
diag::remark_indexing_system_module,
filename, skipIndexingModule);
}
// Pairs of (recordFile, groupName).
std::vector<std::pair<std::string, std::string>> records;
if (skipIndexingModule) {
// Don't add anything to records but keep going so we still mark the module
// as indexed to avoid rebuilds of broken swiftinterfaces.
} else if (!module->isStdlibModule()) {
std::string recordFile;
bool failed = false;
auto consumer = makeRecordingConsumer(filename.str(), indexStorePath.str(),
includeLocals, &diags, &recordFile, &failed);
indexModule(module, *consumer);
if (failed)
return true;
records.emplace_back(recordFile, moduleName);
} else {
// Record stdlib groups as if they were submodules.
auto makeSubmoduleNameFromGroupName = [](StringRef groupName, SmallString<128> &buf) {
buf += "Swift";
if (groupName.empty())
return;
buf += '.';
for (char ch : groupName) {
if (ch == '/')
buf += '.';
else
buf += ch;
}
};
auto appendGroupNameForFilename = [](StringRef groupName, SmallString<256> &buf) {
if (groupName.empty())
return;
buf += '_';
for (char ch : groupName) {
if (ch == '/' || ch ==' ')
buf += '_';
else
buf += ch;
}
};
bool failed = false;
StdlibGroupsIndexRecordingConsumer groupIndexConsumer([&](StringRef groupName, SymbolTracker &tracker) -> bool {
SmallString<128> moduleName;
makeSubmoduleNameFromGroupName(groupName, moduleName);
SmallString<256> fileNameWithGroup = filename;
appendGroupNameForFilename(groupName, fileNameWithGroup);
std::string outRecordFile;
failed =
failed || writeRecord(tracker, std::string(fileNameWithGroup.str()),
indexStorePath.str(), &diags, outRecordFile);
if (failed)
return false;
records.emplace_back(outRecordFile, moduleName.str().str());
return true;
});
indexModule(module, groupIndexConsumer);
if (failed)
return true;
}
auto &fileMgr = clangCI.getFileManager();
bool isSystem = module->isNonUserModule();
// FIXME: Get real values for the following.
StringRef swiftVersion;
StringRef sysrootPath = clangCI.getHeaderSearchOpts().Sysroot;
// For indexing serialized modules 'debug compilation' is irrelevant, so
// set it to true by default.
bool isDebugCompilation = true;
auto clangRemapper = pathRemapper.asClangPathRemapper();
IndexUnitWriter unitWriter(
fileMgr, indexStorePath, "swift", swiftVersion, filename, moduleName,
/*MainFile=*/nullptr, isSystem, /*IsModuleUnit=*/true, isDebugCompilation,
targetTriple, sysrootPath, clangRemapper, getModuleInfoFromOpaqueModule);
auto FE = fileMgr.getFile(filename);
for (auto &pair : records) {
std::string &recordFile = pair.first;
std::string &groupName = pair.second;
if (recordFile.empty())
continue;
clang::index::writer::OpaqueModule mod = &groupName;
unitWriter.addRecordFile(recordFile, *FE, isSystem, mod);
}
SmallVector<ImportedModule, 8> imports;
module->getImportedModules(imports, {ModuleDecl::ImportFilterKind::Exported,
ModuleDecl::ImportFilterKind::Default});
StringScratchSpace moduleNameScratch;
addModuleDependencies(imports, indexStorePath, indexClangModules,
indexSystemModules, skipStdlib, includeLocals,
explicitModuleBuild,
targetTriple, clangCI, diags, unitWriter,
moduleNameScratch, pathRemapper, initialFile);
if (unitWriter.write(error)) {
diags.diagnose(SourceLoc(), diag::error_write_index_unit, error);
return true;
}
return false;
}
static bool
recordSourceFileUnit(SourceFile *primarySourceFile, StringRef indexUnitToken,
StringRef indexStorePath, bool indexClangModules,
bool indexSystemModules, bool skipStdlib,
bool includeLocals, bool isDebugCompilation,
bool isExplicitModuleBuild, StringRef targetTriple,
ArrayRef<const clang::FileEntry *> fileDependencies,
const clang::CompilerInstance &clangCI,
const PathRemapper &pathRemapper,
DiagnosticEngine &diags) {
auto &fileMgr = clangCI.getFileManager();
auto *module = primarySourceFile->getParentModule();
bool isSystem = module->isNonUserModule();
auto mainFile = fileMgr.getFile(primarySourceFile->getFilename());
auto clangRemapper = pathRemapper.asClangPathRemapper();
// FIXME: Get real values for the following.
StringRef swiftVersion;
StringRef sysrootPath = clangCI.getHeaderSearchOpts().Sysroot;
IndexUnitWriter unitWriter(
fileMgr, indexStorePath, "swift", swiftVersion, indexUnitToken,
module->getNameStr(), mainFile ? *mainFile : nullptr, isSystem,
/*isModuleUnit=*/false, isDebugCompilation, targetTriple, sysrootPath,
clangRemapper, getModuleInfoFromOpaqueModule);
// Module dependencies.
SmallVector<ImportedModule, 8> imports;
primarySourceFile->getImportedModules(imports,
ModuleDecl::getImportFilterLocal());
StringScratchSpace moduleNameScratch;
addModuleDependencies(imports, indexStorePath, indexClangModules,
indexSystemModules, skipStdlib, includeLocals,
isExplicitModuleBuild, targetTriple, clangCI, diags,
unitWriter, moduleNameScratch, pathRemapper,
primarySourceFile);
// File dependencies.
for (auto *F : fileDependencies)
unitWriter.addFileDependency(F, /*FIXME:isSystem=*/false, /*Module=*/nullptr);
recordSourceFile(primarySourceFile, indexStorePath, includeLocals, diags,
[&](StringRef recordFile, StringRef filename) {
auto file = fileMgr.getFile(filename);
unitWriter.addRecordFile(
recordFile, file ? *file : nullptr,
module->isNonUserModule(), /*Module=*/nullptr);
});
std::string error;
if (unitWriter.write(error)) {
diags.diagnose(SourceLoc(), diag::error_write_index_unit, error);
return true;
}
return false;
}
// Not currently used, see related comments in the call sites.
#if 0
static void
collectFileDependencies(llvm::SetVector<const clang::FileEntry *> &result,
const DependencyTracker &dependencyTracker,
ModuleDecl *module, clang::FileManager &fileMgr) {
for (auto *F : module->getFiles()) {
if (auto *SF = dyn_cast<SourceFile>(F)) {
if (auto *dep = fileMgr.getFile(SF->getFilename())) {
result.insert(dep);
}
}
}
for (StringRef filename : dependencyTracker.getDependencies()) {
if (auto *F = fileMgr.getFile(filename))
result.insert(F);
}
}
#endif
//===----------------------------------------------------------------------===//
// Indexing entry points
//===----------------------------------------------------------------------===//
bool index::indexAndRecord(SourceFile *primarySourceFile,
StringRef indexUnitToken,
StringRef indexStorePath,
bool indexClangModules,
bool indexSystemModules,
bool skipStdlib,
bool includeLocals,
bool isDebugCompilation,
bool isExplicitModuleBuild,
StringRef targetTriple,
const DependencyTracker &dependencyTracker,
const PathRemapper &pathRemapper) {
auto &astContext = primarySourceFile->getASTContext();
auto &clangCI = astContext.getClangModuleLoader()->getClangInstance();
auto &diags = astContext.Diags;
std::string error;
if (IndexUnitWriter::initIndexDirectory(indexStorePath, error)) {
diags.diagnose(SourceLoc(), diag::error_create_index_dir, error);
return true;
}
llvm::SetVector<const clang::FileEntry *> fileDependencies;
// FIXME: This is not desirable because:
// 1. It picks shim header files as file dependencies
// 2. Having all the other swift files of the module as file dependencies ends
// up making all of them associated with all the other files as main files.
// It's better to associate each swift file with the unit that recorded it
// as the main one.
// Keeping the code in case we want to revisit.
#if 0
auto *module = primarySourceFile->getParentModule();
collectFileDependencies(fileDependencies, dependencyTracker, module, fileMgr);
#endif
return recordSourceFileUnit(primarySourceFile, indexUnitToken,
indexStorePath, indexClangModules,
indexSystemModules, skipStdlib, includeLocals,
isDebugCompilation, isExplicitModuleBuild,
targetTriple, fileDependencies.getArrayRef(),
clangCI, pathRemapper, diags);
}
bool index::indexAndRecord(ModuleDecl *module,
ArrayRef<std::string> indexUnitTokens,
StringRef moduleUnitToken,
StringRef indexStorePath,
bool indexClangModules,
bool indexSystemModules,
bool skipStdlib,
bool includeLocals,
bool isDebugCompilation,
bool isExplicitModuleBuild,
StringRef targetTriple,
const DependencyTracker &dependencyTracker,
const PathRemapper &pathRemapper) {
auto &astContext = module->getASTContext();
auto &clangCI = astContext.getClangModuleLoader()->getClangInstance();
auto &diags = astContext.Diags;
std::string error;
if (IndexUnitWriter::initIndexDirectory(indexStorePath, error)) {
diags.diagnose(SourceLoc(), diag::error_create_index_dir, error);
return true;
}
// Add the current module's source files to the dependencies.
llvm::SetVector<const clang::FileEntry *> fileDependencies;
// FIXME: This is not desirable because:
// 1. It picks shim header files as file dependencies
// 2. Having all the other swift files of the module as file dependencies ends
// up making all of them associated with all the other files as main files.
// It's better to associate each swift file with the unit that recorded it
// as the main one.
// Keeping the code in case we want to revisit.
#if 0
collectFileDependencies(fileDependencies, dependencyTracker, module, fileMgr);
#endif
// Write a unit for each source file.
unsigned unitIndex = 0;
for (auto *F : module->getFiles()) {
if (auto *SF = dyn_cast<SourceFile>(F)) {
if (unitIndex == indexUnitTokens.size()) {
diags.diagnose(SourceLoc(), diag::error_index_inputs_more_than_outputs);
return true;
}
if (recordSourceFileUnit(SF, indexUnitTokens[unitIndex],
indexStorePath, indexClangModules,
indexSystemModules, skipStdlib, includeLocals,
isDebugCompilation, isExplicitModuleBuild,
targetTriple, fileDependencies.getArrayRef(),
clangCI, pathRemapper, diags))
return true;
unitIndex += 1;
}
}
// In the case where inputs are swift modules, like in the merge-module step,
// ignore the inputs; associated unit files for the modules' source inputs
// should have been generated at swift module creation time.
return false;
}
|