Skip to content

Commit a37ebd2

Browse files
committed
[clang][cas] Use the new CompilerInvocation::visitPaths() API
1 parent da921a5 commit a37ebd2

File tree

4 files changed

+32
-86
lines changed

4 files changed

+32
-86
lines changed

clang/include/clang/Frontend/CompilerInvocation.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,14 @@ class CompilerInvocation : public CompilerInvocationBase {
313313
}
314314
/// @}
315315

316+
/// Visitation.
317+
/// @{
318+
/// Visits paths stored in the invocation. The callback may return true to
319+
/// short-circuit the visitation, or return false to continue visiting. This
320+
/// is allowed to mutate the visited paths.
321+
void visitPaths(llvm::function_ref<bool(std::string &)> Callback);
322+
/// @}
323+
316324
/// Create a compiler invocation from a list of input options.
317325
/// \returns true on success.
318326
///

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5804,6 +5804,9 @@ void CompilerInvocationBase::visitPathsImpl(
58045804
for (auto &Entry : HeaderSearchOpts.UserEntries)
58055805
if (Entry.IgnoreSysRoot)
58065806
RETURN_IF(Entry.Path);
5807+
// TODO: Consider upstreaming this.
5808+
for (auto &Prefix : HeaderSearchOpts.SystemHeaderPrefixes)
5809+
RETURN_IF(Prefix.Prefix);
58075810
RETURN_IF(HeaderSearchOpts.ResourceDir);
58085811
RETURN_IF(HeaderSearchOpts.ModuleCachePath);
58095812
RETURN_IF(HeaderSearchOpts.ModuleUserBuildPath);
@@ -5837,6 +5840,7 @@ void CompilerInvocationBase::visitPathsImpl(
58375840
// Filesystem options.
58385841
auto &FileSystemOpts = *this->FSOpts;
58395842
RETURN_IF(FileSystemOpts.WorkingDir);
5843+
RETURN_IF(FileSystemOpts.CASFileSystemWorkingDirectory);
58405844

58415845
// Codegen options.
58425846
auto &CodeGenOpts = *this->CodeGenOpts;
@@ -5864,6 +5868,14 @@ void CompilerInvocationBase::visitPaths(
58645868
[&Callback](std::string &Path) { return Callback(StringRef(Path)); });
58655869
}
58665870

5871+
void CompilerInvocation::visitPaths(
5872+
llvm::function_ref<bool(std::string &)> Callback) {
5873+
// Unlike the copy-on-write variant (and therefore potentially also the base
5874+
// class), regular CompilerInvocation does not share the option objects with
5875+
// anyone else. This makes the potential mutation in the callback safe.
5876+
visitPathsImpl(Callback);
5877+
}
5878+
58675879
void CompilerInvocationBase::generateCC1CommandLine(
58685880
ArgumentConsumer Consumer) const {
58695881
llvm::Triple T(getTargetOpts().Triple);

clang/lib/Tooling/DependencyScanning/IncludeTreeActionController.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,8 @@ Error IncludeTreeActionController::initialize(
312312

313313
addReversePrefixMappingFileSystem(PrefixMapper, ScanInstance);
314314

315+
// TODO: Confirm why it's not enough to do this in
316+
// DepscanPrefixMapping::remapInvocationPaths.
315317
// These are written in the predefines buffer, so we need to remap them.
316318
for (std::string &Include : PPOpts.Includes)
317319
PrefixMapper.mapInPlace(Include);

clang/lib/Tooling/DependencyScanning/ScanAndUpdateArgs.cpp

Lines changed: 10 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -145,94 +145,18 @@ void DepscanPrefixMapping::remapInvocationPaths(CompilerInvocation &Invocation,
145145
FrontendOpts.PathPrefixMappings.emplace_back(Map.Old, Map.New);
146146
}
147147

148-
auto mapInPlaceAll = [&](std::vector<std::string> &Vector) {
149-
for (auto &Path : Vector)
150-
Mapper.mapInPlace(Path);
151-
};
152-
153-
auto &FileSystemOpts = Invocation.getFileSystemOpts();
154-
Mapper.mapInPlace(FileSystemOpts.CASFileSystemWorkingDirectory);
155-
156-
// Remap header search.
157-
auto &HeaderSearchOpts = Invocation.getHeaderSearchOpts();
158-
Mapper.mapInPlace(HeaderSearchOpts.Sysroot);
159-
for (auto &Entry : HeaderSearchOpts.UserEntries)
160-
if (Entry.IgnoreSysRoot)
161-
Mapper.mapInPlace(Entry.Path);
162-
163-
for (auto &Prefix : HeaderSearchOpts.SystemHeaderPrefixes)
164-
Mapper.mapInPlace(Prefix.Prefix);
165-
Mapper.mapInPlace(HeaderSearchOpts.ResourceDir);
166-
Mapper.mapInPlace(HeaderSearchOpts.ModuleCachePath);
167-
Mapper.mapInPlace(HeaderSearchOpts.ModuleUserBuildPath);
168-
for (auto I = HeaderSearchOpts.PrebuiltModuleFiles.begin(),
169-
E = HeaderSearchOpts.PrebuiltModuleFiles.end();
170-
I != E;) {
171-
auto Current = I++;
172-
Mapper.mapInPlace(Current->second);
173-
}
174-
mapInPlaceAll(HeaderSearchOpts.PrebuiltModulePaths);
175-
mapInPlaceAll(HeaderSearchOpts.VFSOverlayFiles);
176-
177-
// Preprocessor options.
178-
auto &PPOpts = Invocation.getPreprocessorOpts();
179-
mapInPlaceAll(PPOpts.MacroIncludes);
180-
mapInPlaceAll(PPOpts.Includes);
181-
Mapper.mapInPlace(PPOpts.ImplicitPCHInclude);
182-
183-
// Frontend options.
184-
for (FrontendInputFile &Input : FrontendOpts.Inputs) {
185-
if (Input.isBuffer())
186-
continue; // FIXME: Can this happen when parsing command-line?
187-
188-
SmallString<256> RemappedFile;
189-
Mapper.map(Input.getFile(), RemappedFile);
190-
if (RemappedFile != Input.getFile())
191-
Input =
192-
FrontendInputFile(RemappedFile, Input.getKind(), Input.isSystem());
193-
}
194-
195-
// Skip the output file. That's not the input CAS filesystem.
196-
// Mapper.mapInPlace(OutputFile); <-- this doesn't make sense.
197-
198-
Mapper.mapInPlace(FrontendOpts.CodeCompletionAt.FileName);
199-
200-
// Don't remap plugins (for now), since we don't know how to remap their
201-
// arguments. Maybe they should be loaded outside of the CAS filesystem?
148+
// Note: We don't remap plugins for now, since we don't know how to remap
149+
// their arguments. Maybe they should be loaded outside of the CAS filesystem?
202150
// Maybe we should error?
203151
//
204-
// Mapper.mapInPlaceOrFilterOut(FrontendOpts.Plugins);
205-
206-
mapInPlaceAll(FrontendOpts.ModuleMapFiles);
207-
mapInPlaceAll(FrontendOpts.ModuleFiles);
208-
mapInPlaceAll(FrontendOpts.ModulesEmbedFiles);
209-
mapInPlaceAll(FrontendOpts.ASTMergeFiles);
210-
Mapper.mapInPlace(FrontendOpts.OverrideRecordLayoutsFile);
211-
Mapper.mapInPlace(FrontendOpts.StatsFile);
212-
213-
// Filesystem options.
214-
Mapper.mapInPlace(FileSystemOpts.WorkingDir);
215-
216-
// Code generation options.
217-
auto &CodeGenOpts = Invocation.getCodeGenOpts();
218-
Mapper.mapInPlace(CodeGenOpts.DebugCompilationDir);
219-
Mapper.mapInPlace(CodeGenOpts.CoverageCompilationDir);
220-
221-
// Sanitizer options.
222-
mapInPlaceAll(Invocation.getLangOpts().NoSanitizeFiles);
223-
224-
// Handle coverage mappings.
225-
Mapper.mapInPlace(CodeGenOpts.ProfileInstrumentUsePath);
226-
Mapper.mapInPlace(CodeGenOpts.SampleProfileFile);
227-
Mapper.mapInPlace(CodeGenOpts.ProfileRemappingFile);
228-
229-
// Dependency output options.
230-
// Note: these are not in the cache key, but they are in the module context
231-
// hash, which indirectly impacts the cache key when importing a module.
232-
// In the future we may change how -fmodule-file-cache-key works when
233-
// remapping to avoid needing this.
234-
for (auto &ExtraDep : Invocation.getDependencyOutputOpts().ExtraDeps)
235-
Mapper.mapInPlace(ExtraDep.first);
152+
// Note: DependencyOutputOptions::ExtraDeps are not in the cache key, but they
153+
// are in the module context hash, which indirectly impacts the cache key when
154+
// importing a module. In the future we may change how -fmodule-file-cache-key
155+
// works when remapping to avoid needing this.
156+
Invocation.visitPaths([&Mapper](std::string &Path) {
157+
Mapper.mapInPlace(Path);
158+
return false;
159+
});
236160
}
237161

238162
void DepscanPrefixMapping::configurePrefixMapper(const CompilerInvocation &CI,

0 commit comments

Comments
 (0)