[clang-tools-extra] r280640 - [clang-rename] Enforce LLVM policy about braces around single line control flow statement body.

Kirill Bobyrev via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 4 15:50:41 PDT 2016


Author: omtcyfz
Date: Sun Sep  4 17:50:41 2016
New Revision: 280640

URL: http://llvm.org/viewvc/llvm-project?rev=280640&view=rev
Log:
[clang-rename] Enforce LLVM policy about braces around single line control flow statement body.

Although it is not explicitly stated in LLVM Coding Standards, LLVM developers
prefer to omit braces around flow control statements with single line body.

For example the following piece of code

```
if (condition)
  std::cout << "Hello, world!" << std::endl;
```

is preferred to

```
if (condition) {
  std::cout << "Hello, world!" << std::endl;
}
```

So far clang-rename has ignored this. This patch makes clang-rename more
"LLVM-ish".

Modified:
    clang-tools-extra/trunk/clang-rename/RenamingAction.cpp
    clang-tools-extra/trunk/clang-rename/USRFinder.cpp
    clang-tools-extra/trunk/clang-rename/USRFindingAction.cpp
    clang-tools-extra/trunk/clang-rename/USRLocFinder.cpp

Modified: clang-tools-extra/trunk/clang-rename/RenamingAction.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-rename/RenamingAction.cpp?rev=280640&r1=280639&r2=280640&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-rename/RenamingAction.cpp (original)
+++ clang-tools-extra/trunk/clang-rename/RenamingAction.cpp Sun Sep  4 17:50:41 2016
@@ -44,9 +44,8 @@ public:
         FileToReplaces(FileToReplaces), PrintLocations(PrintLocations) {}
 
   void HandleTranslationUnit(ASTContext &Context) override {
-    for (unsigned I = 0; I < NewNames.size(); ++I) {
+    for (unsigned I = 0; I < NewNames.size(); ++I)
       HandleOneRename(Context, NewNames[I], PrevNames[I], USRList[I]);
-    }
   }
 
   void HandleOneRename(ASTContext &Context, const std::string &NewName,

Modified: clang-tools-extra/trunk/clang-rename/USRFinder.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-rename/USRFinder.cpp?rev=280640&r1=280639&r2=280640&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-rename/USRFinder.cpp (original)
+++ clang-tools-extra/trunk/clang-rename/USRFinder.cpp Sun Sep  4 17:50:41 2016
@@ -78,9 +78,8 @@ public:
     const SourceLocation TypeEndLoc = Lexer::getLocForEndOfToken(
         TypeBeginLoc, 0, Context.getSourceManager(), Context.getLangOpts());
     if (const auto *TemplateTypeParm =
-            dyn_cast<TemplateTypeParmType>(Loc.getType())) {
+            dyn_cast<TemplateTypeParmType>(Loc.getType()))
       return setResult(TemplateTypeParm->getDecl(), TypeBeginLoc, TypeEndLoc);
-    }
     if (const auto *TemplateSpecType =
             dyn_cast<TemplateSpecializationType>(Loc.getType())) {
       return setResult(TemplateSpecType->getTemplateName().getAsTemplateDecl(),
@@ -92,18 +91,16 @@ public:
 
   bool VisitCXXConstructorDecl(clang::CXXConstructorDecl *ConstructorDecl) {
     for (const auto *Initializer : ConstructorDecl->inits()) {
-      if (!Initializer->isWritten()) {
-        // Ignore implicit initializers.
+      // Ignore implicit initializers.
+      if (!Initializer->isWritten())
         continue;
-      }
       if (const clang::FieldDecl *FieldDecl = Initializer->getMember()) {
         const SourceLocation InitBeginLoc = Initializer->getSourceLocation(),
                              InitEndLoc = Lexer::getLocForEndOfToken(
                                  InitBeginLoc, 0, Context.getSourceManager(),
                                  Context.getLangOpts());
-        if (!setResult(FieldDecl, InitBeginLoc, InitEndLoc)) {
+        if (!setResult(FieldDecl, InitBeginLoc, InitEndLoc))
           return false;
-        }
       }
     }
     return true;
@@ -129,20 +126,17 @@ private:
   // \returns false on success.
   bool setResult(const NamedDecl *Decl, SourceLocation Start,
                  SourceLocation End) {
-    if (!Decl) {
+    if (!Decl)
       return true;
-    }
     if (Name.empty()) {
       // Offset is used to find the declaration.
       if (!Start.isValid() || !Start.isFileID() || !End.isValid() ||
-          !End.isFileID() || !isPointWithin(Start, End)) {
+          !End.isFileID() || !isPointWithin(Start, End))
         return true;
-      }
     } else {
       // Fully qualified name is used to find the declaration.
-      if (Name != Decl->getQualifiedNameAsString()) {
+      if (Name != Decl->getQualifiedNameAsString())
         return true;
-      }
     }
     Result = Decl;
     return false;
@@ -182,15 +176,13 @@ const NamedDecl *getNamedDeclAt(const AS
     const SourceLocation FileLoc = CurrDecl->getLocStart();
     StringRef FileName = Context.getSourceManager().getFilename(FileLoc);
     // FIXME: Add test.
-    if (FileName == SearchFile) {
+    if (FileName == SearchFile)
       Visitor.TraverseDecl(CurrDecl);
-    }
   }
 
   NestedNameSpecifierLocFinder Finder(const_cast<ASTContext &>(Context));
-  for (const auto &Location : Finder.getNestedNameSpecifierLocations()) {
+  for (const auto &Location : Finder.getNestedNameSpecifierLocations())
     Visitor.handleNestedNameSpecifierLoc(Location);
-  }
 
   return Visitor.getNamedDecl();
 }

Modified: clang-tools-extra/trunk/clang-rename/USRFindingAction.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-rename/USRFindingAction.cpp?rev=280640&r1=280639&r2=280640&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-rename/USRFindingAction.cpp (original)
+++ clang-tools-extra/trunk/clang-rename/USRFindingAction.cpp Sun Sep  4 17:50:41 2016
@@ -55,9 +55,8 @@ public:
     if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(FoundDecl)) {
       addUSRsOfOverridenFunctions(MethodDecl);
       for (const auto &OverriddenMethod : OverriddenMethods) {
-        if (checkIfOverriddenFunctionAscends(OverriddenMethod)) {
+        if (checkIfOverriddenFunctionAscends(OverriddenMethod))
           USRSet.insert(getUSRForDecl(OverriddenMethod));
-        }
       }
     } else if (const auto *RecordDecl = dyn_cast<CXXRecordDecl>(FoundDecl)) {
       handleCXXRecordDecl(RecordDecl);
@@ -71,9 +70,8 @@ public:
   }
 
   bool VisitCXXMethodDecl(const CXXMethodDecl *MethodDecl) {
-    if (MethodDecl->isVirtual()) {
+    if (MethodDecl->isVirtual())
       OverriddenMethods.push_back(MethodDecl);
-    }
     return true;
   }
 
@@ -87,46 +85,43 @@ private:
   void handleCXXRecordDecl(const CXXRecordDecl *RecordDecl) {
     RecordDecl = RecordDecl->getDefinition();
     if (const auto *ClassTemplateSpecDecl =
-            dyn_cast<ClassTemplateSpecializationDecl>(RecordDecl)) {
+            dyn_cast<ClassTemplateSpecializationDecl>(RecordDecl))
       handleClassTemplateDecl(ClassTemplateSpecDecl->getSpecializedTemplate());
-    }
     addUSRsOfCtorDtors(RecordDecl);
   }
 
   void handleClassTemplateDecl(const ClassTemplateDecl *TemplateDecl) {
-    for (const auto *Specialization : TemplateDecl->specializations()) {
+    for (const auto *Specialization : TemplateDecl->specializations())
       addUSRsOfCtorDtors(Specialization);
-    }
+
     for (const auto *PartialSpec : PartialSpecs) {
-      if (PartialSpec->getSpecializedTemplate() == TemplateDecl) {
+      if (PartialSpec->getSpecializedTemplate() == TemplateDecl)
         addUSRsOfCtorDtors(PartialSpec);
-      }
     }
     addUSRsOfCtorDtors(TemplateDecl->getTemplatedDecl());
   }
 
   void addUSRsOfCtorDtors(const CXXRecordDecl *RecordDecl) {
     RecordDecl = RecordDecl->getDefinition();
-    for (const auto *CtorDecl : RecordDecl->ctors()) {
+
+    for (const auto *CtorDecl : RecordDecl->ctors())
       USRSet.insert(getUSRForDecl(CtorDecl));
-    }
+
     USRSet.insert(getUSRForDecl(RecordDecl->getDestructor()));
     USRSet.insert(getUSRForDecl(RecordDecl));
   }
 
   void addUSRsOfOverridenFunctions(const CXXMethodDecl *MethodDecl) {
     USRSet.insert(getUSRForDecl(MethodDecl));
-    for (const auto &OverriddenMethod : MethodDecl->overridden_methods()) {
-      // Recursively visit each OverridenMethod.
+    // Recursively visit each OverridenMethod.
+    for (const auto &OverriddenMethod : MethodDecl->overridden_methods())
       addUSRsOfOverridenFunctions(OverriddenMethod);
-    }
   }
 
   bool checkIfOverriddenFunctionAscends(const CXXMethodDecl *MethodDecl) {
     for (const auto &OverriddenMethod : MethodDecl->overridden_methods()) {
-      if (USRSet.find(getUSRForDecl(OverriddenMethod)) != USRSet.end()) {
+      if (USRSet.find(getUSRForDecl(OverriddenMethod)) != USRSet.end())
         return true;
-      }
       return checkIfOverriddenFunctionAscends(OverriddenMethod);
     }
     return false;
@@ -151,11 +146,10 @@ struct NamedDeclFindingConsumer : public
     if (!Point.isValid())
       return;
     const NamedDecl *FoundDecl = nullptr;
-    if (OldName.empty()) {
+    if (OldName.empty())
       FoundDecl = getNamedDeclAt(Context, Point);
-    } else {
+    else
       FoundDecl = getNamedDeclFor(Context, OldName);
-    }
     if (FoundDecl == nullptr) {
       if (OldName.empty()) {
         FullSourceLoc FullLoc(Point, SourceMgr);
@@ -164,18 +158,19 @@ struct NamedDeclFindingConsumer : public
                << FullLoc.getSpellingLineNumber() << ":"
                << FullLoc.getSpellingColumnNumber() << " (offset "
                << SymbolOffset << ").\n";
-      } else
+      } else {
         errs() << "clang-rename: could not find symbol " << OldName << ".\n";
+      }
       return;
     }
 
     // If FoundDecl is a constructor or destructor, we want to instead take the
     // Decl of the corresponding class.
