[cmake-developers] [PATCH 1/7] Refactor TargetTypeNames.

Peter Collingbourne peter at pcc.me.uk
Mon Sep 26 23:48:52 EDT 2011


From: Nicolas Despres <nicolas.despres at gmail.com>

Make it a static method instead of an array. It is safer for the
type checking and if we add a new target type we will be warned to add
a case to the switch.
---
 Source/cmComputeTargetDepends.cxx |    2 +-
 Source/cmLocalGenerator.cxx       |    2 +-
 Source/cmMakefile.cxx             |    2 +-
 Source/cmTarget.cxx               |   81 ++++++++++++++++++-------------------
 Source/cmTarget.h                 |    2 +-
 5 files changed, 44 insertions(+), 45 deletions(-)

diff --git a/Source/cmComputeTargetDepends.cxx b/Source/cmComputeTargetDepends.cxx
index 3a0ed06..8e701c4 100644
--- a/Source/cmComputeTargetDepends.cxx
+++ b/Source/cmComputeTargetDepends.cxx
@@ -404,7 +404,7 @@ cmComputeTargetDepends
 
     // Describe the depender.
     e << "  \"" << depender->GetName() << "\" of type "
-      << cmTarget::TargetTypeNames[depender->GetType()] << "\n";
+      << cmTarget::GetTargetTypeName(depender->GetType()) << "\n";
 
     // List its dependencies that are inside the component.
     EdgeList const& nl = this->InitialGraph[i];
diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx
index 1d1e8da..6af7fd5 100644
--- a/Source/cmLocalGenerator.cxx
+++ b/Source/cmLocalGenerator.cxx
@@ -996,7 +996,7 @@ cmLocalGenerator::ExpandRuleVariable(std::string const& variable,
       }
     if(variable == "TARGET_TYPE")
       {
-      return cmTarget::TargetTypeNames[replaceValues.CMTarget->GetType()];
+      return cmTarget::GetTargetTypeName(replaceValues.CMTarget->GetType());
       }
     }
   if(replaceValues.Output)
diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx
index e5b5443..573c430 100644
--- a/Source/cmMakefile.cxx
+++ b/Source/cmMakefile.cxx
@@ -1379,7 +1379,7 @@ void cmMakefile::AddLinkLibraryForTarget(const char *target,
         {
         cmOStringStream e;
         e << "Target \"" << lib << "\" of type "
-          << cmTarget::TargetTypeNames[static_cast<int>(tgt->GetType())]
+          << cmTarget::GetTargetTypeName(tgt->GetType())
           << " may not be linked into another target.  "
           << "One may link only to STATIC or SHARED libraries, or "
           << "to executables with the ENABLE_EXPORTS property set.";
diff --git a/Source/cmTarget.cxx b/Source/cmTarget.cxx
index fb92016..c0dc80d 100644
--- a/Source/cmTarget.cxx
+++ b/Source/cmTarget.cxx
@@ -25,12 +25,44 @@
 #include <queue>
 #include <stdlib.h> // required for atof
 #include <assert.h>
-const char* cmTarget::TargetTypeNames[] = {
-  "EXECUTABLE", "STATIC_LIBRARY",
-  "SHARED_LIBRARY", "MODULE_LIBRARY", "UTILITY", "GLOBAL_TARGET",
-  "INSTALL_FILES", "INSTALL_PROGRAMS", "INSTALL_DIRECTORY",
-  "UNKNOWN_LIBRARY"
-};
+
+const char* cmTarget::GetTargetTypeName(TargetType targetType)
+{
+  switch( targetType )
+    {
+      case cmTarget::STATIC_LIBRARY:
+        return "STATIC_LIBRARY";
+        // break; /* unreachable */
+      case cmTarget::MODULE_LIBRARY:
+        return "MODULE_LIBRARY";
+        // break; /* unreachable */
+      case cmTarget::SHARED_LIBRARY:
+        return "SHARED_LIBRARY";
+        // break; /* unreachable */
+      case cmTarget::EXECUTABLE:
+        return "EXECUTABLE";
+        // break; /* unreachable */
+      case cmTarget::UTILITY:
+        return "UTILITY";
+        // break; /* unreachable */
+      case cmTarget::GLOBAL_TARGET:
+        return "GLOBAL_TARGET";
+        // break; /* unreachable */
+      case cmTarget::INSTALL_FILES:
+        return "INSTALL_FILES";
+        // break; /* unreachable */
+      case cmTarget::INSTALL_PROGRAMS:
+        return "INSTALL_PROGRAMS";
+        // break; /* unreachable */
+      case cmTarget::INSTALL_DIRECTORY:
+        return "INSTALL_DIRECTORY";
+        // break; /* unreachable */
+      case cmTarget::UNKNOWN_LIBRARY:
+        return "UNKNOWN_LIBRARY";
+        // break; /* unreachable */
+    }
+  return 0;
+}
 
 //----------------------------------------------------------------------------
 struct cmTarget::OutputInfo
@@ -2346,7 +2378,7 @@ cmTarget::OutputInfo const* cmTarget::GetOutputInfo(const char* config)
     std::string msg = "cmTarget::GetOutputInfo called for ";
     msg += this->GetName();
     msg += " which has type ";
-    msg += cmTarget::TargetTypeNames[this->GetType()];
+    msg += cmTarget::GetTargetTypeName(this->GetType());
     this->GetMakefile()->IssueMessage(cmake::INTERNAL_ERROR, msg);
     abort();
     return 0;
@@ -2645,40 +2677,7 @@ const char *cmTarget::GetProperty(const char* prop,
   // the type property returns what type the target is
   if (!strcmp(prop,"TYPE"))
     {
-    switch( this->GetType() )
-      {
-      case cmTarget::STATIC_LIBRARY:
-        return "STATIC_LIBRARY";
-        // break; /* unreachable */
-      case cmTarget::MODULE_LIBRARY:
-        return "MODULE_LIBRARY";
-        // break; /* unreachable */
-      case cmTarget::SHARED_LIBRARY:
-        return "SHARED_LIBRARY";
-        // break; /* unreachable */
-      case cmTarget::EXECUTABLE:
-        return "EXECUTABLE";
-        // break; /* unreachable */
-      case cmTarget::UTILITY:
-        return "UTILITY";
-        // break; /* unreachable */
-      case cmTarget::GLOBAL_TARGET:
-        return "GLOBAL_TARGET";
-        // break; /* unreachable */
-      case cmTarget::INSTALL_FILES:
-        return "INSTALL_FILES";
-        // break; /* unreachable */
-      case cmTarget::INSTALL_PROGRAMS:
-        return "INSTALL_PROGRAMS";
-        // break; /* unreachable */
-      case cmTarget::INSTALL_DIRECTORY:
-        return "INSTALL_DIRECTORY";
-        // break; /* unreachable */
-      case cmTarget::UNKNOWN_LIBRARY:
-        return "UNKNOWN_LIBRARY";
-        // break; /* unreachable */
-      }
-    return 0;
+    return cmTarget::GetTargetTypeName(this->GetType());
     }
   bool chain = false;
   const char *retVal =
diff --git a/Source/cmTarget.h b/Source/cmTarget.h
index 26fcef2..0abdddb 100644
--- a/Source/cmTarget.h
+++ b/Source/cmTarget.h
@@ -62,7 +62,7 @@ public:
                     SHARED_LIBRARY, MODULE_LIBRARY, UTILITY, GLOBAL_TARGET,
                     INSTALL_FILES, INSTALL_PROGRAMS, INSTALL_DIRECTORY,
                     UNKNOWN_LIBRARY};
-  static const char* TargetTypeNames[];
+  static const char* GetTargetTypeName(TargetType targetType);
   enum CustomCommandType { PRE_BUILD, PRE_LINK, POST_BUILD };
 
   /**
-- 
1.7.5.3




More information about the cmake-developers mailing list