From df1e3cb157d8bdbb645e444bf4211e9b43d08d77 Mon Sep 17 00:00:00 2001 From: Alexander Kornienko Date: Thu, 6 Mar 2014 10:17:46 +0000 Subject: [PATCH] Use range-based for loops for better readability. No functional changes intended. Reviewers: chandlerc Reviewed By: chandlerc CC: cfe-commits Differential Revision: http://llvm-reviews.chandlerc.com/D2979 llvm-svn: 203101 --- clang-tools-extra/clang-tidy/ClangTidy.cpp | 104 +++++++----------- .../ClangTidyDiagnosticConsumer.cpp | 4 +- .../clang-tidy/ClangTidyModule.cpp | 17 ++- 3 files changed, 49 insertions(+), 76 deletions(-) diff --git a/clang-tools-extra/clang-tidy/ClangTidy.cpp b/clang-tools-extra/clang-tidy/ClangTidy.cpp index 6c79aa54b4ab..3f4fcb43ea9a 100644 --- a/clang-tools-extra/clang-tidy/ClangTidy.cpp +++ b/clang-tools-extra/clang-tidy/ClangTidy.cpp @@ -66,24 +66,18 @@ public: void FlushDiagnosticsImpl(std::vector &Diags, FilesMade *filesMade) override { - for (std::vector::iterator I = Diags.begin(), - E = Diags.end(); - I != E; ++I) { - const ento::PathDiagnostic *PD = *I; + for (const ento::PathDiagnostic *PD : Diags) { SmallString<64> CheckName(AnalyzerCheckNamePrefix); CheckName += PD->getCheckName(); addRanges(Context.diag(CheckName, PD->getLocation().asLocation(), PD->getShortDescription()), PD->path.back()->getRanges()); - ento::PathPieces FlatPath = - PD->path.flatten(/*ShouldFlattenMacros=*/true); - for (ento::PathPieces::const_iterator PI = FlatPath.begin(), - PE = FlatPath.end(); - PI != PE; ++PI) { - addRanges(Context.diag(CheckName, (*PI)->getLocation().asLocation(), - (*PI)->getString(), DiagnosticIDs::Note), - (*PI)->getRanges()); + for (const auto &DiagPiece : + PD->path.flatten(/*ShouldFlattenMacros=*/true)) { + addRanges(Context.diag(CheckName, DiagPiece->getLocation().asLocation(), + DiagPiece->getString(), DiagnosticIDs::Note), + DiagPiece->getRanges()); } } } @@ -98,9 +92,8 @@ private: // FIXME: Convert to operator<<(DiagnosticBuilder&, ArrayRef). static const DiagnosticBuilder &addRanges(const DiagnosticBuilder &DB, ArrayRef Ranges) { - for (ArrayRef::iterator I = Ranges.begin(), E = Ranges.end(); - I != E; ++I) - DB << *I; + for (const SourceRange &Range : Ranges) + DB << Range; return DB; } }; @@ -121,19 +114,15 @@ ClangTidyASTConsumerFactory::ClangTidyASTConsumerFactory( CheckFactories->createChecks(Filter, Checks); - for (SmallVectorImpl::iterator I = Checks.begin(), - E = Checks.end(); - I != E; ++I) { - (*I)->setContext(&Context); - (*I)->registerMatchers(&Finder); + for (ClangTidyCheck *Check : Checks) { + Check->setContext(&Context); + Check->registerMatchers(&Finder); } } ClangTidyASTConsumerFactory::~ClangTidyASTConsumerFactory() { - for (SmallVectorImpl::iterator I = Checks.begin(), - E = Checks.end(); - I != E; ++I) - delete *I; + for (ClangTidyCheck *Check : Checks) + delete Check; } clang::ASTConsumer *ClangTidyASTConsumerFactory::CreateASTConsumer( @@ -141,10 +130,8 @@ clang::ASTConsumer *ClangTidyASTConsumerFactory::CreateASTConsumer( // FIXME: Move this to a separate method, so that CreateASTConsumer doesn't // modify Compiler. Context.setSourceManager(&Compiler.getSourceManager()); - for (SmallVectorImpl::iterator I = Checks.begin(), - E = Checks.end(); - I != E; ++I) - (*I)->registerPPCallbacks(Compiler); + for (ClangTidyCheck *Check : Checks) + Check->registerPPCallbacks(Compiler); SmallVector Consumers; if (!CheckFactories->empty()) @@ -169,19 +156,13 @@ clang::ASTConsumer *ClangTidyASTConsumerFactory::CreateASTConsumer( std::vector ClangTidyASTConsumerFactory::getCheckNames() { std::vector CheckNames; - for (ClangTidyCheckFactories::FactoryMap::const_iterator - I = CheckFactories->begin(), - E = CheckFactories->end(); - I != E; ++I) { - if (Filter.IsCheckEnabled(I->first)) - CheckNames.push_back(I->first); + for (const auto &CheckFactory : *CheckFactories) { + if (Filter.IsCheckEnabled(CheckFactory.first)) + CheckNames.push_back(CheckFactory.first); } - CheckersList AnalyzerChecks = getCheckersControlList(); - for (CheckersList::const_iterator I = AnalyzerChecks.begin(), - E = AnalyzerChecks.end(); - I != E; ++I) - CheckNames.push_back(AnalyzerCheckNamePrefix + I->first); + for (const auto &AnalyzerCheck : getCheckersControlList()) + CheckNames.push_back(AnalyzerCheckNamePrefix + AnalyzerCheck.first); std::sort(CheckNames.begin(), CheckNames.end()); return CheckNames; @@ -190,13 +171,12 @@ std::vector ClangTidyASTConsumerFactory::getCheckNames() { ClangTidyASTConsumerFactory::CheckersList ClangTidyASTConsumerFactory::getCheckersControlList() { CheckersList List; - ArrayRef Checks(StaticAnalyzerChecks); bool AnalyzerChecksEnabled = false; - for (unsigned i = 0; i < Checks.size(); ++i) { - std::string Checker((AnalyzerCheckNamePrefix + Checks[i]).str()); + for (StringRef CheckName : StaticAnalyzerChecks) { + std::string Checker((AnalyzerCheckNamePrefix + CheckName).str()); AnalyzerChecksEnabled |= - Filter.IsCheckEnabled(Checker) && !Checks[i].startswith("debug"); + Filter.IsCheckEnabled(Checker) && !CheckName.startswith("debug"); } if (AnalyzerChecksEnabled) { @@ -207,12 +187,12 @@ ClangTidyASTConsumerFactory::getCheckersControlList() { // Always add all core checkers if any other static analyzer checks are // enabled. This is currently necessary, as other path sensitive checks // rely on the core checkers. - for (unsigned i = 0; i < Checks.size(); ++i) { - std::string Checker((AnalyzerCheckNamePrefix + Checks[i]).str()); + for (StringRef CheckName : StaticAnalyzerChecks) { + std::string Checker((AnalyzerCheckNamePrefix + CheckName).str()); - if (Checks[i].startswith("core") || - (!Checks[i].startswith("debug") && Filter.IsCheckEnabled(Checker))) - List.push_back(std::make_pair(Checks[i], true)); + if (CheckName.startswith("core") || + (!CheckName.startswith("debug") && Filter.IsCheckEnabled(Checker))) + List.push_back(std::make_pair(CheckName, true)); } } return List; @@ -302,20 +282,18 @@ static void reportDiagnostic(const ClangTidyMessage &Message, SourceManager &SourceMgr, DiagnosticsEngine::Level Level, DiagnosticsEngine &Diags, - tooling::Replacements *Fixes = NULL) { + const tooling::Replacements *Fixes = NULL) { SourceLocation Loc = getLocation(SourceMgr, Message.FilePath, Message.FileOffset); DiagnosticBuilder Diag = Diags.Report(Loc, Diags.getCustomDiagID(Level, "%0")) << Message.Message; if (Fixes != NULL) { - for (tooling::Replacements::const_iterator I = Fixes->begin(), - E = Fixes->end(); - I != E; ++I) { + for (const tooling::Replacement &Fix : *Fixes) { SourceLocation FixLoc = - getLocation(SourceMgr, I->getFilePath(), I->getOffset()); + getLocation(SourceMgr, Fix.getFilePath(), Fix.getOffset()); Diag << FixItHint::CreateReplacement( - SourceRange(FixLoc, FixLoc.getLocWithOffset(I->getLength())), - I->getReplacementText()); + SourceRange(FixLoc, FixLoc.getLocWithOffset(Fix.getLength())), + Fix.getReplacementText()); } } } @@ -332,15 +310,13 @@ void handleErrors(SmallVectorImpl &Errors, bool Fix) { DiagPrinter->BeginSourceFile(LangOpts); SourceManager SourceMgr(Diags, Files); Rewriter Rewrite(SourceMgr, LangOpts); - for (SmallVectorImpl::iterator I = Errors.begin(), - E = Errors.end(); - I != E; ++I) { - reportDiagnostic(I->Message, SourceMgr, DiagnosticsEngine::Warning, Diags, - &I->Fix); - for (unsigned i = 0, e = I->Notes.size(); i != e; ++i) { - reportDiagnostic(I->Notes[i], SourceMgr, DiagnosticsEngine::Note, Diags); - } - tooling::applyAllReplacements(I->Fix, Rewrite); + for (const ClangTidyError &Error : Errors) { + reportDiagnostic(Error.Message, SourceMgr, DiagnosticsEngine::Warning, Diags, + &Error.Fix); + for (const ClangTidyMessage &Note : Error.Notes) + reportDiagnostic(Note, SourceMgr, DiagnosticsEngine::Note, Diags); + + tooling::applyAllReplacements(Error.Fix, Rewrite); } // FIXME: Run clang-format on changes. if (Fix) diff --git a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp index 81fb86bf73f2..03beb15a02b5 100644 --- a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp +++ b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp @@ -119,8 +119,8 @@ void ClangTidyDiagnosticConsumer::HandleDiagnostic( // Flushes the internal diagnostics buffer to the ClangTidyContext. void ClangTidyDiagnosticConsumer::finish() { finalizeLastError(); - for (unsigned i = 0, e = Errors.size(); i != e; ++i) - Context.storeError(Errors[i]); + for (const ClangTidyError &Error : Errors) + Context.storeError(Error); Errors.clear(); } diff --git a/clang-tools-extra/clang-tidy/ClangTidyModule.cpp b/clang-tools-extra/clang-tidy/ClangTidyModule.cpp index 87ab2be3c485..8a6842b498c9 100644 --- a/clang-tools-extra/clang-tidy/ClangTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/ClangTidyModule.cpp @@ -17,24 +17,21 @@ namespace clang { namespace tidy { ClangTidyCheckFactories::~ClangTidyCheckFactories() { - for (FactoryMap::iterator I = Factories.begin(), E = Factories.end(); I != E; - ++I) { - delete I->second; - } + for (const auto &Factory : Factories) + delete Factory.second; } + void ClangTidyCheckFactories::addCheckFactory(StringRef Name, CheckFactoryBase *Factory) { - Factories[Name] = Factory; } void ClangTidyCheckFactories::createChecks( ChecksFilter &Filter, SmallVectorImpl &Checks) { - for (FactoryMap::iterator I = Factories.begin(), E = Factories.end(); I != E; - ++I) { - if (Filter.IsCheckEnabled(I->first)) { - ClangTidyCheck *Check = I->second->createCheck(); - Check->setName(I->first); + for (const auto &Factory : Factories) { + if (Filter.IsCheckEnabled(Factory.first)) { + ClangTidyCheck *Check = Factory.second->createCheck(); + Check->setName(Factory.first); Checks.push_back(Check); } }