-    if (const auto *CtorDecl = dyn_cast<CXXConstructorDecl>(FoundDecl)) {
+    if (const auto *CtorDecl = dyn_cast<CXXConstructorDecl>(FoundDecl))
       FoundDecl = CtorDecl->getParent();
-    } else if (const auto *DtorDecl = dyn_cast<CXXDestructorDecl>(FoundDecl)) {
+    else if (const auto *DtorDecl = dyn_cast<CXXDestructorDecl>(FoundDecl))
       FoundDecl = DtorDecl->getParent();
-    }
+
     *SpellingName = FoundDecl->getNameAsString();
 
     AdditionalUSRFinder Finder(FoundDecl, Context, USRs);

Modified: clang-tools-extra/trunk/clang-rename/USRLocFinder.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-rename/USRLocFinder.cpp?rev=280640&r1=280639&r2=280640&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-rename/USRLocFinder.cpp (original)
+++ clang-tools-extra/trunk/clang-rename/USRLocFinder.cpp Sun Sep  4 17:50:41 2016
@@ -44,23 +44,20 @@ public:
 
   bool VisitCXXConstructorDecl(clang::CXXConstructorDecl *ConstructorDecl) {
     for (const auto *Initializer : ConstructorDecl->inits()) {
-      if (!Initializer->isWritten()) {
-        // Ignore implicit initializers.
+      // Ignore implicit initializers.
+      if (!Initializer->isWritten())
         continue;
-      }
       if (const clang::FieldDecl *FieldDecl = Initializer->getMember()) {
-        if (USRSet.find(getUSRForDecl(FieldDecl)) != USRSet.end()) {
+        if (USRSet.find(getUSRForDecl(FieldDecl)) != USRSet.end())
           LocationsFound.push_back(Initializer->getSourceLocation());
-        }
       }
     }
     return true;
   }
 
   bool VisitNamedDecl(const NamedDecl *Decl) {
-    if (USRSet.find(getUSRForDecl(Decl)) != USRSet.end()) {
+    if (USRSet.find(getUSRForDecl(Decl)) != USRSet.end())
       checkAndAddLocation(Decl->getLocation());
-    }
     return true;
   }
 
