Skip to content

Commit 3a3a7c9

Browse files
committed
[clang][cas] Move include-tree-specific code out of core scanner
This hoists all the include-tree specific code out of the core dependency scanner, and instead injects the necessary PPCallbacks as part IncludeTreeActionController::initialize. In addition to improved division of responsibility in the code, this allows include-tree to be used with ModuleDepCollector, which will (in future commits) allow us to use include-tree to build and import modules. (cherry picked from commit 91fa5b1)
1 parent ebd9ddc commit 3a3a7c9

File tree

2 files changed

+71
-58
lines changed

2 files changed

+71
-58
lines changed

clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,50 @@ class IncludeTreeActionController : public CallbackActionController {
234234
llvm::DenseMap<const FileEntry *, Optional<cas::ObjectRef>> ObjectForFile;
235235
Optional<llvm::Error> ErrorToReport;
236236
};
237+
238+
/// A utility for adding \c PPCallbacks to a compiler instance at the
239+
/// appropriate time.
240+
struct PPCallbacksDependencyCollector : public DependencyCollector {
241+
using MakeCB =
242+
llvm::unique_function<std::unique_ptr<PPCallbacks>(Preprocessor &)>;
243+
MakeCB Create;
244+
PPCallbacksDependencyCollector(MakeCB Create) : Create(std::move(Create)) {}
245+
void attachToPreprocessor(Preprocessor &PP) final {
246+
std::unique_ptr<PPCallbacks> CB = Create(PP);
247+
assert(CB);
248+
PP.addPPCallbacks(std::move(CB));
249+
}
250+
};
251+
252+
struct IncludeTreePPCallbacks : public PPCallbacks {
253+
DependencyActionController &Controller;
254+
Preprocessor &PP;
255+
256+
public:
257+
IncludeTreePPCallbacks(DependencyActionController &Controller,
258+
Preprocessor &PP)
259+
: Controller(Controller), PP(PP) {}
260+
261+
void LexedFileChanged(FileID FID, LexedFileChangeReason Reason,
262+
SrcMgr::CharacteristicKind FileType, FileID PrevFID,
263+
SourceLocation Loc) override {
264+
switch (Reason) {
265+
case LexedFileChangeReason::EnterFile:
266+
Controller.enteredInclude(PP, FID);
267+
break;
268+
case LexedFileChangeReason::ExitFile: {
269+
Controller.exitedInclude(PP, FID, PrevFID, Loc);
270+
break;
271+
}
272+
}
273+
}
274+
275+
void HasInclude(SourceLocation Loc, StringRef FileName, bool IsAngled,
276+
OptionalFileEntryRef File,
277+
SrcMgr::CharacteristicKind FileType) override {
278+
Controller.handleHasIncludeCheck(PP, File.has_value());
279+
}
280+
};
237281
} // namespace
238282

239283
/// The PCH recorded file paths with canonical paths, create a VFS that
@@ -274,6 +318,14 @@ Error IncludeTreeActionController::initialize(
274318
};
275319
ensurePathRemapping();
276320

321+
// Attach callbacks for the IncludeTree of the TU. The preprocessor
322+
// does not exist yet, so we need to indirect this via DependencyCollector.
323+
auto DC = std::make_shared<PPCallbacksDependencyCollector>(
324+
[this](Preprocessor &PP) {
325+
return std::make_unique<IncludeTreePPCallbacks>(*this, PP);
326+
});
327+
ScanInstance.addDependencyCollector(std::move(DC));
328+
277329
return Error::success();
278330
}
279331

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 19 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -161,48 +161,17 @@ static void sanitizeDiagOpts(DiagnosticOptions &DiagOpts) {
161161
DiagOpts.IgnoreWarnings = true;
162162
}
163163

