Skip to content

Commit 21f85e2

Browse files
committed
[NFC] [C++20] [Modules] Pulling out getCXX20NamedModuleOutputPath into a seperate function
Required in the review process of #85050.
1 parent 38113a0 commit 21f85e2

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5814,19 +5814,9 @@ static const char *GetModuleOutputPath(Compilation &C, const JobAction &JA,
58145814
(C.getArgs().hasArg(options::OPT_fmodule_output) ||
58155815
C.getArgs().hasArg(options::OPT_fmodule_output_EQ)));
58165816

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

5821-
SmallString<64> OutputPath;
5822-
Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
5823-
if (FinalOutput && C.getArgs().hasArg(options::OPT_c))
5824-
OutputPath = FinalOutput->getValue();
5825-
else
5826-
OutputPath = BaseInput;
5827-
5828-
const char *Extension = types::getTypeTempSuffix(JA.getType());
5829-
llvm::sys::path::replace_extension(OutputPath, Extension);
58305820
return C.addResultFile(C.getArgs().MakeArgString(OutputPath.c_str()), &JA);
58315821
}
58325822

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3839,6 +3839,24 @@ bool Driver::getDefaultModuleCachePath(SmallVectorImpl<char> &Result) {
38393839
return false;
38403840
}
38413841

3842+
llvm::SmallString<256>
3843+
clang::driver::tools::getCXX20NamedModuleOutputPath(const ArgList &Args,
3844+
const char *BaseInput) {
3845+
if (Arg *ModuleOutputEQ = Args.getLastArg(options::OPT_fmodule_output_EQ))
3846+
return StringRef(ModuleOutputEQ->getValue());
3847+
3848+
SmallString<256> OutputPath;
3849+
if (Arg *FinalOutput = Args.getLastArg(options::OPT_o);
3850+
FinalOutput && Args.hasArg(options::OPT_c))
3851+
OutputPath = FinalOutput->getValue();
3852+
else
3853+
OutputPath = BaseInput;
3854+
3855+
const char *Extension = types::getTypeTempSuffix(types::TY_ModuleFile);
3856+
llvm::sys::path::replace_extension(OutputPath, Extension);
3857+
return OutputPath;
3858+
}
3859+
38423860
static bool RenderModulesOptions(Compilation &C, const Driver &D,
38433861
const ArgList &Args, const InputInfo &Input,
38443862
const InputInfo &Output, bool HaveStd20,

clang/lib/Driver/ToolChains/Clang.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,21 @@ DwarfFissionKind getDebugFissionKind(const Driver &D,
193193
const llvm::opt::ArgList &Args,
194194
llvm::opt::Arg *&Arg);
195195

196+
// Calculate the output path of the module file when compiling a module unit
197+
// with the `-fmodule-output` option or `-fmodule-output=` option specified.
198+
// The behavior is:
199+
// - If `-fmodule-output=` is specfied, then the module file is
200+
// writing to the value.
201+
// - Otherwise if the output object file of the module unit is specified, the
202+
// output path
203+
// of the module file should be the same with the output object file except
204+
// the corresponding suffix. This requires both `-o` and `-c` are specified.
205+
// - Otherwise, the output path of the module file will be the same with the
206+
// input with the corresponding suffix.
207+
llvm::SmallString<256>
208+
getCXX20NamedModuleOutputPath(const llvm::opt::ArgList &Args,
209+
const char *BaseInput);
210+
196211
} // end namespace tools
197212

198213
} // end namespace driver

0 commit comments

Comments
 (0)