Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -3281,7 +3281,8 @@ defm declspec : BoolOption<"f", "declspec",
def fmodules_cache_path : Joined<["-"], "fmodules-cache-path=">, Group<i_Group>,
Flags<[]>, Visibility<[ClangOption, CC1Option]>,
MetaVarName<"<directory>">,
HelpText<"Specify the module cache path">;
HelpText<"Specify the module cache path">,
MarshallingInfoString<HeaderSearchOpts<"ModuleCachePath">>;
def fmodules_user_build_path : Separate<["-"], "fmodules-user-build-path">, Group<i_Group>,
Flags<[]>, Visibility<[ClangOption, CC1Option]>,
MetaVarName<"<directory>">,
Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/Frontend/CompilerInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,8 @@ class CompilerInstance : public ModuleLoader {
GetDependencyDirectives = std::move(Getter);
}

static SmallString<256> normalizeModuleCachePath(FileManager &FileMgr,
StringRef ModuleCachePath);
std::string getSpecificModuleCachePath(StringRef ModuleHash);
std::string getSpecificModuleCachePath() {
return getSpecificModuleCachePath(getInvocation().getModuleHash());
Expand Down
19 changes: 17 additions & 2 deletions clang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,10 +547,25 @@ void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) {
PP->setDependencyDirectivesGetter(*GetDependencyDirectives);
}

SmallString<256>
CompilerInstance::normalizeModuleCachePath(FileManager &FileMgr,
StringRef ModuleCachePath) {
SmallString<256> NormalizedModuleCachePath(ModuleCachePath);
FileMgr.makeAbsolutePath(NormalizedModuleCachePath);
llvm::sys::path::remove_dots(NormalizedModuleCachePath);
return NormalizedModuleCachePath;
}

