Skip to content

[Dependency Scanning] When re-using a CompilerInvocation in batch scanning, update it with the entry-specific arguments. #36294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions lib/DependencyScan/ScanDependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
#include "llvm/Support/YAMLParser.h"
#include "llvm/Support/YAMLTraits.h"
#include <set>
#include <string>
#include <sstream>

using namespace swift;
using namespace swift::dependencies;
Expand Down Expand Up @@ -916,16 +918,30 @@ using CompilerArgInstanceCacheMap =
llvm::StringMap<std::pair<std::unique_ptr<CompilerInstance>,
std::unique_ptr<ModuleDependenciesCache>>>;

static void
updateCachedInstanceSearchPaths(CompilerInstance &cachedInstance,
const CompilerInstance &invocationInstance) {
static void updateCachedInstanceOpts(CompilerInstance &cachedInstance,
const CompilerInstance &invocationInstance,
llvm::StringRef entryArguments) {
cachedInstance.getASTContext().SearchPathOpts =
invocationInstance.getASTContext().SearchPathOpts;
// The Clang Importer arguments must also match those of the current
// invocation instead of the cached one because they include search
// path directives for Clang.

// The Clang Importer arguments must consiste of a combination of
// Clang Importer arguments of the current invocation to inherit its Clang-specific
// search path options, followed by the options speicific to the given batch-entry,
// which may overload some of the invocation's options (e.g. target)
cachedInstance.getASTContext().ClangImporterOpts =
invocationInstance.getASTContext().ClangImporterOpts;
std::istringstream iss(entryArguments.str());
std::vector<std::string> splitArguments(
std::istream_iterator<std::string>{iss},
std::istream_iterator<std::string>());
for (auto it = splitArguments.begin(), end = splitArguments.end(); it != end;
++it) {
if ((*it) == "-Xcc") {
assert((it + 1 != end) && "Expected option following '-Xcc'");
cachedInstance.getASTContext().ClangImporterOpts.ExtraArgs.push_back(
*(it + 1));
}
}
}

static bool
Expand Down Expand Up @@ -964,7 +980,7 @@ forEachBatchEntry(CompilerInstance &invocationInstance,
pCache = (*subInstanceMap)[entry.arguments].second.get();
// We must update the search paths of this instance to instead reflect
// those of the current scanner invocation.
updateCachedInstanceSearchPaths(*pInstance, invocationInstance);
updateCachedInstanceOpts(*pInstance, invocationInstance, entry.arguments);
} else {
// Create a new instance by the arguments and save it in the map.
subInstanceMap->insert(
Expand Down
10 changes: 5 additions & 5 deletions test/ScanDependencies/batch_module_scan_versioned.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@

// RUN: echo "[{" > %/t/inputs/input.json
// RUN: echo "\"clangModuleName\": \"G\"," >> %/t/inputs/input.json
// RUN: echo "\"arguments\": \"-Xcc -target -Xcc x86_64-apple-macosx10.9\"," >> %/t/inputs/input.json
// RUN: echo "\"output\": \"%/t/outputs/G_109.pcm.json\"" >> %/t/inputs/input.json
// RUN: echo "\"arguments\": \"-Xcc -target -Xcc x86_64-apple-macosx11.0\"," >> %/t/inputs/input.json
// RUN: echo "\"output\": \"%/t/outputs/G_110.pcm.json\"" >> %/t/inputs/input.json
// RUN: echo "}," >> %/t/inputs/input.json
// RUN: echo "{" >> %/t/inputs/input.json
// RUN: echo "\"clangModuleName\": \"G\"," >> %/t/inputs/input.json
// RUN: echo "\"arguments\": \"-Xcc -target -Xcc x86_64-apple-macosx11.0\"," >> %/t/inputs/input.json
// RUN: echo "\"output\": \"%/t/outputs/G_110.pcm.json\"" >> %/t/inputs/input.json
// RUN: echo "\"arguments\": \"-Xcc -target -Xcc x86_64-apple-macosx10.9\"," >> %/t/inputs/input.json
// RUN: echo "\"output\": \"%/t/outputs/G_109.pcm.json\"" >> %/t/inputs/input.json
// RUN: echo "}]" >> %/t/inputs/input.json

// RUN: %target-swift-frontend -scan-dependencies -module-cache-path %t/clang-module-cache %s -o %t/deps.json -I %S/Inputs/CHeaders -I %S/Inputs/Swift -emit-dependencies -emit-dependencies-path %t/deps.d -import-objc-header %S/Inputs/CHeaders/Bridging.h -swift-version 4 -batch-scan-input-file %/t/inputs/input.json
// RUN: %target-swift-frontend -scan-dependencies -target x86_64-apple-macosx11.0 -module-cache-path %t/clang-module-cache %s -o %t/deps.json -I %S/Inputs/CHeaders -I %S/Inputs/Swift -emit-dependencies -emit-dependencies-path %t/deps.d -import-objc-header %S/Inputs/CHeaders/Bridging.h -swift-version 4 -batch-scan-input-file %/t/inputs/input.json

// Check the contents of the JSON output
// RUN: %FileCheck %s -check-prefix=CHECK-PCM109 < %t/outputs/G_109.pcm.json
Expand Down