Skip to content

Commit 7cf99ba

Browse files
committed
[clang] Extract CompilerInvocation::anyPath()
1 parent 9f0c449 commit 7cf99ba

File tree

3 files changed

+86
-76
lines changed

3 files changed

+86
-76
lines changed

clang/include/clang/Frontend/CompilerInvocation.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ class CompilerInvocationBase {
147147
}
148148
/// @}
149149

150+
/// Visitation.
151+
/// @{
152+
bool anyPath(llvm::function_ref<bool(StringRef)> Predicate) const;
153+
/// @}
154+
150155
/// Command line generation.
151156
/// @{
152157
using StringAllocator = llvm::function_ref<const char *(const Twine &)>;

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5280,6 +5280,84 @@ std::string CompilerInvocation::getModuleHash() const {
52805280
return toString(llvm::APInt(64, Hash), 36, /*Signed=*/false);
52815281
}
52825282

5283+
bool CompilerInvocationBase::anyPath(
5284+
llvm::function_ref<bool(StringRef)> Predicate) const {
5285+
#define PROPAGATE_TRUE_IF(PATH) \
5286+
do { \
5287+
if (Predicate(PATH)) \
5288+
return true; \
5289+
} while (0)
5290+
5291+
#define PROPAGATE_TRUE_IF_MANY(PATHS) \
5292+
do { \
5293+
if (llvm::any_of(PATHS, Predicate)) \
5294+
return true; \
5295+
} while (0)
5296+
5297+
// Header search paths.
5298+
const auto &HeaderSearchOpts = getHeaderSearchOpts();
5299+
PROPAGATE_TRUE_IF(HeaderSearchOpts.Sysroot);
5300+
for (auto &Entry : HeaderSearchOpts.UserEntries)
5301+
if (Entry.IgnoreSysRoot)
5302+
PROPAGATE_TRUE_IF(Entry.Path);
5303+
PROPAGATE_TRUE_IF(HeaderSearchOpts.ResourceDir);
5304+
PROPAGATE_TRUE_IF(HeaderSearchOpts.ModuleCachePath);
5305+
PROPAGATE_TRUE_IF(HeaderSearchOpts.ModuleUserBuildPath);
5306+
for (auto I = HeaderSearchOpts.PrebuiltModuleFiles.begin(),
5307+
E = HeaderSearchOpts.PrebuiltModuleFiles.end();
5308+
I != E;) {
5309+
auto Current = I++;
5310+
PROPAGATE_TRUE_IF(Current->second);
5311+
}
5312+
PROPAGATE_TRUE_IF_MANY(HeaderSearchOpts.PrebuiltModulePaths);
5313+
PROPAGATE_TRUE_IF_MANY(HeaderSearchOpts.VFSOverlayFiles);
5314+
5315+
// Preprocessor options.
5316+
const auto &PPOpts = getPreprocessorOpts();
5317+
PROPAGATE_TRUE_IF_MANY(PPOpts.MacroIncludes);
5318+
PROPAGATE_TRUE_IF_MANY(PPOpts.Includes);
5319+
PROPAGATE_TRUE_IF(PPOpts.ImplicitPCHInclude);
5320+
5321+
// Frontend options.
5322+
const auto &FrontendOpts = getFrontendOpts();
5323+
for (const FrontendInputFile &Input : FrontendOpts.Inputs) {
5324+
if (Input.isBuffer())
5325+
continue; // FIXME: Can this happen when parsing command-line?
5326+
5327+
PROPAGATE_TRUE_IF(Input.getFile());
5328+
}
5329+
PROPAGATE_TRUE_IF(FrontendOpts.CodeCompletionAt.FileName);
5330+
PROPAGATE_TRUE_IF_MANY(FrontendOpts.ModuleMapFiles);
5331+
PROPAGATE_TRUE_IF_MANY(FrontendOpts.ModuleFiles);
5332+
PROPAGATE_TRUE_IF_MANY(FrontendOpts.ModulesEmbedFiles);
5333+
PROPAGATE_TRUE_IF_MANY(FrontendOpts.ASTMergeFiles);
5334+
PROPAGATE_TRUE_IF(FrontendOpts.OverrideRecordLayoutsFile);
5335+
PROPAGATE_TRUE_IF(FrontendOpts.StatsFile);
5336+
5337+
// Filesystem options.
5338+
const auto &FileSystemOpts = getFileSystemOpts();
5339+
PROPAGATE_TRUE_IF(FileSystemOpts.WorkingDir);
5340+
5341+
// Codegen options.
5342+
const auto &CodeGenOpts = getCodeGenOpts();
5343+
PROPAGATE_TRUE_IF(CodeGenOpts.DebugCompilationDir);
5344+
PROPAGATE_TRUE_IF(CodeGenOpts.CoverageCompilationDir);
5345+
5346+
// Sanitizer options.
5347+
PROPAGATE_TRUE_IF_MANY(getLangOpts().NoSanitizeFiles);
5348+
5349+
// Coverage mappings.
5350+
PROPAGATE_TRUE_IF(CodeGenOpts.ProfileInstrumentUsePath);
5351+
PROPAGATE_TRUE_IF(CodeGenOpts.SampleProfileFile);
5352+
PROPAGATE_TRUE_IF(CodeGenOpts.ProfileRemappingFile);
5353+
5354+
// Dependency output options.
5355+
for (auto &ExtraDep : getDependencyOutputOpts().ExtraDeps)
5356+
PROPAGATE_TRUE_IF(ExtraDep.first);
5357+
5358+
return false;
5359+
}
5360+
52835361
void CompilerInvocationBase::generateCC1CommandLine(
52845362
ArgumentConsumer Consumer) const {
52855363
llvm::Triple T(getTargetOpts().Triple);

clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

Lines changed: 3 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -471,82 +471,9 @@ static bool isSafeToIgnoreCWD(const CowCompilerInvocation &CI) {
471471
// Check if the command line input uses relative paths.
472472
// It is not safe to ignore the current working directory if any of the
473473
// command line inputs use relative paths.
474-
#define IF_RELATIVE_RETURN_FALSE(PATH) \
475-
do { \
476-
if (!PATH.empty() && !llvm::sys::path::is_absolute(PATH)) \
477-
return false; \
478-
} while (0)
479-
480-
#define IF_ANY_RELATIVE_RETURN_FALSE(PATHS) \
481-
do { \
482-
if (llvm::any_of(PATHS, [](const auto &P) { \
483-
return !P.empty() && !llvm::sys::path::is_absolute(P); \
484-
})) \
485-
return false; \
486-
} while (0)
487-
488-
// Header search paths.
489-
const auto &HeaderSearchOpts = CI.getHeaderSearchOpts();
490-
IF_RELATIVE_RETURN_FALSE(HeaderSearchOpts.Sysroot);
491-
for (auto &Entry : HeaderSearchOpts.UserEntries)
492-
if (Entry.IgnoreSysRoot)
493-
IF_RELATIVE_RETURN_FALSE(Entry.Path);
494-
IF_RELATIVE_RETURN_FALSE(HeaderSearchOpts.ResourceDir);
495-
IF_RELATIVE_RETURN_FALSE(HeaderSearchOpts.ModuleCachePath);
496-
IF_RELATIVE_RETURN_FALSE(HeaderSearchOpts.ModuleUserBuildPath);
497-
for (auto I = HeaderSearchOpts.PrebuiltModuleFiles.begin(),
498-
E = HeaderSearchOpts.PrebuiltModuleFiles.end();
499-
I != E;) {
500-
auto Current = I++;
501-
IF_RELATIVE_RETURN_FALSE(Current->second);
502-
}
503-
IF_ANY_RELATIVE_RETURN_FALSE(HeaderSearchOpts.PrebuiltModulePaths);
504-
IF_ANY_RELATIVE_RETURN_FALSE(HeaderSearchOpts.VFSOverlayFiles);
505-
506-
// Preprocessor options.
507-
const auto &PPOpts = CI.getPreprocessorOpts();
508-
IF_ANY_RELATIVE_RETURN_FALSE(PPOpts.MacroIncludes);
509-
IF_ANY_RELATIVE_RETURN_FALSE(PPOpts.Includes);
510-
IF_RELATIVE_RETURN_FALSE(PPOpts.ImplicitPCHInclude);
511-
512-
// Frontend options.
513-
const auto &FrontendOpts = CI.getFrontendOpts();
514-
for (const FrontendInputFile &Input : FrontendOpts.Inputs) {
515-
if (Input.isBuffer())
516-
continue; // FIXME: Can this happen when parsing command-line?
517-
518-
IF_RELATIVE_RETURN_FALSE(Input.getFile());
519-
}
520-
IF_RELATIVE_RETURN_FALSE(FrontendOpts.CodeCompletionAt.FileName);
521-
IF_ANY_RELATIVE_RETURN_FALSE(FrontendOpts.ModuleMapFiles);
522-
IF_ANY_RELATIVE_RETURN_FALSE(FrontendOpts.ModuleFiles);
523-
IF_ANY_RELATIVE_RETURN_FALSE(FrontendOpts.ModulesEmbedFiles);
524-
IF_ANY_RELATIVE_RETURN_FALSE(FrontendOpts.ASTMergeFiles);
525-
IF_RELATIVE_RETURN_FALSE(FrontendOpts.OverrideRecordLayoutsFile);
526-
IF_RELATIVE_RETURN_FALSE(FrontendOpts.StatsFile);
527-
528-
// Filesystem options.
529-
const auto &FileSystemOpts = CI.getFileSystemOpts();
530-
IF_RELATIVE_RETURN_FALSE(FileSystemOpts.WorkingDir);
531-
532-
// Codegen options.
533-
const auto &CodeGenOpts = CI.getCodeGenOpts();
534-
IF_RELATIVE_RETURN_FALSE(CodeGenOpts.DebugCompilationDir);
535-
IF_RELATIVE_RETURN_FALSE(CodeGenOpts.CoverageCompilationDir);
536-
537-
// Sanitizer options.
538-
IF_ANY_RELATIVE_RETURN_FALSE(CI.getLangOpts().NoSanitizeFiles);
539-
540-
// Coverage mappings.
541-
IF_RELATIVE_RETURN_FALSE(CodeGenOpts.ProfileInstrumentUsePath);
542-
IF_RELATIVE_RETURN_FALSE(CodeGenOpts.SampleProfileFile);
543-
IF_RELATIVE_RETURN_FALSE(CodeGenOpts.ProfileRemappingFile);
544-
545-
// Dependency output options.
546-
for (auto &ExtraDep : CI.getDependencyOutputOpts().ExtraDeps)
547-
IF_RELATIVE_RETURN_FALSE(ExtraDep.first);
548-
549-
return true;
474+
return !CI.anyPath([](StringRef Path) {
475+
return !Path.empty() && !llvm::sys::path::is_absolute(Path);
476+
});
550477
}
551478

552479
static std::string getModuleContextHash(const ModuleDeps &MD,

0 commit comments

Comments
 (0)