164-
struct IncludeTreePPCallbacks : public PPCallbacks {
165-
DependencyActionController &Controller;
166-
Preprocessor &PP;
167-
168-
public:
169-
IncludeTreePPCallbacks(DependencyActionController &Controller,
170-
Preprocessor &PP)
171-
: Controller(Controller), PP(PP) {}
172-
173-
void LexedFileChanged(FileID FID, LexedFileChangeReason Reason,
174-
SrcMgr::CharacteristicKind FileType, FileID PrevFID,
175-
SourceLocation Loc) override {
176-
switch (Reason) {
177-
case LexedFileChangeReason::EnterFile:
178-
Controller.enteredInclude(PP, FID);
179-
break;
180-
case LexedFileChangeReason::ExitFile: {
181-
Controller.exitedInclude(PP, FID, PrevFID, Loc);
182-
break;
183-
}
184-
}
185-
}
186-
187-
void HasInclude(SourceLocation Loc, StringRef FileName, bool IsAngled,
188-
Optional<FileEntryRef> File,
189-
SrcMgr::CharacteristicKind FileType) override {
190-
Controller.handleHasIncludeCheck(PP, File.has_value());
191-
}
192-
};
193-
194-
class IncludeTreeCollector : public DependencyFileGenerator {
195-
DependencyActionController &Controller;
196-
std::unique_ptr<DependencyOutputOptions> Opts;
197-
bool EmitDependencyFile = false;
164+
/// Builds a dependency file after reversing prefix mappings. This allows
165+
/// emitting a .d file that has real paths where they would otherwise be
166+
/// canonicalized.
167+
class ReversePrefixMappingDependencyFileGenerator
168+
: public DependencyFileGenerator {
198169
llvm::PrefixMapper ReverseMapper;
199170

200171
public:
201-
IncludeTreeCollector(DependencyActionController &Controller,
202-
std::unique_ptr<DependencyOutputOptions> Opts,
203-
bool EmitDependencyFile)
204-
: DependencyFileGenerator(*Opts), Controller(Controller),
205-
Opts(std::move(Opts)), EmitDependencyFile(EmitDependencyFile) {}
172+
ReversePrefixMappingDependencyFileGenerator(
173+
const DependencyOutputOptions &Opts)
174+
: DependencyFileGenerator(Opts) {}
206175

207176
Error initialize(const CompilerInvocation &CI,
208177
const DepscanPrefixMapping &PrefixMapping) {
@@ -217,11 +186,6 @@ class IncludeTreeCollector : public DependencyFileGenerator {
217186
return Error::success();
218187
}
219188

220-
void attachToPreprocessor(Preprocessor &PP) override {
221-
PP.addPPCallbacks(std::make_unique<IncludeTreePPCallbacks>(Controller, PP));
222-
DependencyFileGenerator::attachToPreprocessor(PP);
223-
}
224-
225189
void maybeAddDependency(StringRef Filename, bool FromModule, bool IsSystem,
226190
bool IsModuleFile, bool IsMissing) override {
227191
if (ReverseMapper.empty())
@@ -235,11 +199,6 @@ class IncludeTreeCollector : public DependencyFileGenerator {
235199
return DependencyFileGenerator::maybeAddDependency(
236200
New, FromModule, IsSystem, IsModuleFile, IsMissing);
237201
}
238-
239-
void finishedMainFile(DiagnosticsEngine &Diags) override {
240-
if (EmitDependencyFile)
241-
DependencyFileGenerator::finishedMainFile(Diags);
242-
}
243202
};
244203

245204
/// See \c WrapScanModuleBuildAction.
@@ -455,17 +414,19 @@ class DependencyScanningAction : public tooling::ToolAction {
455414
std::make_shared<DependencyConsumerForwarder>(
456415
std::move(Opts), WorkingDirectory, Consumer, EmitDependencyFile));
457416
break;
458-
case ScanningOutputFormat::IncludeTree: {
459-
auto IncTreeCollector = std::make_shared<IncludeTreeCollector>(
460-
Controller, std::move(Opts), EmitDependencyFile);
461-
if (Error E = IncTreeCollector->initialize(
462-
ScanInstance.getInvocation(), *Controller.getPrefixMapping()))
463-
return reportError(std::move(E));
464-
ScanInstance.addDependencyCollector(std::move(IncTreeCollector));
465-
break;
466-
}
417+
case ScanningOutputFormat::IncludeTree:
467418
case ScanningOutputFormat::Full:
468419
case ScanningOutputFormat::FullTree:
420+
if (EmitDependencyFile) {
421+
auto DFG =
422+
std::make_shared<ReversePrefixMappingDependencyFileGenerator>(
423+
*Opts);
424+
if (auto *Mapping = Controller.getPrefixMapping())
425+
if (auto E = DFG->initialize(ScanInstance.getInvocation(), *Mapping))
426+
return reportError(std::move(E));
427+
ScanInstance.addDependencyCollector(std::move(DFG));
428+
}
429+
469430
MDC = std::make_shared<ModuleDepCollector>(
470431
std::move(Opts), ScanInstance, Consumer, Controller,
471432
OriginalInvocation, OptimizeArgs, EagerLoadModules);

0 commit comments

Comments
 (0)