Skip to content

Commit f3d217e

Browse files
committed
[Dependency Scanning] Add a C++ itnerface for a tool that answers dependency-scanning queries.
1 parent 33f34d1 commit f3d217e

File tree

8 files changed

+186
-11
lines changed

8 files changed

+186
-11
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//===-------------- DependencyScanningTool.h - Swift Compiler -------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_DEPENDENCY_SCANNING_TOOL_H
14+
#define SWIFT_DEPENDENCY_SCANNING_TOOL_H
15+
16+
#include "swift/AST/ModuleDependencies.h"
17+
#include "llvm/Support/Error.h"
18+
#include "llvm/Support/StringSaver.h"
19+
#include "llvm/ADT/StringMap.h"
20+
#include "llvm/ADT/StringSet.h"
21+
#include "llvm/ADT/StringRef.h"
22+
23+
namespace swift {
24+
namespace dependencies {
25+
26+
/// The high-level implementation of the dependency scanner that runs on
27+
/// an individual worker thread.
28+
class DependencyScanningTool {
29+
public:
30+
/// Construct a dependency scanning tool.
31+
DependencyScanningTool();
32+
33+
/// Collect the full module depenedency graph for the input, ignoring any placeholder
34+
/// modules.
35+
///
36+
/// \returns a \c StringError with the diagnostic output if clang errors
37+
/// occurred, \c FullDependencies otherwise.
38+
std::string
39+
getFullDependencies(ArrayRef<const char *> Command,
40+
const llvm::StringSet<> &InputFiles,
41+
const llvm::StringSet<> &PlaceholderModules);
42+
43+
private:
44+
/// Shared cache of module dependencies, re-used by individual queries
45+
/// during the lifetime of this Tool
46+
ModuleDependenciesCache GlobalCache;
47+
48+
llvm::BumpPtrAllocator Alloc;
49+
llvm::StringSaver Saver;
50+
};
51+
52+
} // end namespace dependencies
53+
} // end namespace swift
54+
55+
#endif // SWIFT_DEPENDENCY_SCANNING_TOOL_H

lib/FrontendTool/ScanDependencies.h renamed to include/swift/SwiftScan/ScanDependencies.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ class CompilerInstance;
2424
bool batchScanModuleDependencies(CompilerInstance &instance,
2525
llvm::StringRef batchInputFile);
2626

27+
/// Scans the dependencies of the main module of \c instance and writes out
28+
/// the result in JSON
29+
bool scanAndOutputDependencies(CompilerInstance &instance);
30+
2731
/// Scans the dependencies of the main module of \c instance.
28-
bool scanDependencies(CompilerInstance &instance);
32+
bool scanDependencies(CompilerInstance &instance,
33+
llvm::raw_ostream &out);
2934

3035
/// Scans the dependencies of the underlying clang module of the main module
3136
/// of \c instance.

lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ add_subdirectory(PrintAsObjC)
3636
add_subdirectory(RemoteAST)
3737
add_subdirectory(Sema)
3838
add_subdirectory(Serialization)
39+
add_subdirectory(SwiftScan)
3940
add_subdirectory(SwiftDemangle)
4041
add_subdirectory(SwiftReflection)
4142
add_subdirectory(SwiftRemoteMirror)

