From 44e147df0ae056d1775e8c9726cc3d88b0e2979c Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Thu, 16 Oct 2014 17:23:58 +0000 Subject: [PATCH] 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@219938 91177308-0d34-0410-b5e6-96231b3b80d8 --- tools/driver/driver.cpp | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/tools/driver/driver.cpp b/tools/driver/driver.cpp index 3e98b2c6c9..b7f7fbf851 100644 --- a/tools/driver/driver.cpp +++ b/tools/driver/driver.cpp @@ -243,24 +243,20 @@ static void ParseProgName(SmallVectorImpl &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::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 &ArgVector, std::string IgnoredError; if (llvm::TargetRegistry::lookupTarget(Prefix, IgnoredError)) { - SmallVectorImpl::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)); } } -- 2.40.0