[Beignet] [PATCH V4] Fix the bug when we pass argument with spaces.

junyan.he at inbox.com junyan.he at inbox.com
Thu Jan 21 02:44:27 PST 2016


From: Junyan He <junyan.he at linux.intel.com>

We failed to handle -I "/XX X/YY YY/" like path or
-DAAA=BBB"CC DDD"EEE like defines from the build option.
We need to consider the spaces here and pass it correctly
to Clang.

V4:
  Fix a minor mistake.

Signed-off-by: Junyan He <junyan.he at linux.intel.com>
---
 backend/src/backend/program.cpp |   73 +++++++++++++++++++++++++++++++++++----
 1 file changed, 67 insertions(+), 6 deletions(-)

diff --git a/backend/src/backend/program.cpp b/backend/src/backend/program.cpp
index f886d03..e3df36b 100644
--- a/backend/src/backend/program.cpp
+++ b/backend/src/backend/program.cpp
@@ -770,9 +770,9 @@ namespace gbe {
     bool useDefaultCLCVersion = true;
 
     if (options) {
-      char *str = (char *)malloc(sizeof(char) * (strlen(options) + 1));
-      memcpy(str, options, strlen(options) + 1);
-      std::string optionStr(str);
+      char *c_str = (char *)malloc(sizeof(char) * (strlen(options) + 1));
+      memcpy(c_str, options, strlen(options) + 1);
+      std::string optionStr(c_str);
       const std::string unsupportedOptions("-cl-denorms-are-zero, -cl-strict-aliasing, -cl-opt-disable,"
                        "-cl-no-signed-zeros, -cl-fp32-correctly-rounded-divide-sqrt");
 
@@ -781,14 +781,75 @@ namespace gbe {
       while (end != std::string::npos) {
         end = optionStr.find(' ', start);
         std::string str = optionStr.substr(start, end - start);
-        start = end + 1;
-        if(str.size() == 0)
+
+        if(str.size() == 0) {
+          start = end + 1;
           continue;
+        }
+
+EXTEND_QUOTE:
+        /* We need to find the ", if the there are odd number of " within this string,
+           we need to extend the string to the matched " of the last one. */
+        int quoteNum = 0;
+        for (size_t i = 0; i < str.size(); i++) {
+          if (str[i] == '"') {
+            quoteNum++;
+          }
+        }
+
+        if (quoteNum % 2) { // Odd number of ", need to extend the string.
+          /* find the second " */
+          while (end < optionStr.size() && optionStr[end] != '"')
+            end++;
+
+          if (end == optionStr.size()) {
+            printf("Warning: Unmatched \" number in build option\n");
+            free(c_str);
+            return false;
+          }
+
+          GBE_ASSERT(optionStr[end] == '"');
+          end++;
+
+          if (end < optionStr.size() && optionStr[end] != ' ') {
+            // "CC AAA"BBDDDD case, need to further extend.
+            end = optionStr.find(' ', end);
+            str = optionStr.substr(start, end - start);
+            goto EXTEND_QUOTE;
+          } else {
+            str = optionStr.substr(start, end - start);
+          }
+        }
+        start = end + 1;
 
         if(unsupportedOptions.find(str) != std::string::npos) {
           continue;
         }
 
+        /* if -I, we need to extract "path" to path, no " */
+        if (clOpt.back() == "-I") {
+          if (str[0] == '"') {
+            GBE_ASSERT(str[str.size() - 1] == '"');
+            if (str.size() > 2) {
+              clOpt.push_back(str.substr(1, str.size() - 2));
+            } else {
+              clOpt.push_back("");
+            }
+            continue;
+          }
+        }
+        // The -I"YYYY" like case.
+        if (str.size() > 4 && str[0] == '-' && str[1] == 'I' && str[2] == '"') {
+          GBE_ASSERT(str[str.size() - 1] == '"');
+          clOpt.push_back("-I");
+          if (str.size() > 4) {
+            clOpt.push_back(str.substr(3, str.size() - 4));
+          } else {
+            clOpt.push_back("");
+          }
+          continue;
+        }
+
         if(str.find("-cl-std=") != std::string::npos) {
           useDefaultCLCVersion = false;
           if (str == "-cl-std=CL1.1")
@@ -822,7 +883,7 @@ namespace gbe {
 
         clOpt.push_back(str);
       }
-      free(str);
+      free(c_str);
     }
 
     if (useDefaultCLCVersion) {
-- 
1.7.9.5





More information about the Beignet mailing list