Skip to content

Commit

Permalink
[clang] Pull some utility functions into CompilerInvocation NFC
Browse files Browse the repository at this point in the history
Move copying compiler arguments to a vector<string> and modifying
common module-related options into CompilerInvocation in preparation for
using some of them in more places and to avoid duplicating this code
accidentally in the future.

Differential Revision: https://reviews.llvm.org/D132419
  • Loading branch information
benlangmuir committed Aug 23, 2022
1 parent 86bfab2 commit 3708a14
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 33 deletions.
16 changes: 15 additions & 1 deletion clang/include/clang/Frontend/CompilerInvocation.h
Expand Up @@ -224,7 +224,7 @@ class CompilerInvocation : public CompilerInvocationRefBase,
std::string getModuleHash() const;

using StringAllocator = llvm::function_ref<const char *(const llvm::Twine &)>;
/// Generate a cc1-compatible command line arguments from this instance.
/// Generate cc1-compatible command line arguments from this instance.
///
/// \param [out] Args - The generated arguments. Note that the caller is
/// responsible for inserting the path to the clang executable and "-cc1" if
Expand All @@ -235,6 +235,20 @@ class CompilerInvocation : public CompilerInvocationRefBase,
void generateCC1CommandLine(llvm::SmallVectorImpl<const char *> &Args,
StringAllocator SA) const;

/// Generate cc1-compatible command line arguments from this instance,
/// wrapping the result as a std::vector<std::string>.
///
/// This is a (less-efficient) wrapper over generateCC1CommandLine().
std::vector<std::string> getCC1CommandLine() const;

/// Reset all of the options that are not considered when building a
/// module.
void resetNonModularOptions();

/// Disable implicit modules and canonicalize options that are only used by
/// implicit modules.
void clearImplicitModuleBuildOptions();

private:
static bool CreateFromArgsImpl(CompilerInvocation &Res,
ArrayRef<const char *> CommandLineArgs,
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/Frontend/CompilerInstance.cpp
Expand Up @@ -1150,8 +1150,7 @@ compileModuleImpl(CompilerInstance &ImportingInstance, SourceLocation ImportLoc,

// For any options that aren't intended to affect how a module is built,
// reset them to their default values.
Invocation->getLangOpts()->resetNonModularOptions();
PPOpts.resetNonModularOptions();
Invocation->resetNonModularOptions();

// Remove any macro definitions that are explicitly ignored by the module.
// They aren't supposed to affect how the module is built anyway.
Expand Down
31 changes: 31 additions & 0 deletions clang/lib/Frontend/CompilerInvocation.cpp
Expand Up @@ -4672,6 +4672,37 @@ void CompilerInvocation::generateCC1CommandLine(
GenerateDependencyOutputArgs(DependencyOutputOpts, Args, SA);
}

std::vector<std::string> CompilerInvocation::getCC1CommandLine() const {
// Set up string allocator.
llvm::BumpPtrAllocator Alloc;
llvm::StringSaver Strings(Alloc);
auto SA = [&Strings](const Twine &Arg) { return Strings.save(Arg).data(); };

// Synthesize full command line from the CompilerInvocation, including "-cc1".
SmallVector<const char *, 32> Args{"-cc1"};
generateCC1CommandLine(Args, SA);

// Convert arguments to the return type.
return std::vector<std::string>{Args.begin(), Args.end()};
}

void CompilerInvocation::resetNonModularOptions() {
getLangOpts()->resetNonModularOptions();
getPreprocessorOpts().resetNonModularOptions();
}

void CompilerInvocation::clearImplicitModuleBuildOptions() {
getLangOpts()->ImplicitModules = false;
getHeaderSearchOpts().ImplicitModuleMaps = false;
getHeaderSearchOpts().ModuleCachePath.clear();
getHeaderSearchOpts().ModulesValidateOncePerBuildSession = false;
getHeaderSearchOpts().BuildSessionTimestamp = 0;
// The specific values we canonicalize to for pruning don't affect behaviour,
/// so use the default values so they may be dropped from the command-line.
getHeaderSearchOpts().ModuleCachePruneInterval = 7 * 24 * 60 * 60;
getHeaderSearchOpts().ModuleCachePruneAfter = 31 * 24 * 60 * 60;
}

IntrusiveRefCntPtr<llvm::vfs::FileSystem>
clang::createVFSFromCompilerInvocation(const CompilerInvocation &CI,
DiagnosticsEngine &Diags) {
Expand Down
33 changes: 3 additions & 30 deletions clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
Expand Up @@ -98,8 +98,8 @@ ModuleDepCollector::makeInvocationForModuleBuildWithoutOutputs(
// Make a deep copy of the original Clang invocation.
CompilerInvocation CI(OriginalInvocation);

CI.getLangOpts()->resetNonModularOptions();
CI.getPreprocessorOpts().resetNonModularOptions();
CI.resetNonModularOptions();
CI.clearImplicitModuleBuildOptions();

// Remove options incompatible with explicit module build or are likely to
// differ between identical modules discovered from different translation
Expand All @@ -120,18 +120,6 @@ ModuleDepCollector::makeInvocationForModuleBuildWithoutOutputs(
CI.getLangOpts()->ModuleName = Deps.ID.ModuleName;
CI.getFrontendOpts().IsSystemModule = Deps.IsSystem;

// Disable implicit modules and canonicalize options that are only used by
// implicit modules.
CI.getLangOpts()->ImplicitModules = false;
CI.getHeaderSearchOpts().ImplicitModuleMaps = false;
CI.getHeaderSearchOpts().ModuleCachePath.clear();
CI.getHeaderSearchOpts().ModulesValidateOncePerBuildSession = false;
CI.getHeaderSearchOpts().BuildSessionTimestamp = 0;
// The specific values we canonicalize to for pruning don't affect behaviour,
/// so use the default values so they will be dropped from the command-line.
CI.getHeaderSearchOpts().ModuleCachePruneInterval = 7 * 24 * 60 * 60;
CI.getHeaderSearchOpts().ModuleCachePruneAfter = 31 * 24 * 60 * 60;

// Inputs
InputKind ModuleMapInputKind(CI.getFrontendOpts().DashX.getLanguage(),
InputKind::Format::ModuleMap);
Expand Down Expand Up @@ -182,23 +170,8 @@ ModuleDepCollector::makeInvocationForModuleBuildWithoutOutputs(
return CI;
}

static std::vector<std::string>
serializeCompilerInvocation(const CompilerInvocation &CI) {
// Set up string allocator.
llvm::BumpPtrAllocator Alloc;
llvm::StringSaver Strings(Alloc);
auto SA = [&Strings](const Twine &Arg) { return Strings.save(Arg).data(); };

// Synthesize full command line from the CompilerInvocation, including "-cc1".
SmallVector<const char *, 32> Args{"-cc1"};
CI.generateCC1CommandLine(Args, SA);

// Convert arguments to the return type.
return std::vector<std::string>{Args.begin(), Args.end()};
}

std::vector<std::string> ModuleDeps::getCanonicalCommandLine() const {
return serializeCompilerInvocation(BuildInvocation);
return BuildInvocation.getCC1CommandLine();
}

static std::string getModuleContextHash(const ModuleDeps &MD,
Expand Down

0 comments on commit 3708a14

Please sign in to comment.