]> granicus.if.org Git - clang/commitdiff
Use iterators and algorithms to possibly make this code a bit tidier
authorDavid Blaikie <dblaikie@gmail.com>
Thu, 16 Oct 2014 17:23:58 +0000 (17:23 +0000)
committerDavid Blaikie <dblaikie@gmail.com>
Thu, 16 Oct 2014 17:23:58 +0000 (17:23 +0000)
(also, the code executed once the element was found was split half
inside the loop and half after it - now put it all together after the
find operation)

I'm a bit concerned that this code is rather untested (commenting out
this whole function and running check-clang doesn't fail any tests)...

And I wish I had polymorphic lambdas.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@219938 91177308-0d34-0410-b5e6-96231b3b80d8

tools/driver/driver.cpp

index 3e98b2c6c956b137658e97cb76f0fcfd04aca553..b7f7fbf851c31596105dad688db4c7f7f5710443 100644 (file)
@@ -243,24 +243,20 @@ static void ParseProgName(SmallVectorImpl<const char *> &ArgVector,
   StringRef Prefix;
 
   for (int Components = 2; Components; --Components) {
-    bool FoundMatch = false;
-    size_t i;
-
-    for (i = 0; i < llvm::array_lengthof(suffixes); ++i) {
-      if (ProgNameRef.endswith(suffixes[i].Suffix)) {
-        FoundMatch = true;
-        SmallVectorImpl<const char *>::iterator it = ArgVector.begin();
+    auto I = std::find_if(std::begin(suffixes), std::end(suffixes),
+                          [&](const decltype(suffixes[0]) &suffix) {
+      return ProgNameRef.endswith(suffix.Suffix);
+    });
+
+    if (I != std::end(suffixes)) {
+      if (I->ModeFlag) {
+        auto it = ArgVector.begin();
         if (it != ArgVector.end())
           ++it;
-        if (suffixes[i].ModeFlag)
-          ArgVector.insert(it, suffixes[i].ModeFlag);
-        break;
+        ArgVector.insert(it, I->ModeFlag);
       }
-    }
-
-    if (FoundMatch) {
       StringRef::size_type LastComponent = ProgNameRef.rfind('-',
-        ProgNameRef.size() - strlen(suffixes[i].Suffix));
+        ProgNameRef.size() - strlen(I->Suffix));
       if (LastComponent != StringRef::npos)
         Prefix = ProgNameRef.slice(0, LastComponent);
       break;
@@ -277,13 +273,13 @@ static void ParseProgName(SmallVectorImpl<const char *> &ArgVector,
 
   std::string IgnoredError;
   if (llvm::TargetRegistry::lookupTarget(Prefix, IgnoredError)) {
-    SmallVectorImpl<const char *>::iterator it = ArgVector.begin();
+    auto it = ArgVector.begin();
     if (it != ArgVector.end())
       ++it;
     const char* Strings[] =
       { GetStableCStr(SavedStrings, std::string("-target")),
         GetStableCStr(SavedStrings, Prefix) };
-    ArgVector.insert(it, Strings, Strings + llvm::array_lengthof(Strings));
+    ArgVector.insert(it, std::begin(Strings), std::end(Strings));
   }
 }