std::string CompilerInstance::getSpecificModuleCachePath(StringRef ModuleHash) {
assert(FileMgr && "Specific module cache path requires a FileManager");

if (getHeaderSearchOpts().ModuleCachePath.empty())
return "";

// Set up the module path, including the hash for the module-creation options.
SmallString<256> SpecificModuleCache(getHeaderSearchOpts().ModuleCachePath);
if (!SpecificModuleCache.empty() && !getHeaderSearchOpts().DisableModuleHash)
SmallString<256> SpecificModuleCache =
normalizeModuleCachePath(*FileMgr, getHeaderSearchOpts().ModuleCachePath);
if (!getHeaderSearchOpts().DisableModuleHash)
llvm::sys::path::append(SpecificModuleCache, ModuleHash);
return std::string(SpecificModuleCache);
}
Expand Down
20 changes: 2 additions & 18 deletions clang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3315,9 +3315,6 @@ static void GenerateHeaderSearchArgs(const HeaderSearchOptions &Opts,
if (Opts.UseLibcxx)
GenerateArg(Consumer, OPT_stdlib_EQ, "libc++");

if (!Opts.ModuleCachePath.empty())
GenerateArg(Consumer, OPT_fmodules_cache_path, Opts.ModuleCachePath);

for (const auto &File : Opts.PrebuiltModuleFiles)
GenerateArg(Consumer, OPT_fmodule_file, File.first + "=" + File.second);

Expand Down Expand Up @@ -3420,8 +3417,7 @@ static void GenerateHeaderSearchArgs(const HeaderSearchOptions &Opts,
}

static bool ParseHeaderSearchArgs(HeaderSearchOptions &Opts, ArgList &Args,
DiagnosticsEngine &Diags,
const std::string &WorkingDir) {
DiagnosticsEngine &Diags) {
unsigned NumErrorsBefore = Diags.getNumErrors();

HeaderSearchOptions *HeaderSearchOpts = &Opts;
Expand All @@ -3434,17 +3430,6 @@ static bool ParseHeaderSearchArgs(HeaderSearchOptions &Opts, ArgList &Args,
if (const Arg *A = Args.getLastArg(OPT_stdlib_EQ))
Opts.UseLibcxx = (strcmp(A->getValue(), "libc++") == 0);

// Canonicalize -fmodules-cache-path before storing it.
SmallString<128> P(Args.getLastArgValue(OPT_fmodules_cache_path));
if (!(P.empty() || llvm::sys::path::is_absolute(P))) {
if (WorkingDir.empty())
llvm::sys::fs::make_absolute(P);
else
llvm::sys::fs::make_absolute(WorkingDir, P);
}
llvm::sys::path::remove_dots(P);
Opts.ModuleCachePath = std::string(P);

// Only the -fmodule-file=<name>=<file> form.
for (const auto *A : Args.filtered(OPT_fmodule_file)) {
StringRef Val = A->getValue();
Expand Down Expand Up @@ -5021,8 +5006,7 @@ bool CompilerInvocation::CreateFromArgsImpl(
InputKind DashX = Res.getFrontendOpts().DashX;
ParseTargetArgs(Res.getTargetOpts(), Args, Diags);
llvm::Triple T(Res.getTargetOpts().Triple);
ParseHeaderSearchArgs(Res.getHeaderSearchOpts(), Args, Diags,
Res.getFileSystemOpts().WorkingDir);
ParseHeaderSearchArgs(Res.getHeaderSearchOpts(), Args, Diags);
if (Res.getFrontendOpts().GenReducedBMI ||
Res.getFrontendOpts().ProgramAction ==
frontend::GenerateReducedModuleInterface ||
Expand Down
6 changes: 5 additions & 1 deletion clang/lib/Serialization/ASTWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#include "clang/Basic/TargetInfo.h"
#include "clang/Basic/TargetOptions.h"
#include "clang/Basic/Version.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/HeaderSearch.h"
#include "clang/Lex/HeaderSearchOptions.h"
#include "clang/Lex/MacroInfo.h"
Expand Down Expand Up @@ -1710,9 +1711,12 @@ void ASTWriter::WriteControlBlock(Preprocessor &PP, StringRef isysroot) {
const HeaderSearchOptions &HSOpts =
PP.getHeaderSearchInfo().getHeaderSearchOpts();

auto HSOpts_ModuleCachePath = CompilerInstance::normalizeModuleCachePath(
PP.getFileManager(), HSOpts.ModuleCachePath);

AddString(HSOpts.Sysroot, Record);
AddString(HSOpts.ResourceDir, Record);
AddString(HSOpts.ModuleCachePath, Record);
AddString(HSOpts_ModuleCachePath, Record);
AddString(HSOpts.ModuleUserBuildPath, Record);
Record.push_back(HSOpts.DisableModuleHash);
Record.push_back(HSOpts.ImplicitModuleMaps);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,8 @@ class DependencyScanningAction {

// Use the dependency scanning optimized file system if requested to do so.
if (DepFS) {
StringRef ModulesCachePath =
ScanInstance.getHeaderSearchOpts().ModuleCachePath;

auto ModulesCachePath = CompilerInstance::normalizeModuleCachePath(
*FileMgr, ScanInstance.getHeaderSearchOpts().ModuleCachePath);
DepFS->resetBypassedPathPrefix();
if (!ModulesCachePath.empty())
DepFS->setBypassedPathPrefix(ModulesCachePath);
Expand Down
18 changes: 18 additions & 0 deletions clang/test/Modules/modules-cache-path-canonicalization-output.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// This checks that implicitly-built modules produce identical PCM
// files regardless of the spelling of the same module cache path.

// RUN: rm -rf %t
// RUN: split-file %s %t

// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fsyntax-only %t/tu.c \
// RUN: -fmodules-cache-path=%t/cache -fdisable-module-hash
// RUN: mv %t/cache/M.pcm %t/M.pcm
// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fsyntax-only %t/tu.c \
// RUN: -fmodules-cache-path=%t/./cache -fdisable-module-hash
// RUN: diff %t/./cache/M.pcm %t/M.pcm

//--- tu.c
#include "M.h"
//--- M.h
//--- module.modulemap
module M { header "M.h" }