[clang-tools-extra] r275387 - [clang-rename] exit code-related bugfix and code cleanup

Benjamin Kramer via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 14 02:46:03 PDT 2016


Author: d0k
Date: Thu Jul 14 04:46:03 2016
New Revision: 275387

URL: http://llvm.org/viewvc/llvm-project?rev=275387&view=rev
Log:
[clang-rename] exit code-related bugfix and code cleanup

This patch does the following:

* enforces proper formatting for few files (i.e. deals with 80 linewidth violations and few other things)
* ensures '\n' chars are passed to the output streams instead of "\n" strings
* fixes a bug caused by calling cl::PrintHelpMessage(), which occasionally calls exit(0), so that exit(1) (which is right after cl::PrintHelpMessage line) becomes dead code

Patch by Kirill Bobyrev!

Differential Revision: http://reviews.llvm.org/D22091

Added:
    clang-tools-extra/trunk/test/clang-rename/NoNewName.cpp
Modified:
    clang-tools-extra/trunk/clang-rename/RenamingAction.cpp
    clang-tools-extra/trunk/clang-rename/USRLocFinder.cpp
    clang-tools-extra/trunk/clang-rename/USRLocFinder.h
    clang-tools-extra/trunk/clang-rename/tool/ClangRename.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=275387&r1=275386&r2=275387&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-rename/RenamingAction.cpp (original)
