Skip to content

Commit 0305176

Browse files
committed
Address code review comments.
1 parent 94344cc commit 0305176

File tree

9 files changed

+24
-77
lines changed

9 files changed

+24
-77
lines changed

clang/include/clang/Frontend/FrontendActions.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -320,15 +320,6 @@ class PrintPreprocessedAction : public PreprocessorFrontendAction {
320320
bool hasPCHSupport() const override { return true; }
321321
};
322322

323-
class GetDependenciesByModuleNameAction : public PreprocessOnlyAction {
324-
StringRef ModuleName;
325-
void ExecuteAction() override;
326-
327-
public:
328-
GetDependenciesByModuleNameAction(StringRef ModuleName)
329-
: ModuleName(ModuleName) {}
330-
};
331-
332323
//===----------------------------------------------------------------------===//
333324
// HLSL Specific Actions
334325
//===----------------------------------------------------------------------===//

clang/include/clang/Tooling/DependencyScanning/DependencyScanningTool.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class DependencyScanningTool {
162162
/// @param CWD The current working directory used during the scan.
163163
/// @param CommandLine The commandline used for the scan.
164164
/// @return Error if the initializaiton fails.
165-
llvm::Error initializeCompilerInstacneWithContext(
165+
llvm::Error initializeCompilerInstanceWithContext(
166166
StringRef CWD, const std::vector<std::string> &CommandLine);
167167

168168
/// @brief Computes the dependeny for the module named ModuleName.

clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "clang/Basic/FileManager.h"
1414
#include "clang/Basic/LLVM.h"
1515
#include "clang/Frontend/PCHContainerOperations.h"
16-
#include "clang/Tooling/DependencyScanning/DependencyScannerImpl.h"
1716
#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
1817
#include "clang/Tooling/DependencyScanning/ModuleDepCollector.h"
1918
#include "llvm/Support/Error.h"
@@ -30,6 +29,7 @@ namespace tooling {
3029
namespace dependencies {
3130

3231
class DependencyScanningWorkerFilesystem;
32+
class CompilerInstanceWithContext;
3333

3434
/// A command-line tool invocation that is part of building a TU.
3535
///
@@ -90,6 +90,8 @@ class DependencyScanningWorker {
9090
DependencyScanningWorker(DependencyScanningService &Service,
9191
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS);
9292

93+
~DependencyScanningWorker();
94+
9395
/// Run the dependency scanning tool for a given clang driver command-line,
9496
/// and report the discovered dependencies to the provided consumer. If
9597
/// TUBuffer is not nullopt, it is used as TU input for the dependency

clang/lib/Frontend/FrontendActions.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,20 +1233,6 @@ void PrintDependencyDirectivesSourceMinimizerAction::ExecuteAction() {
12331233
llvm::outs());
12341234
}
12351235

1236-
void GetDependenciesByModuleNameAction::ExecuteAction() {
1237-
CompilerInstance &CI = getCompilerInstance();
1238-
Preprocessor &PP = CI.getPreprocessor();
1239-
SourceManager &SM = PP.getSourceManager();
1240-
FileID MainFileID = SM.getMainFileID();
1241-
SourceLocation FileStart = SM.getLocForStartOfFile(MainFileID);
1242-
SmallVector<IdentifierLoc, 2> Path;
1243-
IdentifierInfo *ModuleID = PP.getIdentifierInfo(ModuleName);
1244-
Path.emplace_back(FileStart, ModuleID);
1245-
auto ModResult = CI.loadModule(FileStart, Path, Module::Hidden, false);
1246-
PPCallbacks *CB = PP.getPPCallbacks();
1247-
CB->moduleImport(SourceLocation(), Path, ModResult);
1248-
}
1249-
12501236
//===----------------------------------------------------------------------===//
12511237
// HLSL Specific Actions
12521238
//===----------------------------------------------------------------------===//

clang/lib/Tooling/DependencyScanning/DependencyScannerImpl.cpp

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "clang/Tooling/DependencyScanning/DependencyScannerImpl.h"
9+
#include "DependencyScannerImpl.h"
1010
#include "clang/Basic/DiagnosticFrontend.h"
1111
#include "clang/Basic/DiagnosticSerialization.h"
1212
#include "clang/Driver/Driver.h"
@@ -490,9 +490,6 @@ bool initializeScanCompilerInstance(
490490
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
491491
DiagnosticConsumer *DiagConsumer, DependencyScanningService &Service,
492492
IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS) {
493-
// TODO: the commented out code here should be un-commented when
494-
// we enable CAS.
495-
// ScanInstance.getInvocation().getCASOpts() = Worker.CASOpts;
496493
ScanInstance.setBuildingModule(false);
497494

498495
ScanInstance.createVirtualFileSystem(FS, DiagConsumer);
@@ -728,20 +725,22 @@ llvm::Error CompilerInstanceWithContext::initialize() {
728725
llvm::inconvertibleErrorCode());
729726
}
730727

728+
assert(Compilation->getJobs().size() &&
729+
"Must have a job list of non-zero size");
731730
const driver::Command &Command = *(Compilation->getJobs().begin());
732731
const auto &CommandArgs = Command.getArguments();
733732
size_t ArgSize = CommandArgs.size();
734733
assert(ArgSize >= 1 && "Cannot have a command with 0 args");
735734
const char *FirstArg = CommandArgs[0];
736-
if (strcmp(FirstArg, "-cc1"))
735+
if (StringRef(FirstArg) != "-cc1")
737736
return llvm::make_error<llvm::StringError>(
738737
"Incorrect compilation command, missing cc1",
739738
llvm::inconvertibleErrorCode());
740-
Invocation = std::make_unique<CompilerInvocation>();
739+
OriginalInvocation = std::make_unique<CompilerInvocation>();
741740

742-
if (!CompilerInvocation::CreateFromArgs(*Invocation, Command.getArguments(),
743-
*DiagEngineWithCmdAndOpts->DiagEngine,
744-
Command.getExecutable())) {
741+
if (!CompilerInvocation::CreateFromArgs(
742+
*OriginalInvocation, Command.getArguments(),
743+
*DiagEngineWithCmdAndOpts->DiagEngine, Command.getExecutable())) {
745744
DiagEngineWithCmdAndOpts->DiagEngine->Report(
746745
diag::err_fe_expected_compiler_job)
747746
<< llvm::join(CommandLine, " ");
@@ -750,36 +749,15 @@ llvm::Error CompilerInstanceWithContext::initialize() {
750749
llvm::inconvertibleErrorCode());
751750
}
752751

753-
// TODO: CMDArgsStrVector is making string copies. We should optimize later
754-
// and avoid the copies.
755-
std::vector<std::string> CMDArgsStrVector(ArgSize + 1);
756-
CMDArgsStrVector.push_back(Command.getExecutable());
757-
llvm::transform(CommandArgs, CMDArgsStrVector.begin() + 1,
758-
[](const char *s) { return std::string(s); });
759-
760-
Invocation = createCompilerInvocation(CMDArgsStrVector,
761-
*DiagEngineWithCmdAndOpts->DiagEngine);
762-
if (!Invocation) {
763-
DiagEngineWithCmdAndOpts->DiagEngine->Report(
764-
diag::err_fe_expected_compiler_job)
765-
<< llvm::join(CommandLine, " ");
766-
return llvm::make_error<llvm::StringError>(
767-
"Cannot create CompilerInvocation from Args",
768-
llvm::inconvertibleErrorCode());
769-
}
770-
771-
Invocation->getFrontendOpts().DisableFree = false;
772-
Invocation->getCodeGenOpts().DisableFree = false;
773-
774752
if (any(Worker.Service.getOptimizeArgs() & ScanningOptimizations::Macros))
775-
canonicalizeDefines(Invocation->getPreprocessorOpts());
753+
canonicalizeDefines(OriginalInvocation->getPreprocessorOpts());
776754

777755
// Create the CompilerInstance.
778756
IntrusiveRefCntPtr<ModuleCache> ModCache =
779757
makeInProcessModuleCache(Worker.Service.getModuleCacheEntries());
780758
CIPtr = std::make_unique<CompilerInstance>(
781-
std::make_shared<CompilerInvocation>(*Invocation), Worker.PCHContainerOps,
782-
ModCache.get());
759+
std::make_shared<CompilerInvocation>(*OriginalInvocation),
760+
Worker.PCHContainerOps, ModCache.get());
783761
auto &CI = *CIPtr;
784762

785763
if (!initializeScanCompilerInstance(
@@ -801,7 +779,6 @@ llvm::Error CompilerInstanceWithContext::initialize() {
801779
OutputOpts = takeDependencyOutputOptionsFrom(CI);
802780

803781
CI.createTarget();
804-
// CI.initializeDelayedInputFileFromCAS();
805782

806783
return llvm::Error::success();
807784
}
@@ -810,21 +787,19 @@ llvm::Error CompilerInstanceWithContext::computeDependencies(
810787
StringRef ModuleName, DependencyConsumer &Consumer,
811788
DependencyActionController &Controller) {
812789
auto &CI = *CIPtr;
813-
CompilerInvocation Inv(*Invocation);
790+
CompilerInvocation Inv(*OriginalInvocation);
814791

815792
CI.clearDependencyCollectors();
816793
auto MDC = initializeScanInstanceDependencyCollector(
817794
CI, std::make_unique<DependencyOutputOptions>(*OutputOpts), CWD, Consumer,
818-
Worker.Service, *Invocation, Controller, PrebuiltModuleASTMap,
819-
StableDirs);
795+
Worker.Service, Inv, Controller, PrebuiltModuleASTMap, StableDirs);
820796

821797
if (!SrcLocOffset) {
822798
// When SrcLocOffset is zero, we are at the beginning of the fake source
823799
// file. In this case, we call BeginSourceFile to initialize.
824800
std::unique_ptr<FrontendAction> Action =
825-
std::make_unique<GetDependenciesByModuleNameAction>(ModuleName);
801+
std::make_unique<PreprocessOnlyAction>();
826802
auto InputFile = CI.getFrontendOpts().Inputs.begin();
827-
828803
Action->BeginSourceFile(CI, *InputFile);
829804
}
830805

@@ -872,14 +847,6 @@ llvm::Error CompilerInstanceWithContext::computeDependencies(
872847
MDC->applyDiscoveredDependencies(Inv);
873848
Consumer.handleBuildCommand({CommandLine[0], Inv.getCC1CommandLine()});
874849

875-
// TODO: enable CAS
876-
// std::string ID = Inv.getFileSystemOpts().CASFileSystemRootID;
877-
// if (!ID.empty())
878-
// Consumer.handleCASFileSystemRootID(std::move(ID));
879-
// ID = Inv.getFrontendOpts().CASIncludeTreeID;
880-
// if (!ID.empty())
881-
// Consumer.handleIncludeTreeID(std::move(ID));
882-
883850
// Remove the PPCallbacks since they are going out of scope.
884851
CI.getPreprocessor().removePPCallbacks();
885852
return llvm::Error::success();

clang/include/clang/Tooling/DependencyScanning/DependencyScannerImpl.h renamed to clang/lib/Tooling/DependencyScanning/DependencyScannerImpl.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#define LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNER_H
1111

1212
#include "clang/Driver/Compilation.h"
13-
#include "clang/Driver/Driver.h"
1413
#include "clang/Frontend/CompilerInstance.h"
1514
#include "clang/Frontend/CompilerInvocation.h"
1615
#include "clang/Frontend/TextDiagnosticPrinter.h"
@@ -170,8 +169,7 @@ class CompilerInstanceWithContext {
170169
// Context - compiler invocation
171170
std::unique_ptr<clang::driver::Driver> Driver;
172171
std::unique_ptr<clang::driver::Compilation> Compilation;
173-
std::unique_ptr<CompilerInvocation> Invocation;
174-
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFSFromCompilerInvocation;
172+
std::unique_ptr<CompilerInvocation> OriginalInvocation;
175173

176174
// Context - output options
177175
std::unique_ptr<DependencyOutputOptions> OutputOpts;

clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ DependencyScanningTool::getTranslationUnitDependencies(
155155
return Consumer.takeTranslationUnitDeps();
156156
}
157157

158-
llvm::Error DependencyScanningTool::initializeCompilerInstacneWithContext(
158+
llvm::Error DependencyScanningTool::initializeCompilerInstanceWithContext(
159159
StringRef CWD, const std::vector<std::string> &CommandLine) {
160160
return Worker.initializeCompierInstanceWithContext(CWD, CommandLine);
161161
}

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "clang/Tooling/DependencyScanning/DependencyScanningWorker.h"
10+
#include "DependencyScannerImpl.h"
1011
#include "clang/Basic/DiagnosticFrontend.h"
1112
#include "clang/Driver/Driver.h"
1213
#include "clang/Driver/Tool.h"
@@ -42,6 +43,8 @@ DependencyScanningWorker::DependencyScanningWorker(
4243
}
4344
}
4445

46+
DependencyScanningWorker::~DependencyScanningWorker() = default;
47+
4548
llvm::Error DependencyScanningWorker::computeDependencies(
4649
StringRef WorkingDirectory, const std::vector<std::string> &CommandLine,
4750
DependencyConsumer &Consumer, DependencyActionController &Controller,

clang/tools/clang-scan-deps/ClangScanDeps.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ int clang_scan_deps_main(int argc, char **argv, const llvm::ToolContext &) {
10851085
HadErrors = true;
10861086
}
10871087
} else if (ModuleName) {
1088-
if (llvm::Error Err = WorkerTool.initializeCompilerInstacneWithContext(
1088+
if (llvm::Error Err = WorkerTool.initializeCompilerInstanceWithContext(
10891089
CWD, Input->CommandLine)) {
10901090
handleErrorWithInfoString(
10911091
"Compiler instance with context setup error", std::move(Err),

0 commit comments

Comments
 (0)