lib/FrontendTool/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ add_swift_host_library(swiftFrontendTool STATIC
88
TBD.cpp)
99
add_dependencies(swiftFrontendTool
1010
swift-syntax-generated-headers
11+
SwiftScanTool
1112
SwiftOptions)
1213
target_link_libraries(swiftFrontendTool INTERFACE
1314
clangAPINotes
15+
SwiftScanTool
1416
clangBasic)
1517
target_link_libraries(swiftFrontendTool PRIVATE
1618
swiftClangImporter
1719
swiftDemangling
20+
SwiftScanTool
1821
swiftFrontend
1922
swiftIDE
2023
swiftImmediate

lib/FrontendTool/FrontendTool.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -21,8 +21,8 @@
2121
//===----------------------------------------------------------------------===//
2222

2323
#include "swift/FrontendTool/FrontendTool.h"
24-
#include "Dependencies.h"
25-
#include "ScanDependencies.h"
24+
#include "swift/SwiftScan/ScanDependencies.h"
25+
#include "ImportedModules.h"
2626
#include "TBD.h"
2727
#include "swift/Subsystems.h"
2828
#include "swift/AST/DiagnosticsFrontend.h"
@@ -1135,7 +1135,7 @@ static bool performScanDependencies(CompilerInstance &Instance) {
11351135
auto batchScanInput =
11361136
Instance.getASTContext().SearchPathOpts.BatchScanInputFilePath;
11371137
if (batchScanInput.empty()) {
1138-
return scanDependencies(Instance);
1138+
return scanAndOutputDependencies(Instance);
11391139
} else {
11401140
return batchScanModuleDependencies(Instance, batchScanInput);
11411141
}

lib/SwiftScan/CMakeLists.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
set_swift_llvm_is_available()
2+
add_swift_host_library(SwiftScanTool STATIC
3+
DependencyScanningTool.cpp
4+
ScanDependencies.cpp)
5+
add_dependencies(SwiftScanTool
6+
swift-syntax-generated-headers
7+
SwiftOptions)
8+
9+
target_link_libraries(SwiftScanTool INTERFACE
10+
clangAPINotes
11+
clangBasic)
12+
13+
target_link_libraries(SwiftScanTool PRIVATE
14+
swiftClangImporter
15+
swiftDemangling
16+
swiftFrontend
17+
swiftIDE
18+
swiftImmediate
19+
swiftIndex
20+
swiftIRGen
21+
swiftOption
22+
swiftPrintAsObjC
23+
swiftSerialization
24+
swiftSIL
25+
swiftSILGen
26+
swiftSILOptimizer
27+
swiftTBDGen)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//===------------ DependencyScanningTool.cpp - Swift Compiler -------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "swift/SwiftScan/DependencyScanningTool.h"
14+
#include "swift/SwiftScan/ScanDependencies.h"
15+
#include "swift/AST/DiagnosticEngine.h"
16+
#include "swift/AST/DiagnosticsFrontend.h"
17+
#include "swift/Basic/LLVMInitialize.h"
18+
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
19+
#include "swift/Frontend/Frontend.h"
20+
#include "llvm/Support/CommandLine.h"
21+
#include "llvm/Support/FileSystem.h"
22+
23+
#include <sstream>
24+
25+
namespace swift {
26+
namespace dependencies {
27+
28+
DependencyScanningTool::DependencyScanningTool()
29+
: GlobalCache(), Alloc(), Saver(Alloc) {
30+
31+
32+
}
33+
34+
std::string
35+
DependencyScanningTool::getFullDependencies(ArrayRef<const char *> Command,
36+
const llvm::StringSet<> &InputFiles,
37+
const llvm::StringSet<> &PlaceholderModules) {
38+
PrintingDiagnosticConsumer PDC;
39+
// State unique to an individual scan
40+
auto Instance = std::make_unique<CompilerInstance>();
41+
Instance->addDiagnosticConsumer(&PDC);
42+
43+
// Basic error checking on the arguments
44+
if (Command.empty()) {
45+
Instance->getDiags().diagnose(SourceLoc(), diag::error_no_frontend_args);
46+
return "Error";
47+
}
48+
49+
CompilerInvocation Invocation;
50+
SmallString<128> WorkingDirectory;
51+
llvm::sys::fs::current_path(WorkingDirectory);
52+
53+
// Parse arguments.
54+
std::string CommandString;
55+
for (const auto *c : Command) {
56+
CommandString.append(c);
57+
CommandString.append(" ");
58+
}
59+
SmallVector<const char*, 4> Args;
60+
llvm::cl::TokenizeGNUCommandLine(CommandString, Saver, Args);
61+
if (Invocation.parseArgs(Args, Instance->getDiags())) {
62+
return "Error";
63+
}
64+
65+
// Setup the instance
66+
Instance->setup(Invocation);
67+
68+
std::string JSONOutput;
69+
llvm::raw_string_ostream Oss(JSONOutput);
70+
scanDependencies(*Instance, Oss);
71+
Oss.flush();
72+
73+
return JSONOutput;
74+
}
75+
}
76+
}

lib/FrontendTool/ScanDependencies.cpp renamed to lib/SwiftScan/ScanDependencies.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include "ScanDependencies.h"
1413
#include "swift/AST/ASTContext.h"
1514
#include "swift/AST/Decl.h"
1615
#include "swift/AST/DiagnosticEngine.h"
@@ -27,6 +26,7 @@
2726
#include "swift/Frontend/FrontendOptions.h"
2827
#include "swift/Frontend/ModuleInterfaceLoader.h"
2928
#include "swift/Strings.h"
29+
#include "swift/SwiftScan/ScanDependencies.h"
3030
#include "clang/Basic/Module.h"
3131
#include "llvm/ADT/SetVector.h"
3232
#include "llvm/ADT/StringMap.h"
@@ -794,12 +794,9 @@ bool swift::batchScanModuleDependencies(CompilerInstance &instance,
794794
return false;
795795
}
796796

797-
bool swift::scanDependencies(CompilerInstance &instance) {
797+
bool swift::scanAndOutputDependencies(CompilerInstance &instance) {
798798
ASTContext &Context = instance.getASTContext();
799-
ModuleDecl *mainModule = instance.getMainModule();
800-
const CompilerInvocation &invocation = instance.getInvocation();
801-
const FrontendOptions &opts = invocation.getFrontendOptions();
802-
799+
const FrontendOptions &opts = instance.getInvocation().getFrontendOptions();
803800
std::string path = opts.InputsAndOutputs.getSingleOutputFilename();
804801
std::error_code EC;
805802
llvm::raw_fd_ostream out(path, EC, llvm::sys::fs::F_None);
@@ -811,6 +808,17 @@ bool swift::scanDependencies(CompilerInstance &instance) {
811808
return true;
812809
}
813810

811+
// Execute scan, writing JSON output to the output stream
812+
return scanDependencies(instance, out);
813+
}
814+
815+
bool swift::scanDependencies(CompilerInstance &instance,
816+
llvm::raw_ostream &out) {
817+
ASTContext &Context = instance.getASTContext();
818+
ModuleDecl *mainModule = instance.getMainModule();
819+
const CompilerInvocation &invocation = instance.getInvocation();
820+
const FrontendOptions &opts = invocation.getFrontendOptions();
821+
814822
// Main module file name.
815823
auto newExt = file_types::getExtension(file_types::TY_SwiftModuleFile);
816824
llvm::SmallString<32> mainModulePath = mainModule->getName().str();

0 commit comments

Comments
 (0)