+++ clang-tools-extra/trunk/clang-rename/RenamingAction.cpp Thu Jul 14 04:46:03 2016
@@ -49,7 +49,8 @@ public:
     std::vector<SourceLocation> NewCandidates;
 
     for (const auto &USR : USRs) {
-      NewCandidates = getLocationsOfUSR(USR, PrevName, Context.getTranslationUnitDecl());
+      NewCandidates = getLocationsOfUSR(USR, PrevName,
+                                        Context.getTranslationUnitDecl());
       RenamingCandidates.insert(RenamingCandidates.end(), NewCandidates.begin(),
                                 NewCandidates.end());
       NewCandidates.clear();

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=275387&r1=275386&r2=275387&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-rename/USRLocFinder.cpp (original)
+++ clang-tools-extra/trunk/clang-rename/USRLocFinder.cpp Thu Jul 14 04:46:03 2016
@@ -34,8 +34,8 @@ namespace {
 class USRLocFindingASTVisitor
     : public clang::RecursiveASTVisitor<USRLocFindingASTVisitor> {
 public:
-  explicit USRLocFindingASTVisitor(StringRef USR, StringRef PrevName) : USR(USR), PrevName(PrevName) {
-  }
+  explicit USRLocFindingASTVisitor(StringRef USR, StringRef PrevName)
+      : USR(USR), PrevName(PrevName) {}
 
   // Declaration visitors:
 
@@ -60,8 +60,7 @@ public:
 
   bool VisitCXXConstructorDecl(clang::CXXConstructorDecl *ConstructorDecl) {
     const ASTContext &Context = ConstructorDecl->getASTContext();
-    for (clang::CXXConstructorDecl::init_const_iterator it = ConstructorDecl->init_begin(); it != ConstructorDecl->init_end(); ++it) {
-      const clang::CXXCtorInitializer* Initializer = *it;
+    for (auto &Initializer : ConstructorDecl->inits()) {
       if (Initializer->getSourceOrder() == -1) {
         // Ignore implicit initializers.
         continue;
@@ -71,9 +70,12 @@ public:
         if (getUSRForDecl(FieldDecl) == USR) {
           // The initializer refers to a field that is to be renamed.
           SourceLocation Location = Initializer->getSourceLocation();
-          StringRef TokenName = Lexer::getSourceText(CharSourceRange::getTokenRange(Location), Context.getSourceManager(), Context.getLangOpts());
+          StringRef TokenName = Lexer::getSourceText(
+              CharSourceRange::getTokenRange(Location),
+              Context.getSourceManager(), Context.getLangOpts());
           if (TokenName == PrevName) {
-            // 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.
             LocationsFound.push_back(Initializer->getSourceLocation());
           }
         }
@@ -183,14 +185,15 @@ private:
   bool handleCXXNamedCastExpr(clang::CXXNamedCastExpr *Expr) {
     clang::QualType Type = Expr->getType();
     // See if this a cast of a pointer.
-    const RecordDecl* Decl = Type->getPointeeCXXRecordDecl();
+    const RecordDecl *Decl = Type->getPointeeCXXRecordDecl();
     if (!Decl) {
       // See if this is a cast of a reference.
       Decl = Type->getAsCXXRecordDecl();
     }
 
     if (Decl && getUSRForDecl(Decl) == USR) {
-      SourceLocation Location = Expr->getTypeInfoAsWritten()->getTypeLoc().getBeginLoc();
+      SourceLocation Location =
+          Expr->getTypeInfoAsWritten()->getTypeLoc().getBeginLoc();
       LocationsFound.push_back(Location);
     }
 
@@ -205,8 +208,7 @@ private:
 };
 } // namespace
 
-std::vector<SourceLocation> getLocationsOfUSR(StringRef USR,
-                                              StringRef PrevName,
+std::vector<SourceLocation> getLocationsOfUSR(StringRef USR, StringRef PrevName,
                                               Decl *Decl) {
   USRLocFindingASTVisitor visitor(USR, PrevName);
 

Modified: clang-tools-extra/trunk/clang-rename/USRLocFinder.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-rename/USRLocFinder.h?rev=275387&r1=275386&r2=275387&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-rename/USRLocFinder.h (original)
+++ clang-tools-extra/trunk/clang-rename/USRLocFinder.h Thu Jul 14 04:46:03 2016
@@ -16,23 +16,19 @@
 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_RENAME_USR_LOC_FINDER_H
 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_RENAME_USR_LOC_FINDER_H
 
+#include "clang/AST/AST.h"
+#include "llvm/ADT/StringRef.h"
 #include <string>
 #include <vector>
 
-#include "llvm/ADT/StringRef.h"
-
 namespace clang {
-
-class Decl;
-class SourceLocation;
-
 namespace rename {
 
 // FIXME: make this an AST matcher. Wouldn't that be awesome??? I agree!
-std::vector<SourceLocation> getLocationsOfUSR(llvm::StringRef usr,
-                                              llvm::StringRef PrevName,
-                                              Decl *decl);
-}
-}
+std::vector<SourceLocation>
+getLocationsOfUSR(llvm::StringRef USR, llvm::StringRef PrevName, Decl *Decl);
+
+} // namespace rename
+} // namespace clang
 
 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_RENAME_USR_LOC_FINDER_H

Modified: clang-tools-extra/trunk/clang-rename/tool/ClangRename.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-rename/tool/ClangRename.cpp?rev=275387&r1=275386&r2=275387&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-rename/tool/ClangRename.cpp (original)
+++ clang-tools-extra/trunk/clang-rename/tool/ClangRename.cpp Thu Jul 14 04:46:03 2016
@@ -36,7 +36,6 @@
 #include "clang/Tooling/Tooling.h"
 #include "llvm/ADT/IntrusiveRefCntPtr.h"
 #include "llvm/Support/Host.h"
-#include <cstdlib>
 #include <string>
 
 using namespace llvm;
@@ -83,7 +82,7 @@ ExportFixes(
 #define CLANG_RENAME_VERSION "0.0.1"
 
 static void PrintVersion() {
-  outs() << "clang-rename version " << CLANG_RENAME_VERSION << "\n";
+  outs() << "clang-rename version " << CLANG_RENAME_VERSION << '\n';
 }
 
 using namespace clang;
@@ -101,7 +100,6 @@ int main(int argc, const char **argv) {
 
   if (NewName.empty()) {
     errs() << "clang-rename: no new name provided.\n\n";
-    cl::PrintHelpMessage();
     exit(1);
   }
 
@@ -115,12 +113,14 @@ int main(int argc, const char **argv) {
   const auto &USRs = USRAction.getUSRs();
   const auto &PrevName = USRAction.getUSRSpelling();
 
-  if (PrevName.empty())
+  if (PrevName.empty()) {
     // An error should have already been printed.
     exit(1);
+  }
 
-  if (PrintName)
-    errs() << "clang-rename: found name: " << PrevName << "\n";
+  if (PrintName) {
+    errs() << "clang-rename: found name: " << PrevName << '\n';
+  }
 
   // Perform the renaming.
   rename::RenamingAction RenameAction(NewName, PrevName, USRs,

Added: clang-tools-extra/trunk/test/clang-rename/NoNewName.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-rename/NoNewName.cpp?rev=275387&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-rename/NoNewName.cpp (added)
+++ clang-tools-extra/trunk/test/clang-rename/NoNewName.cpp Thu Jul 14 04:46:03 2016
@@ -0,0 +1,20 @@
+// This test is a copy of ConstCastExpr.cpp with a single change:
+// -new-name hasn't been passed to clang-rename, so this test should give an
+// error.
+// RUN: not clang-rename -offset=133 %s 2>&1 | FileCheck %s
+// CHECK: clang-rename: no new name provided.
+
+class Cla {
+public:
+  int getValue() {
+    return 0;
+  }
+};
+
+int main() {
+  const Cla *C = new Cla();
+  const_cast<Cla *>(C)->getValue();
+}
+
+// Use grep -FUbo 'Cla' <file> to get the correct offset of foo when changing
+// this file.




More information about the cfe-commits mailing list