@@ -92,15 +89,13 @@ public:
 
   bool VisitTypeLoc(const TypeLoc Loc) {
     if (USRSet.find(getUSRForDecl(Loc.getType()->getAsCXXRecordDecl())) !=
-        USRSet.end()) {
+        USRSet.end())
       checkAndAddLocation(Loc.getBeginLoc());
-    }
     if (const auto *TemplateTypeParm =
             dyn_cast<TemplateTypeParmType>(Loc.getType())) {
       if (USRSet.find(getUSRForDecl(TemplateTypeParm->getDecl())) !=
-          USRSet.end()) {
+          USRSet.end())
         checkAndAddLocation(Loc.getBeginLoc());
-      }
     }
     return true;
   }
@@ -118,9 +113,8 @@ public:
     while (NameLoc) {
       const NamespaceDecl *Decl =
           NameLoc.getNestedNameSpecifier()->getAsNamespace();
-      if (Decl && USRSet.find(getUSRForDecl(Decl)) != USRSet.end()) {
+      if (Decl && USRSet.find(getUSRForDecl(Decl)) != USRSet.end())
         checkAndAddLocation(NameLoc.getLocalBeginLoc());
-      }
       NameLoc = NameLoc.getPrefix();
     }
   }
@@ -134,11 +128,11 @@ private:
         Lexer::getSourceText(CharSourceRange::getTokenRange(BeginLoc, EndLoc),
                              Context.getSourceManager(), Context.getLangOpts());
     size_t Offset = TokenName.find(PrevName);
-    if (Offset != StringRef::npos) {
-      // The token of the source location we find actually has the old
-      // name.
+
+    // The token of the source location we find actually has the old
+    // name.
+    if (Offset != StringRef::npos)
       LocationsFound.push_back(BeginLoc.getLocWithOffset(Offset));
-    }
   }
 
   const std::set<std::string> USRSet;
@@ -154,9 +148,10 @@ getLocationsOfUSRs(const std::vector<std
   USRLocFindingASTVisitor Visitor(USRs, PrevName, Decl->getASTContext());
   Visitor.TraverseDecl(Decl);
   NestedNameSpecifierLocFinder Finder(Decl->getASTContext());
-  for (const auto &Location : Finder.getNestedNameSpecifierLocations()) {
+
+  for (const auto &Location : Finder.getNestedNameSpecifierLocations())
     Visitor.handleNestedNameSpecifierLoc(Location);
-  }
+
   return Visitor.getLocationsFound();
 }
 




More information about the cfe-commits mailing list