plotfi created this revision.
plotfi added reviewers: aaron.ballman, compnerd.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

I am working to remove this concept of the "FinalPhase" in the clang driver, 
but it is used in a lot of different places to do argument handling for 
different combinatios of phase pipelines and arguments. I am trying to 
consolidate most of the uses of "FinalPhase" into its own separate scope.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D65969

Files:
  clang/lib/Driver/Driver.cpp

Index: clang/lib/Driver/Driver.cpp
===================================================================
--- clang/lib/Driver/Driver.cpp
+++ clang/lib/Driver/Driver.cpp
@@ -3157,16 +3157,6 @@
     return;
   }
 
-  Arg *FinalPhaseArg;
-  phases::ID FinalPhase = getFinalPhase(Args, &FinalPhaseArg);
-
-  if (FinalPhase == phases::Link) {
-    if (Args.hasArg(options::OPT_emit_llvm))
-      Diag(clang::diag::err_drv_emit_llvm_link);
-    if (IsCLMode() && LTOMode != LTOK_None &&
-        !Args.getLastArgValue(options::OPT_fuse_ld_EQ).equals_lower("lld"))
-      Diag(clang::diag::err_drv_lto_without_lld);
-  }
 
   // Reject -Z* at the top level, these options should never have been exposed
   // by gcc.
@@ -3220,7 +3210,29 @@
     Args.eraseArg(options::OPT__SLASH_Yc);
     YcArg = nullptr;
   }
-  if (FinalPhase == phases::Preprocess || Args.hasArg(options::OPT__SLASH_Y_)) {
+
+  // Builder to be used to build offloading actions.
+  OffloadingActionBuilder OffloadBuilder(C, Args, Inputs);
+
+  // Construct the actions to perform.
+  HeaderModulePrecompileJobAction *HeaderModuleAction = nullptr;
+  ActionList LinkerInputs;
+
+  phases::ID FinalPhase;
+  {
+    Arg *FinalPhaseArg;
+    FinalPhase = getFinalPhase(Args, &FinalPhaseArg);
+
+    if (FinalPhase == phases::Link) {
+      if (Args.hasArg(options::OPT_emit_llvm))
+        Diag(clang::diag::err_drv_emit_llvm_link);
+      if (IsCLMode() && LTOMode != LTOK_None &&
+          !Args.getLastArgValue(options::OPT_fuse_ld_EQ).equals_lower("lld"))
+        Diag(clang::diag::err_drv_lto_without_lld);
+    }
+
+    if (FinalPhase == phases::Preprocess ||
+        Args.hasArg(options::OPT__SLASH_Y_)) {
       // If only preprocessing or /Y- is used, all pch handling is disabled.
       // Rather than check for it everywhere, just remove clang-cl pch-related
       // flags here.
@@ -3230,13 +3242,6 @@
       YcArg = YuArg = nullptr;
     }
 
-  // Builder to be used to build offloading actions.
-  OffloadingActionBuilder OffloadBuilder(C, Args, Inputs);
-
-  // Construct the actions to perform.
-  HeaderModulePrecompileJobAction *HeaderModuleAction = nullptr;
-  ActionList LinkerInputs;
-
     unsigned LastPLSize = 0;
     for (auto &I : Inputs) {
       types::ID InputType = I.first;
@@ -3268,7 +3273,10 @@
         // Special case '-E' warning on a previously preprocessed file to make
         // more sense.
         else if (InitialPhase == phases::Compile &&
-               FinalPhase == phases::Preprocess &&
+                 (Args.getLastArg(options::OPT__SLASH_EP,
+                                  options::OPT__SLASH_P) ||
+                  Args.getLastArg(options::OPT_E) ||
+                  Args.getLastArg(options::OPT_M, options::OPT_MM)) &&
                  getPreprocessedType(InputType) == types::TY_INVALID)
           Diag(clang::diag::warn_drv_preprocessed_input_file_unused)
               << InputArg->getAsString(Args) << !!FinalPhaseArg
@@ -3288,8 +3296,7 @@
           llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> PCHPL;
           types::getCompilationPhases(HeaderType, PCHPL);
           // Build the pipeline for the pch file.
-        Action *ClangClPch =
-            C.MakeAction<InputAction>(*InputArg, HeaderType);
+          Action *ClangClPch = C.MakeAction<InputAction>(*InputArg, HeaderType);
           for (phases::ID Phase : PCHPL)
             ClangClPch = ConstructPhaseAction(C, Args, Phase, ClangClPch);
           assert(ClangClPch);
@@ -3301,6 +3308,25 @@
           // probably not be considered successful either.
         }
       }
+    }
+
+    // If we are linking, claim any options which are obviously only used for
+    // compilation.
+    // FIXME: Understand why the last Phase List length is used here.
+    if (FinalPhase == phases::Link && LastPLSize == 1) {
+      Args.ClaimAllArgs(options::OPT_CompileOnly_Group);
+      Args.ClaimAllArgs(options::OPT_cl_compile_Group);
+    }
+  }
+
+  for (auto &I : Inputs) {
+    types::ID InputType = I.first;
+    const Arg *InputArg = I.second;
+
+    llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> PL;
+    types::getCompilationPhases(InputType, PL);
+    if (PL[0] > FinalPhase)
+      continue;
 
     // Build the pipeline for this file.
     Action *Current = C.MakeAction<InputAction>(*InputArg, InputType);
@@ -3379,14 +3405,6 @@
     Actions.push_back(LA);
   }
 
-  // If we are linking, claim any options which are obviously only used for
-  // compilation.
-  // FIXME: Understand why the last Phase List length is used here.
-  if (FinalPhase == phases::Link && LastPLSize == 1) {
-    Args.ClaimAllArgs(options::OPT_CompileOnly_Group);
-    Args.ClaimAllArgs(options::OPT_cl_compile_Group);
-  }
-
   // If --print-supported-cpus, -mcpu=? or -mtune=? is specified, build a custom
   // Compile phase that prints out supported cpu models and quits.
   if (Arg *A = Args.getLastArg(options::OPT_print_supported_cpus)) {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to