Skip to content

Commit d92656d

Browse files
author
rsmith
committed
Move functionality for handling module maps as inputs from the -emit-module
action to the general FrontendAction infrastructure. This permits applying -E, -ast-dump, -fsyntax-only, and so on to a module map compilation. (The -E form is not currently especially useful yet as there's no good way to take the output and use it to actually build a module.) In order to support this, -cc1 now accepts -x <lang>-module-map in all cases where it accepts -x <lang> for a language we can parse (not ir/ast). And for uniformity, we also accept -x <lang>-header for all such languages (we used to reject for cuda and renderscript), and -x <lang>-cpp-output for all such languages (we used to reject for c, cl, and renderscript). (None of these new alternatives are accepted by the driver yet, so no user-visible changes.) git-svn-id: http://llvm.org/svn/llvm-project/cfe/trunk@301610 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 7d6ce41 commit d92656d

File tree

8 files changed

+353
-286
lines changed

8 files changed

+353
-286
lines changed

include/clang/Frontend/FrontendActions.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ class GenerateModuleAction : public ASTFrontendAction {
9999
CreateOutputFile(CompilerInstance &CI, StringRef InFile) = 0;
100100

101101
protected:
102-
bool BeginSourceFileAction(CompilerInstance &CI, StringRef Filename) override;
103-
104102
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
105103
StringRef InFile) override;
106104

@@ -112,20 +110,11 @@ class GenerateModuleAction : public ASTFrontendAction {
112110
};
113111

114112
class GenerateModuleFromModuleMapAction : public GenerateModuleAction {
115-
clang::Module *Module = nullptr;
116-
const FileEntry *ModuleMapForUniquing = nullptr;
117-
bool IsSystem = false;
118-
119113
private:
120114
bool BeginSourceFileAction(CompilerInstance &CI, StringRef Filename) override;
121115

122116
std::unique_ptr<raw_pwrite_stream>
123117
CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
124-
125-
public:
126-
GenerateModuleFromModuleMapAction() {}
127-
GenerateModuleFromModuleMapAction(const FileEntry *ModuleMap, bool IsSystem)
128-
: ModuleMapForUniquing(ModuleMap), IsSystem(IsSystem) {}
129118
};
130119

131120
class GenerateModuleInterfaceAction : public GenerateModuleAction {

include/clang/Frontend/FrontendOptions.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class MemoryBuffer;
2323
}
2424

2525
namespace clang {
26+
class FileEntry;
2627

2728
namespace frontend {
2829
enum ActionKind {
@@ -100,9 +101,8 @@ class InputKind {
100101
Precompiled
101102
};
102103

103-
constexpr InputKind(Language L = Unknown, bool PP = false)
104-
: Lang(L), Fmt(Source), Preprocessed(PP) {}
105-
constexpr InputKind(Language L, Format F, bool PP = false)
104+
constexpr InputKind(Language L = Unknown, Format F = Source,
105+
bool PP = false)
106106
: Lang(L), Fmt(F), Preprocessed(PP) {}
107107

108108
Language getLanguage() const { return static_cast<Language>(Lang); }
@@ -118,6 +118,9 @@ class InputKind {
118118
InputKind getPreprocessed() const {
119119
return InputKind(getLanguage(), getFormat(), true);
120120
}
121+
InputKind withFormat(Format F) const {
122+
return InputKind(getLanguage(), F, isPreprocessed());
123+
}
121124
};
122125

123126
/// \brief An input file for the front end.
@@ -256,6 +259,10 @@ class FrontendOptions {
256259
/// The input files and their types.
257260
std::vector<FrontendInputFile> Inputs;
258261

262+
/// When the input is a module map, the original module map file from which
263+
/// that map was inferred, if any (for umbrella modules).
264+
std::string OriginalModuleMap;
265+
259266
/// The output file, if any.
260267
std::string OutputFile;
261268

lib/Frontend/CompilerInstance.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,8 @@ static bool compileModuleImpl(CompilerInstance &ImportingInstance,
10801080
FrontendOpts.DisableFree = false;
10811081
FrontendOpts.GenerateGlobalModuleIndex = false;
10821082
FrontendOpts.BuildingImplicitModule = true;
1083+
FrontendOpts.OriginalModuleMap =
1084+
ModMap.getModuleMapFileForUniquing(Module)->getName();
10831085
// Force implicitly-built modules to hash the content of the module file.
10841086
HSOpts.ModulesHashContent = true;
10851087
FrontendOpts.Inputs.clear();
@@ -1129,11 +1131,12 @@ static bool compileModuleImpl(CompilerInstance &ImportingInstance,
11291131
if (const FileEntry *ModuleMapFile =
11301132
ModMap.getContainingModuleMapFile(Module)) {
11311133
// Use the module map where this module resides.
1132-
FrontendOpts.Inputs.emplace_back(ModuleMapFile->getName(), IK);
1134+
FrontendOpts.Inputs.emplace_back(ModuleMapFile->getName(), IK,
1135+
+Module->IsSystem);
11331136
} else {
11341137
SmallString<128> FakeModuleMapFile(Module->Directory->getName());
11351138
llvm::sys::path::append(FakeModuleMapFile, "__inferred_module.map");
1136-
FrontendOpts.Inputs.emplace_back(FakeModuleMapFile, IK);
1139+
FrontendOpts.Inputs.emplace_back(FakeModuleMapFile, IK, +Module->IsSystem);
11371140

11381141
llvm::raw_string_ostream OS(InferredModuleMapContent);
11391142
Module->print(OS);
@@ -1146,11 +1149,6 @@ static bool compileModuleImpl(CompilerInstance &ImportingInstance,
11461149
SourceMgr.overrideFileContents(ModuleMapFile, std::move(ModuleMapBuffer));
11471150
}
11481151

1149-
// Construct a module-generating action. Passing through the module map is
1150-
// safe because the FileManager is shared between the compiler instances.
1151-
GenerateModuleFromModuleMapAction CreateModuleAction(
1152-
ModMap.getModuleMapFileForUniquing(Module), Module->IsSystem);
1153-
11541152
ImportingInstance.getDiagnostics().Report(ImportLoc,
11551153
diag::remark_module_build)
11561154
<< Module->Name << ModuleFileName;
@@ -1159,8 +1157,12 @@ static bool compileModuleImpl(CompilerInstance &ImportingInstance,
11591157
// thread so that we get a stack large enough.
11601158
const unsigned ThreadStackSize = 8 << 20;
11611159
llvm::CrashRecoveryContext CRC;
1162-
CRC.RunSafelyOnThread([&]() { Instance.ExecuteAction(CreateModuleAction); },
1163-
ThreadStackSize);
1160+
CRC.RunSafelyOnThread(
1161+
[&]() {
1162+
GenerateModuleFromModuleMapAction Action;
1163+
Instance.ExecuteAction(Action);
1164+
},
1165+
ThreadStackSize);
11641166

11651167
ImportingInstance.getDiagnostics().Report(ImportLoc,
11661168
diag::remark_module_build_done)

lib/Frontend/CompilerInvocation.cpp

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,30 +1350,51 @@ static InputKind ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
13501350
InputKind DashX(InputKind::Unknown);
13511351
if (const Arg *A = Args.getLastArg(OPT_x)) {
13521352
StringRef XValue = A->getValue();
1353+
1354+
// Parse suffixes: '<lang>(-header|[-module-map][-cpp-output])'.
1355+
// FIXME: Supporting '<lang>-header-cpp-output' would be useful.
1356+
bool Preprocessed = XValue.consume_back("-cpp-output");
1357+
bool ModuleMap = XValue.consume_back("-module-map");
1358+
IsHeaderFile =
1359+
!Preprocessed && !ModuleMap && XValue.consume_back("-header");
1360+
1361+
// Principal languages.
13531362
DashX = llvm::StringSwitch<InputKind>(XValue)
1354-
.Cases("c", "c-header", "cpp-output", InputKind::C)
1355-
.Cases("cl", "cl-header", InputKind::OpenCL)
1356-
.Cases("cuda", "cuda-cpp-output", InputKind::CUDA)
1357-
.Cases("c++", "c++-header", "c++-cpp-output", InputKind::CXX)
1358-
.Cases("objective-c", "objective-c-header",
1359-
"objective-c-cpp-output", "objc-cpp-output",
1360-
InputKind::ObjC)
1361-
.Cases("objective-c++", "objective-c++-header",
1362-
"objective-c++-cpp-output", "objc++-cpp-output",
1363-
InputKind::ObjCXX)
1364-
.Case("renderscript", InputKind::RenderScript)
1365-
.Case("assembler-with-cpp", InputKind::Asm)
1366-
.Cases("ast", "pcm",
1367-
InputKind(InputKind::Unknown, InputKind::Precompiled))
1368-
.Case("ir", InputKind::LLVM_IR)
1369-
.Default(InputKind::Unknown);
1363+
.Case("c", InputKind::C)
1364+
.Case("cl", InputKind::OpenCL)
1365+
.Case("cuda", InputKind::CUDA)
1366+
.Case("c++", InputKind::CXX)
1367+
.Case("objective-c", InputKind::ObjC)
1368+
.Case("objective-c++", InputKind::ObjCXX)
1369+
.Case("renderscript", InputKind::RenderScript)
1370+
.Default(InputKind::Unknown);
1371+
1372+
// "objc[++]-cpp-output" is an acceptable synonym for
1373+
// "objective-c[++]-cpp-output".
1374+
if (DashX.isUnknown() && Preprocessed && !IsHeaderFile && !ModuleMap)
1375+
DashX = llvm::StringSwitch<InputKind>(XValue)
1376+
.Case("objc", InputKind::ObjC)
1377+
.Case("objc++", InputKind::ObjCXX)
1378+
.Default(InputKind::Unknown);
1379+
1380+
// Some special cases cannot be combined with suffixes.
1381+
if (DashX.isUnknown() && !Preprocessed && !ModuleMap && !IsHeaderFile)
1382+
DashX = llvm::StringSwitch<InputKind>(XValue)
1383+
.Case("cpp-output", InputKind(InputKind::C).getPreprocessed())
1384+
.Case("assembler-with-cpp", InputKind::Asm)
1385+
.Cases("ast", "pcm",
1386+
InputKind(InputKind::Unknown, InputKind::Precompiled))
1387+
.Case("ir", InputKind::LLVM_IR)
1388+
.Default(InputKind::Unknown);
1389+
13701390
if (DashX.isUnknown())
13711391
Diags.Report(diag::err_drv_invalid_value)
13721392
<< A->getAsString(Args) << A->getValue();
13731393

1374-
if (XValue.endswith("cpp-output"))
1394+
if (Preprocessed)
13751395
DashX = DashX.getPreprocessed();
1376-
IsHeaderFile = XValue.endswith("-header");
1396+
if (ModuleMap)
1397+
DashX = DashX.withFormat(InputKind::ModuleMap);
13771398
}
13781399

13791400
// '-' is the default input if none is given.
@@ -1393,6 +1414,12 @@ static InputKind ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
13931414
if (i == 0)
13941415
DashX = IK;
13951416
}
1417+
1418+
// The -emit-module action implicitly takes a module map.
1419+
if (Opts.ProgramAction == frontend::GenerateModule &&
1420+
IK.getFormat() == InputKind::Source)
1421+
IK = IK.withFormat(InputKind::ModuleMap);
1422+
13961423
Opts.Inputs.emplace_back(std::move(Inputs[i]), IK);
13971424
}
13981425

0 commit comments

Comments
 (0)