From 5ca130e22394978814a9e59418529a7a3e1a61bd Mon Sep 17 00:00:00 2001
From: Marc Chevrier <marc.chevrier@gmail.com>
Date: Sat, 22 Sep 2018 14:25:21 +0200
Subject: Refactoring: introduce function to check if a string is a generator
 expression

---
 Source/cmGeneratorExpression.cxx     | 6 +++---
 Source/cmGeneratorExpression.h       | 9 +++++++++
 Source/cmIncludeDirectoryCommand.cxx | 8 ++------
 Source/cmTarget.cxx                  | 4 +---
 4 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/Source/cmGeneratorExpression.cxx b/Source/cmGeneratorExpression.cxx
index 658e9a7..2727d9a 100644
--- a/Source/cmGeneratorExpression.cxx
+++ b/Source/cmGeneratorExpression.cxx
@@ -168,7 +168,7 @@ static std::string stripAllGeneratorExpressions(const std::string& input)
     const char* c = input.c_str() + pos;
     const char* const cStart = c;
     for (; *c; ++c) {
-      if (c[0] == '$' && c[1] == '<') {
+      if (cmGeneratorExpression::StartsWithGeneratorExpression(c)) {
         ++nestingLevel;
         ++c;
         continue;
@@ -243,7 +243,7 @@ static std::string stripExportInterface(
     const char* c = input.c_str() + pos;
     const char* const cStart = c;
     for (; *c; ++c) {
-      if (c[0] == '$' && c[1] == '<') {
+      if (cmGeneratorExpression::StartsWithGeneratorExpression(c)) {
         ++nestingLevel;
         ++c;
         continue;
@@ -310,7 +310,7 @@ void cmGeneratorExpression::Split(const std::string& input,
     const char* c = input.c_str() + pos;
     const char* const cStart = c;
     for (; *c; ++c) {
-      if (c[0] == '$' && c[1] == '<') {
+      if (cmGeneratorExpression::StartsWithGeneratorExpression(c)) {
         ++nestingLevel;
         ++c;
         continue;
diff --git a/Source/cmGeneratorExpression.h b/Source/cmGeneratorExpression.h
index 8176d5c..9c05f60 100644
--- a/Source/cmGeneratorExpression.h
+++ b/Source/cmGeneratorExpression.h
@@ -63,6 +63,15 @@ public:
 
   static std::string StripEmptyListElements(const std::string& input);
 
+  static inline bool StartsWithGeneratorExpression(const std::string& input)
+  {
+    return input.length() >= 2 && input[0] == '$' && input[1] == '<';
+  }
+  static inline bool StartsWithGeneratorExpression(const char* input)
+  {
+    return input != nullptr && input[0] == '$' && input[1] == '<';
+  }
+
 private:
   cmListFileBacktrace Backtrace;
 };
diff --git a/Source/cmIncludeDirectoryCommand.cxx b/Source/cmIncludeDirectoryCommand.cxx
index eaaf64d..caec67d 100644
--- a/Source/cmIncludeDirectoryCommand.cxx
+++ b/Source/cmIncludeDirectoryCommand.cxx
@@ -5,6 +5,7 @@
 #include <algorithm>
 #include <set>
 
+#include "cmGeneratorExpression.h"
 #include "cmMakefile.h"
 #include "cmSystemTools.h"
 
@@ -69,11 +70,6 @@ bool cmIncludeDirectoryCommand::InitialPass(
   return true;
 }
 
-static bool StartsWithGeneratorExpression(const std::string& input)
-{
-  return input[0] == '$' && input[1] == '<';
-}
-
 // do a lot of cleanup on the arguments because this is one place where folks
 // sometimes take the output of a program and pass it directly into this
 // command not thinking that a single argument could be filled with spaces
@@ -124,7 +120,7 @@ void cmIncludeDirectoryCommand::NormalizeInclude(std::string& inc)
     cmSystemTools::ConvertToUnixSlashes(inc);
 
     if (!cmSystemTools::FileIsFullPath(inc)) {
-      if (!StartsWithGeneratorExpression(inc)) {
+      if (!cmGeneratorExpression::StartsWithGeneratorExpression(inc)) {
         std::string tmp = this->Makefile->GetCurrentSourceDirectory();
         tmp += "/";
         tmp += inc;
diff --git a/Source/cmTarget.cxx b/Source/cmTarget.cxx
index 4e353c7..6cf7c42 100644
--- a/Source/cmTarget.cxx
+++ b/Source/cmTarget.cxx
@@ -514,9 +514,7 @@ void cmTarget::AddSources(std::vector<std::string> const& srcs)
   std::string srcFiles;
   const char* sep = "";
   for (auto filename : srcs) {
-    const char* src = filename.c_str();
-
-    if (!(src[0] == '$' && src[1] == '<')) {
+    if (!cmGeneratorExpression::StartsWithGeneratorExpression(filename)) {
       if (!filename.empty()) {
         filename = this->ProcessSourceItemCMP0049(filename);
         if (filename.empty()) {
-- 
cgit v0.12