Skip to content

Commit

Permalink
[NFC] [C++20] [Modules] Pulling out getCXX20NamedModuleOutputPath int…
Browse files Browse the repository at this point in the history
…o a seperate function

Required in the review process of
#85050.
  • Loading branch information
ChuanqiXu9 committed Apr 2, 2024
1 parent 38113a0 commit 21f85e2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
14 changes: 2 additions & 12 deletions clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5814,19 +5814,9 @@ static const char *GetModuleOutputPath(Compilation &C, const JobAction &JA,
(C.getArgs().hasArg(options::OPT_fmodule_output) ||
C.getArgs().hasArg(options::OPT_fmodule_output_EQ)));

if (Arg *ModuleOutputEQ =
C.getArgs().getLastArg(options::OPT_fmodule_output_EQ))
return C.addResultFile(ModuleOutputEQ->getValue(), &JA);
SmallString<256> OutputPath =
tools::getCXX20NamedModuleOutputPath(C.getArgs(), BaseInput);

SmallString<64> OutputPath;
Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
if (FinalOutput && C.getArgs().hasArg(options::OPT_c))
OutputPath = FinalOutput->getValue();
else
OutputPath = BaseInput;

const char *Extension = types::getTypeTempSuffix(JA.getType());
llvm::sys::path::replace_extension(OutputPath, Extension);
return C.addResultFile(C.getArgs().MakeArgString(OutputPath.c_str()), &JA);
}

Expand Down
18 changes: 18 additions & 0 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3839,6 +3839,24 @@ bool Driver::getDefaultModuleCachePath(SmallVectorImpl<char> &Result) {
return false;
}

llvm::SmallString<256>
clang::driver::tools::getCXX20NamedModuleOutputPath(const ArgList &Args,
const char *BaseInput) {
if (Arg *ModuleOutputEQ = Args.getLastArg(options::OPT_fmodule_output_EQ))
return StringRef(ModuleOutputEQ->getValue());

SmallString<256> OutputPath;
if (Arg *FinalOutput = Args.getLastArg(options::OPT_o);
FinalOutput && Args.hasArg(options::OPT_c))
OutputPath = FinalOutput->getValue();
else
OutputPath = BaseInput;

const char *Extension = types::getTypeTempSuffix(types::TY_ModuleFile);
llvm::sys::path::replace_extension(OutputPath, Extension);
return OutputPath;
}

static bool RenderModulesOptions(Compilation &C, const Driver &D,
const ArgList &Args, const InputInfo &Input,
const InputInfo &Output, bool HaveStd20,
Expand Down
15 changes: 15 additions & 0 deletions clang/lib/Driver/ToolChains/Clang.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,21 @@ DwarfFissionKind getDebugFissionKind(const Driver &D,
const llvm::opt::ArgList &Args,
llvm::opt::Arg *&Arg);

// Calculate the output path of the module file when compiling a module unit
// with the `-fmodule-output` option or `-fmodule-output=` option specified.
// The behavior is:
// - If `-fmodule-output=` is specfied, then the module file is
// writing to the value.
// - Otherwise if the output object file of the module unit is specified, the
// output path
// of the module file should be the same with the output object file except
// the corresponding suffix. This requires both `-o` and `-c` are specified.
// - Otherwise, the output path of the module file will be the same with the
// input with the corresponding suffix.
llvm::SmallString<256>
getCXX20NamedModuleOutputPath(const llvm::opt::ArgList &Args,
const char *BaseInput);

} // end namespace tools

} // end namespace driver
Expand Down

0 comments on commit 21f85e2

Please sign in to comment.