[PATCH] Move code to handle one input arg to its own function

Matthew Curtis mcurtis at codeaurora.org
Mon Mar 4 09:28:33 PST 2013


No functionality change intended.

Thanks,
Matthew Curtis

-- 
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation

-------------- next part --------------
>From a47935f7ae1e587773106eeaeff7ec6ef1b78e0f Mon Sep 17 00:00:00 2001
From: Matthew Curtis <mcurtis at codeaurora.org>
Date: Mon, 4 Mar 2013 10:02:43 -0600
Subject: [PATCH] Move code to handle one input arg to its own function

No functionality change intended.
---
 include/clang/Driver/Driver.h |    5 +
 lib/Driver/Driver.cpp         |  172 ++++++++++++++++++++++-------------------
 2 files changed, 97 insertions(+), 80 deletions(-)

diff --git a/include/clang/Driver/Driver.h b/include/clang/Driver/Driver.h
index c1f476f..32d250a 100644
--- a/include/clang/Driver/Driver.h
+++ b/include/clang/Driver/Driver.h
@@ -170,6 +170,11 @@ private:
   phases::ID getFinalPhase(const DerivedArgList &DAL, Arg **FinalPhaseArg = 0)
     const;
 
+  types::ID HandleInputArg(const ToolChain &TC, const DerivedArgList &Args,
+                           const char *Value,
+                           types::ID InputType, Arg *InputTypeArg) const;
+
+
 public:
   Driver(StringRef _ClangExecutable,
          StringRef _DefaultTargetTriple,
diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp
index 8229129..40288f9 100644
--- a/lib/Driver/Driver.cpp
+++ b/lib/Driver/Driver.cpp
@@ -893,6 +893,95 @@ void Driver::BuildUniversalActions(const ToolChain &TC,
   }
 }
 
