From: Richard Trieu Date: Fri, 2 Jun 2017 04:24:46 +0000 (+0000) Subject: Minor fixes to for-loop warning. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fae6a43ca3b185e456ad8c478f913cf83b36908a;p=clang Minor fixes to for-loop warning. The warning for unchanged loop variables outputted a diagnostic that was dependent on iteration order from a pointer set, which is not always deterministic. Switch to a set vector, which allows fast querying and preserves ordering. Also make other minor changes in this area. Use more range-based for-loops. Remove limitation on SourceRanges that no logner exists. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@304519 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp index 33a8f9c4af..dcb2c11c73 100644 --- a/lib/Sema/SemaStmt.cpp +++ b/lib/Sema/SemaStmt.cpp @@ -1288,17 +1288,22 @@ Sema::ActOnDoStmt(SourceLocation DoLoc, Stmt *Body, } namespace { + // Use SetVector since the diagnostic cares about the ordering of the Decl's. + using DeclSetVector = + llvm::SetVector, + llvm::SmallPtrSet>; + // This visitor will traverse a conditional statement and store all // the evaluated decls into a vector. Simple is set to true if none // of the excluded constructs are used. class DeclExtractor : public EvaluatedExprVisitor { - llvm::SmallPtrSetImpl &Decls; + DeclSetVector &Decls; SmallVectorImpl &Ranges; bool Simple; public: typedef EvaluatedExprVisitor Inherited; - DeclExtractor(Sema &S, llvm::SmallPtrSetImpl &Decls, + DeclExtractor(Sema &S, DeclSetVector &Decls, SmallVectorImpl &Ranges) : Inherited(S.Context), Decls(Decls), @@ -1370,14 +1375,13 @@ namespace { // DeclMatcher checks to see if the decls are used in a non-evaluated // context. class DeclMatcher : public EvaluatedExprVisitor { - llvm::SmallPtrSetImpl &Decls; + DeclSetVector &Decls; bool FoundDecl; public: typedef EvaluatedExprVisitor Inherited; - DeclMatcher(Sema &S, llvm::SmallPtrSetImpl &Decls, - Stmt *Statement) : + DeclMatcher(Sema &S, DeclSetVector &Decls, Stmt *Statement) : Inherited(S.Context), Decls(Decls), FoundDecl(false) { if (!Statement) return; @@ -1459,7 +1463,7 @@ namespace { return; PartialDiagnostic PDiag = S.PDiag(diag::warn_variables_not_in_loop_body); - llvm::SmallPtrSet Decls; + DeclSetVector Decls; SmallVector Ranges; DeclExtractor DE(S, Decls, Ranges); DE.Visit(Second); @@ -1471,11 +1475,9 @@ namespace { if (Decls.size() == 0) return; // Don't warn on volatile, static, or global variables. - for (llvm::SmallPtrSetImpl::iterator I = Decls.begin(), - E = Decls.end(); - I != E; ++I) - if ((*I)->getType().isVolatileQualified() || - (*I)->hasGlobalStorage()) return; + for (auto *VD : Decls) + if (VD->getType().isVolatileQualified() || VD->hasGlobalStorage()) + return; if (DeclMatcher(S, Decls, Second).FoundDeclInUse() || DeclMatcher(S, Decls, Third).FoundDeclInUse() || @@ -1483,25 +1485,16 @@ namespace { return; // Load decl names into diagnostic. - if (Decls.size() > 4) + if (Decls.size() > 4) { PDiag << 0; - else { - PDiag << Decls.size(); - for (llvm::SmallPtrSetImpl::iterator I = Decls.begin(), - E = Decls.end(); - I != E; ++I) - PDiag << (*I)->getDeclName(); - } - - // Load SourceRanges into diagnostic if there is room. - // Otherwise, load the SourceRange of the conditional expression. - if (Ranges.size() <= PartialDiagnostic::MaxArguments) - for (SmallVectorImpl::iterator I = Ranges.begin(), - E = Ranges.end(); - I != E; ++I) - PDiag << *I; - else - PDiag << Second->getSourceRange(); + } else { + PDiag << (unsigned)Decls.size(); + for (auto *VD : Decls) + PDiag << VD->getDeclName(); + } + + for (auto Range : Ranges) + PDiag << Range; S.Diag(Ranges.begin()->getBegin(), PDiag); }