r219938 - Use iterators and algorithms to possibly make this code a bit tidier

David Blaikie dblaikie at gmail.com
Thu Oct 16 10:23:59 PDT 2014


Author: dblaikie
Date: Thu Oct 16 12:23:58 2014
New Revision: 219938

URL: http://llvm.org/viewvc/llvm-project?rev=219938&view=rev
Log:
Use iterators and algorithms to possibly make this code a bit tidier

(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.

Modified:
    cfe/trunk/tools/driver/driver.cpp

Modified: cfe/trunk/tools/driver/driver.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/driver/driver.cpp?rev=219938&r1=219937&r2=219938&view=diff
==============================================================================
--- cfe/trunk/tools/driver/driver.cpp (original)
+++ cfe/trunk/tools/driver/driver.cpp Thu Oct 16 12:23:58 2014
@@ -243,24 +243,20 @@ static void ParseProgName(SmallVectorImp
   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(SmallVectorImp
 
   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));
   }
 }
 





More information about the cfe-commits mailing list