[clang] 4681ae9 - [clang-format] Use range-for loops. NFC.

Marek Kurdej via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 7 07:06:22 PST 2022


Author: Marek Kurdej
Date: 2022-01-07T16:06:11+01:00
New Revision: 4681ae9353ed89d28a95f4d07e8050f8772ae724

URL: https://github.com/llvm/llvm-project/commit/4681ae9353ed89d28a95f4d07e8050f8772ae724
DIFF: https://github.com/llvm/llvm-project/commit/4681ae9353ed89d28a95f4d07e8050f8772ae724.diff

LOG: [clang-format] Use range-for loops. NFC.

* Avoid if check on every element of the loop when printing symbols.

Added: 
    

Modified: 
    clang/lib/Format/QualifierAlignmentFixer.cpp
    clang/lib/Format/SortJavaScriptImports.cpp
    clang/lib/Format/TokenAnalyzer.cpp
    clang/lib/Format/TokenAnnotator.h

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/QualifierAlignmentFixer.cpp b/clang/lib/Format/QualifierAlignmentFixer.cpp
index ec19a38537683..a53db5d11848d 100644
--- a/clang/lib/Format/QualifierAlignmentFixer.cpp
+++ b/clang/lib/Format/QualifierAlignmentFixer.cpp
@@ -88,11 +88,11 @@ std::pair<tooling::Replacements, unsigned> QualifierAlignmentFixer::analyze(
   // Don't make replacements that replace nothing.
   tooling::Replacements NonNoOpFixes;
 
-  for (auto I = Fixes.begin(), E = Fixes.end(); I != E; ++I) {
-    StringRef OriginalCode = Code.substr(I->getOffset(), I->getLength());
+  for (const tooling::Replacement &Fix : Fixes) {
+    StringRef OriginalCode = Code.substr(Fix.getOffset(), Fix.getLength());
 
-    if (!OriginalCode.equals(I->getReplacementText())) {
-      auto Err = NonNoOpFixes.add(*I);
+    if (!OriginalCode.equals(Fix.getReplacementText())) {
+      auto Err = NonNoOpFixes.add(Fix);
       if (Err)
         llvm::errs() << "Error adding replacements : "
                      << llvm::toString(std::move(Err)) << "\n";
@@ -396,9 +396,9 @@ LeftRightQualifierAlignmentFixer::analyze(
   tok::TokenKind QualifierToken = getTokenFromQualifier(Qualifier);
   assert(QualifierToken != tok::identifier && "Unrecognised Qualifier");
 
-  for (size_t I = 0, E = AnnotatedLines.size(); I != E; ++I) {
-    FormatToken *First = AnnotatedLines[I]->First;
-    const auto *Last = AnnotatedLines[I]->Last;
+  for (AnnotatedLine *Line : AnnotatedLines) {
+    FormatToken *First = Line->First;
+    const auto *Last = Line->Last;
 
     for (const auto *Tok = First; Tok && Tok != Last && Tok->Next;
          Tok = Tok->Next) {

diff  --git a/clang/lib/Format/SortJavaScriptImports.cpp b/clang/lib/Format/SortJavaScriptImports.cpp
index 52c222a8b3dc2..21f0bdd7323d4 100644
--- a/clang/lib/Format/SortJavaScriptImports.cpp
+++ b/clang/lib/Format/SortJavaScriptImports.cpp
@@ -338,10 +338,12 @@ class JavaScriptImportSorter : public TokenAnalyzer {
     // Stitch together the module reference start...
     Buffer += getSourceText(Reference.Range.getBegin(), Reference.SymbolsStart);
     // ... then the references in order ...
-    for (auto I = Symbols.begin(), E = Symbols.end(); I != E; ++I) {
-      if (I != Symbols.begin())
+    if (!Symbols.empty()) {
+      Buffer += getSourceText(Symbols.front().Range);
+      for (const JsImportedSymbol &Symbol : llvm::drop_begin(Symbols)) {
         Buffer += ",";
-      Buffer += getSourceText(I->Range);
+        Buffer += getSourceText(Symbol.Range);
+      }
     }
     // ... followed by the module reference end.
     Buffer += getSourceText(Reference.SymbolsEnd, Reference.Range.getEnd());
@@ -410,9 +412,8 @@ class JavaScriptImportSorter : public TokenAnalyzer {
                      << ", cat: " << Reference.Category
                      << ", url: " << Reference.URL
                      << ", prefix: " << Reference.Prefix;
-        for (size_t I = 0; I < Reference.Symbols.size(); ++I)
-          llvm::dbgs() << ", " << Reference.Symbols[I].Symbol << " as "
-                       << Reference.Symbols[I].Alias;
+        for (const JsImportedSymbol &Symbol : Reference.Symbols)
+          llvm::dbgs() << ", " << Symbol.Symbol << " as " << Symbol.Alias;
         llvm::dbgs() << ", text: " << getSourceText(Reference.Range);
         llvm::dbgs() << "}\n";
       });

diff  --git a/clang/lib/Format/TokenAnalyzer.cpp b/clang/lib/Format/TokenAnalyzer.cpp
index 3f77220931753..d0754e0c11128 100644
--- a/clang/lib/Format/TokenAnalyzer.cpp
+++ b/clang/lib/Format/TokenAnalyzer.cpp
@@ -127,8 +127,8 @@ std::pair<tooling::Replacements, unsigned> TokenAnalyzer::process() {
 
     LLVM_DEBUG({
       llvm::dbgs() << "Replacements for run " << Run << ":\n";
-      for (const tooling::Replacement &Replacement : RunResult.first)
-        llvm::dbgs() << Replacement.toString() << "\n";
+      for (const tooling::Replacement &Fix : RunResult.first)
+        llvm::dbgs() << Fix.toString() << "\n";
     });
     for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
       delete AnnotatedLines[i];

diff  --git a/clang/lib/Format/TokenAnnotator.h b/clang/lib/Format/TokenAnnotator.h
index 6e5e62cd4d82a..384a671c981f2 100644
--- a/clang/lib/Format/TokenAnnotator.h
+++ b/clang/lib/Format/TokenAnnotator.h
@@ -53,10 +53,9 @@ class AnnotatedLine {
     // left them in a 
diff erent state.
     First->Previous = nullptr;
     FormatToken *Current = First;
-    for (auto I = ++Line.Tokens.begin(), E = Line.Tokens.end(); I != E; ++I) {
-      const UnwrappedLineNode &Node = *I;
-      Current->Next = I->Tok;
-      I->Tok->Previous = Current;
+    for (const UnwrappedLineNode &Node : llvm::drop_begin(Line.Tokens)) {
+      Current->Next = Node.Tok;
+      Node.Tok->Previous = Current;
       Current = Current->Next;
       Current->Children.clear();
       for (const auto &Child : Node.Children) {


        


More information about the cfe-commits mailing list