+// Helper function to handle a single input argument 'Value'. Returns the type
+// ID for the 'Value' or TY_INVALID if it is not a valid input. May also claim
+// 'InputTypeArg'.
+types::ID Driver::HandleInputArg(const ToolChain &TC,
+                                 const DerivedArgList &Args, const char *Value,
+                                 types::ID InputType, Arg *InputTypeArg) const {
+
+  types::ID Ty = types::TY_INVALID;
+
+  // Infer the input type if necessary.
+  if (InputType == types::TY_Nothing) {
+    // If there was an explicit arg for this, claim it.
+    if (InputTypeArg)
+      InputTypeArg->claim();
+
+    // stdin must be handled specially.
+    if (memcmp(Value, "-", 2) == 0) {
+      // If running with -E, treat as a C input (this changes the builtin
+      // macros, for example). This may be overridden by -ObjC below.
+      //
+      // Otherwise emit an error but still use a valid type to avoid
+      // spurious errors (e.g., no inputs).
+      if (!Args.hasArgNoClaim(options::OPT_E) && !CCCIsCPP)
+        Diag(clang::diag::err_drv_unknown_stdin_type);
+      Ty = types::TY_C;
+    } else {
+      // Otherwise lookup by extension.
+      // Fallback is C if invoked as C preprocessor or Object otherwise.
+      // We use a host hook here because Darwin at least has its own
+      // idea of what .s is.
+      if (const char *Ext = strrchr(Value, '.'))
+        Ty = TC.LookupTypeForExtension(Ext + 1);
+
+      if (Ty == types::TY_INVALID) {
+        if (CCCIsCPP)
+          Ty = types::TY_C;
+        else
+          Ty = types::TY_Object;
+      }
+
+      // If the driver is invoked as C++ compiler (like clang++ or c++) it
+      // should autodetect some input files as C++ for g++ compatibility.
+      if (CCCIsCXX) {
+        types::ID OldTy = Ty;
+        Ty = types::lookupCXXTypeForCType(Ty);
+
+        if (Ty != OldTy)
+          Diag(clang::diag::warn_drv_treating_input_as_cxx)
+            << getTypeName(OldTy) << getTypeName(Ty);
+      }
+    }
+
+    // -ObjC and -ObjC++ override the default language, but only for "source
+    // files". We just treat everything that isn't a linker input as a
+    // source file.
+    //
+    // FIXME: Clean this up if we move the phase sequence into the type.
+    if (Ty != types::TY_Object) {
+      if (Args.hasArg(options::OPT_ObjC))
+        Ty = types::TY_ObjC;
+      else if (Args.hasArg(options::OPT_ObjCXX))
+        Ty = types::TY_ObjCXX;
+    }
+  } else {
+    assert(InputTypeArg && "InputType set w/o InputTypeArg");
+    InputTypeArg->claim();
+    Ty = InputType;
+  }
+
+  // Check that the file exists, if enabled.
+  if (CheckInputsExist && memcmp(Value, "-", 2) != 0) {
+    SmallString<64> Path(Value);
+    if (Arg *WorkDir = Args.getLastArg(options::OPT_working_directory)) {
+      if (!llvm::sys::path::is_absolute(Path.str())) {
+        SmallString<64> Directory(WorkDir->getValue());
+        llvm::sys::path::append(Directory, Value);
+        Path.assign(Directory);
+      }
+    }
+
+    bool exists = false;
+    if (llvm::sys::fs::exists(Path.c_str(), exists) || !exists) {
+      Diag(clang::diag::err_drv_no_such_file) << Path.str();
+      Ty = types::TY_INVALID;
+    }
+  }
+  return Ty;
+}
+
 // Construct a the list of inputs and their types.
 void Driver::BuildInputs(const ToolChain &TC, const DerivedArgList &Args,
                          InputList &Inputs) const {
@@ -907,86 +996,9 @@ void Driver::BuildInputs(const ToolChain &TC, const DerivedArgList &Args,
     Arg *A = *it;
 
     if (A->getOption().getKind() == Option::InputClass) {
-      const char *Value = A->getValue();
-      types::ID Ty = types::TY_INVALID;
-
-      // Infer the input type if necessary.
-      if (InputType == types::TY_Nothing) {
-        // If there was an explicit arg for this, claim it.
-        if (InputTypeArg)
-          InputTypeArg->claim();
-
-        // stdin must be handled specially.
-        if (memcmp(Value, "-", 2) == 0) {
-          // If running with -E, treat as a C input (this changes the builtin
-          // macros, for example). This may be overridden by -ObjC below.
-          //
-          // Otherwise emit an error but still use a valid type to avoid
-          // spurious errors (e.g., no inputs).
-          if (!Args.hasArgNoClaim(options::OPT_E) && !CCCIsCPP)
-            Diag(clang::diag::err_drv_unknown_stdin_type);
-          Ty = types::TY_C;
-        } else {
-          // Otherwise lookup by extension.
-          // Fallback is C if invoked as C preprocessor or Object otherwise.
-          // We use a host hook here because Darwin at least has its own
-          // idea of what .s is.
-          if (const char *Ext = strrchr(Value, '.'))
-            Ty = TC.LookupTypeForExtension(Ext + 1);
-
-          if (Ty == types::TY_INVALID) {
-            if (CCCIsCPP)
-              Ty = types::TY_C;
-            else
-              Ty = types::TY_Object;
-          }
-
-          // If the driver is invoked as C++ compiler (like clang++ or c++) it
-          // should autodetect some input files as C++ for g++ compatibility.
-          if (CCCIsCXX) {
-            types::ID OldTy = Ty;
-            Ty = types::lookupCXXTypeForCType(Ty);
-
-            if (Ty != OldTy)
-              Diag(clang::diag::warn_drv_treating_input_as_cxx)
-                << getTypeName(OldTy) << getTypeName(Ty);
-          }
-        }
-
-        // -ObjC and -ObjC++ override the default language, but only for "source
-        // files". We just treat everything that isn't a linker input as a
-        // source file.
-        //
-        // FIXME: Clean this up if we move the phase sequence into the type.
-        if (Ty != types::TY_Object) {
-          if (Args.hasArg(options::OPT_ObjC))
-            Ty = types::TY_ObjC;
-          else if (Args.hasArg(options::OPT_ObjCXX))
-            Ty = types::TY_ObjCXX;
-        }
-      } else {
-        assert(InputTypeArg && "InputType set w/o InputTypeArg");
-        InputTypeArg->claim();
-        Ty = InputType;
-      }
-
-      // Check that the file exists, if enabled.
-      if (CheckInputsExist && memcmp(Value, "-", 2) != 0) {
-        SmallString<64> Path(Value);
-        if (Arg *WorkDir = Args.getLastArg(options::OPT_working_directory)) {
-          if (!llvm::sys::path::is_absolute(Path.str())) {
-            SmallString<64> Directory(WorkDir->getValue());
-            llvm::sys::path::append(Directory, Value);
-            Path.assign(Directory);
-          }
-        }
-
-        bool exists = false;
-        if (llvm::sys::fs::exists(Path.c_str(), exists) || !exists)
-          Diag(clang::diag::err_drv_no_such_file) << Path.str();
-        else
-          Inputs.push_back(std::make_pair(Ty, A));
-      } else
+      types::ID Ty;
+      Ty = HandleInputArg(TC, Args, A->getValue(), InputType, InputTypeArg);
+      if (Ty != types::TY_INVALID)
         Inputs.push_back(std::make_pair(Ty, A));
 
     } else if (A->getOption().hasFlag(options::LinkerInput)) {
-- 
1.7.8.3



More information about the cfe-commits mailing list