[clang] 27a3ece - [clang][Modules] Add -fsystem-module flag

Michael Spencer via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 3 14:14:36 PST 2020


Author: Michael Spencer
Date: 2020-03-03T14:14:24-08:00
New Revision: 27a3ecee45584f6e78b46741111ebbbe5554faad

URL: https://github.com/llvm/llvm-project/commit/27a3ecee45584f6e78b46741111ebbbe5554faad
DIFF: https://github.com/llvm/llvm-project/commit/27a3ecee45584f6e78b46741111ebbbe5554faad.diff

LOG: [clang][Modules] Add -fsystem-module flag

The -fsystem-module flag is used when explicitly building a module. It
forces the module to be treated as a system module. This is used when
converting an implicit build to an explicit build to match the
systemness the implicit build would have had for a given module.

Differential Revision: https://reviews.llvm.org/D75395

Added: 
    clang/test/Modules/fsystem-module.m

Modified: 
    clang/include/clang/Driver/Options.td
    clang/include/clang/Frontend/FrontendOptions.h
    clang/lib/Frontend/CompilerInvocation.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index f1801e3e89e7..2aaf85434214 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -1445,6 +1445,8 @@ def fmodule_name_EQ : Joined<["-"], "fmodule-name=">, Group<f_Group>,
 def fmodule_name : Separate<["-"], "fmodule-name">, Alias<fmodule_name_EQ>;
 def fmodule_implementation_of : Separate<["-"], "fmodule-implementation-of">,
   Flags<[CC1Option]>, Alias<fmodule_name_EQ>;
+def fsystem_module : Flag<["-"], "fsystem-module">, Flags<[CC1Option]>,
+  HelpText<"Build this module as a system module. Only used with -emit-module">;
 def fmodule_map_file : Joined<["-"], "fmodule-map-file=">,
   Group<f_Group>, Flags<[DriverOption,CC1Option]>, MetaVarName<"<file>">,
   HelpText<"Load this module map file">;

diff  --git a/clang/include/clang/Frontend/FrontendOptions.h b/clang/include/clang/Frontend/FrontendOptions.h
index 2adc6319810c..66fec6436a40 100644
--- a/clang/include/clang/Frontend/FrontendOptions.h
+++ b/clang/include/clang/Frontend/FrontendOptions.h
@@ -297,6 +297,9 @@ class FrontendOptions {
   /// Should a temporary file be used during compilation.
   unsigned UseTemporary : 1;
 
+  /// When using -emit-module, treat the modulemap as a system module.
+  unsigned IsSystemModule : 1;
+
   CodeCompleteOptions CodeCompleteOpts;
 
   /// Specifies the output format of the AST.

diff  --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 8638d4300b21..48c65aded817 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -1905,6 +1905,11 @@ static InputKind ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
   Opts.ModulesEmbedAllFiles = Args.hasArg(OPT_fmodules_embed_all_files);
   Opts.IncludeTimestamps = !Args.hasArg(OPT_fno_pch_timestamp);
   Opts.UseTemporary = !Args.hasArg(OPT_fno_temp_file);
+  Opts.IsSystemModule = Args.hasArg(OPT_fsystem_module);
+
+  if (Opts.ProgramAction != frontend::GenerateModule && Opts.IsSystemModule)
+    Diags.Report(diag::err_drv_argument_only_allowed_with) << "-fsystem-module"
+                                                           << "-emit-module";
 
   Opts.CodeCompleteOpts.IncludeMacros
     = Args.hasArg(OPT_code_completion_macros);
@@ -2061,12 +2066,16 @@ static InputKind ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
         DashX = IK;
     }
 
+    bool IsSystem = false;
+
     // The -emit-module action implicitly takes a module map.
     if (Opts.ProgramAction == frontend::GenerateModule &&
-        IK.getFormat() == InputKind::Source)
+        IK.getFormat() == InputKind::Source) {
       IK = IK.withFormat(InputKind::ModuleMap);
+      IsSystem = Opts.IsSystemModule;
+    }
 
-    Opts.Inputs.emplace_back(std::move(Inputs[i]), IK);
+    Opts.Inputs.emplace_back(std::move(Inputs[i]), IK, IsSystem);
   }
 
   return DashX;

diff  --git a/clang/test/Modules/fsystem-module.m b/clang/test/Modules/fsystem-module.m
new file mode 100644
index 000000000000..6ef91b762b0f
--- /dev/null
+++ b/clang/test/Modules/fsystem-module.m
@@ -0,0 +1,18 @@
+// RUN: rm -rf %t.dir
+// RUN: mkdir %t.dir
+
+// -fsystem-module requires -emit-module
+// RUN: not %clang_cc1 -fsyntax-only -fsystem-module %s 2>&1 | grep "-emit-module"
+
+// RUN: not %clang_cc1 -fmodules -I %S/Inputs \
+// RUN:   -emit-module -fmodule-name=warning -pedantic -Werror \
+// RUN:   %S/Inputs/module.map -o %t.dir/warning.pcm
+
+// RUN: %clang_cc1 -fmodules -I %S/Inputs \
+// RUN:   -emit-module -fmodule-name=warning -pedantic -Werror \
+// RUN:   %S/Inputs/module.map -o %t.dir/warning-system.pcm -fsystem-module
+
+// RUN: not %clang_cc1 -fmodules -I %S/Inputs \
+// RUN:   -emit-module -fmodule-name=warning -pedantic -Werror \
+// RUN:   %S/Inputs/module.map -o %t.dir/warning-system.pcm -fsystem-module \
+// RUN:   -Wsystem-headers


        


More information about the cfe-commits mailing list