[clang-tools-extra] r278760 - [clang-rename] cleanup `auto` usages

Kirill Bobyrev via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 15 16:20:06 PDT 2016


Author: omtcyfz
Date: Mon Aug 15 18:20:05 2016
New Revision: 278760

URL: http://llvm.org/viewvc/llvm-project?rev=278760&view=rev
Log:
[clang-rename] cleanup `auto` usages

As Alexander pointed out, LLVM Coding Standards are more conservative about
using auto, i.e. it should be used in the following situations:

* When the type is obvious, i.e. explicitly mentioned in the same expression.
For example `if (const clang::FieldDecl *FieldDecl = Initializer->getMember())`.
* When the type is totally non-obvious and one iterates over something. For
example
`for (const auto &CurrDecl : Context.getTranslationUnitDecl()->decls())`.

Otherwise the type should be explicitly stated.

Reviewers: alexfh

Differential Revision: https://reviews.llvm.org/D23397

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=278760&r1=278759&r2=278760&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-rename/RenamingAction.cpp (original)
+++ clang-tools-extra/trunk/clang-rename/RenamingAction.cpp Mon Aug 15 18:20:05 2016
@@ -52,7 +52,7 @@ public:
   void HandleOneRename(ASTContext &Context, const std::string &NewName,
                        const std::string &PrevName,
                        const std::vector<std::string> &USRs) {
-    const auto &SourceMgr = Context.getSourceManager();
+    const SourceManager &SourceMgr = Context.getSourceManager();
     std::vector<SourceLocation> RenamingCandidates;
     std::vector<SourceLocation> NewCandidates;
 
@@ -61,7 +61,7 @@ public:
     RenamingCandidates.insert(RenamingCandidates.end(), NewCandidates.begin(),
                               NewCandidates.end());
 
-    auto PrevNameLen = PrevName.length();
+    unsigned PrevNameLen = PrevName.length();
     for (const auto &Loc : RenamingCandidates) {
       if (PrintLocations) {
         FullSourceLoc FullLoc(Loc, SourceMgr);
@@ -70,8 +70,8 @@ public:
                << FullLoc.getSpellingColumnNumber() << "\n";
       }
       // FIXME: better error handling.
-      auto Replace = tooling::Replacement(SourceMgr, Loc, PrevNameLen, NewName);
-      auto Err = FileToReplaces[Replace.getFilePath()].add(Replace);
+      tooling::Replacement Replace(SourceMgr, Loc, PrevNameLen, NewName);
+      llvm::Error Err = FileToReplaces[Replace.getFilePath()].add(Replace);
       if (Err)
         llvm::errs() << "Renaming failed in " << Replace.getFilePath() << "! "
                      << llvm::toString(std::move(Err)) << "\n";

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=278760&r1=278759&r2=278760&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-rename/USRFinder.cpp (original)
+++ clang-tools-extra/trunk/clang-rename/USRFinder.cpp Mon Aug 15 18:20:05 2016
@@ -60,13 +60,13 @@ public:
   // Expression visitors:
 
   bool VisitDeclRefExpr(const DeclRefExpr *Expr) {
-    const auto *Decl = Expr->getFoundDecl();
+    const NamedDecl *Decl = Expr->getFoundDecl();
     return setResult(Decl, Expr->getLocation(),
                      Decl->getNameAsString().length());
   }
 
   bool VisitMemberExpr(const MemberExpr *Expr) {
-    const auto *Decl = Expr->getFoundDecl().getDecl();
+    const NamedDecl *Decl = Expr->getFoundDecl().getDecl();
     return setResult(Decl, Expr->getMemberLoc(),
                      Decl->getNameAsString().length());
   }
@@ -74,9 +74,10 @@ public:
   // Other visitors:
 
   bool VisitTypeLoc(const TypeLoc Loc) {
-    const auto TypeBeginLoc = Loc.getBeginLoc();
-    const auto TypeEndLoc = Lexer::getLocForEndOfToken(
-        TypeBeginLoc, 0, Context.getSourceManager(), Context.getLangOpts());
+    const SourceLocation TypeBeginLoc = Loc.getBeginLoc();
+    const SourceLocation TypeEndLoc = Lexer::getLocForEndOfToken(
+                             TypeBeginLoc, 0, Context.getSourceManager(),
+                             Context.getLangOpts());
     if (const auto *TemplateTypeParm =
             dyn_cast<TemplateTypeParmType>(Loc.getType())) {
       return setResult(TemplateTypeParm->getDecl(), TypeBeginLoc, TypeEndLoc);
@@ -117,7 +118,8 @@ public:
   // \returns false on success and sets Result.
   void handleNestedNameSpecifierLoc(NestedNameSpecifierLoc NameLoc) {
     while (NameLoc) {
-      const auto *Decl = NameLoc.getNestedNameSpecifier()->getAsNamespace();
+      const NamespaceDecl *Decl =
+          NameLoc.getNestedNameSpecifier()->getAsNamespace();
       setResult(Decl, NameLoc.getLocalBeginLoc(), NameLoc.getLocalEndLoc());
       NameLoc = NameLoc.getPrefix();
     }
@@ -173,14 +175,13 @@ private:
 
 const NamedDecl *getNamedDeclAt(const ASTContext &Context,
                                 const SourceLocation Point) {
-  const auto SearchFile = Context.getSourceManager().getFilename(Point);
+  StringRef SearchFile = Context.getSourceManager().getFilename(Point);
   NamedDeclFindingASTVisitor Visitor(Point, Context);
 
   // We only want to search the decls that exist in the same file as the point.
-  auto Decls = Context.getTranslationUnitDecl()->decls();
-  for (auto &CurrDecl : Decls) {
-    const auto FileLoc = CurrDecl->getLocStart();
-    const auto FileName = Context.getSourceManager().getFilename(FileLoc);
+  for (const auto *CurrDecl : Context.getTranslationUnitDecl()->decls()) {
+    const SourceLocation FileLoc = CurrDecl->getLocStart();
+    StringRef FileName = Context.getSourceManager().getFilename(FileLoc);
     // FIXME: Add test.
     if (FileName == SearchFile) {
       Visitor.TraverseDecl(CurrDecl);

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=278760&r1=278759&r2=278760&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-rename/USRFindingAction.cpp (original)
+++ clang-tools-extra/trunk/clang-rename/USRFindingAction.cpp Mon Aug 15 18:20:05 2016
@@ -116,14 +116,14 @@ private:
 
   void addUSRsOfOverridenFunctions(const CXXMethodDecl *MethodDecl) {
     USRSet.insert(getUSRForDecl(MethodDecl));
-    for (auto &OverriddenMethod : MethodDecl->overridden_methods()) {
+    for (const auto &OverriddenMethod : MethodDecl->overridden_methods()) {
       // Recursively visit each OverridenMethod.
       addUSRsOfOverridenFunctions(OverriddenMethod);
     }
   }
 
   bool checkIfOverriddenFunctionAscends(const CXXMethodDecl *MethodDecl) {
-    for (auto &OverriddenMethod : MethodDecl->overridden_methods()) {
+    for (const auto &OverriddenMethod : MethodDecl->overridden_methods()) {
       if (USRSet.find(getUSRForDecl(OverriddenMethod)) != USRSet.end()) {
         return true;
       }
@@ -143,10 +143,11 @@ private:
 
 struct NamedDeclFindingConsumer : public ASTConsumer {
   void HandleTranslationUnit(ASTContext &Context) override {
-    const auto &SourceMgr = Context.getSourceManager();
+    const SourceManager &SourceMgr = Context.getSourceManager();
     // The file we look for the USR in will always be the main source file.
-    const auto Point = SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID())
-                           .getLocWithOffset(SymbolOffset);
+    const SourceLocation Point =
+        SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID())
+            .getLocWithOffset(SymbolOffset);
     if (!Point.isValid())
       return;
     const NamedDecl *FoundDecl = nullptr;

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=278760&r1=278759&r2=278760&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-rename/USRLocFinder.cpp (original)
+++ clang-tools-extra/trunk/clang-rename/USRLocFinder.cpp Mon Aug 15 18:20:05 2016
@@ -67,7 +67,7 @@ public:
   // Expression visitors:
 
   bool VisitDeclRefExpr(const DeclRefExpr *Expr) {
-    const auto *Decl = Expr->getFoundDecl();
+    const NamedDecl *Decl = Expr->getFoundDecl();
 
     if (USRSet.find(getUSRForDecl(Decl)) != USRSet.end()) {
       const SourceManager &Manager = Decl->getASTContext().getSourceManager();
@@ -79,7 +79,7 @@ public:
   }
 
   bool VisitMemberExpr(const MemberExpr *Expr) {
-    const auto *Decl = Expr->getFoundDecl().getDecl();
+    const NamedDecl *Decl = Expr->getFoundDecl().getDecl();
     if (USRSet.find(getUSRForDecl(Decl)) != USRSet.end()) {
       const SourceManager &Manager = Decl->getASTContext().getSourceManager();
       SourceLocation Location = Manager.getSpellingLoc(Expr->getMemberLoc());
@@ -116,7 +116,8 @@ public:
   // Namespace traversal:
   void handleNestedNameSpecifierLoc(NestedNameSpecifierLoc NameLoc) {
     while (NameLoc) {
-      const auto *Decl = NameLoc.getNestedNameSpecifier()->getAsNamespace();
+      const NamespaceDecl *Decl =
+          NameLoc.getNestedNameSpecifier()->getAsNamespace();
       if (Decl && USRSet.find(getUSRForDecl(Decl)) != USRSet.end()) {
         checkAndAddLocation(NameLoc.getLocalBeginLoc());
       }
@@ -126,8 +127,8 @@ public:
 
 private:
   void checkAndAddLocation(SourceLocation Loc) {
-    const auto BeginLoc = Loc;
-    const auto EndLoc = Lexer::getLocForEndOfToken(
+    const SourceLocation BeginLoc = Loc;
+    const SourceLocation EndLoc = Lexer::getLocForEndOfToken(
         BeginLoc, 0, Context.getSourceManager(), Context.getLangOpts());
     StringRef TokenName =
         Lexer::getSourceText(CharSourceRange::getTokenRange(BeginLoc, EndLoc),




More information about the cfe-commits mailing list