Skip to content
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
14 changes: 14 additions & 0 deletions clang/include/clang/Tooling/DependencyScanningTool.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,20 @@ class DependencyScanningTool {

llvm::vfs::FileSystem &getWorkerVFS() const { return Worker.getVFS(); }

/// @brief Initialize the worker's compiler instance from the commandline.
/// The compiler instance only takes a `-cc1` job, so this method
/// builds the `-cc1` job from the CommandLine input.
/// @param Worker The dependency scanning worker whose compiler instance
/// with context is initialized.
/// @param CWD The current working directory.
/// @param CommandLine This command line may be a driver command or a cc1
/// command.
/// @param DC A diagnostics consumer to report error if the initialization
/// fails.
static bool initializeWorkerCIWithContextFromCommandline(
clang::dependencies::DependencyScanningWorker &Worker, StringRef CWD,
ArrayRef<std::string> CommandLine, DiagnosticConsumer &DC);

private:
dependencies::DependencyScanningWorker Worker;
std::unique_ptr<dependencies::TextDiagnosticsPrinterWithOutput>
Expand Down
39 changes: 23 additions & 16 deletions clang/lib/Tooling/DependencyScanningTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,19 +217,13 @@ static llvm::Error makeErrorFromDiagnosticsOS(
DiagPrinterWithOS.DiagnosticsOS.str(), llvm::inconvertibleErrorCode());
}

llvm::Error
DependencyScanningTool::initializeCompilerInstanceWithContextOrError(
StringRef CWD, ArrayRef<std::string> CommandLine) {
DiagPrinterWithOS =
std::make_unique<TextDiagnosticsPrinterWithOutput>(CommandLine);

bool DependencyScanningTool::initializeWorkerCIWithContextFromCommandline(
DependencyScanningWorker &Worker, StringRef CWD,
ArrayRef<std::string> CommandLine, DiagnosticConsumer &DC) {
if (CommandLine.size() >= 2 && CommandLine[1] == "-cc1") {
// The input command line is already a -cc1 invocation; initialize the
// compiler instance directly from it.
if (Worker.initializeCompilerInstanceWithContext(
CWD, CommandLine, DiagPrinterWithOS->DiagPrinter))
return llvm::Error::success();
return makeErrorFromDiagnosticsOS(*DiagPrinterWithOS);
return Worker.initializeCompilerInstanceWithContext(CWD, CommandLine, DC);
}

// The input command line is either a driver-style command line, or
Expand All @@ -241,18 +235,31 @@ DependencyScanningTool::initializeCompilerInstanceWithContextOrError(
const auto &ModifiedCommandLine = OverlayFSAndArgs.second;

auto DiagEngineWithCmdAndOpts =
std::make_unique<DiagnosticsEngineWithDiagOpts>(
ModifiedCommandLine, OverlayFS, DiagPrinterWithOS->DiagPrinter);
std::make_unique<DiagnosticsEngineWithDiagOpts>(ModifiedCommandLine,
OverlayFS, DC);

const auto MaybeFirstCC1 = getFirstCC1CommandLine(
ModifiedCommandLine, *DiagEngineWithCmdAndOpts->DiagEngine, OverlayFS);
if (!MaybeFirstCC1)
return makeErrorFromDiagnosticsOS(*DiagPrinterWithOS);
return false;

return Worker.initializeCompilerInstanceWithContext(
CWD, *MaybeFirstCC1, std::move(DiagEngineWithCmdAndOpts), OverlayFS);
}

if (Worker.initializeCompilerInstanceWithContext(
CWD, *MaybeFirstCC1, std::move(DiagEngineWithCmdAndOpts), OverlayFS))
llvm::Error
DependencyScanningTool::initializeCompilerInstanceWithContextOrError(
StringRef CWD, ArrayRef<std::string> CommandLine) {
DiagPrinterWithOS =
std::make_unique<TextDiagnosticsPrinterWithOutput>(CommandLine);

bool Result = initializeWorkerCIWithContextFromCommandline(
Worker, CWD, CommandLine, DiagPrinterWithOS->DiagPrinter);

if (Result)
return llvm::Error::success();
return makeErrorFromDiagnosticsOS(*DiagPrinterWithOS);
else
return makeErrorFromDiagnosticsOS(*DiagPrinterWithOS);
}

llvm::Expected<TranslationUnitDeps>
Expand Down